Allow tftest fixtures to run tests in parallel

This commit is contained in:
Julio Castillo 2022-01-10 20:06:02 +01:00
parent 8df9ef9035
commit 144e841ce4
3 changed files with 34 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -2,3 +2,4 @@ pytest>=6.2.5
PyYAML>=6.0
tftest>=1.6.3
marko>=1.2.0
pytest-xdist>=2.5.0