Skip to content

Commit

Permalink
test: refactor e2e setup and add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannibaratta committed Nov 13, 2023
1 parent 4b672ec commit 5bf502a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion e2e-tests/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set -e
SCRIPT_DIR=$(readlink -f $(dirname "$0"))

docker build --tag bats-e2e-custom-image "${SCRIPT_DIR}"
docker run -it -v "${SCRIPT_DIR}/suites:/code/suites:ro" -v "${SCRIPT_DIR}/terraapprove:/code/terraapprove:ro" bats-e2e-custom-image -r suites
docker run -t -v "${SCRIPT_DIR}/suites:/code/suites:ro" -v "${SCRIPT_DIR}/terraapprove:/code/terraapprove:ro" bats-e2e-custom-image -r suites
13 changes: 13 additions & 0 deletions e2e-tests/suites/error_handling/artifacts/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "null_resource" "untagged_resource" {
triggers = {
always_run = timestamp()
}
}

terraform {
required_providers {
null = {
source = "hashicorp/null"
}
}
}
39 changes: 39 additions & 0 deletions e2e-tests/suites/error_handling/error_handling.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 2 if the plan does not exist" {
# When
run "/code/terraapprove" "." "./test.tfplan.json"

# Expect
[ "$status" -ge 2 ]
grep "Error while reading plan" <<< "$output"
}

@test "should return 2 if the code base does not exist" {
#Given
terraform plan -out test.tfplan > /dev/null
terraform show -json test.tfplan > ${BATS_TEST_TMPDIR}/test.tfplan.json

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

# Expect
[ "$status" -ge 2 ]
grep "Error while reading code base" <<< "$output"
}
4 changes: 4 additions & 0 deletions e2e-tests/suites/simple/artifacts/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @RequireApproval()
resource "null_resource" "do_nothing" {
triggers = {}
}

resource "null_resource" "untagged_resource" {
triggers = {
always_run = timestamp()
}
Expand Down
31 changes: 25 additions & 6 deletions e2e-tests/suites/simple/simple.bats
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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
export TF_DATA_DIR="${BATS_TEST_TMPDIR}"
terraform -chdir=$BATS_TEST_TMPDIR init > /dev/null
cd $BATS_TEST_TMPDIR
terraform init > /dev/null

}

teardown() {
TF_DATA_DIR=
cd $ORIGINAL_DIR

# Print additional information when a test fails
echo "$status"
Expand All @@ -16,12 +18,29 @@ teardown() {

@test "should return 1 if the plan requires approval" {
# Given
terraform -chdir=$BATS_TEST_TMPDIR plan -out "${BATS_TEST_TMPDIR}/test.tfplan" > /dev/null
terraform -chdir=$BATS_TEST_TMPDIR show -json "${BATS_TEST_TMPDIR}/test.tfplan" > "${BATS_TEST_TMPDIR}/test.tfplan.json"
terraform plan -out test.tfplan > /dev/null
terraform show -json test.tfplan > ${BATS_TEST_TMPDIR}/test.tfplan.json

# When
run "/code/terraapprove" "${BATS_TEST_TMPDIR}" "${BATS_TEST_TMPDIR}/test.tfplan.json"
run "/code/terraapprove" "." "./test.tfplan.json"

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

@test "should return 0 if the plan does not requires approval" {
# Given

# During first apply two resources should be added
terraform apply -auto-approve > /dev/null

# A second apply should triggered the recreation of the untagged resource only.
terraform plan -out test.tfplan > /dev/null
terraform show -json test.tfplan > test.tfplan.json

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

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

0 comments on commit 5bf502a

Please sign in to comment.