Skip to content

Commit 5da906a

Browse files
authored
Add tftest POC (#349)
Add tftest about terraform init, add it to the terraform github workflow.
1 parent 936e778 commit 5da906a

8 files changed

Lines changed: 79 additions & 38 deletions

File tree

.github/workflows/ansible.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ name: Ansible
33
on:
44
push:
55
branches:
6-
- "main"
6+
- 'main'
77
pull_request:
88
paths:
9-
- ansible/**
10-
- requirements.txt
11-
- .github/workflows/ansible.yml
9+
- 'ansible/**'
10+
- 'requirements.txt'
11+
- '.github/workflows/ansible.yml'
1212

1313
jobs:
1414
build:
@@ -26,4 +26,4 @@ jobs:
2626
- name: Run ansible static tests
2727
run: |
2828
make -n static-ansible
29-
make SHELL='sh -x' static-ansible
29+
make SHELL='sh -x' static-ansible

.github/workflows/gluescript.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ name: Glue Script
33
on:
44
push:
55
branches:
6-
- "main"
6+
- 'main'
77
pull_request:
88
paths:
9-
- scripts/qesap/**
10-
- .github/workflows/gluescript.yml
9+
- 'scripts/qesap/**'
10+
- '.github/workflows/gluescript.yml'
1111

1212
env:
1313
TF_VERSION: 1.5.7
1414

1515
jobs:
1616
build:
17-
runs-on: ${{ matrix.platform }}
17+
runs-on: ubuntu-latest
1818
strategy:
1919
matrix:
20-
platform: [ubuntu-latest]
2120
# all available versions are in https://github.com/actions/python-versions/blob/main/versions-manifest.json
2221
python-version: ['3.10.14', '3.11.9', '3.12.3']
2322

2423
steps:
25-
- uses: actions/checkout@v3
24+
- name: Checkout
25+
uses: actions/checkout@v3
2626

2727
- name: Set up Python ${{ matrix.python-version }}
2828
uses: actions/setup-python@v4
@@ -68,6 +68,7 @@ jobs:
6868
env:
6969
PLATFORM: '3.11.9'
7070

71+
# Install the specific version of Terraform that the project support and
7172
- name: Setup Terraform
7273
uses: hashicorp/setup-terraform@v2
7374
with:

.github/workflows/markdown.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ name: 'Markdown'
33
on:
44
push:
55
branches:
6-
- "main"
6+
- 'main'
77
pull_request:
88
paths:
9-
- README.md
10-
- CONTRIBUTING.md
11-
- terraform/aws/README.md
12-
- terraform/azure/README.md
13-
- terraform/gcp/README.md
14-
- .github/workflows/markdown.yml
9+
- 'README.md'
10+
- 'CONTRIBUTING.md'
11+
- 'terraform/aws/README.md'
12+
- 'terraform/azure/README.md'
13+
- 'terraform/gcp/README.md'
14+
- '.github/workflows/markdown.yml'
1515

1616
permissions:
1717
contents: read

.github/workflows/terraform.yml

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
1-
name: 'Terraform'
1+
name: Terraform
22

33
on:
44
push:
55
branches:
6-
- "main"
6+
- 'main'
77
pull_request:
88
paths:
9-
- terraform/**
10-
- .github/workflows/terraform.yml
11-
12-
permissions:
13-
contents: read
9+
- 'terraform/**'
10+
- '.github/workflows/terraform.yml'
1411

1512
env:
13+
PYTEST_ADDOPTS: "--color=yes"
14+
PYTHON_VERSION: 3.11
15+
TF_PLUGIN_CACHE_DIR: "/home/runner/.terraform.d/plugin-cache"
1616
TF_VERSION: 1.5.7
17+
TFTESTS_FOLDER: "terraform/tests"
1718

1819
jobs:
1920
terraform:
20-
name: 'Terraform'
2121
runs-on: ubuntu-latest
22-
environment: production
2322
strategy:
2423
matrix:
2524
provider: ['azure', 'gcp', 'aws']
2625

27-
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
2826
defaults:
2927
run:
3028
shell: bash
3129
working-directory: terraform/${{ matrix.provider }}
3230

3331
steps:
34-
# Checkout the repository to the GitHub Actions runner
3532
- name: Checkout
3633
uses: actions/checkout@v3
3734

38-
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
35+
# Install the specific version of Terraform that the project support and
3936
- name: Setup Terraform
4037
uses: hashicorp/setup-terraform@v2
4138
with:
4239
terraform_version: ${{ env.TF_VERSION }}
4340
terraform_wrapper: false
4441

45-
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
42+
# Initialize each CSP Terraform working directory
43+
# by creating initial files, downloading modules, etc.
4644
- name: Terraform Init
4745
run: terraform init
4846

@@ -53,3 +51,23 @@ jobs:
5351
# Validate
5452
- name: Terraform Validate
5553
run: terraform validate
54+
55+
tftest:
56+
runs-on: ubuntu-latest
57+
58+
steps:
59+
- name: Checkout
60+
uses: actions/checkout@v3
61+
62+
# Install the specific version of Terraform that the project support and
63+
- name: Setup Terraform
64+
uses: hashicorp/setup-terraform@v2
65+
with:
66+
terraform_version: ${{ env.TF_VERSION }}
67+
terraform_wrapper: false
68+
69+
- name: Run tftests
70+
run: |
71+
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
72+
pip install -r ${{ env.TFTESTS_FOLDER }}/requirements.txt
73+
pytest -vv -o log_cli=true -o log_cli_level=10 ${{ env.TFTESTS_FOLDER }}

.github/workflows/tools.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
name: Tools environment
2-
2+
# Purpose of this workflow is to check that all binary the project
3+
# is recommending are installable and usable on openSUSE TW.
4+
# This workflow refer to th eDockerfile in the root of the repo.
35
on:
46
push:
57
branches:
6-
- "main"
8+
- 'main'
79
pull_request:
810
paths:
9-
- Dockerfile
10-
- requirements.txt
11-
- requirements.yml
12-
- .github/workflows/tools.yml
13-
11+
- 'Dockerfile'
12+
- 'requirements.txt'
13+
- 'requirements.yml'
14+
- '.github/workflows/tools.yml'
15+
- 'terraform/azure/version.tf'
16+
- 'terraform/aws/version.tf'
17+
- 'terraform/gcp/version.tf'
1418
env:
1519
REGISTRY: ghcr.io
1620

terraform/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
import pytest
3+
4+
5+
@pytest.fixture(scope='session')
6+
def fixtures_dir():
7+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')

terraform/tests/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest>=7.2
2+
tftest==1.8.5

terraform/tests/test_init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
import tftest
3+
4+
5+
@pytest.mark.parametrize("csp", ['azure', 'aws', 'gcp'])
6+
def test_init(fixtures_dir, csp):
7+
tf = tftest.TerraformTest(csp, fixtures_dir)
8+
init_out = tf.init(output=True)
9+
assert 'Terraform has been successfully initialized' in init_out

0 commit comments

Comments
 (0)