rougail-output-ansible/tests/test_load.py
2024-12-09 10:51:46 +01:00

93 lines
3 KiB
Python

from pytest import fixture # , raises
from pathlib import Path
from rougail import Rougail
from rougail.output_ansible import RougailOutputAnsible as RougailOutput
from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config
EXT = 'json'
excludes = []
#excludes = [
# '60_5family_dynamic_unknown_suffix',
# '60_5family_dynamic_variable_outside_sub_suffix',
#]
test_ok = get_structures_list(excludes)
#test_ok = [Path('../rougail-tests/structures/00_0version_underscore')]
def idfn(fixture_value):
return fixture_value.name
@fixture(scope="module", params=test_ok, ids=idfn)
def test_dir(request):
return request.param
def _test_dictionaries(test_dir, namespace, ext, *, read_write=True, mandatory=False, do_calc=True):
rougailconfig = get_rougail_config(test_dir, namespace)
if not rougailconfig:
return
##################################
rougailconfig['step.output'] = 'ansible'
rougailconfig["json.read_write"] = read_write
rougailconfig["json.mandatory"] = mandatory
extra_dictionaries = rougailconfig["extra_dictionaries"]
extra_dictionaries["hosts"] = [str(Path(__file__).parent / "hosts")]
rougailconfig['extra_dictionaries'] = extra_dictionaries
##################################
dir_name = 'test'
if namespace:
dir_name += '_namespace'
elif (test_dir / 'force_namespace').is_file():
return
##################################
if read_write:
dir_name += '_read_write'
if mandatory:
dir_name += '_mandatory'
if not do_calc:
dir_name += '_errors'
##################################
rougail = Rougail(rougailconfig)
config = rougail.run()
##################################
if do_calc and (mandatory or not read_write):
get_values_for_config(config)
##################################
generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()
output_file = Path(__file__).parent / 'results' / dir_name / (test_dir.name + "." + ext)
if not output_file.is_file():
if not output_file.parent.is_dir():
output_file.parent.mkdir()
with output_file.open('w') as outfh:
outfh.write(generated_output)
with output_file.open() as outfh:
attented_output = outfh.read()
assert generated_output == attented_output, f'filename {output_file}'
# do not test without namespace, ansible needs namespaces
def test_dictionaries_ansible_namespace(test_dir):
_test_dictionaries(test_dir, True, EXT)
def test_dictionaries_ansible_namespace_errors(test_dir):
_test_dictionaries(test_dir, True, EXT, do_calc=False, mandatory=True)
def test_dictionaries_ansible_namespace_read_only(test_dir):
_test_dictionaries(test_dir, True, EXT, read_write=False)
def test_dictionaries_ansible_namespace_mandatory(test_dir):
_test_dictionaries(test_dir, True, EXT, mandatory=True)
def test_dictionaries_ansible_namespace_mandatory_read_only(test_dir):
_test_dictionaries(test_dir, True, EXT, read_write=False, mandatory=True)