Skip to content
Snippets Groups Projects
Unverified Commit b73e81f1 authored by Timo Furrer's avatar Timo Furrer
Browse files

Add tofu tests and template

parent aac4ec40
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,7 @@ include: ...@@ -37,7 +37,7 @@ include:
version: <VERSION> version: <VERSION>
opentofu_version: <OPENTOFU_VERSION> opentofu_version: <OPENTOFU_VERSION>
stages: [validate, build, deploy, cleanup] stages: [validate, test, build, deploy, cleanup]
--- ---
...@@ -67,7 +67,7 @@ include: ...@@ -67,7 +67,7 @@ include:
version: 0.10.0 version: 0.10.0
opentofu_version: 1.6.1 opentofu_version: 1.6.1
stages: [validate, build, deploy, cleanup] stages: [validate, test, build, deploy, cleanup]
--- ---
...@@ -82,7 +82,7 @@ include: ...@@ -82,7 +82,7 @@ include:
version: latest version: latest
opentofu_version: 1.6.1 opentofu_version: 1.6.1
stages: [validate, build, deploy, cleanup] stages: [validate, test, build, deploy, cleanup]
``` ```
Or import all jobs as hidden templates ready to be extended: Or import all jobs as hidden templates ready to be extended:
...@@ -161,6 +161,7 @@ The following job components exist: ...@@ -161,6 +161,7 @@ The following job components exist:
- [`fmt`](templates/fmt.yml) - [`fmt`](templates/fmt.yml)
- [`validate`](templates/validate.yml) - [`validate`](templates/validate.yml)
- [`test`](templates/test.yml)
- [`plan`](templates/plan.yml) - [`plan`](templates/plan.yml)
- [`apply`](templates/apply.yml) - [`apply`](templates/apply.yml)
- [`destroy`](templates/destroy.yml) - [`destroy`](templates/destroy.yml)
......
...@@ -39,7 +39,7 @@ include: ...@@ -39,7 +39,7 @@ include:
version: <VERSION> version: <VERSION>
opentofu_version: <OPENTOFU_VERSION> opentofu_version: <OPENTOFU_VERSION>
stages: [validate, build, deploy, cleanup] stages: [validate, test, build, deploy, cleanup]
--- ---
...@@ -69,7 +69,7 @@ include: ...@@ -69,7 +69,7 @@ include:
version: 0.10.0 version: 0.10.0
opentofu_version: 1.6.1 opentofu_version: 1.6.1
stages: [validate, build, deploy, cleanup] stages: [validate, test, build, deploy, cleanup]
--- ---
...@@ -84,7 +84,7 @@ include: ...@@ -84,7 +84,7 @@ include:
version: latest version: latest
opentofu_version: 1.6.1 opentofu_version: 1.6.1
stages: [validate, build, deploy, cleanup] stages: [validate, test, build, deploy, cleanup]
``` ```
Or import all jobs as hidden templates ready to be extended: Or import all jobs as hidden templates ready to be extended:
...@@ -163,6 +163,7 @@ The following job components exist: ...@@ -163,6 +163,7 @@ The following job components exist:
- [`fmt`](templates/fmt.yml) - [`fmt`](templates/fmt.yml)
- [`validate`](templates/validate.yml) - [`validate`](templates/validate.yml)
- [`test`](templates/test.yml)
- [`plan`](templates/plan.yml) - [`plan`](templates/plan.yml)
- [`apply`](templates/apply.yml) - [`apply`](templates/apply.yml)
- [`destroy`](templates/destroy.yml) - [`destroy`](templates/destroy.yml)
...@@ -176,6 +177,7 @@ Have a look at the individual template spec to learn about the available inputs. ...@@ -176,6 +177,7 @@ Have a look at the individual template spec to learn about the available inputs.
| Name | Default | Description | | Name | Default | Description |
| ---- | ------- | ----------- | | ---- | ------- | ----------- |
| `stage_validate` | `validate` | Defines the validate stage. This stage includes the `fmt` and `validate` jobs. | | `stage_validate` | `validate` | Defines the validate stage. This stage includes the `fmt` and `validate` jobs. |
| `stage_test` | `test` | Defines the test stage. This stage includes the `test` job. |
| `stage_build` | `build` | Defines the build stage. This stage includes the `plan` job. | | `stage_build` | `build` | Defines the build stage. This stage includes the `plan` job. |
| `stage_deploy` | `deploy` | Defines the deploy stage. This stage includes the `apply` job. | | `stage_deploy` | `deploy` | Defines the deploy stage. This stage includes the `apply` job. |
| `stage_cleanup` | `cleanup` | Defines the cleanup stage. This stage includes the `destroy` and `delete-state` jobs. | | `stage_cleanup` | `cleanup` | Defines the cleanup stage. This stage includes the `destroy` and `delete-state` jobs. |
......
...@@ -172,6 +172,10 @@ if [ $sourced -eq 0 ]; then ...@@ -172,6 +172,10 @@ if [ $sourced -eq 0 ]; then
$TF_IMPLICIT_INIT && terraform_init -backend=false $TF_IMPLICIT_INIT && terraform_init -backend=false
tofu "${TF_CHDIR_OPT}" "${@}" tofu "${TF_CHDIR_OPT}" "${@}"
;; ;;
"test")
$TF_IMPLICIT_INIT && terraform_init -backend=false
tofu "${TF_CHDIR_OPT}" "${@}"
;;
--) --)
shift shift
tofu "${TF_CHDIR_OPT}" "${@}" tofu "${TF_CHDIR_OPT}" "${@}"
......
...@@ -4,6 +4,9 @@ spec: ...@@ -4,6 +4,9 @@ spec:
stage_validate: stage_validate:
default: 'validate' default: 'validate'
description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.'
stage_test:
default: 'test'
description: 'Defines the test stage. This stage includes the `test` job.'
stage_build: stage_build:
default: 'build' default: 'build'
description: 'Defines the build stage. This stage includes the `plan` job.' description: 'Defines the build stage. This stage includes the `plan` job.'
...@@ -82,6 +85,19 @@ include: ...@@ -82,6 +85,19 @@ include:
image_name: $[[ inputs.image_name ]] image_name: $[[ inputs.image_name ]]
root_dir: $[[ inputs.root_dir ]] root_dir: $[[ inputs.root_dir ]]
state_name: $[[ inputs.state_name ]] state_name: $[[ inputs.state_name ]]
- local: '/templates/test.yml'
inputs:
as: 'test'
stage: $[[ inputs.stage_test ]]
version: $[[ inputs.version ]]
opentofu_version: $[[ inputs.opentofu_version ]]
image_registry_base: $[[ inputs.image_registry_base ]]
image_name: $[[ inputs.image_name ]]
root_dir: $[[ inputs.root_dir ]]
state_name: $[[ inputs.state_name ]]
rules:
- exists:
- $[[ inputs.root_dir ]]/**/*.tftest.hcl
- local: '/templates/plan.yml' - local: '/templates/plan.yml'
inputs: inputs:
as: 'plan' as: 'plan'
......
...@@ -4,6 +4,9 @@ spec: ...@@ -4,6 +4,9 @@ spec:
stage_validate: stage_validate:
default: 'validate' default: 'validate'
description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.'
stage_test:
default: 'test'
description: 'Defines the test stage. This stage includes the `test` job.'
stage_build: stage_build:
default: 'build' default: 'build'
description: 'Defines the build stage. This stage includes the `plan` job.' description: 'Defines the build stage. This stage includes the `plan` job.'
...@@ -85,6 +88,16 @@ include: ...@@ -85,6 +88,16 @@ include:
image_name: $[[ inputs.image_name ]] image_name: $[[ inputs.image_name ]]
root_dir: $[[ inputs.root_dir ]] root_dir: $[[ inputs.root_dir ]]
state_name: $[[ inputs.state_name ]] state_name: $[[ inputs.state_name ]]
- local: '/templates/test.yml'
inputs:
as: '$[[ inputs.job_name_prefix ]]test'
stage: $[[ inputs.stage_test ]]
version: $[[ inputs.version ]]
opentofu_version: $[[ inputs.opentofu_version ]]
image_registry_base: $[[ inputs.image_registry_base ]]
image_name: $[[ inputs.image_name ]]
root_dir: $[[ inputs.root_dir ]]
state_name: $[[ inputs.state_name ]]
- local: '/templates/plan.yml' - local: '/templates/plan.yml'
inputs: inputs:
as: '$[[ inputs.job_name_prefix ]]plan' as: '$[[ inputs.job_name_prefix ]]plan'
......
spec:
inputs:
# Job and Stage name
as:
default: 'test'
description: 'Defines the name of this job.'
stage:
default: 'test'
description: 'Defines the stage that this job will belong to.'
# Versions
# This version is only required, because we cannot access the context of the component,
# see https://gitlab.com/gitlab-org/gitlab/-/issues/438275
version:
default: 'latest'
description: 'Version of this component. Has to be the same as the one in the component include entry.'
opentofu_version:
default: '1.6.2'
options:
- '$OPENTOFU_VERSION'
- '1.6.2'
- '1.6.1'
- '1.6.0'
description: 'OpenTofu version that should be used.'
# Images
image_registry_base:
default: '$CI_REGISTRY/components/opentofu'
description: 'Host URI to the job images. Will be combined with `image_name` to construct the actual image URI.'
# FIXME: not yet possible because of https://gitlab.com/gitlab-org/gitlab/-/issues/438722
# gitlab_opentofu_image:
# # FIXME: This should reference the component tag that is used.
# # Currently, blocked by https://gitlab.com/gitlab-org/gitlab/-/issues/438275
# # default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.opentofu_version ]]'
# default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
# description: 'Tag of the gitlab-opentofu image.'
image_name:
default: 'gitlab-opentofu'
description: 'Image name for the job images. Hosted under `image_registry_base`.'
# Configuration
root_dir:
default: ${CI_PROJECT_DIR}
description: 'Root directory for the OpenTofu project.'
state_name:
default: default
description: 'Remote OpenTofu state name.'
---
'$[[ inputs.as ]]':
stage: $[[ inputs.stage ]]
needs: []
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_OPEN_MERGE_REQUESTS # Don't add it to a *branch* pipeline if it's already in a merge request pipeline.
when: never
- if: $CI_COMMIT_BRANCH # If there's no open merge request, add it to a *branch* pipeline instead.
cache:
key: "$__CACHE_KEY_HACK"
paths:
- $TF_ROOT/.terraform/
variables:
# FIXME: work around to make slashes work in `cache:key`. see https://gitlab.com/gitlab-org/gitlab/-/issues/439898
__CACHE_KEY_HACK: "$[[ inputs.root_dir ]]"
TF_ROOT: $[[ inputs.root_dir ]]
TF_STATE_NAME: $[[ inputs.state_name ]]
image:
name: '$[[ inputs.image_registry_base ]]/$[[ inputs.image_name ]]:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
script:
- gitlab-tofu test
...@@ -7,8 +7,6 @@ terraform { ...@@ -7,8 +7,6 @@ terraform {
} }
} }
provider "random" {}
resource "random_pet" "random_pet" { resource "random_pet" "random_pet" {
length = var.length length = var.length
} }
run "test" {
assert {
condition = file(local_file.foo.filename) == "foo!"
error_message = "Incorrect content in ${local_file.foo.filename}"
}
}
...@@ -17,6 +17,9 @@ fmt: ...@@ -17,6 +17,9 @@ fmt:
validate: validate:
rules: [{when: always}] rules: [{when: always}]
test:
rules: [{when: always}]
plan: plan:
rules: [{when: always}] rules: [{when: always}]
......
...@@ -7,7 +7,7 @@ include: ...@@ -7,7 +7,7 @@ include:
root_dir: $TEST_TF_ROOT root_dir: $TEST_TF_ROOT
state_name: $TEST_TF_STATE_NAME state_name: $TEST_TF_STATE_NAME
stages: [validate, test, build, deploy, cleanup] stages: [validate, build, deploy, cleanup]
# Required to run everything immediately, instead of manually. # Required to run everything immediately, instead of manually.
......
include:
- component: gitlab.com/$CI_PROJECT_PATH/test@$CI_COMMIT_SHA
inputs:
image_registry_base: $GITLAB_OPENTOFU_IMAGE_BASE
version: $CI_COMMIT_SHA
opentofu_version: $OPENTOFU_VERSION
root_dir: $TEST_TF_ROOT
state_name: $TEST_TF_STATE_NAME
stages: [test]
# Required to run everything immediately, instead of manually.
test:
rules: [{when: always}]
...@@ -11,6 +11,7 @@ component: ...@@ -11,6 +11,7 @@ component:
matrix: matrix:
- PIPELINE_NAME: [Defaults] - PIPELINE_NAME: [Defaults]
- PIPELINE_NAME: [JobTemplates] - PIPELINE_NAME: [JobTemplates]
- PIPELINE_NAME: [TestJob]
backport-templates: backport-templates:
stage: test-integration stage: test-integration
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment