Skip to content

Commit d3e3b1f

Browse files
authored
test: add unit test framework for Terraform (#1038)
## Description This PR adds the possibility to add automated tests for the module. We start simple with a `terraform test` command. The tests live at `tests/`. Tests are executed only if the base branch was created in the cattle-ops repository. Terraform needs cloud access which is restricted to the cattle-ops organization at the moment. The AWS account is sponsored by Hapag-Lloyd. ## Migrations required No ## Verification No verification done as this PR adds tests only.
1 parent 4a4c2f8 commit d3e3b1f

File tree

13 files changed

+95
-1293
lines changed

13 files changed

+95
-1293
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"tflint",
4747
"tftpl",
4848
"tfsec",
49+
"tftest",
4950
"tftpl",
5051
"tfvars",
5152
"tmpfs",

.github/pull_request_template.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
A few sentences describing the overall goals of the pull request's commits.
44

5+
Note: The whole PR is used as commit message.
6+
57
## Migrations required
68

7-
YES | NO - If yes please describe the migration.
9+
Yes or No - If yes please describe the migration. For bigger changes please provide a migration script.
810

911
## Verification
1012

11-
Please mention the examples you have verified.
13+
Please mention how you test the changes. Ideally add automated tests (see tests/ folder)

.github/workflows/ci.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ permissions:
1212
contents: read
1313
pull-requests: write
1414

15+
env:
16+
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/.terraform.d/plugin-cache
17+
1518
jobs:
1619
verify_module:
1720
name: Verify module
@@ -107,7 +110,7 @@ jobs:
107110
steps:
108111
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
109112
# ignore: "tags not used", "access analyzer not used", "shield advanced not used"
110-
- run: kics scan -p . -o . --exclude-queries e38a8e0a-b88b-4902-b3fe-b0fcb17d5c10,e592a0c5-5bdb-414c-9066-5dba7cdea370,084c6686-2a70-4710-91b1-000393e54c12
113+
- run: kics scan -p . -o . --config .kics.yml --exclude-queries e38a8e0a-b88b-4902-b3fe-b0fcb17d5c10,e592a0c5-5bdb-414c-9066-5dba7cdea370,084c6686-2a70-4710-91b1-000393e54c12
111114

112115
tflint:
113116
runs-on: ubuntu-latest
@@ -151,3 +154,30 @@ jobs:
151154
uses: aquasecurity/tfsec-pr-commenter-action@7a44c5dcde5dfab737363e391800629e27b6376b # v1.3.1
152155
with:
153156
github_token: ${{ github.token }}
157+
158+
tests:
159+
name: Test
160+
runs-on: ubuntu-latest
161+
permissions:
162+
id-token: write
163+
contents: read
164+
if: ${{ !github.event.pull_request.head.repo.fork }}
165+
steps:
166+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
167+
- uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3
168+
name: Cache plugin dir
169+
with:
170+
key: ${{ runner.os }}-terraform-plugin-cache
171+
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
172+
- name: Configure AWS credentials
173+
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
174+
with:
175+
aws-region: eu-central-1
176+
role-to-assume: ${{ secrets.TERRAFORM_ADMIN_ROLE_ARN }}
177+
role-session-name: GitHubActions
178+
- name: Setup Terraform CLI
179+
uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
180+
with:
181+
terraform_version: "1.6.3"
182+
- run: terraform init -get -backend=false -input=false
183+
- run: terraform test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ debug/
3434
!.release/package*
3535
!.release/*.lock
3636

37+
# tests
38+
*.tfplan.json
39+
3740
# VS Code
3841
.vscode/
3942

.kics.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude-paths:
2+
- "tests/"

test/README.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

test/aws_spot_gitlab_runner_test.go

Lines changed: 0 additions & 125 deletions
This file was deleted.

test/config/config.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

test/go.mod

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)