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

Merge branch 'refactor/variables' into 'main'

Refactor environment variable handling in gitlab-tofu

See merge request components/opentofu!189
parents 4545a68d 17e0213d
Branches
Tags
No related merge requests found
......@@ -5,8 +5,8 @@ include:
version: $CI_COMMIT_SHA
base_os: $GITLAB_OPENTOFU_BASE_IMAGE_OS
opentofu_version: $OPENTOFU_VERSION
root_dir: $TEST_TF_ROOT
state_name: $TEST_TF_STATE_NAME
root_dir: $TEST_GITLAB_TOFU_ROOT_DIR
state_name: $TEST_GITLAB_TOFU_STATE_NAME
fmt_rules: [{when: on_success}]
validate_rules: [{when: on_success}]
plan_rules: [{when: on_success}]
......@@ -19,7 +19,7 @@ include:
- component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/delete-state@$CI_COMMIT_SHA
inputs:
stage: cleanup
state_name: $TEST_TF_STATE_NAME
state_name: $TEST_GITLAB_TOFU_STATE_NAME
rules: [{when: always}]
stages: [test, cleanup]
......@@ -6,8 +6,8 @@ include:
opentofu_version: $OPENTOFU_VERSION
as: 'setup:apply'
stage: setup
root_dir: $TEST_TF_ROOT
state_name: $TEST_TF_STATE_NAME
root_dir: $TEST_GITLAB_TOFU_ROOT_DIR
state_name: $TEST_GITLAB_TOFU_STATE_NAME
no_plan: true
# Required to run everything immediately, instead of manually.
rules: [{when: always}]
......@@ -17,8 +17,8 @@ include:
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
root_dir: $TEST_GITLAB_TOFU_ROOT_DIR
state_name: $TEST_GITLAB_TOFU_STATE_NAME
# Required to run everything immediately, instead of manually.
fmt_rules: [{when: always}]
validate_rules: [{when: always}]
......
......@@ -5,8 +5,8 @@ include:
version: $CI_COMMIT_SHA
base_os: $GITLAB_OPENTOFU_BASE_IMAGE_OS
opentofu_version: $OPENTOFU_VERSION
root_dir: $TEST_TF_ROOT
state_name: $TEST_TF_STATE_NAME
root_dir: $TEST_GITLAB_TOFU_ROOT_DIR
state_name: $TEST_GITLAB_TOFU_STATE_NAME
var_file: varfile.integration-test.tfvars
# Required to run everything immediately, instead of manually.
fmt_rules: [{when: always}]
......
......@@ -5,14 +5,14 @@ include:
version: $CI_COMMIT_SHA
base_os: $GITLAB_OPENTOFU_BASE_IMAGE_OS
opentofu_version: $OPENTOFU_VERSION
root_dir: $TEST_TF_ROOT
state_name: $TEST_TF_STATE_NAME
root_dir: $TEST_GITLAB_TOFU_ROOT_DIR
state_name: $TEST_GITLAB_TOFU_STATE_NAME
warning_on_non_empty_plan: true
# For CI Terraform state cleanup
- component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/delete-state@$CI_COMMIT_SHA
inputs:
state_name: $TEST_TF_STATE_NAME
state_name: $TEST_GITLAB_TOFU_STATE_NAME
rules: [{when: always}]
stages: [build, cleanup, verify]
......
......@@ -2,8 +2,8 @@ component:
stage: test-integration
variables:
OPENTOFU_VERSION: $LATEST_OPENTOFU_VERSION
TEST_TF_STATE_NAME: ci-integration-$CI_PIPELINE_IID-$CI_NODE_INDEX
TEST_TF_ROOT: tests/iac
TEST_GITLAB_TOFU_STATE_NAME: ci-integration-$CI_PIPELINE_IID-$CI_NODE_INDEX
TEST_GITLAB_TOFU_ROOT_DIR: tests/iac
trigger:
include: tests/integration-tests/$PIPELINE_NAME.gitlab-ci.yml
strategy: depend
......
......@@ -7,7 +7,7 @@ load "$BATS_LIBS/bats-support/load"
load "$BATS_LIBS/bats-assert/load"
setup() {
export DEBUG_OUTPUT=true
export GITLAB_TOFU_DEBUG=true
# Change cwd to test specific directory
cd "$BATS_TEST_TMPDIR"
......@@ -16,14 +16,14 @@ setup() {
cp -r "$TEST_PROJECT_DIR" "$BATS_TEST_TMPDIR"
# Set TF root directory to temporary location
export TF_ROOT="$BATS_TEST_TMPDIR/$(basename "$TEST_PROJECT_DIR")"
export GITLAB_TOFU_ROOT_DIR="$BATS_TEST_TMPDIR/$(basename "$TEST_PROJECT_DIR")"
# Set state so that each test has its own
export TF_STATE_NAME="ci-unit-$CI_JOB_ID-$BATS_SUITE_TEST_NUMBER"
export GITLAB_TOFU_STATE_NAME="ci-unit-$CI_JOB_ID-$BATS_SUITE_TEST_NUMBER"
}
teardown() {
curl --request DELETE -u "gitlab-ci-token:$CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$TF_STATE_NAME"
curl --request DELETE -u "gitlab-ci-token:$CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$GITLAB_TOFU_STATE_NAME"
}
@test "gitlab-tofu init" {
......@@ -35,37 +35,37 @@ teardown() {
}
@test "gitlab-tofu init with environment variable flags" {
export TF_INIT_FLAGS="-get=true -no-color"
export GITLAB_TOFU_INIT_FLAGS="-get=true -no-color"
gitlab-tofu init
}
@test "gitlab-tofu init with args and environment variable flags" {
export TF_INIT_FLAGS="-get=true"
export GITLAB_TOFU_INIT_FLAGS="-get=true"
gitlab-tofu init -no-color
}
@test "gitlab-tofu init within TF_ROOT set" {
cd "$TF_ROOT"
unset "$TF_ROOT"
@test "gitlab-tofu init within GITLAB_TOFU_ROOT_DIR set" {
cd "$GITLAB_TOFU_ROOT_DIR"
unset "$GITLAB_TOFU_ROOT_DIR"
gitlab-tofu init -no-color
}
@test "gitlab-tofu init without reconfigure" {
gitlab-tofu init
cat <<EOF > $TF_ROOT/backend_override.tf
cat <<EOF > $GITLAB_TOFU_ROOT_DIR/backend_override.tf
terraform {
backend "local" {}
}
EOF
export TF_INIT_NO_RECONFIGURE="true"
export GITLAB_TOFU_INIT_NO_RECONFIGURE="true"
run ! gitlab-tofu init -no-color
assert_output --partial "Error: Backend configuration changed"
}
@test "gitlab-tofu init with reconfigure" {
cat <<EOF > $TF_ROOT/backend_override.tf
cat <<EOF > $GITLAB_TOFU_ROOT_DIR/backend_override.tf
terraform {
backend "local" {}
}
......@@ -82,56 +82,56 @@ EOF
}
@test "gitlab-tofu plan" {
export TF_PLAN_CACHE="test-plan.cache"
export GITLAB_TOFU_PLAN_CACHE="test-plan.cache"
gitlab-tofu plan
if [ ! -f "$TF_ROOT/$TF_PLAN_CACHE" ]; then
if [ ! -f "$GITLAB_TOFU_ROOT_DIR/$GITLAB_TOFU_PLAN_CACHE" ]; then
echo "expected to find a plan.cache file"
exit 1
fi
gitlab-tofu plan-json
if [ ! -f "$TF_ROOT/plan.json" ]; then
if [ ! -f "$GITLAB_TOFU_ROOT_DIR/plan.json" ]; then
echo "expected to find a plan.json file"
exit 1
fi
}
@test "gitlab-tofu apply" {
export TF_PLAN_CACHE="test-plan.cache"
export GITLAB_TOFU_PLAN_CACHE="test-plan.cache"
gitlab-tofu plan
gitlab-tofu apply
}
@test "gitlab-tofu destroy" {
export TF_PLAN_CACHE="test-plan.cache"
export GITLAB_TOFU_PLAN_CACHE="test-plan.cache"
gitlab-tofu plan
gitlab-tofu apply
gitlab-tofu destroy
}
@test "gitlab-tofu validate without implicit init" {
export TF_IMPLICIT_INIT=false
export GITLAB_TOFU_IMPLICIT_INIT=false
run ! gitlab-tofu validate -no-color
assert_output --partial 'This module is not yet installed'
}
@test "gitlab-tofu plan without implicit init" {
export TF_IMPLICIT_INIT=false
export GITLAB_TOFU_IMPLICIT_INIT=false
run ! gitlab-tofu plan -no-color
assert_output --partial 'Error: Backend initialization required'
}
@test "gitlab-tofu apply without implicit init" {
export TF_IMPLICIT_INIT=false
export GITLAB_TOFU_IMPLICIT_INIT=false
run ! gitlab-tofu apply -no-color
assert_output --partial 'Error: Failed to load '
}
@test "gitlab-tofu destroy without implicit init" {
export TF_IMPLICIT_INIT=false
export GITLAB_TOFU_IMPLICIT_INIT=false
run ! gitlab-tofu destroy -no-color
assert_output --partial 'Error: Backend initialization required'
......@@ -139,7 +139,7 @@ EOF
@test "gitlab-tofu no wrap" {
# NOTE: running `gitlab-tofu apply` wouldn't fail
# because of the implicit `terraform init`.
# because of the implicit `tofu init`.
run gitlab-tofu -- apply -no-color
assert_failure
assert_output --partial 'Error: Backend initialization required, please run "tofu init"'
......@@ -187,9 +187,9 @@ EOF
cat <<'EOF' > test.sh
set -x
export TF_STATE_NAME=production/europe
export GITLAB_TOFU_STATE_NAME=production/europe
. $(which gitlab-tofu)
test "$TF_STATE_NAME" = "production%2Feurope"
test "$backend_state_name" = "production%2Feurope"
EOF
$SHELL test.sh
......@@ -206,7 +206,7 @@ set -x
export CI_SERVER_HOST=gitlab.example.com
export TF_TOKEN_gitlab_example_com=mysecrettoken
. $(which gitlab-tofu)
terraform_authenticate_private_registry
tofu_authenticate_private_registry
test "$TF_TOKEN_gitlab_example_com" = "mysecrettoken"
EOF
......@@ -224,7 +224,7 @@ set -x
export CI_SERVER_HOST=gitlab.example.com
export TF_TOKEN_gitlab_example_com=mysecrettoken
. $(which gitlab-tofu)
terraform_authenticate_private_registry
tofu_authenticate_private_registry
test "$TF_TOKEN_gitlab_example_com" = "mysecrettoken"
EOF
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment