2019-10-30 22:29:17 +00:00
|
|
|
"""Unit tests: template_default"""
|
2020-07-11 19:13:24 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
FILE_MODE = 0o754
|
2019-10-01 13:12:18 +00:00
|
|
|
|
|
|
|
# these values are also testing the handling of bizarre characters
|
2019-10-30 22:29:17 +00:00
|
|
|
LOCAL_CLASS = "default_Test+@-!^Class"
|
|
|
|
LOCAL_SYSTEM = "default_Test+@-!^System"
|
|
|
|
LOCAL_HOST = "default_Test+@-!^Host"
|
|
|
|
LOCAL_USER = "default_Test+@-!^User"
|
|
|
|
LOCAL_DISTRO = "default_Test+@-!^Distro"
|
2019-10-01 13:12:18 +00:00
|
|
|
TEMPLATE = f'''
|
|
|
|
start of template
|
2019-10-30 22:29:17 +00:00
|
|
|
default class = >{{{{yadm.class}}}}<
|
|
|
|
default os = >{{{{yadm.os}}}}<
|
|
|
|
default host = >{{{{yadm.hostname}}}}<
|
|
|
|
default user = >{{{{yadm.user}}}}<
|
|
|
|
default distro = >{{{{yadm.distro}}}}<
|
2019-10-05 16:01:13 +00:00
|
|
|
{{% if yadm.class == "else1" %}}
|
|
|
|
wrong else 1
|
|
|
|
{{% else %}}
|
|
|
|
Included section from else
|
|
|
|
{{% endif %}}
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% if yadm.class == "wrongclass1" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong class 1
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.class == "{LOCAL_CLASS}" %}}
|
|
|
|
Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated)
|
2019-10-05 16:01:13 +00:00
|
|
|
Multiple lines
|
|
|
|
{{% else %}}
|
|
|
|
Should not be included...
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.class == "wrongclass2" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong class 2
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.os == "wrongos1" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong os 1
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.os == "{LOCAL_SYSTEM}" %}}
|
|
|
|
Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.os == "wrongos2" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong os 2
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.hostname == "wronghost1" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong host 1
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.hostname == "{LOCAL_HOST}" %}}
|
|
|
|
Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.hostname == "wronghost2" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong host 2
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.user == "wronguser1" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong user 1
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.user == "{LOCAL_USER}" %}}
|
|
|
|
Included section for user = {{{{yadm.user}}}} ({{{{yadm.user}}}} repeated)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.user == "wronguser2" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong user 2
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro == "wrongdistro1" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong distro 1
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro == "{LOCAL_DISTRO}" %}}
|
|
|
|
Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro == "wrongdistro2" %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
wrong distro 2
|
2019-10-01 13:17:28 +00:00
|
|
|
{{% endif %}}
|
2019-10-01 13:12:18 +00:00
|
|
|
end of template
|
|
|
|
'''
|
|
|
|
EXPECTED = f'''
|
|
|
|
start of template
|
2019-10-30 22:29:17 +00:00
|
|
|
default class = >{LOCAL_CLASS}<
|
|
|
|
default os = >{LOCAL_SYSTEM}<
|
|
|
|
default host = >{LOCAL_HOST}<
|
|
|
|
default user = >{LOCAL_USER}<
|
|
|
|
default distro = >{LOCAL_DISTRO}<
|
2019-10-05 16:01:13 +00:00
|
|
|
Included section from else
|
2019-10-01 13:12:18 +00:00
|
|
|
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
2019-10-05 16:01:13 +00:00
|
|
|
Multiple lines
|
2019-10-01 13:12:18 +00:00
|
|
|
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
2019-10-01 13:17:28 +00:00
|
|
|
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
2019-10-01 13:12:18 +00:00
|
|
|
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
2019-10-01 13:17:28 +00:00
|
|
|
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
|
2019-10-01 13:12:18 +00:00
|
|
|
end of template
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2019-10-30 22:29:17 +00:00
|
|
|
def test_template_default(runner, yadm, tmpdir):
|
|
|
|
"""Test template_default"""
|
2019-10-01 13:12:18 +00:00
|
|
|
|
|
|
|
input_file = tmpdir.join('input')
|
|
|
|
input_file.write(TEMPLATE, ensure=True)
|
2020-07-11 19:13:24 +00:00
|
|
|
input_file.chmod(FILE_MODE)
|
2019-10-01 13:12:18 +00:00
|
|
|
output_file = tmpdir.join('output')
|
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2019-12-05 04:18:55 +00:00
|
|
|
set_awk
|
2019-10-01 13:12:18 +00:00
|
|
|
local_class="{LOCAL_CLASS}"
|
|
|
|
local_system="{LOCAL_SYSTEM}"
|
|
|
|
local_host="{LOCAL_HOST}"
|
|
|
|
local_user="{LOCAL_USER}"
|
|
|
|
local_distro="{LOCAL_DISTRO}"
|
2019-10-30 22:29:17 +00:00
|
|
|
template_default "{input_file}" "{output_file}"
|
2019-10-01 13:12:18 +00:00
|
|
|
"""
|
|
|
|
run = runner(command=['bash'], inp=script)
|
|
|
|
assert run.success
|
|
|
|
assert run.err == ''
|
|
|
|
assert output_file.read() == EXPECTED
|
2020-07-11 19:13:24 +00:00
|
|
|
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|
2019-10-10 13:09:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_source(runner, yadm, tmpdir):
|
|
|
|
"""Test yadm.source"""
|
|
|
|
|
|
|
|
input_file = tmpdir.join('input')
|
|
|
|
input_file.write('{{yadm.source}}', ensure=True)
|
2020-07-11 19:13:24 +00:00
|
|
|
input_file.chmod(FILE_MODE)
|
2019-10-10 13:09:31 +00:00
|
|
|
output_file = tmpdir.join('output')
|
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2019-12-05 04:18:55 +00:00
|
|
|
set_awk
|
2019-10-30 22:29:17 +00:00
|
|
|
template_default "{input_file}" "{output_file}"
|
2019-10-10 13:09:31 +00:00
|
|
|
"""
|
|
|
|
run = runner(command=['bash'], inp=script)
|
|
|
|
assert run.success
|
|
|
|
assert run.err == ''
|
|
|
|
assert output_file.read().strip() == str(input_file)
|
2020-07-11 19:13:24 +00:00
|
|
|
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|