Allow passing an inventory to examples test directive
This commit is contained in:
parent
93361d7f3d
commit
aba14fe574
|
@ -26,7 +26,7 @@ SUBMODULES_PATH = MODULES_PATH / 'cloud-config-container'
|
||||||
|
|
||||||
FILE_TEST_RE = re.compile(r'# tftest file (\w+) ([\S]+)')
|
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')
|
File = collections.namedtuple('File', 'path content')
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,8 +64,8 @@ def pytest_generate_tests(metafunc):
|
||||||
if 'tftest skip' in code:
|
if 'tftest skip' in code:
|
||||||
continue
|
continue
|
||||||
if child.lang == 'hcl':
|
if child.lang == 'hcl':
|
||||||
examples.append(Example(code, files))
|
|
||||||
path = module.relative_to(FABRIC_ROOT)
|
path = module.relative_to(FABRIC_ROOT)
|
||||||
|
examples.append(Example(code, path, files))
|
||||||
name = f'{path}:{last_header}'
|
name = f'{path}:{last_header}'
|
||||||
if index > 1:
|
if index > 1:
|
||||||
name += f' {index}'
|
name += f' {index}'
|
||||||
|
|
|
@ -16,16 +16,20 @@ import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
BASE_PATH = Path(__file__).parent
|
BASE_PATH = Path(__file__).parent
|
||||||
COUNT_TEST_RE = re.compile(
|
COUNT_TEST_RE = re.compile(r'# tftest modules=(\d+) resources=(\d+)' +
|
||||||
r'# tftest modules=(\d+) resources=(\d+)(?: files=([\w,]+))?')
|
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):
|
if match := COUNT_TEST_RE.search(example.code):
|
||||||
(tmp_path / 'fabric').symlink_to(Path(BASE_PATH, '../../'))
|
(tmp_path / 'fabric').symlink_to(BASE_PATH.parents[1])
|
||||||
(tmp_path / 'variables.tf').symlink_to(Path(BASE_PATH, 'variables.tf'))
|
(tmp_path / 'variables.tf').symlink_to(BASE_PATH / 'variables.tf')
|
||||||
(tmp_path / 'main.tf').write_text(example.code)
|
(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:
|
if match.group(3) is not None:
|
||||||
requested_files = match.group(3).split(',')
|
requested_files = match.group(3).split(',')
|
||||||
for f in requested_files:
|
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.parent.mkdir(parents=True, exist_ok=True)
|
||||||
destination.write_text(example.files[f].content)
|
destination.write_text(example.files[f].content)
|
||||||
|
|
||||||
expected_modules = int(match.group(1)) if match is not None else 1
|
inventory = []
|
||||||
expected_resources = int(match.group(2)) if match is not None else 1
|
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(
|
# TODO: force plan_validator to never copy files (we're already
|
||||||
str(tmp_path), tmpdir=False)
|
# 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_modules == num_modules, 'wrong number of modules'
|
||||||
assert expected_resources == num_resources, 'wrong number of resources'
|
assert expected_resources == num_resources, 'wrong number of resources'
|
||||||
|
|
||||||
|
|
|
@ -103,6 +103,7 @@ def plan_summary(module_path, basedir, tf_var_files=None, **tf_vars):
|
||||||
# compute resource type counts and address->values map
|
# compute resource type counts and address->values map
|
||||||
values = {}
|
values = {}
|
||||||
counts = collections.defaultdict(int)
|
counts = collections.defaultdict(int)
|
||||||
|
counts['modules'] = counts['resources'] = 0
|
||||||
q = collections.deque([plan.root_module])
|
q = collections.deque([plan.root_module])
|
||||||
while q:
|
while q:
|
||||||
e = q.popleft()
|
e = q.popleft()
|
||||||
|
@ -113,8 +114,10 @@ def plan_summary(module_path, basedir, tf_var_files=None, **tf_vars):
|
||||||
values[e['address']] = e['values']
|
values[e['address']] = e['values']
|
||||||
|
|
||||||
for x in e.get('resources', []):
|
for x in e.get('resources', []):
|
||||||
|
counts['resources'] += 1
|
||||||
q.append(x)
|
q.append(x)
|
||||||
for x in e.get('child_modules', []):
|
for x in e.get('child_modules', []):
|
||||||
|
counts['modules'] += 1
|
||||||
q.append(x)
|
q.append(x)
|
||||||
|
|
||||||
# extract planned outputs
|
# extract planned outputs
|
||||||
|
@ -224,7 +227,7 @@ def plan_validator_fixture(request):
|
||||||
basedir = Path(request.fspath).parent
|
basedir = Path(request.fspath).parent
|
||||||
return plan_validator(module_path=module_path,
|
return plan_validator(module_path=module_path,
|
||||||
inventory_paths=inventory_paths, basedir=basedir,
|
inventory_paths=inventory_paths, basedir=basedir,
|
||||||
tf_var_files=tf_var_paths, **tf_vars)
|
tf_var_files=tf_var_files, **tf_vars)
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue