Allow passing an inventory to examples test directive

This commit is contained in:
Julio Castillo 2022-12-15 12:27:53 +01:00
parent 93361d7f3d
commit aba14fe574
3 changed files with 26 additions and 12 deletions

View File

@ -26,7 +26,7 @@ SUBMODULES_PATH = MODULES_PATH / 'cloud-config-container'
FILE_TEST_RE = re.compile(r'# tftest file (\w+) ([\S]+)')
Example = collections.namedtuple('Example', 'code files')
Example = collections.namedtuple('Example', 'code module files')
File = collections.namedtuple('File', 'path content')
@ -64,8 +64,8 @@ def pytest_generate_tests(metafunc):
if 'tftest skip' in code:
continue
if child.lang == 'hcl':
examples.append(Example(code, files))
path = module.relative_to(FABRIC_ROOT)
examples.append(Example(code, path, files))
name = f'{path}:{last_header}'
if index > 1:
name += f' {index}'

View File

@ -16,16 +16,20 @@ import re
from pathlib import Path
BASE_PATH = Path(__file__).parent
COUNT_TEST_RE = re.compile(
r'# tftest modules=(\d+) resources=(\d+)(?: files=([\w,]+))?')
COUNT_TEST_RE = re.compile(r'# tftest modules=(\d+) resources=(\d+)' +
r'(?: files=([\w,-.]+))?' +
r'(?: inventory=([\w\-.]+))?')
def test_example(recursive_e2e_plan_runner, tmp_path, example):
def test_example(plan_validator, tmp_path, example):
if match := COUNT_TEST_RE.search(example.code):
(tmp_path / 'fabric').symlink_to(Path(BASE_PATH, '../../'))
(tmp_path / 'variables.tf').symlink_to(Path(BASE_PATH, 'variables.tf'))
(tmp_path / 'fabric').symlink_to(BASE_PATH.parents[1])
(tmp_path / 'variables.tf').symlink_to(BASE_PATH / 'variables.tf')
(tmp_path / 'main.tf').write_text(example.code)
expected_modules = int(match.group(1))
expected_resources = int(match.group(2))
if match.group(3) is not None:
requested_files = match.group(3).split(',')
for f in requested_files:
@ -33,11 +37,18 @@ def test_example(recursive_e2e_plan_runner, tmp_path, example):
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(example.files[f].content)
expected_modules = int(match.group(1)) if match is not None else 1
expected_resources = int(match.group(2)) if match is not None else 1
inventory = []
if match.group(4) is not None:
inventory = BASE_PATH.parent / example.module / 'examples'
inventory = inventory / match.group(4)
num_modules, num_resources = recursive_e2e_plan_runner(
str(tmp_path), tmpdir=False)
# TODO: force plan_validator to never copy files (we're already
# running from a temp dir)
summary = plan_validator(module_path=tmp_path, inventory_paths=inventory,
tf_var_files=[])
counts = summary.counts
num_modules, num_resources = counts['modules'], counts['resources']
assert expected_modules == num_modules, 'wrong number of modules'
assert expected_resources == num_resources, 'wrong number of resources'

View File

@ -103,6 +103,7 @@ def plan_summary(module_path, basedir, tf_var_files=None, **tf_vars):
# compute resource type counts and address->values map
values = {}
counts = collections.defaultdict(int)
counts['modules'] = counts['resources'] = 0
q = collections.deque([plan.root_module])
while q:
e = q.popleft()
@ -113,8 +114,10 @@ def plan_summary(module_path, basedir, tf_var_files=None, **tf_vars):
values[e['address']] = e['values']
for x in e.get('resources', []):
counts['resources'] += 1
q.append(x)
for x in e.get('child_modules', []):
counts['modules'] += 1
q.append(x)
# extract planned outputs
@ -224,7 +227,7 @@ def plan_validator_fixture(request):
basedir = Path(request.fspath).parent
return plan_validator(module_path=module_path,
inventory_paths=inventory_paths, basedir=basedir,
tf_var_files=tf_var_paths, **tf_vars)
tf_var_files=tf_var_files, **tf_vars)
return inner