Skip to content

Commit

Permalink
AB#76 tests: add test for safe to apply global actions
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannibaratta committed Nov 20, 2023
1 parent 228f628 commit 1c836cf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
safeToApply:
allResources:
actions: [ "CREATE" ]
13 changes: 13 additions & 0 deletions e2e-tests/suites/safe_to_apply/global_actions/artifacts/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "null_resource" "safe_tp_apply" {
triggers = {
always_run = timestamp()
}
}

terraform {
required_providers {
null = {
source = "hashicorp/null"
}
}
}
43 changes: 43 additions & 0 deletions e2e-tests/suites/safe_to_apply/global_actions/global_actions.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
setup() {
# Copy files to a read/write directory. This is necessary because
# Terraform generates a lock file in the working directory.
export ORIGINAL_DIR=$(pwd)
cp -r "$BATS_TEST_DIRNAME/artifacts/." $BATS_TEST_TMPDIR
cd $BATS_TEST_TMPDIR
terraform init > /dev/null
}

teardown() {
cd $ORIGINAL_DIR

# Print additional information when a test fails
echo "$status"
echo "$output"
}

@test "should return 1 if the plan contains resources that are not safe to apply" {
# Given
terraform apply -auto-approve > /dev/null

# A second apply should trigger the re-creation of the resource
terraform plan -out test.tfplan > /dev/null
terraform show -json test.tfplan > ${BATS_TEST_TMPDIR}/test.tfplan.json

# When
run "/code/terraapprove" "." "./test.tfplan.json" "--reverse"

# Expect
[ "$status" -eq 1 ]
}

@test "should return 0 if the only resource in the plan is safe to apply" {
# Given
terraform plan -out test.tfplan > /dev/null
terraform show -json test.tfplan > ${BATS_TEST_TMPDIR}/test.tfplan.json

# When
run "/code/terraapprove" "." "./test.tfplan.json"

# Expect
[ "$status" -eq 0 ]
}

0 comments on commit 1c836cf

Please sign in to comment.