Skip to content

Commit

Permalink
Test different versions of Terraform (#2399)
Browse files Browse the repository at this point in the history
Add capability to run tests using terraform version 1.5.7 and OpenTofu. 

Automatic checks are run with  Terraform 1.7.4 and OpenTofu 1.7.2, it should be possible to override terraform version when Tests workflow is run manually.
  • Loading branch information
wiktorn authored Jul 5, 2024
1 parent 45fd48d commit afa6e74
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 18 deletions.
17 changes: 14 additions & 3 deletions .github/actions/fabric-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ inputs:
required: true
TERRAFORM_VERSION:
required: true
TERRAFORM_FLAVOUR:
required: true
runs:
using: composite
steps:
Expand All @@ -33,11 +35,20 @@ runs:
python-version: ${{ inputs.PYTHON_VERSION }}
cache: 'pip'
cache-dependency-path: 'tests/requirements.txt'

- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
if: ${{ inputs.TERRAFORM_FLAVOUR == 'terraform' }}
with:
terraform_version: ${{ inputs.TERRAFORM_VERSION }}
terraform_wrapper: false

- uses: opentofu/setup-opentofu@v1
if: ${{ inputs.TERRAFORM_FLAVOUR == 'tofu' }}
with:
tofu_version: ${{ inputs.TERRAFORM_VERSION }}
tofu_wrapper: false

- name: Configure provider cache
shell: bash
run: |
Expand All @@ -49,19 +60,19 @@ runs:
- name: Download lockfile
uses: actions/download-artifact@v4
with:
name: lockfile
name: lockfile-${{ runner.os }}-${{ inputs.TERRAFORM_FLAVOUR }}-${{ inputs.TERRAFORM_VERSION }}
path: tools/lockfile
- name: Download Terraform provider cache
uses: actions/cache@v4
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }}
key: ${{ runner.os }}-${{ inputs.TERRAFORM_FLAVOUR }}-${{ inputs.TERRAFORM_VERSION }}-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }}
# avoid conflicts with user-installed providers on local machines
- name: Pin provider versions
shell: bash
run: |
for f in $(find . -name versions.tf); do
sed -i 's/>=\(.*# tftest\)/=\1/g' $f;
sed -i -e 's/>=\(.*# tftest\)/=\1/g' -e 's/required_version = .*$/required_version = ">= ${{ inputs.TERRAFORM_VERSION }}"/g' $f;
done
- name: Install Python Dependencies
shell: bash
Expand Down
100 changes: 85 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,86 @@ on:
pull_request:
branches:
- master
workflow_dispatch:
inputs:
terraform_version:
description: "Use '1.5.7' to test last MPLv2 Terraform version"
required: true
default: 1.7.4
type: string

env:
GOOGLE_APPLICATION_CREDENTIALS: "/home/runner/credentials.json"
PYTEST_ADDOPTS: "--color=yes"
PYTHON_VERSION: "3.10"
TF_PLUGIN_CACHE_DIR: "/home/runner/.terraform.d/plugin-cache"
TF_VERSION: 1.7.4
TFTEST_COPY: 1
DEFAULT_TERRAFORM_FLAVOUR: terraform
DEFAULT_TERRAFORM_VERSION: ${{ inputs.terraform_version || '1.7.4' }}
DEFAULT_TOFU_VERSION: "1.7.2"

jobs:
compute-matrix:
runs-on: ubuntu-latest
outputs:
DEFAULT_TERRAFORM_FLAVOUR: ${{ env.DEFAULT_TERRAFORM_FLAVOUR }}
DEFAULT_TERRAFORM_VERSION: ${{ env.DEFAULT_TERRAFORM_VERSION }}
DEFAULT_TOFU_VERSION: ${{ env.DEFAULT_TOFU_VERSION }}
steps:
- name: dummy
run: echo

setup-tf-providers:
runs-on: ubuntu-latest
needs:
- compute-matrix
strategy:
matrix:
include:
- flavour: terraform
version: "${{ needs.compute-matrix.outputs.DEFAULT_TERRAFORM_VERSION }}"
- flavour: tofu
version: "${{ needs.compute-matrix.outputs.DEFAULT_TOFU_VERSION }}"
steps:
- uses: actions/checkout@v4

- uses: hashicorp/setup-terraform@v3
if: ${{ matrix.flavour == 'terraform' }}
with:
terraform_version: ${{ env.TF_VERSION }}
terraform_version: ${{ matrix.version }}
terraform_wrapper: false

- uses: opentofu/setup-opentofu@v1
if: ${{ matrix.flavour == 'tofu' }}
with:
tofu_version: ${{ matrix.version }}
tofu_wrapper: false

- name: Build lockfile and fetch providers
shell: bash
run: |
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
echo 'plugin_cache_dir = "${{ env.TF_PLUGIN_CACHE_DIR }}"' | tee -a /home/runner/.terraformrc
echo 'disable_checkpoint = true' | tee -a /home/runner/.terraformrc
cp default-versions.tf tools/lockfile
sed -i 's/>=\(.*# tftest\)/=\1/g' tools/lockfile/default-versions.tf
sed -i -e 's/>=\(.*# tftest\)/=\1/g' tools/lockfile/default-versions.tf
# change terraform version to the one that is running
sed -i 's/required_version = .*$/required_version = ">= ${{ matrix.version }}"/g' tools/lockfile/default-versions.tf
cd tools/lockfile
terraform init -upgrade=true
${{ matrix.flavour }} init -upgrade=true
- name: Upload Terraform provider cache
uses: actions/cache@v4
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }}
key: ${{ runner.os }}-${{ matrix.flavour }}-${{ matrix.version }}-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }}

- name: Upload lockfile
uses: actions/upload-artifact@v4
with:
name: lockfile
name: lockfile-${{ runner.os }}-${{ matrix.flavour }}-${{ matrix.version }}
path: tools/lockfile/.terraform.lock.hcl
overwrite: true

Expand All @@ -74,7 +113,8 @@ jobs:
uses: ./.github/actions/fabric-tests
with:
PYTHON_VERSION: ${{ env.PYTHON_VERSION }}
TERRAFORM_VERSION: ${{ env.TF_VERSION }}
TERRAFORM_VERSION: ${{ env.DEFAULT_TERRAFORM_VERSION }}
TERRAFORM_FLAVOUR: ${{ env.DEFAULT_TERRAFORM_FLAVOUR }}

- name: Run tests on documentation examples
run: pytest -vv -n4 --tb=line --junit-xml=test-results-raw.xml -k blueprints/ tests/examples
Expand All @@ -87,18 +127,31 @@ jobs:

examples-modules:
runs-on: ubuntu-latest
needs: setup-tf-providers
needs:
- setup-tf-providers
- compute-matrix
strategy:
matrix:
include:
- flavour: terraform
version: "${{ needs.compute-matrix.outputs.DEFAULT_TERRAFORM_VERSION }}"
- flavour: tofu
version: "${{ needs.compute-matrix.outputs.DEFAULT_TOFU_VERSION }}"

steps:
- uses: actions/checkout@v4

- name: Call composite action fabric-tests
uses: ./.github/actions/fabric-tests
with:
PYTHON_VERSION: ${{ env.PYTHON_VERSION }}
TERRAFORM_VERSION: ${{ env.TF_VERSION }}
TERRAFORM_VERSION: ${{ matrix.version }}
TERRAFORM_FLAVOUR: ${{ matrix.flavour }}

- name: Run tests on documentation examples
run: pytest -vv -n4 --tb=line --junit-xml=test-results-raw.xml -k modules/ tests/examples
env:
TERRAFORM: ${{ matrix.flavour }}
run: pytest -vv ${{ matrix.flavour == 'terraform' && '-n4' || '-n4' }} --tb=line --junit-xml=test-results-raw.xml -k modules/ tests/examples

- name: Create report
uses: ./.github/actions/post-fabric-tests
Expand All @@ -116,7 +169,8 @@ jobs:
uses: ./.github/actions/fabric-tests
with:
PYTHON_VERSION: ${{ env.PYTHON_VERSION }}
TERRAFORM_VERSION: ${{ env.TF_VERSION }}
TERRAFORM_VERSION: ${{ env.DEFAULT_TERRAFORM_VERSION }}
TERRAFORM_FLAVOUR: ${{ env.DEFAULT_TERRAFORM_FLAVOUR }}

- name: Run tests environments
run: pytest -vv -n4 --tb=line --junit-xml=test-results-raw.xml tests/blueprints
Expand All @@ -129,18 +183,33 @@ jobs:

modules:
runs-on: ubuntu-latest
needs: setup-tf-providers
needs:
- setup-tf-providers
- compute-matrix
strategy:
matrix:
include:
- flavour: terraform
version: "${{ needs.compute-matrix.outputs.DEFAULT_TERRAFORM_VERSION }}"
- flavour: tofu
version: "${{ needs.compute-matrix.outputs.DEFAULT_TOFU_VERSION }}"

steps:
- uses: actions/checkout@v4

- name: Call composite action fabric-tests
uses: ./.github/actions/fabric-tests
env:
TERRAFORM: ${{ matrix.flavour }}
with:
PYTHON_VERSION: ${{ env.PYTHON_VERSION }}
TERRAFORM_VERSION: ${{ env.TF_VERSION }}
TERRAFORM_VERSION: ${{ matrix.version }}
TERRAFORM_FLAVOUR: ${{ matrix.flavour }}

- name: Run tests modules
run: pytest -vv -n4 --tb=line --junit-xml=test-results-raw.xml tests/modules
env:
TERRAFORM: ${{ matrix.flavour }}
run: pytest -vv ${{ matrix.flavour == 'terraform' && '-n4' || '-n4' }} --tb=line --junit-xml=test-results-raw.xml tests/modules

- name: Create report
uses: ./.github/actions/post-fabric-tests
Expand All @@ -158,7 +227,8 @@ jobs:
uses: ./.github/actions/fabric-tests
with:
PYTHON_VERSION: ${{ env.PYTHON_VERSION }}
TERRAFORM_VERSION: ${{ env.TF_VERSION }}
TERRAFORM_VERSION: ${{ env.DEFAULT_TERRAFORM_VERSION }}
TERRAFORM_FLAVOUR: ${{ env.DEFAULT_TERRAFORM_FLAVOUR }}

- name: Run tests on FAST stages
run: pytest -vv -n4 --tb=line --junit-xml=test-results-raw.xml tests/fast
Expand Down

0 comments on commit afa6e74

Please sign in to comment.