2024-09-23 14:34:15 +02:00
|
|
|
from .custom import CustomOption
|
|
|
|
|
2024-09-18 11:43:22 +02:00
|
|
|
from pathlib import Path
|
2024-09-30 16:40:24 +02:00
|
|
|
from os import getcwd, listdir, environ, makedirs
|
|
|
|
from os.path import isfile, join, isdir, dirname
|
|
|
|
from pathlib import Path
|
2024-10-01 12:03:52 +02:00
|
|
|
from json import load, dump
|
2024-09-30 15:23:47 +02:00
|
|
|
|
2024-09-30 16:40:24 +02:00
|
|
|
from pytest import fixture, raises
|
2024-09-18 11:43:22 +02:00
|
|
|
from ruamel.yaml import YAML
|
2024-09-24 16:42:03 +02:00
|
|
|
from dotenv import load_dotenv
|
2024-09-18 11:43:22 +02:00
|
|
|
|
2024-09-23 14:34:15 +02:00
|
|
|
from rougail import RougailConfig, Rougail
|
2024-09-24 16:42:03 +02:00
|
|
|
from rougail.user_data_environment.data import RougailUserDataEnvironment
|
2024-09-18 11:43:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
dico_dirs = Path('../rougail/tests/dictionaries')
|
2024-09-30 16:40:24 +02:00
|
|
|
#env_test_folder = '00_6string'
|
|
|
|
|
|
|
|
# path configuration
|
2024-10-01 11:57:38 +02:00
|
|
|
#_here = Path(__file__).resolve().parent
|
|
|
|
#envtest = _here / 'tests' / 'envvars'
|
|
|
|
#test_ok = [dico_dirs / env_test_folder]
|
2024-09-30 16:40:24 +02:00
|
|
|
|
|
|
|
test_ok = set()
|
|
|
|
excludes = set()
|
|
|
|
test_ok -= excludes
|
2024-09-18 11:43:22 +02:00
|
|
|
|
2024-10-01 11:57:38 +02:00
|
|
|
for test in dico_dirs.iterdir():
|
|
|
|
if (test / 'tiramisu').is_dir() and test.name not in excludes:
|
|
|
|
test_ok.add(test)
|
|
|
|
|
|
|
|
test_ok = list(test_ok)
|
|
|
|
test_ok.sort()
|
|
|
|
|
|
|
|
|
2024-09-18 11:43:22 +02:00
|
|
|
@fixture(scope="module", params=test_ok)
|
|
|
|
def test_dir(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
2024-09-23 14:34:15 +02:00
|
|
|
def _test_dictionaries(test_dir):
|
2024-09-25 08:52:13 +02:00
|
|
|
"rougail config settings"
|
2024-09-18 11:43:22 +02:00
|
|
|
rougailconfig = RougailConfig.copy()
|
2024-09-24 16:42:03 +02:00
|
|
|
rougailconfig['step.user_data'] = ['environment']
|
|
|
|
rougailconfig['main_namespace'] = None
|
2024-10-01 11:57:38 +02:00
|
|
|
dirs = [str(test_dir / 'dictionaries' / 'rougail')]
|
2024-09-18 11:43:22 +02:00
|
|
|
rougailconfig['custom_types']['custom'] = CustomOption
|
2024-09-23 14:34:15 +02:00
|
|
|
rougailconfig['main_dictionaries'] = dirs
|
|
|
|
return rougailconfig
|
|
|
|
|
|
|
|
|
|
|
|
def test_dictionaries_warning(test_dir):
|
2024-10-01 12:03:52 +02:00
|
|
|
"tests the env output"
|
2024-10-01 11:57:38 +02:00
|
|
|
current_dirname = test_dir.name
|
2024-09-23 14:34:15 +02:00
|
|
|
rougailconfig = _test_dictionaries(test_dir)
|
2024-09-30 14:13:42 +02:00
|
|
|
# populate tests if not already exists
|
|
|
|
dest_dir = Path('tests') / 'envvars' / test_dir.name
|
|
|
|
populate(dest_dir, rougailconfig)
|
2024-10-01 12:03:52 +02:00
|
|
|
# loads the config in the tiramisu's meaning
|
2024-09-23 14:34:15 +02:00
|
|
|
config = Rougail(rougailconfig).run() #Config(optiondescription["option_0"])
|
2024-09-24 16:42:03 +02:00
|
|
|
config_dict = dict(config.value.get())
|
|
|
|
user_datas = {'errors': [],
|
|
|
|
'warnings': [],
|
|
|
|
}
|
|
|
|
# loading the env file
|
2024-09-30 15:23:47 +02:00
|
|
|
envfile = dest_dir / 'env'/ 'all.env'
|
2024-09-24 16:42:03 +02:00
|
|
|
load_dotenv(envfile)
|
2024-09-25 08:52:13 +02:00
|
|
|
# loads the environment variables in the tiramisu config
|
2024-09-24 16:42:03 +02:00
|
|
|
environment = RougailUserDataEnvironment(config,
|
|
|
|
rougailconfig=rougailconfig,
|
|
|
|
user_datas=user_datas,
|
|
|
|
)
|
|
|
|
environment.run()
|
2024-10-01 12:13:30 +02:00
|
|
|
# expected output
|
2024-10-01 11:57:38 +02:00
|
|
|
with open(Path('tests') / 'envvars' / current_dirname / 'makedict' / 'all.env') as json_file:
|
2024-10-01 12:03:52 +02:00
|
|
|
expected = load(json_file)
|
2024-10-01 12:13:30 +02:00
|
|
|
# here is the effective test
|
2024-10-01 12:24:44 +02:00
|
|
|
assert expected == config_dict
|
2024-09-30 14:13:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def populate(dest_dir, rougailconfig):
|
|
|
|
for level in ['all', 'mandatories']:
|
|
|
|
environment_file = dest_dir / 'env' / f'{level}.env'
|
|
|
|
makedict_file = dest_dir / 'makedict' / f'{level}.env'
|
2024-10-01 11:57:38 +02:00
|
|
|
environment_file.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
makedict_file.parent.mkdir(parents=True, exist_ok=True)
|
2024-09-30 15:23:47 +02:00
|
|
|
if not environment_file.is_file() or not makedict_file.is_file():
|
2024-09-30 14:13:42 +02:00
|
|
|
config = Rougail(rougailconfig).run()
|
|
|
|
if level == 'all':
|
|
|
|
root_config = config.unrestraint
|
|
|
|
else:
|
|
|
|
root_config = config.unrestraint.value.mandatory()
|
|
|
|
values = ['ROUGAIL_' + variable.path().upper() + '="' + get_value(variable) + '"' for variable in get_variables(root_config)]
|
|
|
|
if not environment_file.is_file():
|
|
|
|
with environment_file.open('w') as envfh:
|
|
|
|
envfh.write('\n'.join(values) + '\n')
|
2024-09-30 15:23:47 +02:00
|
|
|
if not makedict_file.is_file():
|
2024-10-01 11:57:38 +02:00
|
|
|
with makedict_file.open('w') as envfh:
|
|
|
|
#envfh.write('\n'.join(values) + '\n')
|
|
|
|
config_dict = dict(option_value(config.value.get()))
|
2024-10-01 12:03:52 +02:00
|
|
|
dump(config_dict, envfh, indent=4)
|
2024-10-01 11:57:38 +02:00
|
|
|
envfh.write('\n')
|
2024-09-30 14:13:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_value(variable):
|
|
|
|
tests = variable.information.get('test', None)
|
|
|
|
if not tests:
|
|
|
|
if variable.type() == 'integer':
|
|
|
|
tests = [1, 2, 3]
|
|
|
|
elif variable.type() == 'float':
|
|
|
|
tests = [1.1, 2.2, 3.3]
|
|
|
|
elif variable.type() == 'port':
|
|
|
|
tests = ['80', '443']
|
|
|
|
elif variable.type() == 'boolean':
|
|
|
|
tests = [True]
|
|
|
|
elif variable.type() == 'domain name':
|
|
|
|
tests = ['domain1.lan', 'domain2.lan']
|
|
|
|
elif variable.type() == 'choice':
|
|
|
|
tests = variable.value.list()
|
|
|
|
else:
|
|
|
|
tests = ['string1', 'string2', 'string3']
|
|
|
|
if not variable.ismulti() or (variable.isfollower() and variable.ismulti() is True):
|
|
|
|
tests = tests[0]
|
|
|
|
variable.value.set(tests)
|
|
|
|
if isinstance(tests, list):
|
|
|
|
tests = ','.join([str(test) for test in tests])
|
|
|
|
else:
|
|
|
|
tests = str(tests)
|
|
|
|
return tests
|
|
|
|
|
|
|
|
|
|
|
|
def get_variables(config):
|
|
|
|
for key in config:
|
|
|
|
if key.isoptiondescription():
|
|
|
|
yield from get_variables(key)
|
|
|
|
else:
|
|
|
|
yield key
|
|
|
|
|
|
|
|
|
|
|
|
def option_value(parent, key_is_option=False):
|
|
|
|
for option, value in parent.items():
|
|
|
|
if option.isoptiondescription():
|
|
|
|
if not key_is_option and option.isleadership():
|
|
|
|
ret = []
|
|
|
|
for idx, datas in enumerate(option_value(value, key_is_option=True)):
|
|
|
|
sub_option, sub_value = datas
|
|
|
|
if not idx:
|
|
|
|
sub_option = sub_option.path()
|
|
|
|
key = sub_option
|
|
|
|
for val in sub_value:
|
|
|
|
ret.append({sub_option: val})
|
|
|
|
else:
|
|
|
|
index = sub_option.index()
|
|
|
|
sub_option = sub_option.path()
|
|
|
|
ret[index][sub_option] = sub_value
|
|
|
|
yield key, ret
|
|
|
|
else:
|
|
|
|
yield from option_value(value, key_is_option)
|
|
|
|
elif key_is_option:
|
|
|
|
yield option, value
|
|
|
|
else:
|
|
|
|
yield option.path(), value
|