test base and systemd templating

This commit is contained in:
egarette@silique.fr 2023-01-07 17:16:31 +01:00
parent 2d8ef64904
commit 3c5d4a4fa6
25 changed files with 34 additions and 9 deletions

View file

View file

View file

@ -1,11 +1,11 @@
from os import listdir, mkdir, readlink from os import listdir, mkdir, readlink
from os.path import join, isdir, isfile, islink from os.path import join, isdir, isfile, islink
from shutil import rmtree from shutil import rmtree
from pytest import fixture, mark from pytest import fixture, mark, raises
from lxml.etree import parse from lxml.etree import parse
from tiramisu import Config from tiramisu import Config
from rougail import RougailConfig, RougailSystemdTemplate from rougail import RougailConfig, RougailBaseTemplate, RougailSystemdTemplate
template_dirs = 'tests/dictionaries' template_dirs = 'tests/dictionaries'
@ -39,7 +39,7 @@ def find_files(dirname: str,
files.add(join(*root_file)) files.add(join(*root_file))
async def template(test_dir, filename, root): async def template(test_dir, filename, root, engine_name):
test_dir = join(template_dirs, test_dir) test_dir = join(template_dirs, test_dir)
tmp_dir = join(test_dir, '..', 'tmp') tmp_dir = join(test_dir, '..', 'tmp')
@ -71,7 +71,10 @@ async def template(test_dir, filename, root):
RougailConfig['tmpfile_dest_dir'] = '/test/new/file' RougailConfig['tmpfile_dest_dir'] = '/test/new/file'
else: else:
RougailConfig['tmpfile_dest_dir'] = '/usr/local/lib' RougailConfig['tmpfile_dest_dir'] = '/usr/local/lib'
engine = RougailSystemdTemplate(config) if engine_name == 'base':
engine = RougailBaseTemplate(config)
else:
engine = RougailSystemdTemplate(config)
await engine.instance_files() await engine.instance_files()
list_templates = set() list_templates = set()
if isdir(dest_dir): if isdir(dest_dir):
@ -85,6 +88,8 @@ async def template(test_dir, filename, root):
[], [],
list_results, list_results,
) )
if engine_name == 'base' and 'tmpfiles.d/0rougail.conf' in list_results:
list_results.remove('tmpfiles.d/0rougail.conf')
assert list_templates == list_results, f'error with {test_dir}:' assert list_templates == list_results, f'error with {test_dir}:'
for result in list_results: for result in list_results:
template_file = join(dest_dir, result) template_file = join(dest_dir, result)
@ -99,20 +104,40 @@ async def template(test_dir, filename, root):
with open(template_file, 'r') as fh: with open(template_file, 'r') as fh:
generated_file = fh.read() generated_file = fh.read()
assert result_file == generated_file, f'{template_file} content : \n{generated_file}\nexpected: \nresult_file\n' assert result_file == generated_file, f'{template_file} content : \n{generated_file}\nexpected: \nresult_file\n'
rmtree(dest_dir) if isdir(dest_dir):
rmtree(tmp_dir) rmtree(dest_dir)
if isdir(tmp_dir):
rmtree(tmp_dir)
@mark.asyncio @mark.asyncio
async def test_template(test_dir): async def test_template(test_dir):
await template(test_dir, 'base', '1') for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
with raises(Exception) as err:
await template(test_dir, 'base', '1', engine)
else:
await template(test_dir, 'base', '1', engine)
@mark.asyncio @mark.asyncio
async def test_template_multi_1(test_dir): async def test_template_multi_1(test_dir):
await template(test_dir, 'multi', '1') for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
with raises(Exception) as err:
await template(test_dir, 'multi', '1', engine)
else:
await template(test_dir, 'multi', '1', engine)
@mark.asyncio @mark.asyncio
async def test_template_multi_2(test_dir): async def test_template_multi_2(test_dir):
await template(test_dir, 'multi', '1') for engine in ['base', 'systemd']:
not_file = join(template_dirs, test_dir, 'no_' + engine)
if isfile(not_file):
with raises(Exception) as err:
await template(test_dir, 'multi', '1', engine)
else:
await template(test_dir, 'multi', '1', engine)