Skip to content

Commit

Permalink
Merge pull request #1340 from GoogleCloudPlatform/jccb/tests-lockfile
Browse files Browse the repository at this point in the history
Extend tests to use lockfile if available
  • Loading branch information
juliocc authored Apr 26, 2023
2 parents 84a7b98 + f4b8a61 commit eedd805
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 275 deletions.
12 changes: 11 additions & 1 deletion .github/actions/fabric-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ runs:
- name: Configure provider cache
shell: bash
run: |
echo 'plugin_cache_dir = "/home/runner/.terraform.d/plugin-cache"' \
echo 'plugin_cache_dir = "${{ env.TF_PLUGIN_CACHE_DIR }}"' \
| tee -a /home/runner/.terraformrc
echo 'disable_checkpoint = true' \
| tee -a /home/runner/.terraformrc
mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }}
- name: Download lockfile
uses: actions/download-artifact@v3
with:
name: lockfile
path: tools/lockfile
- name: Download Terraform provider cache
uses: actions/cache@v3
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }}
# avoid conflicts with user-installed providers on local machines
- name: Pin provider versions
shell: bash
Expand Down
50 changes: 45 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,46 @@ env:
PYTHON_VERSION: "3.10"
TF_PLUGIN_CACHE_DIR: "/home/runner/.terraform.d/plugin-cache"
TF_VERSION: 1.4.4
TFTEST_COPY: 1

jobs:
setup-tf-providers:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
terraform_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
cd tools/lockfile
terraform init -upgrade=true
- name: Upload Terraform provider cache
uses: actions/cache@v3
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }}

- name: Upload lockfile
uses: actions/upload-artifact@v3
with:
name: lockfile
path: tools/lockfile/.terraform.lock.hcl


examples-blueprints:
runs-on: ubuntu-latest
needs: setup-tf-providers
steps:
- uses: actions/checkout@v3

Expand All @@ -43,10 +79,11 @@ jobs:
TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}

- name: Run tests on documentation examples
run: pytest -vv -k blueprints/ tests/examples
run: pytest -vv -n4 -k blueprints/ tests/examples

examples-modules:
runs-on: ubuntu-latest
needs: setup-tf-providers
steps:
- uses: actions/checkout@v3

Expand All @@ -57,10 +94,11 @@ jobs:
TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}

- name: Run tests on documentation examples
run: pytest -vv -k modules/ tests/examples
run: pytest -vv -n4 -k modules/ tests/examples

blueprints:
runs-on: ubuntu-latest
needs: setup-tf-providers
steps:
- uses: actions/checkout@v3

Expand All @@ -71,10 +109,11 @@ jobs:
TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}

- name: Run tests environments
run: pytest -vv tests/blueprints
run: pytest -vv -n4 tests/blueprints

modules:
runs-on: ubuntu-latest
needs: setup-tf-providers
steps:
- uses: actions/checkout@v3

Expand All @@ -85,10 +124,11 @@ jobs:
TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}

- name: Run tests modules
run: pytest -vv tests/modules
run: pytest -vv -n4 tests/modules

fast:
runs-on: ubuntu-latest
needs: setup-tf-providers
steps:
- uses: actions/checkout@v3

Expand All @@ -99,4 +139,4 @@ jobs:
TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}

- name: Run tests on FAST stages
run: pytest -vv tests/fast
run: pytest -vv -n4 tests/fast
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Google LLC
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,5 @@

pytest_plugins = (
'tests.fixtures',
'tests.legacy_fixtures',
'tests.collectors',
)
7 changes: 6 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import tftest
import yaml

_REPO_ROOT = Path(__file__).parents[1]
PlanSummary = collections.namedtuple('PlanSummary', 'values counts outputs')


Expand All @@ -51,10 +52,14 @@ def _prepare_root_module(path):
'*.auto.tfvars.json',
'[0-9]-*-providers.tf',
'terraform.tfstate*',
'.terraform.lock.hcl',
'terraform.tfvars', '.terraform')

shutil.copytree(path, tmp_path, dirs_exist_ok=True,
ignore=ignore_patterns)
lockfile = _REPO_ROOT / 'tools' / 'lockfile' / '.terraform.lock.hcl'
if lockfile.exists():
shutil.copy(lockfile, tmp_path / '.terraform.lock.hcl')

yield tmp_path
else:
Expand Down Expand Up @@ -94,7 +99,7 @@ def plan_summary(module_path, basedir, tf_var_files=None, extra_files=None,
"""
# make the module_path relative to the root of the repo while still
# supporting absolute paths
module_path = Path(__file__).parents[1] / module_path
module_path = _REPO_ROOT / module_path
with _prepare_root_module(module_path) as test_path:
binary = os.environ.get('TERRAFORM', 'terraform')
tf = tftest.TerraformTest(test_path, binary=binary)
Expand Down
57 changes: 0 additions & 57 deletions tests/legacy_fixtures.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/modules/cloud_config_container_coredns/fixture/main.tf

This file was deleted.

19 changes: 0 additions & 19 deletions tests/modules/cloud_config_container_coredns/fixture/outputs.tf

This file was deleted.

50 changes: 0 additions & 50 deletions tests/modules/cloud_config_container_coredns/fixture/variables.tf

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Google LLC
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,10 @@
import yaml


def test_defaults(apply_runner):
"Test defalt configuration."
_, output = apply_runner()
cloud_config = output['cloud_config']
def test_defaults(plan_summary):
"Test default configuration."
summary = plan_summary('modules/cloud-config-container/coredns/')
cloud_config = summary.outputs['cloud_config']['value']
yaml.safe_load(cloud_config)
assert cloud_config.startswith('#cloud-config')
assert re.findall(r'(?m)^\s+\-\s*path:\s*(\S+)', cloud_config) == [
Expand Down
26 changes: 0 additions & 26 deletions tests/modules/cloud_config_container_mysql/fixture/main.tf

This file was deleted.

19 changes: 0 additions & 19 deletions tests/modules/cloud_config_container_mysql/fixture/outputs.tf

This file was deleted.

Loading

0 comments on commit eedd805

Please sign in to comment.