rougail/tests/test_3_template.py

215 lines
8.2 KiB
Python
Raw Normal View History

2021-05-13 22:30:58 +02:00
from os import listdir, mkdir, readlink
2021-02-21 19:48:30 +01:00
from os.path import join, isdir, isfile, islink
2019-12-02 10:33:46 +01:00
from shutil import rmtree
2023-01-07 17:16:31 +01:00
from pytest import fixture, mark, raises
2019-12-02 10:33:46 +01:00
from lxml.etree import parse
from tiramisu import Config
2023-01-07 17:16:31 +01:00
from rougail import RougailConfig, RougailBaseTemplate, RougailSystemdTemplate
2023-01-23 22:55:17 +01:00
from rougail.error import TemplateError
from jinja2.exceptions import TemplateNotFound
2019-12-02 10:33:46 +01:00
template_dirs = 'tests/dictionaries'
excludes = set([])
#excludes = set(['01base_file_utfchar'])
test_ok = {f for f in listdir(template_dirs) if not f.startswith('_') and isdir(join(template_dirs, f, 'tmpl'))}
test_ok -= excludes
test_ok = list(test_ok)
test_ok.sort()
2023-01-25 22:48:13 +01:00
#test_ok = ['70container_files']
2019-12-02 10:33:46 +01:00
@fixture(scope="module", params=test_ok)
def test_dir(request):
return request.param
2020-07-06 19:47:45 +02:00
def find_files(dirname: str,
root: list,
files: set,
) -> None:
2019-12-22 11:04:39 +01:00
for filename in listdir(dirname):
abs_dirname = join(dirname, filename)
root_file = root + [filename]
if isdir(join(dirname, filename)):
2020-07-06 19:47:45 +02:00
find_files(abs_dirname,
root_file,
files,
)
2019-12-22 11:04:39 +01:00
else:
files.add(join(*root_file))
2023-01-23 22:55:17 +01:00
async def templates(test_dir, filename, root, engine_name, only_one=False):
2019-12-02 10:33:46 +01:00
test_dir = join(template_dirs, test_dir)
2022-10-01 22:27:22 +02:00
tmp_dir = join(test_dir, '..', 'tmp')
2020-07-29 08:59:40 +02:00
2022-10-01 22:27:22 +02:00
modulepath = test_dir.replace('/', '.') + f'.tiramisu.{filename}'
2020-07-29 08:59:40 +02:00
mod = __import__(modulepath)
for token in modulepath.split(".")[1:]:
mod = getattr(mod, token)
config = await Config(mod.option_0)
await config.property.read_only()
2022-10-01 22:27:22 +02:00
if filename == 'multi':
sconfig = config
config = config.option(root)
2020-07-29 08:59:40 +02:00
2019-12-02 10:33:46 +01:00
funcs_file = join(template_dirs, '../eosfunc/test.py')
distrib_dir = join(test_dir, 'tmpl')
2019-12-02 10:33:46 +01:00
dest_dir = join(test_dir, 'dest')
2021-02-14 10:10:48 +01:00
RougailConfig['patches_dir'] = join(test_dir, 'patches')
RougailConfig['templates_dir'] = distrib_dir
RougailConfig['tmp_dir'] = tmp_dir
RougailConfig['functions_file'] = funcs_file
RougailConfig['destinations_dir'] = dest_dir
2023-01-19 12:09:44 +01:00
if isfile(join(test_dir, 'file_tmpfiles_factory_dir')):
RougailConfig['systemd_tmpfile_factory_dir'] = '/test/new/file'
2022-03-06 18:54:57 +01:00
else:
2023-01-19 12:09:44 +01:00
RougailConfig['systemd_tmpfile_factory_dir'] = '/usr/local/lib'
if isfile(join(test_dir, 'file_tmpfiles_delete_before_create')):
RougailConfig['systemd_tmpfile_delete_before_create'] = True
else:
RougailConfig['systemd_tmpfile_delete_before_create'] = False
2023-01-07 17:16:31 +01:00
if engine_name == 'base':
engine = RougailBaseTemplate(config)
else:
engine = RougailSystemdTemplate(config)
2023-01-23 22:55:17 +01:00
if not only_one:
list_templates = [None]
else:
list_templates = set()
find_files(distrib_dir,
2020-07-06 19:47:45 +02:00
[],
list_templates,
)
2023-01-23 22:55:17 +01:00
for template in list_templates:
if isdir(tmp_dir):
rmtree(tmp_dir)
mkdir(tmp_dir)
if isdir(dest_dir):
rmtree(dest_dir)
mkdir(dest_dir)
if template is None:
await engine.instance_files()
list_results = set()
if isdir(join(test_dir, 'result')):
find_files(join(test_dir, 'result'),
[],
list_results,
)
else:
list_results = set()
included = False
activate = True
for service in await config.option('services').option.list('all'):
names = [await o.option.name() for o in await service.option.list('optiondescription')]
if 'files' in names:
for file in await service.option('files').list('optiondescription'):
if not await file.option('source').value.get() == template:
continue
included = await file.information.get('included', 'no') != 'no'
values = await file.option('name').value.get()
if isinstance(values, list):
for value in values:
list_results.add(value[1:])
else:
list_results.add(values[1:])
if not await file.option('activate').value.get():
activate = False
# if template is an overrides
if not list_results and 'overrides' in names:
for override in await service.option('overrides').list('optiondescription'):
if await override.option.description() == template:
return
# if template is a service
if not list_results and template == await service.option.description() and await service.information.get('engine', None):
return
#this file is include and not declared
if template.startswith('inc') and not list_results:
return
try:
await engine.instance_file(template)
except TemplateError as err:
if 'No such file or directory' in str(err):
continue
if included or not activate:
continue
raise err from err
except TemplateNotFound:
continue
assert activate, 'template should be desactivate'
assert not included, 'include file should not be templated'
# else:
# assert list_results, "should find file"
list_files = set()
if isdir(dest_dir):
find_files(dest_dir,
[],
list_files,
)
if engine_name == 'base' and 'tmpfiles.d/0rougail.conf' in list_results:
list_results.remove('tmpfiles.d/0rougail.conf')
assert list_files == list_results, f'error with {test_dir}:'
for result in list_results:
template_file = join(dest_dir, result)
assert islink(template_file) == islink(join(test_dir, 'result', result))
if islink(template_file):
assert readlink(template_file) == readlink(join(test_dir, 'result', result))
continue
if not isfile(template_file):
raise Exception(f'{template_file} is not generated')
with open(join(test_dir, 'result', result), 'r') as fh:
result_file = fh.read()
with open(template_file, 'r') as fh:
generated_file = fh.read()
assert result_file == generated_file, f'{template_file} content : \n{generated_file}\nexpected: \nresult_file\n'
if isdir(dest_dir):
rmtree(dest_dir)
if isdir(tmp_dir):
rmtree(tmp_dir)
2022-10-01 22:27:22 +02:00
@mark.asyncio
2023-01-23 22:55:17 +01:00
async def test_templates(test_dir):
2023-01-07 17:16:31 +01:00
for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
with raises(Exception) as err:
2023-01-23 22:55:17 +01:00
await templates(test_dir, 'base', '1', engine)
2023-01-07 17:16:31 +01:00
else:
2023-01-23 22:55:17 +01:00
await templates(test_dir, 'base', '1', engine)
@mark.asyncio
async def test_template(test_dir):
for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
continue
await templates(test_dir, 'base', '1', engine, only_one=True)
2022-10-01 22:27:22 +02:00
@mark.asyncio
async def test_template_multi_1(test_dir):
2023-01-07 17:16:31 +01:00
for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
with raises(Exception) as err:
2023-01-23 22:55:17 +01:00
await templates(test_dir, 'multi', '1', engine)
2023-01-07 17:16:31 +01:00
else:
2023-01-23 22:55:17 +01:00
await templates(test_dir, 'multi', '1', engine)
2022-10-01 22:27:22 +02:00
@mark.asyncio
async def test_template_multi_2(test_dir):
2023-01-07 17:16:31 +01:00
for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
with raises(Exception) as err:
2023-01-23 22:55:17 +01:00
await templates(test_dir, 'multi', '1', engine)
2023-01-07 17:16:31 +01:00
else:
2023-01-23 22:55:17 +01:00
await templates(test_dir, 'multi', '1', engine)