-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from dflook/tf-test
Add terraform-test and tofu-test actions
- Loading branch information
Showing
15 changed files
with
826 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.