From 599228d59aed50147f063a5b2603f9a1062fde8d Mon Sep 17 00:00:00 2001 From: Arthur BOUDREAULT Date: Mon, 13 Jan 2025 16:16:36 +0100 Subject: [PATCH 1/4] feat: Test terraform module --- README-fr.md | 17 +++++++++---- README.md | 8 ++++++ includes-if/tags/terraform.yml | 3 +++ src/terraform/main.tf | 10 ++++++++ src/terraform/outputs.tf | 4 +++ src/terraform/variables.tf | 11 ++++++++ templates/bases/variables.yml | 1 + templates/set_packages_version.yml | 1 + templates/terraform.yml | 40 ++++++++++++++++++++++++++++++ test/terraform/main.tf | 10 ++++++++ test/terraform/variables.tf | 11 ++++++++ 11 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 includes-if/tags/terraform.yml create mode 100644 src/terraform/main.tf create mode 100644 src/terraform/outputs.tf create mode 100644 src/terraform/variables.tf create mode 100644 templates/terraform.yml create mode 100644 test/terraform/main.tf create mode 100644 test/terraform/variables.tf diff --git a/README-fr.md b/README-fr.md index 00a6488..da6ce12 100644 --- a/README-fr.md +++ b/README-fr.md @@ -68,6 +68,7 @@ Chaque fonctionnalité testée peut être activée avec une variable `MGCI_TEST_ | `MGCI_TEST_REGISTRY_GENERIC` | Lancement du test du registre de paquets génériques | `false` | | `MGCI_TEST_REGISTRY_CONTAINER` | Lancement du test du registre des conteneurs | `false` | | `MGCI_TEST_RUNNERS_TAGS` | Lancement du test des tags de runner | `false` | +| `MGCI_TEST_TERRAFORM_MODULE` | Lancement du test de module Terraform | `false` | | `MGCI_RUNNERS_TAGS` | Liste des Tags de runners à tester, format | `` | | `MGCI_API_TOKEN` | Token d'accès `Owner` au dépôt pour tester l'API | `` | | `MATTERMOST_URL` | URL de l'instance Mattermost à tester | `` | @@ -220,15 +221,15 @@ MGCI_TEST_CACHE_JOB_TAGS=cache - [x] Registre : NPM - [x] Proxy de dépendances - [x] Releases -- [X] Runners : tags attendus -- [X] Runners : enregistrement et suppression d'un runner +- [x] Runners : tags attendus +- [x] Runners : enregistrement et suppression d'un runner - [x] Health check (uniquement disponible pour une instance auto-hébergée) -- [X] Mattermost : Health check -- [X] Environnement : création et destruction +- [x] Mattermost : Health check +- [x] Environnement : création et destruction +- [x] Terraform module ### 🏗️ À venir -- [ ] [Terraform module](https://gitlab.com/froggit/tools/mgci/-/issues/16) - [ ] [Terraform state](https://gitlab.com/froggit/tools/mgci/-/issues/13) ## Test de l'API @@ -311,6 +312,12 @@ Si vous activez ce test, il faut également créer la variable `MATTERMOST_URL`, - **Nom des jobs** : `runners:register` - **Description** : le job `runners:register` teste la fonctionnalité d'enregistrement et de suppression d'un runner. Il va enregistrer un runner pour le projet avec le tag `test-runner` puis le supprimer tout de suite après. +## Test du module Terraform + +- **Nom du fichier** : `templates/terraform.yml` +- **Nom des jobs** : `terraform:module:upload` et `terraform:module:test` +-- **Description** : le job `terraform:module:upload` va téléverser notre module Terraform de test situé dans `src/terraform` vers le registre de modules Terraform de Gitlab. Dans un second temps, le job `terraform:module:test` va tester ce module. + ## Contribuer Ce projet étant sous la licence [GPL3](https://www.gnu.org/licenses/gpl.html), il est possible d'y contribuer pour y apporter des améliorations, des nouvelles fonctionnalités. diff --git a/README.md b/README.md index 762386d..013bad5 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Each tested feature can be activated with a `MGCI_TEST_` variable that | `MGCI_TEST_REGISTRY_GENERIC` | Run the generic packages registry test | `false` | | `MGCI_TEST_REGISTRY_CONTAINER` | Run the container registry test | `false` | | `MGCI_TEST_RUNNERS_TAGS` | Run the runner tags test | `false` | +| `MGCI_TEST_TERRAFORM_MODULE` | Run the Terraform module test | `false` | | `MGCI_RUNNERS_TAGS` | List of runner tags to test, format | `` | | `MGCI_API_TOKEN` | `Owner` access token to the repository for API testing | `` | | `MATTERMOST_URL` | URL of the Mattermost instance to test | `` | @@ -224,6 +225,7 @@ MGCI_TEST_CACHE_JOB_TAGS=cache - [x] Health check (only available for self-hosted Gitlab) - [x] Mattermost: Health check - [x] Environment: creation and deletion +- [x] Terraform module ### 🏗️ Upcoming @@ -312,6 +314,12 @@ If you enable this test, you also need to create the `MATTERMOST_URL` variable, - **Job names**: `runners:register` - **Description**: the `runners:register` job tests the functionality of registering and deleting a runner. It will register a runner for the project with the `test-runner` tag and then delete it immediately after. +## Test du module Terraform + +- **File name** : `templates/terraform.yml` +- **Job names** : `terraform:module:upload` et `terraform:module:test` +-- **Description** : the`terraform:module:upload` job uploads our Terraform test module located in `src/terraform` to the Gitlab Terraform module registry. Then, the `terraform:module:test` job tests that module. + ## Contributing This project is licensed under [GPL3](https://www.gnu.org/licenses/gpl.html), and contributions are welcome to bring improvements, new features, etc. diff --git a/includes-if/tags/terraform.yml b/includes-if/tags/terraform.yml new file mode 100644 index 0000000..6454f66 --- /dev/null +++ b/includes-if/tags/terraform.yml @@ -0,0 +1,3 @@ +--- +.terraform: + tags: [$MGCI_TEST_TERRAFORM_MODULE_MODULE_JOB_TAGS] diff --git a/src/terraform/main.tf b/src/terraform/main.tf new file mode 100644 index 0000000..623c588 --- /dev/null +++ b/src/terraform/main.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.10.5" +} + +resource "local_file" "file" { + content = var.text + filename = "${var.filename}.txt" + file_permission = "0644" + directory_permission = "0755" +} diff --git a/src/terraform/outputs.tf b/src/terraform/outputs.tf new file mode 100644 index 0000000..1d46eea --- /dev/null +++ b/src/terraform/outputs.tf @@ -0,0 +1,4 @@ +output "bytes" { + value = length(local_file.file.content) + description = "I don't know what is it" +} diff --git a/src/terraform/variables.tf b/src/terraform/variables.tf new file mode 100644 index 0000000..84eb40e --- /dev/null +++ b/src/terraform/variables.tf @@ -0,0 +1,11 @@ +variable "filename" { + description = "The filename of the file to be created." + type = string + default = null +} + +variable "text" { + description = "The text contents of the file to be created." + type = string + default = null +} diff --git a/templates/bases/variables.yml b/templates/bases/variables.yml index 52e56cf..ca5a72f 100644 --- a/templates/bases/variables.yml +++ b/templates/bases/variables.yml @@ -34,3 +34,4 @@ variables: MGCI_TEST_REGISTRY_NPM: "false" MGCI_TEST_REGISTRY_GENERIC: "false" MGCI_TEST_REGISTRY_CONTAINER: "false" + MGCI_TEST_TERRAFORM_MODULE: "false" diff --git a/templates/set_packages_version.yml b/templates/set_packages_version.yml index 4de293e..d74a895 100644 --- a/templates/set_packages_version.yml +++ b/templates/set_packages_version.yml @@ -13,3 +13,4 @@ packages:version: - if: $MGCI_TEST_RELEASE == "true" - if: $MGCI_TEST_REGISTRY_NPM == "true" - if: $MGCI_TEST_REGISTRY_GENERIC == "true" + - if: $MGCI_TEST_TERRAFORM_MODULE == "true" diff --git a/templates/terraform.yml b/templates/terraform.yml new file mode 100644 index 0000000..54c382e --- /dev/null +++ b/templates/terraform.yml @@ -0,0 +1,40 @@ +--- + +terraform:module:upload: + stage: build + image: curlimages/curl:latest + variables: + TERRAFORM_MODULE_DIR: ${CI_PROJECT_DIR}/src/terraform + TERRAFORM_MODULE_NAME: terraform-module-test + TERRAFORM_MODULE_SYSTEM: local + TERRAFORM_MODULE_VERSION: 0.1.0 + script: + - tar -cvzf ${TERRAFORM_MODULE_NAME}-${TERRAFORM_MODULE_SYSTEM}-${TERRAFORM_MODULE_VERSION}.tgz -C ${TERRAFORM_MODULE_DIR} --exclude=./.git . + - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${TERRAFORM_MODULE_NAME}-${TERRAFORM_MODULE_SYSTEM}-${TERRAFORM_MODULE_VERSION}.tgz ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/terraform/modules/${TERRAFORM_MODULE_NAME}/${TERRAFORM_MODULE_SYSTEM}/${TERRAFORM_MODULE_VERSION}/file' + rules: + - if: $MGCI_TEST_TERRAFORM_MODULE == "true" && ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PROJECT_PATH == 'froggit/tools/mgci') + +terraform:module:test: + stage: test + image: + name: hashicorp/terraform:latest + entrypoint: [""] + before_script: + - cd ${CI_PROJECT_DIR}/test/terraform + - sed -i "s/gitlab\.com/${CI_SERVER_HOST}/g" main.tf + variables: + FILENAME: test + TEXT: "Hello world" + script: + - terraform init + - terraform validate + - terraform fmt --check + - terraform apply -auto-approve -var="filename=${FILENAME}" -var="text=${TEXT}" + - cat ${FILENAME}.txt | grep "${TEXT}" + rules: + - if: $MGCI_TEST_TERRAFORM_MODULE == "true" && ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PROJECT_PATH == 'froggit/tools/mgci') + +include: + - local: 'includes-if/tags/terraform.yml' + rules: + - if: $MGCI_TEST_TERRAFORM_MODULE_JOB_TAGS diff --git a/test/terraform/main.tf b/test/terraform/main.tf new file mode 100644 index 0000000..3a2ed88 --- /dev/null +++ b/test/terraform/main.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.10.5" +} + +module "terraform_module_test" { + source = "gitlab.com/froggit/terraform_module_test/local" + version = "0.1.0" + filename = var.filename + text = var.text +} diff --git a/test/terraform/variables.tf b/test/terraform/variables.tf new file mode 100644 index 0000000..84eb40e --- /dev/null +++ b/test/terraform/variables.tf @@ -0,0 +1,11 @@ +variable "filename" { + description = "The filename of the file to be created." + type = string + default = null +} + +variable "text" { + description = "The text contents of the file to be created." + type = string + default = null +} -- GitLab From c8948418cf8e1d93f098aab468e20c8da2e65bee Mon Sep 17 00:00:00 2001 From: Arthur BOUDREAULT Date: Tue, 18 Feb 2025 11:49:05 +0100 Subject: [PATCH 2/4] refactor: disabled REPOSITORY_CHECKOV and TERRAFORM_TERRASCAN lints to prevent wrong error to appear in megalinter --- .mega-linter.yml | 2 ++ src/terraform/main.tf | 2 +- src/terraform/outputs.tf | 4 ++-- test/terraform/main.tf | 10 +++++----- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.mega-linter.yml b/.mega-linter.yml index 202985d..4702c00 100644 --- a/.mega-linter.yml +++ b/.mega-linter.yml @@ -21,6 +21,8 @@ DISABLE: DISABLE_LINTERS: - JSON_V8R - YAML_PRETTIER + - REPOSITORY_CHECKOV + - TERRAFORM_TERRASCAN FLAVOR_SUGGESTIONS: false diff --git a/src/terraform/main.tf b/src/terraform/main.tf index 623c588..0514e4f 100644 --- a/src/terraform/main.tf +++ b/src/terraform/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = ">= 1.10.5" + required_version = "= 1.10.5" } resource "local_file" "file" { diff --git a/src/terraform/outputs.tf b/src/terraform/outputs.tf index 1d46eea..b9a847c 100644 --- a/src/terraform/outputs.tf +++ b/src/terraform/outputs.tf @@ -1,4 +1,4 @@ output "bytes" { - value = length(local_file.file.content) - description = "I don't know what is it" + value = length(local_file.file.content) + description = "Lorem ipsum" } diff --git a/test/terraform/main.tf b/test/terraform/main.tf index 3a2ed88..f5b2d38 100644 --- a/test/terraform/main.tf +++ b/test/terraform/main.tf @@ -1,10 +1,10 @@ terraform { - required_version = ">= 1.10.5" + required_version = "= 1.10.5" } -module "terraform_module_test" { - source = "gitlab.com/froggit/terraform_module_test/local" - version = "0.1.0" +module "terraform-module-test" { + source = "gitlab.com/froggit/terraform-module-test/local" + version = "0.1.0" filename = var.filename - text = var.text + text = var.text } -- GitLab From bb3d82ce49b621bdfe066f88fb17026bb2c59475 Mon Sep 17 00:00:00 2001 From: Arthur BOUDREAULT Date: Tue, 4 Mar 2025 10:25:04 +0100 Subject: [PATCH 3/4] fix: update Terraform image version to 1.10.5 --- templates/terraform.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/terraform.yml b/templates/terraform.yml index 54c382e..e6f2821 100644 --- a/templates/terraform.yml +++ b/templates/terraform.yml @@ -17,7 +17,7 @@ terraform:module:upload: terraform:module:test: stage: test image: - name: hashicorp/terraform:latest + name: hashicorp/terraform:1.10.5 entrypoint: [""] before_script: - cd ${CI_PROJECT_DIR}/test/terraform -- GitLab From cf493952c794d23e2ae9ab0f065b41a0fda0184f Mon Sep 17 00:00:00 2001 From: Arthur BOUDREAULT Date: Mon, 7 Apr 2025 16:03:03 +0200 Subject: [PATCH 4/4] refactor: add Christophe suggestion --- README-fr.md | 4 ++-- README.md | 5 ++--- .../tags/{terraform.yml => terraform_module.yml} | 0 src/terraform/outputs.tf | 4 ---- .../{terraform.yml => terraform_module.yml} | 16 ++++++++++------ test/terraform/main.tf | 6 +++--- 6 files changed, 17 insertions(+), 18 deletions(-) rename includes-if/tags/{terraform.yml => terraform_module.yml} (100%) delete mode 100644 src/terraform/outputs.tf rename templates/{terraform.yml => terraform_module.yml} (83%) diff --git a/README-fr.md b/README-fr.md index da6ce12..195e0e2 100644 --- a/README-fr.md +++ b/README-fr.md @@ -219,6 +219,7 @@ MGCI_TEST_CACHE_JOB_TAGS=cache - [x] Registre : purge des conteneurs - [x] Registre : générique - [x] Registre : NPM +- [x] Registre : Terraform module - [x] Proxy de dépendances - [x] Releases - [x] Runners : tags attendus @@ -226,7 +227,6 @@ MGCI_TEST_CACHE_JOB_TAGS=cache - [x] Health check (uniquement disponible pour une instance auto-hébergée) - [x] Mattermost : Health check - [x] Environnement : création et destruction -- [x] Terraform module ### 🏗️ À venir @@ -314,7 +314,7 @@ Si vous activez ce test, il faut également créer la variable `MATTERMOST_URL`, ## Test du module Terraform -- **Nom du fichier** : `templates/terraform.yml` +- **Nom du fichier** : `templates/terraform_module.yml` - **Nom des jobs** : `terraform:module:upload` et `terraform:module:test` -- **Description** : le job `terraform:module:upload` va téléverser notre module Terraform de test situé dans `src/terraform` vers le registre de modules Terraform de Gitlab. Dans un second temps, le job `terraform:module:test` va tester ce module. diff --git a/README.md b/README.md index 013bad5..8158182 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,7 @@ MGCI_TEST_CACHE_JOB_TAGS=cache - [x] Registry: Container cleanup - [x] Registry: Generic - [x] Registry: NPM +- [x] Registry: Terraform module - [x] Dependency proxy - [x] Releases - [x] Runners: expected tags @@ -225,11 +226,9 @@ MGCI_TEST_CACHE_JOB_TAGS=cache - [x] Health check (only available for self-hosted Gitlab) - [x] Mattermost: Health check - [x] Environment: creation and deletion -- [x] Terraform module ### 🏗️ Upcoming -- [ ] [Terraform module](https://gitlab.com/froggit/tools/mgci/-/issues/16) - [ ] [Terraform state](https://gitlab.com/froggit/tools/mgci/-/issues/13) --- @@ -316,7 +315,7 @@ If you enable this test, you also need to create the `MATTERMOST_URL` variable, ## Test du module Terraform -- **File name** : `templates/terraform.yml` +- **File name** : `templates/terraform_module.yml` - **Job names** : `terraform:module:upload` et `terraform:module:test` -- **Description** : the`terraform:module:upload` job uploads our Terraform test module located in `src/terraform` to the Gitlab Terraform module registry. Then, the `terraform:module:test` job tests that module. diff --git a/includes-if/tags/terraform.yml b/includes-if/tags/terraform_module.yml similarity index 100% rename from includes-if/tags/terraform.yml rename to includes-if/tags/terraform_module.yml diff --git a/src/terraform/outputs.tf b/src/terraform/outputs.tf deleted file mode 100644 index b9a847c..0000000 --- a/src/terraform/outputs.tf +++ /dev/null @@ -1,4 +0,0 @@ -output "bytes" { - value = length(local_file.file.content) - description = "Lorem ipsum" -} diff --git a/templates/terraform.yml b/templates/terraform_module.yml similarity index 83% rename from templates/terraform.yml rename to templates/terraform_module.yml index e6f2821..8dfc152 100644 --- a/templates/terraform.yml +++ b/templates/terraform_module.yml @@ -1,27 +1,33 @@ --- +.terraform: + rules: + - if: $MGCI_TEST_TERRAFORM_MODULE == "true" && ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PROJECT_PATH == 'froggit/tools/mgci') + terraform:module:upload: + extends: .terraform stage: build image: curlimages/curl:latest variables: TERRAFORM_MODULE_DIR: ${CI_PROJECT_DIR}/src/terraform - TERRAFORM_MODULE_NAME: terraform-module-test + TERRAFORM_MODULE_NAME: mgci-test-terraform-module TERRAFORM_MODULE_SYSTEM: local - TERRAFORM_MODULE_VERSION: 0.1.0 + TERRAFORM_MODULE_VERSION: ${PACKAGE_VERSION} script: - tar -cvzf ${TERRAFORM_MODULE_NAME}-${TERRAFORM_MODULE_SYSTEM}-${TERRAFORM_MODULE_VERSION}.tgz -C ${TERRAFORM_MODULE_DIR} --exclude=./.git . - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${TERRAFORM_MODULE_NAME}-${TERRAFORM_MODULE_SYSTEM}-${TERRAFORM_MODULE_VERSION}.tgz ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/terraform/modules/${TERRAFORM_MODULE_NAME}/${TERRAFORM_MODULE_SYSTEM}/${TERRAFORM_MODULE_VERSION}/file' - rules: - - if: $MGCI_TEST_TERRAFORM_MODULE == "true" && ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PROJECT_PATH == 'froggit/tools/mgci') terraform:module:test: + extends: .terraform stage: test + needs: ["terraform:module:upload"] image: name: hashicorp/terraform:1.10.5 entrypoint: [""] before_script: - cd ${CI_PROJECT_DIR}/test/terraform - sed -i "s/gitlab\.com/${CI_SERVER_HOST}/g" main.tf + - sed -i "s/version = \".*\"/version = \"${PACKAGE_VERSION}\"/g" main.tf variables: FILENAME: test TEXT: "Hello world" @@ -31,8 +37,6 @@ terraform:module:test: - terraform fmt --check - terraform apply -auto-approve -var="filename=${FILENAME}" -var="text=${TEXT}" - cat ${FILENAME}.txt | grep "${TEXT}" - rules: - - if: $MGCI_TEST_TERRAFORM_MODULE == "true" && ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_PROJECT_PATH == 'froggit/tools/mgci') include: - local: 'includes-if/tags/terraform.yml' diff --git a/test/terraform/main.tf b/test/terraform/main.tf index f5b2d38..b844fc6 100644 --- a/test/terraform/main.tf +++ b/test/terraform/main.tf @@ -2,9 +2,9 @@ terraform { required_version = "= 1.10.5" } -module "terraform-module-test" { - source = "gitlab.com/froggit/terraform-module-test/local" - version = "0.1.0" +module "mgci-test-terraform-module" { + source = "gitlab.com/froggit/mgci-test-terraform-module/local" + version = "0.0.0" filename = var.filename text = var.text } -- GitLab