Skip to content

Commit

Permalink
Add terraform-test action
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Jun 1, 2024
1 parent e4e2ce4 commit 83975e2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
94 changes: 87 additions & 7 deletions .github/workflows/test-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ jobs:

- 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
Expand All @@ -25,9 +33,17 @@ jobs:

- name: Test
uses: ./terraform-test
id: test
with:
path: tests/workflows/test-test/local
test_filter: main.tftest.hcl
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
Expand All @@ -38,12 +54,76 @@ jobs:

- name: Test
uses: ./terraform-test
id: test
with:
path: tests/workflows/test-test/local
test_path: custom-test-dir
test_filter: |
another.tftest.hcl
a-third.tftest.hcl
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_path: i-dont-exist

- name: Check failure
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
faulty_filter:
runs-on: ubuntu-latest
name: Filter matches no tests
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
filter: |
tests/this-test-does-not-exist.tftest.hcl
tests/nor-does-this-one.tftest.hcl
- name: Check failure
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
failing:
runs-on: ubuntu-latest
Expand All @@ -58,7 +138,7 @@ jobs:
continue-on-error: true
with:
path: tests/workflows/test-test/local
test_filter: main.tftest.hcl
test_filter: tests/main.tftest.hcl
variables: |
length = 1
Expand All @@ -68,8 +148,8 @@ jobs:
echo "Test did not fail correctly"
exit 1
fi
if [[ "${{ steps.failing.outcome }}" != "failure" ]]; then
echo "Test did not fail correctly"
if [[ "${{ steps.failing.outputs.failure-reason }}" != "tests-failed" ]]; then
echo "::error:: failure-reason not set correctly"
exit 1
fi
4 changes: 3 additions & 1 deletion terraform-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Execute automated tests on Terraform configuration

* `test_filter`

The test files to run, one per line
The test files to run, one per line.

These are paths relative to the module path.

- Type: string
- Optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ run "require_provider" {
# This will require terraform to be initialised correctly to bring in the time provider
command = apply

module = {
module {
source = "./sleep"
}
}
Expand Down

0 comments on commit 83975e2

Please sign in to comment.