From 144e841ce4702a0efb4dbf371203fc0668beb64c Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Mon, 10 Jan 2022 20:06:02 +0100 Subject: [PATCH] Allow tftest fixtures to run tests in parallel --- .github/workflows/tests.yml | 6 +++--- tests/conftest.py | 42 ++++++++++++++++++++++++++----------- tests/requirements.txt | 1 + 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b410c5e8..6e71ba73 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -54,7 +54,7 @@ jobs: - name: Run tests environments id: test-environments run: | - pytest -n 4 --dist loadfile -vv tests/examples + pytest -n 4 -vv tests/examples tests-examples: runs-on: ubuntu-latest @@ -83,7 +83,7 @@ jobs: - name: Run tests examples id: test-examples run: | - pytest -n 4 --dist loadfile -vv tests/modules/examples + pytest -n 4 -vv tests/modules/examples tests-modules: runs-on: ubuntu-latest @@ -112,4 +112,4 @@ jobs: - name: Run tests modules id: test-modules run: | - pytest -n 4 --dist loadgroup -vv --ignore=tests/modules/examples tests/modules + pytest -n 4 -vv --ignore=tests/modules/examples tests/modules diff --git a/tests/conftest.py b/tests/conftest.py index f1e8e760..8fa7a3ee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,10 +15,12 @@ "Shared fixtures" import os +import shutil +import tempfile + import pytest import tftest - BASEDIR = os.path.dirname(os.path.dirname(__file__)) @@ -28,10 +30,18 @@ def _plan_runner(): def run_plan(fixture_path, targets=None, refresh=True, **tf_vars): "Runs Terraform plan and returns parsed output." - tf = tftest.TerraformTest(fixture_path, BASEDIR, - os.environ.get('TERRAFORM', 'terraform')) - tf.setup(upgrade=True) - return tf.plan(output=True, refresh=refresh, tf_vars=tf_vars, targets=targets) + 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) + return tf.plan(output=True, refresh=refresh, tf_vars=tf_vars, targets=targets) return run_plan @@ -93,12 +103,20 @@ def apply_runner(): "Returns a function to run Terraform apply on a fixture." def run_apply(fixture_path, **tf_vars): - "Runs Terraform apply and returns parsed output" - tf = tftest.TerraformTest(fixture_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 + "Runs Terraform plan and returns parsed output." + 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/requirements.txt b/tests/requirements.txt index 35fdf3e4..6fb1668a 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,3 +2,4 @@ pytest>=6.2.5 PyYAML>=6.0 tftest>=1.6.3 marko>=1.2.0 +pytest-xdist>=2.5.0