From b097d297ffd94d671aeaadefdd22822a24e6c1a0 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 00:40:05 +0200 Subject: [PATCH 01/16] Extend tests to use lockfile if available This commints allows the test framework to use a lockfile generated within the tools/lockfile directory. This allows reusing the provider cache and also running tests in parallel. --- .github/actions/fabric-tests/action.yml | 11 +++++++++ .github/workflows/tests.yml | 33 +++++++++++++++++++++++++ tests/fixtures.py | 7 +++++- tools/lockfile/main.tf | 26 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tools/lockfile/main.tf diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index aae958fcd..0756607f4 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -46,6 +46,17 @@ runs: echo 'disable_checkpoint = true' \ | tee -a /home/runner/.terraformrc mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} + + - uses: actions/download-artifact@v3 + with: + name: lockfile + + - name: Cache Terraform + 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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 02d332aef..25725cd42 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -31,8 +31,37 @@ env: TF_VERSION: 1.4.4 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: Cache Terraform + uses: actions/cache@v3 + with: + path: ${{ env.TF_PLUGIN_CACHE_DIR }} + key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }} + + - shell: bash + run: | + mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} + cd tools/lockfile + terraform init + + - 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 @@ -47,6 +76,7 @@ jobs: examples-modules: runs-on: ubuntu-latest + needs: setup-tf-providers steps: - uses: actions/checkout@v3 @@ -61,6 +91,7 @@ jobs: blueprints: runs-on: ubuntu-latest + needs: setup-tf-providers steps: - uses: actions/checkout@v3 @@ -75,6 +106,7 @@ jobs: modules: runs-on: ubuntu-latest + needs: setup-tf-providers steps: - uses: actions/checkout@v3 @@ -89,6 +121,7 @@ jobs: fast: runs-on: ubuntu-latest + needs: setup-tf-providers steps: - uses: actions/checkout@v3 diff --git a/tests/fixtures.py b/tests/fixtures.py index c142a6102..98bf3a265 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -25,6 +25,7 @@ import pytest import tftest import yaml +_REPO_ROOT = Path(__file__).parents[1] PlanSummary = collections.namedtuple('PlanSummary', 'values counts outputs') @@ -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: @@ -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) diff --git a/tools/lockfile/main.tf b/tools/lockfile/main.tf new file mode 100644 index 000000000..e9914471e --- /dev/null +++ b/tools/lockfile/main.tf @@ -0,0 +1,26 @@ +# 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. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +resource "local_file" "default" {} +resource "google_service_account" "sa1" {} +resource "google_service_account" "sa2" { + provider = google-beta +} +resource "random_pet" "default" {} +resource "tls_private_key" "default" {} +resource "github_branch" "default" {} +resource "time_static" "default" {} +resource "vsphere_role" "default" {} +resource "azurerm_resource_group" "default" {} +resource "azuread_user" "default" {} From 20ccf7416fa28880a3fcc38e0f36541c5789194c Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 00:46:46 +0200 Subject: [PATCH 02/16] Enable parallel tests --- .github/workflows/tests.yml | 10 +++++----- tests/requirements.txt | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 25725cd42..26c657898 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -72,7 +72,7 @@ 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 @@ -87,7 +87,7 @@ 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 @@ -102,7 +102,7 @@ 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 @@ -117,7 +117,7 @@ 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 @@ -132,4 +132,4 @@ jobs: TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }} - name: Run tests on FAST stages - run: pytest -vv tests/fast + run: pytest -vv -n4 tests/fast diff --git a/tests/requirements.txt b/tests/requirements.txt index 1e0921c19..b4eca5c5c 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,3 +4,4 @@ tftest>=1.8.1 marko>=1.2.2 deepdiff>=6.2.3 python-hcl2>=4.3.0 +pytest-xdist>=3.1.0 From 1b4e8eb3054a2897f0b7e6c547a7d1320f22fa7c Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 00:49:28 +0200 Subject: [PATCH 03/16] Copy default versions to lockfile directory before tf init --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 26c657898..a1ded0051 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -50,6 +50,7 @@ jobs: - shell: bash run: | mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} + cp default-versions.tf tools/lockfile cd tools/lockfile terraform init From 89fe36b4f4daa933e16bd902da448a4d09de514c Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 00:58:58 +0200 Subject: [PATCH 04/16] print lockfiles --- .github/actions/fabric-tests/action.yml | 4 ++++ .github/workflows/tests.yml | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index 0756607f4..f225dc268 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -51,6 +51,10 @@ runs: with: name: lockfile + - shell: bash + run: | + cat tools/lockfile/.terraform.lock.hcl + - name: Cache Terraform uses: actions/cache@v3 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a1ded0051..3ae46d128 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,11 +49,17 @@ jobs: - shell: bash run: | + echo 'plugin_cache_dir = "/home/runner/.terraform.d/plugin-cache"' | tee -a /home/runner/.terraformrc + echo 'disable_checkpoint = true' | tee -a /home/runner/.terraformrc mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} cp default-versions.tf tools/lockfile cd tools/lockfile terraform init + - shell: bash + run: | + cat tools/lockfile/.terraform.lock.hcl + - uses: actions/upload-artifact@v3 with: name: lockfile From 49bb72d46167e28760565de40fbc332c97a649b6 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:04:08 +0200 Subject: [PATCH 05/16] Troubleshoot lockfile --- .github/actions/fabric-tests/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index f225dc268..78e834b92 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -53,6 +53,7 @@ runs: - shell: bash run: | + ls -R cat tools/lockfile/.terraform.lock.hcl - name: Cache Terraform From 175d1d38938f6848de6e2869764fad1bbaeb206b Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:11:10 +0200 Subject: [PATCH 06/16] Troubleshoot --- .github/actions/fabric-tests/action.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index 78e834b92..753239c8c 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -50,11 +50,12 @@ runs: - uses: actions/download-artifact@v3 with: name: lockfile + path: tmp/ - shell: bash run: | - ls -R - cat tools/lockfile/.terraform.lock.hcl + ls -Ra tmp + cat tmp/tools/lockfile/.terraform.lock.hcl - name: Cache Terraform uses: actions/cache@v3 From b8d601bd63837fc801665c9d6fca4ede9ae41d6b Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:12:56 +0200 Subject: [PATCH 07/16] Fix lockfile path --- .github/actions/fabric-tests/action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index 753239c8c..7653dd444 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -50,12 +50,12 @@ runs: - uses: actions/download-artifact@v3 with: name: lockfile - path: tmp/ + path: tool/lockfile - shell: bash run: | - ls -Ra tmp - cat tmp/tools/lockfile/.terraform.lock.hcl + ls -Ra tools/lockfile + cat tools/lockfile/.terraform.lock.hcl - name: Cache Terraform uses: actions/cache@v3 From 0bc89d99424de8d51b535494d60b7ba30cfc2a54 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:14:41 +0200 Subject: [PATCH 08/16] Fix path again --- .github/actions/fabric-tests/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index 7653dd444..1c893f194 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -50,7 +50,7 @@ runs: - uses: actions/download-artifact@v3 with: name: lockfile - path: tool/lockfile + path: tools/lockfile - shell: bash run: | From 7dd30f104f1eacedb02adc1e65e5f33a62999bb5 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:19:22 +0200 Subject: [PATCH 09/16] Reorder cache file --- .github/actions/fabric-tests/action.yml | 5 ----- .github/workflows/tests.yml | 17 +++++++++-------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index 1c893f194..c3fa70a98 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -52,11 +52,6 @@ runs: name: lockfile path: tools/lockfile - - shell: bash - run: | - ls -Ra tools/lockfile - cat tools/lockfile/.terraform.lock.hcl - - name: Cache Terraform uses: actions/cache@v3 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3ae46d128..9564a0d00 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -41,12 +41,6 @@ jobs: terraform_version: ${{ env.TERRAFORM_VERSION }} terraform_wrapper: false - - name: Cache Terraform - uses: actions/cache@v3 - with: - path: ${{ env.TF_PLUGIN_CACHE_DIR }} - key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }} - - shell: bash run: | echo 'plugin_cache_dir = "/home/runner/.terraform.d/plugin-cache"' | tee -a /home/runner/.terraformrc @@ -56,9 +50,16 @@ jobs: cd tools/lockfile terraform init - - shell: bash + - name: Cache Terraform + uses: actions/cache@v3 + with: + path: ${{ env.TF_PLUGIN_CACHE_DIR }} + key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }} + + - name: tf provider cache content + shell: bash run: | - cat tools/lockfile/.terraform.lock.hcl + ls -Ra /home/runner/.terraform.d/plugin-cache - uses: actions/upload-artifact@v3 with: From 3cd3106695d69459f67ab2d614f16722a4192e17 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:25:50 +0200 Subject: [PATCH 10/16] troubleshoot --- .github/workflows/tests.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9564a0d00..0f35735fc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -43,9 +43,6 @@ jobs: - shell: bash run: | - echo 'plugin_cache_dir = "/home/runner/.terraform.d/plugin-cache"' | tee -a /home/runner/.terraformrc - echo 'disable_checkpoint = true' | tee -a /home/runner/.terraformrc - mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} cp default-versions.tf tools/lockfile cd tools/lockfile terraform init @@ -56,6 +53,15 @@ jobs: path: ${{ env.TF_PLUGIN_CACHE_DIR }} key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }} + - shell: bash + run: | + mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} + echo 'plugin_cache_dir = "/home/runner/.terraform.d/plugin-cache"' | tee -a /home/runner/.terraformrc + echo 'disable_checkpoint = true' | tee -a /home/runner/.terraformrc + cd tools/lockfile + rm -rf .terraform* + terraform init + - name: tf provider cache content shell: bash run: | From cc73c30c08396283e0c8ef141dced71e93d675fd Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:30:37 +0200 Subject: [PATCH 11/16] Pin provider versions in cache --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0f35735fc..06655ebfc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,6 +44,7 @@ jobs: - shell: bash run: | cp default-versions.tf tools/lockfile + sed -i 's/>=\(.*# tftest\)/=\1/g' tools/lockfile/default-versions.tf; cd tools/lockfile terraform init From a09959539c0dbd2f85e7021b25a9ab02bc133c47 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Sun, 23 Apr 2023 01:36:20 +0200 Subject: [PATCH 12/16] Use tftest copy env var --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 06655ebfc..31723902d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -29,6 +29,7 @@ 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: From 78867b0f99ce33e995f8124cc1b3266de21ac9ea Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Tue, 25 Apr 2023 13:52:30 +0200 Subject: [PATCH 13/16] Merge actions --- .github/actions/fabric-tests/action.yml | 5 +++-- .github/workflows/tests.yml | 20 ++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index c3fa70a98..148b93c83 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -41,13 +41,14 @@ 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 }} - - uses: actions/download-artifact@v3 + - name: Download lockfile + uses: actions/download-artifact@v3 with: name: lockfile path: tools/lockfile diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 31723902d..9e1fc15cd 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,10 +42,14 @@ jobs: terraform_version: ${{ env.TERRAFORM_VERSION }} terraform_wrapper: false - - shell: bash + - 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 's/>=\(.*# tftest\)/=\1/g' tools/lockfile/default-versions.tf cd tools/lockfile terraform init @@ -55,21 +59,13 @@ jobs: path: ${{ env.TF_PLUGIN_CACHE_DIR }} key: ${{ runner.os }}-terraform-${{ hashFiles('tools/lockfile/.terraform.lock.hcl') }} - - shell: bash - run: | - mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} - echo 'plugin_cache_dir = "/home/runner/.terraform.d/plugin-cache"' | tee -a /home/runner/.terraformrc - echo 'disable_checkpoint = true' | tee -a /home/runner/.terraformrc - cd tools/lockfile - rm -rf .terraform* - terraform init - - name: tf provider cache content shell: bash run: | ls -Ra /home/runner/.terraform.d/plugin-cache - - uses: actions/upload-artifact@v3 + - name: Upload lockfile + uses: actions/upload-artifact@v3 with: name: lockfile path: tools/lockfile/.terraform.lock.hcl From ee4a908b10405ed0e85bee1a5c1851c5ca568e77 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Tue, 25 Apr 2023 15:10:29 +0200 Subject: [PATCH 14/16] Remove last remaining legacy fixture --- .github/workflows/tests.yml | 2 +- tests/conftest.py | 3 +- tests/legacy_fixtures.py | 57 ------------------- .../fixture/main.tf | 24 -------- .../fixture/outputs.tf | 19 ------- .../fixture/variables.tf | 50 ---------------- .../{test_apply.py => test_plan.py} | 10 ++-- .../fixture/main.tf | 26 --------- .../fixture/outputs.tf | 19 ------- .../fixture/variables.tf | 54 ------------------ .../{test_apply.py => test_plan.py} | 24 ++++---- 11 files changed, 19 insertions(+), 269 deletions(-) delete mode 100644 tests/legacy_fixtures.py delete mode 100644 tests/modules/cloud_config_container_coredns/fixture/main.tf delete mode 100644 tests/modules/cloud_config_container_coredns/fixture/outputs.tf delete mode 100644 tests/modules/cloud_config_container_coredns/fixture/variables.tf rename tests/modules/cloud_config_container_coredns/{test_apply.py => test_plan.py} (79%) delete mode 100644 tests/modules/cloud_config_container_mysql/fixture/main.tf delete mode 100644 tests/modules/cloud_config_container_mysql/fixture/outputs.tf delete mode 100644 tests/modules/cloud_config_container_mysql/fixture/variables.tf rename tests/modules/cloud_config_container_mysql/{test_apply.py => test_plan.py} (65%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9e1fc15cd..8dbffd54a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -51,7 +51,7 @@ jobs: cp default-versions.tf tools/lockfile sed -i 's/>=\(.*# tftest\)/=\1/g' tools/lockfile/default-versions.tf cd tools/lockfile - terraform init + terraform init -upgrade=true - name: Cache Terraform uses: actions/cache@v3 diff --git a/tests/conftest.py b/tests/conftest.py index 167c74f73..58e117e69 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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. @@ -17,6 +17,5 @@ import pytest pytest_plugins = ( 'tests.fixtures', - 'tests.legacy_fixtures', 'tests.collectors', ) diff --git a/tests/legacy_fixtures.py b/tests/legacy_fixtures.py deleted file mode 100644 index 593709e43..000000000 --- a/tests/legacy_fixtures.py +++ /dev/null @@ -1,57 +0,0 @@ -# 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. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Legacy pytest fixtures. - -The fixtures contained in this file will eventually go away. Consider -using one of the fixtures in fixtures.py -""" - -import inspect -import os -import shutil -import tempfile - -import pytest -import tftest - -BASEDIR = os.path.dirname(os.path.dirname(__file__)) - - -@pytest.fixture(scope='session') -def apply_runner(): - 'Return a function to run Terraform apply on a fixture.' - - def run_apply(fixture_path=None, **tf_vars): - 'Run Terraform plan and returns parsed output.' - if fixture_path is None: - # find out the fixture directory from the caller's directory - caller = inspect.stack()[1] - fixture_path = os.path.join(os.path.dirname(caller.filename), 'fixture') - - fixture_parent = os.path.dirname(fixture_path) - fixture_prefix = os.path.basename(fixture_path) + '_' - - with tempfile.TemporaryDirectory(prefix=fixture_prefix, - dir=fixture_parent) as tmp_path: - # copy fixture to a temporary directory so we can execute - # multiple tests in parallel - shutil.copytree(fixture_path, tmp_path, dirs_exist_ok=True) - tf = tftest.TerraformTest(tmp_path, BASEDIR, - os.environ.get('TERRAFORM', 'terraform')) - tf.setup(upgrade=True) - apply = tf.apply(tf_vars=tf_vars) - output = tf.output(json_format=True) - return apply, output - - return run_apply diff --git a/tests/modules/cloud_config_container_coredns/fixture/main.tf b/tests/modules/cloud_config_container_coredns/fixture/main.tf deleted file mode 100644 index 69a926784..000000000 --- a/tests/modules/cloud_config_container_coredns/fixture/main.tf +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module "test" { - source = "../../../../modules/cloud-config-container/coredns" - cloud_config = var.cloud_config - config_variables = var.config_variables - coredns_config = var.coredns_config - file_defaults = var.file_defaults - files = var.files -} diff --git a/tests/modules/cloud_config_container_coredns/fixture/outputs.tf b/tests/modules/cloud_config_container_coredns/fixture/outputs.tf deleted file mode 100644 index 6aa222fd8..000000000 --- a/tests/modules/cloud_config_container_coredns/fixture/outputs.tf +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -output "cloud_config" { - value = module.test.cloud_config -} diff --git a/tests/modules/cloud_config_container_coredns/fixture/variables.tf b/tests/modules/cloud_config_container_coredns/fixture/variables.tf deleted file mode 100644 index b8b2e27b4..000000000 --- a/tests/modules/cloud_config_container_coredns/fixture/variables.tf +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -variable "cloud_config" { - type = string - default = null -} - -variable "config_variables" { - type = map(any) - default = {} -} - -variable "coredns_config" { - type = string - default = null -} - -variable "file_defaults" { - type = object({ - owner = string - permissions = string - }) - default = { - owner = "root" - permissions = "0644" - } -} - -variable "files" { - type = map(object({ - content = string - owner = string - permissions = string - })) - default = {} -} diff --git a/tests/modules/cloud_config_container_coredns/test_apply.py b/tests/modules/cloud_config_container_coredns/test_plan.py similarity index 79% rename from tests/modules/cloud_config_container_coredns/test_apply.py rename to tests/modules/cloud_config_container_coredns/test_plan.py index 9dc28e2ca..9ac8aa0a0 100644 --- a/tests/modules/cloud_config_container_coredns/test_apply.py +++ b/tests/modules/cloud_config_container_coredns/test_plan.py @@ -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. @@ -16,10 +16,10 @@ import re 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) == [ diff --git a/tests/modules/cloud_config_container_mysql/fixture/main.tf b/tests/modules/cloud_config_container_mysql/fixture/main.tf deleted file mode 100644 index 41528d0cf..000000000 --- a/tests/modules/cloud_config_container_mysql/fixture/main.tf +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -module "test" { - source = "../../../../modules/cloud-config-container/mysql" - cloud_config = var.cloud_config - config_variables = var.config_variables - image = var.image - kms_config = var.kms_config - mysql_config = var.mysql_config - mysql_data_disk = var.mysql_data_disk - mysql_password = var.mysql_password -} diff --git a/tests/modules/cloud_config_container_mysql/fixture/outputs.tf b/tests/modules/cloud_config_container_mysql/fixture/outputs.tf deleted file mode 100644 index 6aa222fd8..000000000 --- a/tests/modules/cloud_config_container_mysql/fixture/outputs.tf +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -output "cloud_config" { - value = module.test.cloud_config -} diff --git a/tests/modules/cloud_config_container_mysql/fixture/variables.tf b/tests/modules/cloud_config_container_mysql/fixture/variables.tf deleted file mode 100644 index 8f78592a7..000000000 --- a/tests/modules/cloud_config_container_mysql/fixture/variables.tf +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2022 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -variable "cloud_config" { - type = string - default = null -} - -variable "config_variables" { - type = map(any) - default = {} -} - -variable "image" { - type = string - default = "mysql:5.7" -} - -variable "kms_config" { - type = object({ - project_id = string - keyring = string - location = string - key = string - }) - default = null -} - -variable "mysql_config" { - type = string - default = null -} - -variable "mysql_data_disk" { - type = string - default = null -} - -variable "mysql_password" { - type = string -} diff --git a/tests/modules/cloud_config_container_mysql/test_apply.py b/tests/modules/cloud_config_container_mysql/test_plan.py similarity index 65% rename from tests/modules/cloud_config_container_mysql/test_apply.py rename to tests/modules/cloud_config_container_mysql/test_plan.py index b88408288..54d90a00d 100644 --- a/tests/modules/cloud_config_container_mysql/test_apply.py +++ b/tests/modules/cloud_config_container_mysql/test_plan.py @@ -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. @@ -16,34 +16,34 @@ import re import yaml -def test_defaults(apply_runner): +def test_defaults(plan_summary): "Test defalt configuration." - _, output = apply_runner(mysql_password='foo') - cloud_config = output['cloud_config'] + # _, output = apply_runner(mysql_password='foo') + summary = plan_summary('modules/cloud-config-container/mysql/', + mysql_password='foo') + 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) == [ - '/var/lib/docker/daemon.json', - '/run/mysql/secrets/mysql-passwd.txt', + '/var/lib/docker/daemon.json', '/run/mysql/secrets/mysql-passwd.txt', '/etc/systemd/system/mysql.service' ] assert 'gcloud' not in cloud_config -def test_kms(apply_runner): +def test_kms(plan_summary): "Test KMS configuration." kms_config = ( '{project_id="my-project", keyring="my-keyring", location="eu", key="foo"}' ) - _, output = apply_runner(mysql_password='foo', - kms_config=kms_config) - cloud_config = output['cloud_config'] + summary = plan_summary('modules/cloud-config-container/mysql/', + mysql_password='foo', kms_config=kms_config) + 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) == [ '/var/lib/docker/daemon.json', - '/run/mysql/secrets/mysql-passwd-cipher.txt', - '/run/mysql/passwd.sh', + '/run/mysql/secrets/mysql-passwd-cipher.txt', '/run/mysql/passwd.sh', '/etc/systemd/system/mysql.service' ] assert 'gcloud' in cloud_config From 4245faae732983a5a6c5ab0ca8fbd30904c7acba Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Tue, 25 Apr 2023 15:22:27 +0200 Subject: [PATCH 15/16] Add archive provider --- tools/lockfile/main.tf | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/lockfile/main.tf b/tools/lockfile/main.tf index e9914471e..470e69f4f 100644 --- a/tools/lockfile/main.tf +++ b/tools/lockfile/main.tf @@ -14,9 +14,7 @@ resource "local_file" "default" {} resource "google_service_account" "sa1" {} -resource "google_service_account" "sa2" { - provider = google-beta -} +resource "google_service_account" "sa2" { provider = google-beta } resource "random_pet" "default" {} resource "tls_private_key" "default" {} resource "github_branch" "default" {} @@ -24,3 +22,4 @@ resource "time_static" "default" {} resource "vsphere_role" "default" {} resource "azurerm_resource_group" "default" {} resource "azuread_user" "default" {} +data "archive_file" "bundle" {} From f4b8a61981f2e42e0ee020c091e6c342abf334ae Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Tue, 25 Apr 2023 15:26:09 +0200 Subject: [PATCH 16/16] Clean workflow files --- .github/actions/fabric-tests/action.yml | 5 +---- .github/workflows/tests.yml | 7 +------ tools/lockfile/main.tf | 12 ++++++------ 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/.github/actions/fabric-tests/action.yml b/.github/actions/fabric-tests/action.yml index 148b93c83..ea89b252d 100644 --- a/.github/actions/fabric-tests/action.yml +++ b/.github/actions/fabric-tests/action.yml @@ -46,19 +46,16 @@ runs: 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: Cache Terraform + - 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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8dbffd54a..c22c888a9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -53,17 +53,12 @@ jobs: cd tools/lockfile terraform init -upgrade=true - - name: Cache Terraform + - 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: tf provider cache content - shell: bash - run: | - ls -Ra /home/runner/.terraform.d/plugin-cache - - name: Upload lockfile uses: actions/upload-artifact@v3 with: diff --git a/tools/lockfile/main.tf b/tools/lockfile/main.tf index 470e69f4f..0aecc3ecc 100644 --- a/tools/lockfile/main.tf +++ b/tools/lockfile/main.tf @@ -12,14 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -resource "local_file" "default" {} +data "archive_file" "bundle" {} +resource "azuread_user" "default" {} +resource "azurerm_resource_group" "default" {} +resource "github_branch" "default" {} resource "google_service_account" "sa1" {} resource "google_service_account" "sa2" { provider = google-beta } +resource "local_file" "default" {} resource "random_pet" "default" {} -resource "tls_private_key" "default" {} -resource "github_branch" "default" {} resource "time_static" "default" {} +resource "tls_private_key" "default" {} resource "vsphere_role" "default" {} -resource "azurerm_resource_group" "default" {} -resource "azuread_user" "default" {} -data "archive_file" "bundle" {}