Skip to content

Commit

Permalink
Merge pull request #340 from dflook/tf-test
Browse files Browse the repository at this point in the history
Add terraform-test and tofu-test actions
  • Loading branch information
dflook authored Jun 1, 2024
2 parents c47e605 + d3cb2ac commit ea260cb
Show file tree
Hide file tree
Showing 15 changed files with 826 additions and 10 deletions.
155 changes: 155 additions & 0 deletions .github/workflows/test-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
name: Test terraform-test

on:
- push

jobs:
default:
runs-on: ubuntu-latest
name: Default inputs
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test
uses: ./terraform-test
id: test
with:
path: tests/workflows/test-test/local

- name: Check Passed
run: |
if [[ "${{ steps.test.outputs.failure-reason }}" != "" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
filter:
runs-on: ubuntu-latest
name: Default path with a filter
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test
uses: ./terraform-test
id: test
with:
path: tests/workflows/test-test/local
test_filter: tests/main.tftest.hcl

- name: Check Passed
run: |
if [[ "${{ steps.test.outputs.failure-reason }}" != "" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
test_dir:
runs-on: ubuntu-latest
name: Custom test directory
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test
uses: ./terraform-test
id: test
with:
path: tests/workflows/test-test/local
test_directory: custom-test-dir
test_filter: |
custom-test-dir/another.tftest.hcl
custom-test-dir/a-third.tftest.hcl
- name: Check Passed
run: |
if [[ "${{ steps.test.outputs.failure-reason }}" != "" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
nonexistent_test_dir:
runs-on: ubuntu-latest
name: Missing test directory
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test
uses: ./terraform-test
id: nonexistent_test_dir
continue-on-error: true
with:
path: tests/workflows/test-test/local
test_directory: i-dont-exist

- name: Check failure
run: |
if [[ "${{ steps.nonexistent_test_dir.outcome }}" != "failure" ]]; then
echo "Test did not fail correctly"
exit 1
fi
if [[ "${{ steps.nonexistent_test_dir.outputs.failure-reason }}" != "no-tests" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
faulty_filter:
runs-on: ubuntu-latest
name: Filter matches no tests
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test
uses: ./terraform-test
id: faulty_filter
continue-on-error: true
with:
path: tests/workflows/test-test/local
test_filter: |
tests/this-test-does-not-exist.tftest.hcl
tests/nor-does-this-one.tftest.hcl
- name: Check failure
run: |
if [[ "${{ steps.faulty_filter.outcome }}" != "failure" ]]; then
echo "Test did not fail correctly"
exit 1
fi
if [[ "${{ steps.faulty_filter.outputs.failure-reason }}" != "no-tests" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
failing:
runs-on: ubuntu-latest
name: A failing test using variables
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test
uses: ./terraform-test
id: failing
continue-on-error: true
with:
path: tests/workflows/test-test/local
test_filter: tests/main.tftest.hcl
variables: |
length = 1
- name: Check failure-reason
run: |
if [[ "${{ steps.failing.outcome }}" != "failure" ]]; then
echo "Test did not fail correctly"
exit 1
fi
if [[ "${{ steps.failing.outputs.failure-reason }}" != "tests-failed" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ First release of the GitHub Actions:
- [dflook/terraform-new-workspace](terraform-new-workspace)
- [dflook/terraform-destroy-workspace](terraform-destroy-workspace)

[1.42.0]: https://github.com/dflook/terraform-github-actions/compare/v1.42.0...v1.42.1
[1.42.1]: https://github.com/dflook/terraform-github-actions/compare/v1.42.0...v1.42.1
[1.42.0]: https://github.com/dflook/terraform-github-actions/compare/v1.41.2...v1.42.0
[1.41.2]: https://github.com/dflook/terraform-github-actions/compare/v1.41.1...v1.41.2
[1.41.1]: https://github.com/dflook/terraform-github-actions/compare/v1.41.0...v1.41.1
Expand Down
42 changes: 33 additions & 9 deletions image/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ function init() {
end_group
}

##
# Initialize terraform for running tests
#
# This installs modules and providers for the module and all tests
function init-test() {
start_group "Initializing $TOOL_PRODUCT_NAME"

rm -rf "$TF_DATA_DIR"

if [[ -n "$INPUT_TEST_DIRECTORY" ]]; then
debug_log $TOOL_COMMAND_NAME init -input=false -backend=false -test-directory "$INPUT_TEST_DIRECTORY"
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false -test-directory $INPUT_TEST_DIRECTORY)
else
debug_log $TOOL_COMMAND_NAME init -input=false -backend=false
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false)
fi

end_group
}

function set-init-args() {
INIT_ARGS=""

Expand Down Expand Up @@ -339,15 +359,7 @@ function set-common-plan-args() {
fi
}

function set-plan-args() {
set-common-plan-args

if [[ -n "$INPUT_VAR" ]]; then
for var in $(echo "$INPUT_VAR" | tr ',' '\n'); do
PLAN_ARGS="$PLAN_ARGS -var $var"
done
fi

function set-variable-args() {
if [[ -n "$INPUT_VAR_FILE" ]]; then
for file in $(echo "$INPUT_VAR_FILE" | tr ',' '\n'); do

Expand All @@ -364,6 +376,18 @@ function set-plan-args() {
echo "$INPUT_VARIABLES" >"$STEP_TMP_DIR/variables.tfvars"
PLAN_ARGS="$PLAN_ARGS -var-file=$STEP_TMP_DIR/variables.tfvars"
fi
}

function set-plan-args() {
set-common-plan-args

if [[ -n "$INPUT_VAR" ]]; then
for var in $(echo "$INPUT_VAR" | tr ',' '\n'); do
PLAN_ARGS="$PLAN_ARGS -var $var"
done
fi

set-variable-args

export PLAN_ARGS
}
Expand Down
65 changes: 65 additions & 0 deletions image/entrypoints/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

# shellcheck source=../actions.sh
source /usr/local/actions.sh

debug
setup
init-test

exec 3>&1

function set-test-args() {
TEST_ARGS=""

if [[ -v INPUT_CLOUD_RUN && -n "$INPUT_CLOUD_RUN" ]]; then
# I have no idea what this does, it is not well documented.
TEST_ARGS="$TEST_ARGS -cloud-run=$INPUT_CLOUD_RUN"
fi

if [[ -n "$INPUT_TEST_DIRECTORY" ]]; then
TEST_ARGS="$TEST_ARGS -test-directory=$INPUT_TEST_DIRECTORY"
fi

if [[ -n "$INPUT_TEST_FILTER" ]]; then
for file in $(echo "$INPUT_TEST_FILTER" | tr ',' '\n'); do
TEST_ARGS="$TEST_ARGS -filter=$file"
done
fi
}

function test() {

debug_log $TOOL_COMMAND_NAME test -no-color $TEST_ARGS '$PLAN_ARGS' # don't expand PLAN_ARGS

set +e
# shellcheck disable=SC2086
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME test -no-color $TEST_ARGS $PLAN_ARGS) \
2>"$STEP_TMP_DIR/terraform_test.stderr" \
| tee /dev/fd/3 \
>"$STEP_TMP_DIR/terraform_test.stdout"

# shellcheck disable=SC2034
TEST_EXIT=${PIPESTATUS[0]}
set -e

cat "$STEP_TMP_DIR/terraform_test.stderr"

if [[ $TEST_EXIT -eq 0 ]]; then
# Workaround a bit of stupidity in the terraform test command
if grep -q "Success! 0 passed, 0 failed." "$STEP_TMP_DIR/terraform_test.stdout"; then
error_log "No tests found"
set_output failure-reason no-tests
exit 1
fi
else
set_output failure-reason tests-failed
exit 1
fi
}

set-test-args
PLAN_ARGS=""
set-variable-args

test
Loading

0 comments on commit ea260cb

Please sign in to comment.