This repository was archived by the owner on Apr 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
116 lines (110 loc) · 3.48 KB
/
terraform-tests.yml
File metadata and controls
116 lines (110 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
name: "Terraform Tests"
on:
pull_request:
paths:
- "**.tf"
- "**.tfvars"
- "**.tftest.hcl"
- "tests/run_test.sh"
- "tests/run_tests.sh"
- ".github/workflows/terraform-tests.yml"
workflow_dispatch:
permissions:
contents: read
jobs:
pre-check:
name: Format and Validate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.1
- name: Terraform Init
run: terraform init -backend=false
- name: Terraform Format
run: terraform fmt -check -recursive
- name: Terraform Validate
run: terraform validate
discover-tests:
runs-on: ubuntu-latest
outputs:
unit_tests: ${{ steps.find-unit-tests.outputs.tests }}
integration_tests: ${{ steps.find-integration-tests.outputs.tests }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Find unit tests
id: find-unit-tests
run: |
TESTS=$(find tests/unit -name "*_test.tftest.hcl" -type f | jq -R -s -c 'split("\n")[:-1]')
echo "tests=$TESTS" >> $GITHUB_OUTPUT
- name: Find integration tests
id: find-integration-tests
run: |
TESTS=$(find tests/integration -name "*_test.tftest.hcl" -type f | jq -R -s -c 'split("\n")[:-1]')
echo "tests=$TESTS" >> $GITHUB_OUTPUT
unit-tests:
needs: [pre-check, discover-tests]
if: needs.discover-tests.outputs.unit_tests != '[]'
runs-on: ubuntu-latest
strategy:
matrix:
test: ${{fromJson(needs.discover-tests.outputs.unit_tests)}}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.1
- name: Terraform Init
run: terraform init -backend=false
- name: Run Unit Test
run: |
TEST_DIR=$(dirname "${{ matrix.test }}")
cd $TEST_DIR
terraform init -input=false
terraform test -verbose $(basename "${{ matrix.test }}")
integration-tests:
needs: [pre-check, discover-tests]
if: needs.discover-tests.outputs.integration_tests != '[]'
runs-on: ubuntu-latest
strategy:
matrix:
test: ${{fromJson(needs.discover-tests.outputs.integration_tests)}}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.1
- name: Terraform Init
run: terraform init -backend=false
- name: Run Integration Test
run: |
TEST_DIR=$(dirname "${{ matrix.test }}")
cd $TEST_DIR
terraform init -input=false
terraform test -verbose $(basename "${{ matrix.test }}")
comprehensive-tests:
needs: [unit-tests, integration-tests]
runs-on: ubuntu-latest
if: always()
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.1
- name: Make test scripts executable
run: |
chmod +x tests/run_test.sh
chmod +x tests/run_tests.sh
- name: Run All Tests
run: ./tests/run_tests.sh