From 53a0a15c666178411213c19aef25ab32ffb14d93 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Thu, 18 Jan 2024 15:16:58 +0100
Subject: [PATCH] Add backport integration tests

---
 Makefile                                      | 15 +++++++----
 backports/{ => OpenTofu}/Base.gitlab-ci.yml   | 10 +++++---
 .../BackportTemplates.gitlab-ci.yml           | 25 +++++++++++++++++++
 tests/integration.gitlab-ci.yml               | 17 +++++++++++++
 4 files changed, 59 insertions(+), 8 deletions(-)
 rename backports/{ => OpenTofu}/Base.gitlab-ci.yml (89%)
 create mode 100644 tests/integration-tests/BackportTemplates.gitlab-ci.yml

diff --git a/Makefile b/Makefile
index 39ce678..5156e34 100644
--- a/Makefile
+++ b/Makefile
@@ -14,25 +14,30 @@ docs:
 	rm -f readme0 readme1 readme_inputs.md
 
 BACKPORTS_DIR := backports
-BACKPORTS_BASE_FILE := $(BACKPORTS_DIR)/Base.gitlab-ci.yml
+BACKPORTS_BASE_DIR := $(BACKPORTS_DIR)/OpenTofu
+BACKPORTS_BASE_FILE := $(BACKPORTS_BASE_DIR)/Base.gitlab-ci.yml
 
 .PHONY: backports
 
 backports:
-	@mkdir -p $(BACKPORTS_DIR)
+	@mkdir -p $(BACKPORTS_BASE_DIR)
 	@echo 'variables:' > $(BACKPORTS_BASE_FILE)
 	@echo '  # OpenTofu CI/CD component version, see https://gitlab.com/components/opentofu/-/releases' >> $(BACKPORTS_BASE_FILE)
 	@echo '  VERSION: "0.1.0-alpha4"' >> $(BACKPORTS_BASE_FILE)
 	@echo '  # Compatible OpenTofu version, see https://gitlab.com/components/opentofu/-/releases' >> $(BACKPORTS_BASE_FILE)
 	@echo '  OPENTOFU_VERSION: "1.6.0"' >> $(BACKPORTS_BASE_FILE)
-	@echo '  TF_ROOT: $${CI_PROJECT_DIR}  # The relative path to the root directory of the OpenTofu project' >> $(BACKPORTS_BASE_FILE)
-	@echo '  TF_STATE_NAME: default  # The name of the state file used by the GitLab Managed Terraform state backend'>> $(BACKPORTS_BASE_FILE)
+	@echo '  # Job Image with `gitlab-tofu`' >> $(BACKPORTS_BASE_FILE)
+	@echo '  GITLAB_OPENTOFU_IMAGE: registry.gitlab.com/components/opentofu/gitlab-opentofu:$$VERSION-opentofu$$OPENTOFU_VERSION' >> $(BACKPORTS_BASE_FILE)
+	@echo '  # The relative path to the root directory of the OpenTofu project' >> $(BACKPORTS_BASE_FILE)
+	@echo '  TF_ROOT: $${CI_PROJECT_DIR}' >> $(BACKPORTS_BASE_FILE)
+	@echo '  # The name of the state file used by the GitLab Managed Terraform state backend' >> $(BACKPORTS_BASE_FILE)
+	@echo '  TF_STATE_NAME: default' >> $(BACKPORTS_BASE_FILE)
 	@sed '1,/^---$$/d' templates/full-pipeline.yml >> $(BACKPORTS_BASE_FILE)
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_validate \]\]/validate/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_build \]\]/build/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_deploy \]\]/deploy/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.stage_cleanup \]\]/cleanup/'
-	@sed -i $(BACKPORTS_BASE_FILE) -e 's#$$\[\[ inputs.gitlab_opentofu_image \]\]#registry.gitlab.com/components/opentofu/gitlab-opentofu:$$VERSION-opentofu$$OPENTOFU_VERSION#'
+	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.gitlab_opentofu_image \]\]/$$GITLAB_OPENTOFU_IMAGE/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.root_dir \]\]/$$TF_ROOT/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.state_name \]\]/$$TF_STATE_NAME/'
 	@sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.auto_apply \]\]/$$TF_AUTO_APPLY/'
diff --git a/backports/Base.gitlab-ci.yml b/backports/OpenTofu/Base.gitlab-ci.yml
similarity index 89%
rename from backports/Base.gitlab-ci.yml
rename to backports/OpenTofu/Base.gitlab-ci.yml
index 76a047c..adec966 100644
--- a/backports/Base.gitlab-ci.yml
+++ b/backports/OpenTofu/Base.gitlab-ci.yml
@@ -3,12 +3,16 @@ variables:
   VERSION: "0.1.0-alpha4"
   # Compatible OpenTofu version, see https://gitlab.com/components/opentofu/-/releases
   OPENTOFU_VERSION: "1.6.0"
-  TF_ROOT: ${CI_PROJECT_DIR}  # The relative path to the root directory of the OpenTofu project
-  TF_STATE_NAME: default  # The name of the state file used by the GitLab Managed Terraform state backend
+  # Job Image with `gitlab-tofu`
+  GITLAB_OPENTOFU_IMAGE: registry.gitlab.com/components/opentofu/gitlab-opentofu:$VERSION-opentofu$OPENTOFU_VERSION
+  # The relative path to the root directory of the OpenTofu project
+  TF_ROOT: ${CI_PROJECT_DIR}
+  # The name of the state file used by the GitLab Managed Terraform state backend
+  TF_STATE_NAME: default
 
 .opentofu:default:
   image:
-    name: registry.gitlab.com/components/opentofu/gitlab-opentofu:$VERSION-opentofu$OPENTOFU_VERSION
+    name: $GITLAB_OPENTOFU_IMAGE
 
   cache:
     key: "$TF_ROOT"
diff --git a/tests/integration-tests/BackportTemplates.gitlab-ci.yml b/tests/integration-tests/BackportTemplates.gitlab-ci.yml
new file mode 100644
index 0000000..889cc37
--- /dev/null
+++ b/tests/integration-tests/BackportTemplates.gitlab-ci.yml
@@ -0,0 +1,25 @@
+include:
+  - template: /backports/OpenTofu.gitlab-ci.yml
+
+stages: [validate, test, build, deploy, cleanup]
+
+# Required to run everything immediately, instead of manually.
+
+fmt:
+  rules: [{when: always}]
+
+validate:
+  rules: [{when: always}]
+
+plan:
+  rules: [{when: always}]
+
+apply: 
+  rules: [{when: always}]
+
+destroy:
+  rules: [{when: always}]
+
+delete-state:
+  rules: [{when: always}]
+
diff --git a/tests/integration.gitlab-ci.yml b/tests/integration.gitlab-ci.yml
index 1301465..1f87139 100644
--- a/tests/integration.gitlab-ci.yml
+++ b/tests/integration.gitlab-ci.yml
@@ -13,3 +13,20 @@ component:
   parallel:
     matrix:
       - PIPELINE: [Defaults.gitlab-ci.yml]
+
+backport-templates:
+  stage: test-integration
+  variables:
+    OPENTOFU_VERSION: $LATEST_OPENTOFU_VERSION
+    GITLAB_OPENTOFU_IMAGE: $GITLAB_OPENTOFU_IMAGE_NAME
+    TF_STATE_NAME: ci-integration-backports-$CI_PIPELINE_IID-$CI_NODE_INDEX
+    TF_ROOT: tests/terraform
+  trigger:
+    include: tests/integration-tests/$PIPELINE
+    strategy: depend
+  rules:
+    - if: '$CI_PROJECT_PATH == "components/opentofu"'
+      #- if: '$CI_MERGE_REQUEST_EVENT_TYPE == "merge_train"'
+  parallel:
+    matrix:
+      - PIPELINE: [BackportTemplates.gitlab-ci.yml]
-- 
GitLab