Skip to content

Commit ea260cb

Browse files
authored
Merge pull request #340 from dflook/tf-test
Add terraform-test and tofu-test actions
2 parents c47e605 + d3cb2ac commit ea260cb

File tree

15 files changed

+826
-10
lines changed

15 files changed

+826
-10
lines changed

.github/workflows/test-test.yaml

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
name: Test terraform-test
2+
3+
on:
4+
- push
5+
6+
jobs:
7+
default:
8+
runs-on: ubuntu-latest
9+
name: Default inputs
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v3
13+
14+
- name: Test
15+
uses: ./terraform-test
16+
id: test
17+
with:
18+
path: tests/workflows/test-test/local
19+
20+
- name: Check Passed
21+
run: |
22+
if [[ "${{ steps.test.outputs.failure-reason }}" != "" ]]; then
23+
echo "::error:: failure-reason not set correctly"
24+
exit 1
25+
fi
26+
27+
filter:
28+
runs-on: ubuntu-latest
29+
name: Default path with a filter
30+
steps:
31+
- name: Checkout
32+
uses: actions/checkout@v3
33+
34+
- name: Test
35+
uses: ./terraform-test
36+
id: test
37+
with:
38+
path: tests/workflows/test-test/local
39+
test_filter: tests/main.tftest.hcl
40+
41+
- name: Check Passed
42+
run: |
43+
if [[ "${{ steps.test.outputs.failure-reason }}" != "" ]]; then
44+
echo "::error:: failure-reason not set correctly"
45+
exit 1
46+
fi
47+
48+
test_dir:
49+
runs-on: ubuntu-latest
50+
name: Custom test directory
51+
steps:
52+
- name: Checkout
53+
uses: actions/checkout@v3
54+
55+
- name: Test
56+
uses: ./terraform-test
57+
id: test
58+
with:
59+
path: tests/workflows/test-test/local
60+
test_directory: custom-test-dir
61+
test_filter: |
62+
custom-test-dir/another.tftest.hcl
63+
custom-test-dir/a-third.tftest.hcl
64+
65+
- name: Check Passed
66+
run: |
67+
if [[ "${{ steps.test.outputs.failure-reason }}" != "" ]]; then
68+
echo "::error:: failure-reason not set correctly"
69+
exit 1
70+
fi
71+
72+
nonexistent_test_dir:
73+
runs-on: ubuntu-latest
74+
name: Missing test directory
75+
steps:
76+
- name: Checkout
77+
uses: actions/checkout@v3
78+
79+
- name: Test
80+
uses: ./terraform-test
81+
id: nonexistent_test_dir
82+
continue-on-error: true
83+
with:
84+
path: tests/workflows/test-test/local
85+
test_directory: i-dont-exist
86+
87+
- name: Check failure
88+
run: |
89+
if [[ "${{ steps.nonexistent_test_dir.outcome }}" != "failure" ]]; then
90+
echo "Test did not fail correctly"
91+
exit 1
92+
fi
93+
94+
if [[ "${{ steps.nonexistent_test_dir.outputs.failure-reason }}" != "no-tests" ]]; then
95+
echo "::error:: failure-reason not set correctly"
96+
exit 1
97+
fi
98+
99+
faulty_filter:
100+
runs-on: ubuntu-latest
101+
name: Filter matches no tests
102+
steps:
103+
- name: Checkout
104+
uses: actions/checkout@v3
105+
106+
- name: Test
107+
uses: ./terraform-test
108+
id: faulty_filter
109+
continue-on-error: true
110+
with:
111+
path: tests/workflows/test-test/local
112+
test_filter: |
113+
tests/this-test-does-not-exist.tftest.hcl
114+
tests/nor-does-this-one.tftest.hcl
115+
116+
- name: Check failure
117+
run: |
118+
if [[ "${{ steps.faulty_filter.outcome }}" != "failure" ]]; then
119+
echo "Test did not fail correctly"
120+
exit 1
121+
fi
122+
123+
if [[ "${{ steps.faulty_filter.outputs.failure-reason }}" != "no-tests" ]]; then
124+
echo "::error:: failure-reason not set correctly"
125+
exit 1
126+
fi
127+
128+
failing:
129+
runs-on: ubuntu-latest
130+
name: A failing test using variables
131+
steps:
132+
- name: Checkout
133+
uses: actions/checkout@v3
134+
135+
- name: Test
136+
uses: ./terraform-test
137+
id: failing
138+
continue-on-error: true
139+
with:
140+
path: tests/workflows/test-test/local
141+
test_filter: tests/main.tftest.hcl
142+
variables: |
143+
length = 1
144+
145+
- name: Check failure-reason
146+
run: |
147+
if [[ "${{ steps.failing.outcome }}" != "failure" ]]; then
148+
echo "Test did not fail correctly"
149+
exit 1
150+
fi
151+
152+
if [[ "${{ steps.failing.outputs.failure-reason }}" != "tests-failed" ]]; then
153+
echo "::error:: failure-reason not set correctly"
154+
exit 1
155+
fi

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ First release of the GitHub Actions:
668668
- [dflook/terraform-new-workspace](terraform-new-workspace)
669669
- [dflook/terraform-destroy-workspace](terraform-destroy-workspace)
670670

671-
[1.42.0]: https://github.com/dflook/terraform-github-actions/compare/v1.42.0...v1.42.1
671+
[1.42.1]: https://github.com/dflook/terraform-github-actions/compare/v1.42.0...v1.42.1
672672
[1.42.0]: https://github.com/dflook/terraform-github-actions/compare/v1.41.2...v1.42.0
673673
[1.41.2]: https://github.com/dflook/terraform-github-actions/compare/v1.41.1...v1.41.2
674674
[1.41.1]: https://github.com/dflook/terraform-github-actions/compare/v1.41.0...v1.41.1

image/actions.sh

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@ function init() {
176176
end_group
177177
}
178178

179+
##
180+
# Initialize terraform for running tests
181+
#
182+
# This installs modules and providers for the module and all tests
183+
function init-test() {
184+
start_group "Initializing $TOOL_PRODUCT_NAME"
185+
186+
rm -rf "$TF_DATA_DIR"
187+
188+
if [[ -n "$INPUT_TEST_DIRECTORY" ]]; then
189+
debug_log $TOOL_COMMAND_NAME init -input=false -backend=false -test-directory "$INPUT_TEST_DIRECTORY"
190+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false -test-directory $INPUT_TEST_DIRECTORY)
191+
else
192+
debug_log $TOOL_COMMAND_NAME init -input=false -backend=false
193+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false)
194+
fi
195+
196+
end_group
197+
}
198+
179199
function set-init-args() {
180200
INIT_ARGS=""
181201

@@ -339,15 +359,7 @@ function set-common-plan-args() {
339359
fi
340360
}
341361

342-
function set-plan-args() {
343-
set-common-plan-args
344-
345-
if [[ -n "$INPUT_VAR" ]]; then
346-
for var in $(echo "$INPUT_VAR" | tr ',' '\n'); do
347-
PLAN_ARGS="$PLAN_ARGS -var $var"
348-
done
349-
fi
350-
362+
function set-variable-args() {
351363
if [[ -n "$INPUT_VAR_FILE" ]]; then
352364
for file in $(echo "$INPUT_VAR_FILE" | tr ',' '\n'); do
353365

@@ -364,6 +376,18 @@ function set-plan-args() {
364376
echo "$INPUT_VARIABLES" >"$STEP_TMP_DIR/variables.tfvars"
365377
PLAN_ARGS="$PLAN_ARGS -var-file=$STEP_TMP_DIR/variables.tfvars"
366378
fi
379+
}
380+
381+
function set-plan-args() {
382+
set-common-plan-args
383+
384+
if [[ -n "$INPUT_VAR" ]]; then
385+
for var in $(echo "$INPUT_VAR" | tr ',' '\n'); do
386+
PLAN_ARGS="$PLAN_ARGS -var $var"
387+
done
388+
fi
389+
390+
set-variable-args
367391

368392
export PLAN_ARGS
369393
}

image/entrypoints/test.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# shellcheck source=../actions.sh
4+
source /usr/local/actions.sh
5+
6+
debug
7+
setup
8+
init-test
9+
10+
exec 3>&1
11+
12+
function set-test-args() {
13+
TEST_ARGS=""
14+
15+
if [[ -v INPUT_CLOUD_RUN && -n "$INPUT_CLOUD_RUN" ]]; then
16+
# I have no idea what this does, it is not well documented.
17+
TEST_ARGS="$TEST_ARGS -cloud-run=$INPUT_CLOUD_RUN"
18+
fi
19+
20+
if [[ -n "$INPUT_TEST_DIRECTORY" ]]; then
21+
TEST_ARGS="$TEST_ARGS -test-directory=$INPUT_TEST_DIRECTORY"
22+
fi
23+
24+
if [[ -n "$INPUT_TEST_FILTER" ]]; then
25+
for file in $(echo "$INPUT_TEST_FILTER" | tr ',' '\n'); do
26+
TEST_ARGS="$TEST_ARGS -filter=$file"
27+
done
28+
fi
29+
}
30+
31+
function test() {
32+
33+
debug_log $TOOL_COMMAND_NAME test -no-color $TEST_ARGS '$PLAN_ARGS' # don't expand PLAN_ARGS
34+
35+
set +e
36+
# shellcheck disable=SC2086
37+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME test -no-color $TEST_ARGS $PLAN_ARGS) \
38+
2>"$STEP_TMP_DIR/terraform_test.stderr" \
39+
| tee /dev/fd/3 \
40+
>"$STEP_TMP_DIR/terraform_test.stdout"
41+
42+
# shellcheck disable=SC2034
43+
TEST_EXIT=${PIPESTATUS[0]}
44+
set -e
45+
46+
cat "$STEP_TMP_DIR/terraform_test.stderr"
47+
48+
if [[ $TEST_EXIT -eq 0 ]]; then
49+
# Workaround a bit of stupidity in the terraform test command
50+
if grep -q "Success! 0 passed, 0 failed." "$STEP_TMP_DIR/terraform_test.stdout"; then
51+
error_log "No tests found"
52+
set_output failure-reason no-tests
53+
exit 1
54+
fi
55+
else
56+
set_output failure-reason tests-failed
57+
exit 1
58+
fi
59+
}
60+
61+
set-test-args
62+
PLAN_ARGS=""
63+
set-variable-args
64+
65+
test

0 commit comments

Comments
 (0)