278 lines
7.6 KiB
Python
278 lines
7.6 KiB
Python
from pytest import fixture # , raises
|
|
from pathlib import Path
|
|
from yaml import safe_load
|
|
|
|
from rougail import Rougail
|
|
from rougail.output_ansible import RougailOutputAnsible as RougailOutput
|
|
from rougail.user_data_yaml import RougailUserDataYaml
|
|
|
|
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):
|
|
if mandatory:
|
|
get_values_for_config(config)
|
|
else:
|
|
get_values_for_config(config, level="mandatories")
|
|
##################################
|
|
generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()[1]
|
|
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)
|
|
|
|
|
|
def test_warnings():
|
|
rougailconfig = get_rougail_config(Path('tests/warnings/structures'), True)
|
|
##################################
|
|
rougailconfig['step.output'] = 'ansible'
|
|
rougailconfig['step.user_data'] = ['yaml']
|
|
rougailconfig['yaml.filename'] = ['tests/warnings/yaml/config.yml']
|
|
extra_dictionaries = rougailconfig["extra_dictionaries"]
|
|
extra_dictionaries["hosts"] = [str(Path(__file__).parent / "hosts")]
|
|
rougailconfig['extra_dictionaries'] = extra_dictionaries
|
|
##################################
|
|
rougail = Rougail(rougailconfig)
|
|
config = rougail.run()
|
|
generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run()
|
|
err_warn = rougail.user_datas(generated_user_data)
|
|
output = RougailOutput(
|
|
config=config,
|
|
rougailconfig=rougailconfig,
|
|
user_data_errors=err_warn["errors"],
|
|
user_data_warnings=err_warn["warnings"],
|
|
)
|
|
ret = output.run()
|
|
assert ret[0] is True
|
|
assert safe_load(ret[1]) == {
|
|
"_meta": {
|
|
"hostvars": {
|
|
"localhost": {
|
|
"_warnings": [
|
|
"variable or family \"rougail.an_unknown_var\" does not exist, it will be ignored when loading from the YAML file \"tests/warnings/yaml/config.yml\""
|
|
]
|
|
},
|
|
"GROUP1_01": {
|
|
"ansible_host": "group1.net",
|
|
"rougail": {
|
|
"a_var": "a_value"
|
|
}
|
|
},
|
|
"GROUP2_01": {
|
|
"ansible_host": "group2.net",
|
|
'rougail': {
|
|
'a_var': 'a_value',
|
|
},
|
|
},
|
|
'GROUP2_02': {
|
|
'ansible_host': 'group3.net',
|
|
'rougail': {
|
|
'a_var': 'a_value',
|
|
},
|
|
},
|
|
'group4.net': {
|
|
'ansible_host': 'group4.net',
|
|
'rougail': {
|
|
'a_var': 'a_value',
|
|
},
|
|
},
|
|
'group5.net': {
|
|
'ansible_host': 'group5.net',
|
|
"rougail": {
|
|
"a_var": "a_value"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"ungrouped": {
|
|
"hosts": [
|
|
"localhost"
|
|
]
|
|
},
|
|
"group1": {
|
|
"hosts": [
|
|
"GROUP1_01"
|
|
]
|
|
},
|
|
"group2": {
|
|
"hosts": [
|
|
"GROUP2_01",
|
|
'GROUP2_02'
|
|
]
|
|
},
|
|
|
|
'group3': {
|
|
'hosts': [
|
|
'group4.net',
|
|
'group5.net',
|
|
]
|
|
},
|
|
|
|
"groups": {
|
|
"children": [
|
|
"group1",
|
|
"group2"
|
|
]
|
|
}
|
|
}
|
|
|
|
|
|
def test_no_warnings():
|
|
rougailconfig = get_rougail_config(Path('tests/warnings/structures'), True)
|
|
##################################
|
|
rougailconfig['step.output'] = 'ansible'
|
|
rougailconfig['ansible.export_warnings'] = False
|
|
rougailconfig['step.user_data'] = ['yaml']
|
|
rougailconfig['yaml.filename'] = ['tests/warnings/yaml/config.yml']
|
|
extra_dictionaries = rougailconfig["extra_dictionaries"]
|
|
extra_dictionaries["hosts"] = [str(Path(__file__).parent / "hosts")]
|
|
rougailconfig['extra_dictionaries'] = extra_dictionaries
|
|
##################################
|
|
rougail = Rougail(rougailconfig)
|
|
config = rougail.run()
|
|
generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run()
|
|
err_warn = rougail.user_datas(generated_user_data)
|
|
output = RougailOutput(
|
|
config=config,
|
|
rougailconfig=rougailconfig,
|
|
user_data_errors=err_warn["errors"],
|
|
user_data_warnings=err_warn["warnings"],
|
|
)
|
|
ret = output.run()
|
|
assert ret[0] is True
|
|
assert safe_load(ret[1]) == {
|
|
"_meta": {
|
|
"hostvars": {
|
|
"GROUP1_01": {
|
|
"ansible_host": "group1.net",
|
|
"rougail": {
|
|
"a_var": "a_value"
|
|
}
|
|
},
|
|
"GROUP2_01": {
|
|
"ansible_host": "group2.net",
|
|
'rougail': {
|
|
'a_var': 'a_value',
|
|
},
|
|
},
|
|
'GROUP2_02': {
|
|
'ansible_host': 'group3.net',
|
|
'rougail': {
|
|
'a_var': 'a_value',
|
|
},
|
|
},
|
|
'group4.net': {
|
|
'ansible_host': 'group4.net',
|
|
'rougail': {
|
|
'a_var': 'a_value',
|
|
},
|
|
},
|
|
'group5.net': {
|
|
'ansible_host': 'group5.net',
|
|
"rougail": {
|
|
"a_var": "a_value"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
'group1': {
|
|
'hosts': [
|
|
'GROUP1_01',
|
|
],
|
|
},
|
|
'group2': {
|
|
'hosts': [
|
|
'GROUP2_01',
|
|
'GROUP2_02',
|
|
],
|
|
},
|
|
'group3': {
|
|
'hosts': [
|
|
'group4.net',
|
|
'group5.net',
|
|
],
|
|
},
|
|
'groups': {
|
|
'children': [
|
|
'group1',
|
|
'group2',
|
|
],
|
|
}
|
|
}
|