2019-10-30 18:29:17 -04:00
|
|
|
"""Unit tests: template_default"""
|
2020-07-11 15:13:24 -04:00
|
|
|
import os
|
|
|
|
|
|
|
|
FILE_MODE = 0o754
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
# these values are also testing the handling of bizarre characters
|
2019-10-30 18:29:17 -04:00
|
|
|
LOCAL_CLASS = "default_Test+@-!^Class"
|
2021-01-10 16:38:42 -05:00
|
|
|
LOCAL_CLASS2 = "default_Test+@-|^2nd_Class withSpace"
|
2021-12-23 16:42:43 -05:00
|
|
|
LOCAL_ARCH = "default_Test+@-!^Arch"
|
2019-10-30 18:29:17 -04:00
|
|
|
LOCAL_SYSTEM = "default_Test+@-!^System"
|
|
|
|
LOCAL_HOST = "default_Test+@-!^Host"
|
|
|
|
LOCAL_USER = "default_Test+@-!^User"
|
|
|
|
LOCAL_DISTRO = "default_Test+@-!^Distro"
|
2022-01-17 14:46:31 -05:00
|
|
|
LOCAL_DISTRO_FAMILY = "default_Test+@-!^Family"
|
2023-07-10 15:43:17 -04:00
|
|
|
TEMPLATE = f"""
|
2019-10-01 09:12:18 -04:00
|
|
|
start of template
|
2022-01-17 14:46:31 -05:00
|
|
|
default class = >{{{{yadm.class}}}}<
|
|
|
|
default arch = >{{{{yadm.arch}}}}<
|
|
|
|
default os = >{{{{yadm.os}}}}<
|
|
|
|
default host = >{{{{yadm.hostname}}}}<
|
|
|
|
default user = >{{{{yadm.user}}}}<
|
|
|
|
default distro = >{{{{yadm.distro}}}}<
|
|
|
|
default distro_family = >{{{{yadm.distro_family}}}}<
|
2022-02-21 12:31:44 -05:00
|
|
|
classes = >{{{{yadm.classes}}}}<
|
2019-10-05 12:01:13 -04:00
|
|
|
{{% if yadm.class == "else1" %}}
|
|
|
|
wrong else 1
|
|
|
|
{{% else %}}
|
|
|
|
Included section from else
|
|
|
|
{{% endif %}}
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% if yadm.class == "wrongclass1" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong class 1
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.class == "{LOCAL_CLASS}" %}}
|
|
|
|
Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated)
|
2019-10-05 12:01:13 -04:00
|
|
|
Multiple lines
|
|
|
|
{{% else %}}
|
|
|
|
Should not be included...
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
2021-01-10 16:38:42 -05:00
|
|
|
{{% if yadm.class == "{LOCAL_CLASS2}" %}}
|
|
|
|
Included section for second class
|
|
|
|
{{% endif %}}
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% if yadm.class == "wrongclass2" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong class 2
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
2021-12-23 16:42:43 -05:00
|
|
|
{{% if yadm.arch == "wrongarch1" %}}
|
|
|
|
wrong arch 1
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.arch == "{LOCAL_ARCH}" %}}
|
|
|
|
Included section for arch = {{{{yadm.arch}}}} ({{{{yadm.arch}}}} repeated)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.arch == "wrongarch2" %}}
|
|
|
|
wrong arch 2
|
|
|
|
{{% endif %}}
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% if yadm.os == "wrongos1" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong os 1
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.os == "{LOCAL_SYSTEM}" %}}
|
|
|
|
Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.os == "wrongos2" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong os 2
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.hostname == "wronghost1" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong host 1
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.hostname == "{LOCAL_HOST}" %}}
|
|
|
|
Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.hostname == "wronghost2" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong host 2
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.user == "wronguser1" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong user 1
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.user == "{LOCAL_USER}" %}}
|
|
|
|
Included section for user = {{{{yadm.user}}}} ({{{{yadm.user}}}} repeated)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.user == "wronguser2" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong user 2
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro == "wrongdistro1" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong distro 1
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro == "{LOCAL_DISTRO}" %}}
|
|
|
|
Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro == "wrongdistro2" %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
wrong distro 2
|
2019-10-01 09:17:28 -04:00
|
|
|
{{% endif %}}
|
2022-01-17 14:46:31 -05:00
|
|
|
{{% if yadm.distro_family == "wrongfamily1" %}}
|
|
|
|
wrong family 1
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro_family == "{LOCAL_DISTRO_FAMILY}" %}}
|
|
|
|
Included section for distro_family = \
|
|
|
|
{{{{yadm.distro_family}}}} ({{{{yadm.distro_family}}}} again)
|
|
|
|
{{% endif %}}
|
|
|
|
{{% if yadm.distro_family == "wrongfamily2" %}}
|
|
|
|
wrong family 2
|
|
|
|
{{% endif %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
end of template
|
2023-07-10 15:43:17 -04:00
|
|
|
"""
|
|
|
|
EXPECTED = f"""
|
2019-10-01 09:12:18 -04:00
|
|
|
start of template
|
2022-01-17 14:46:31 -05:00
|
|
|
default class = >{LOCAL_CLASS}<
|
|
|
|
default arch = >{LOCAL_ARCH}<
|
|
|
|
default os = >{LOCAL_SYSTEM}<
|
|
|
|
default host = >{LOCAL_HOST}<
|
|
|
|
default user = >{LOCAL_USER}<
|
|
|
|
default distro = >{LOCAL_DISTRO}<
|
|
|
|
default distro_family = >{LOCAL_DISTRO_FAMILY}<
|
2022-02-21 12:31:44 -05:00
|
|
|
classes = >{LOCAL_CLASS2}
|
|
|
|
{LOCAL_CLASS}<
|
2019-10-05 12:01:13 -04:00
|
|
|
Included section from else
|
2019-10-01 09:12:18 -04:00
|
|
|
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
2019-10-05 12:01:13 -04:00
|
|
|
Multiple lines
|
2021-01-10 16:38:42 -05:00
|
|
|
Included section for second class
|
2021-12-23 16:42:43 -05:00
|
|
|
Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
|
2019-10-01 09:12:18 -04:00
|
|
|
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
2019-10-01 09:17:28 -04:00
|
|
|
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
2019-10-01 09:12:18 -04:00
|
|
|
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
2019-10-01 09:17:28 -04:00
|
|
|
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
|
2022-01-17 14:46:31 -05:00
|
|
|
Included section for distro_family = \
|
|
|
|
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
|
2019-10-01 09:12:18 -04:00
|
|
|
end of template
|
2023-07-10 15:43:17 -04:00
|
|
|
"""
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
INCLUDE_BASIC = "basic\n"
|
|
|
|
INCLUDE_VARIABLES = """\
|
2020-12-21 16:38:31 -05:00
|
|
|
included <{{ yadm.class }}> file
|
2020-10-09 09:36:58 -04:00
|
|
|
|
|
|
|
empty line above
|
2023-07-10 15:43:17 -04:00
|
|
|
"""
|
|
|
|
INCLUDE_NESTED = "no newline at the end"
|
2020-10-09 09:36:58 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
TEMPLATE_INCLUDE = """\
|
2020-10-09 09:36:58 -04:00
|
|
|
The first line
|
|
|
|
{% include empty %}
|
|
|
|
An empty file removes the line above
|
|
|
|
{%include basic%}
|
|
|
|
{% include "./variables.{{ yadm.os }}" %}
|
|
|
|
{% include dir/nested %}
|
|
|
|
Include basic again:
|
|
|
|
{% include basic %}
|
2023-07-10 15:43:17 -04:00
|
|
|
"""
|
|
|
|
EXPECTED_INCLUDE = f"""\
|
2020-10-09 09:36:58 -04:00
|
|
|
The first line
|
|
|
|
An empty file removes the line above
|
|
|
|
basic
|
|
|
|
included <{LOCAL_CLASS}> file
|
|
|
|
|
|
|
|
empty line above
|
|
|
|
no newline at the end
|
|
|
|
Include basic again:
|
|
|
|
basic
|
2023-07-10 15:43:17 -04:00
|
|
|
"""
|
2020-10-09 09:36:58 -04:00
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2019-10-30 18:29:17 -04:00
|
|
|
def test_template_default(runner, yadm, tmpdir):
|
|
|
|
"""Test template_default"""
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
input_file = tmpdir.join("input")
|
2019-10-01 09:12:18 -04:00
|
|
|
input_file.write(TEMPLATE, ensure=True)
|
2020-07-11 15:13:24 -04:00
|
|
|
input_file.chmod(FILE_MODE)
|
2023-07-10 15:43:17 -04:00
|
|
|
output_file = tmpdir.join("output")
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2021-02-28 11:55:37 -05:00
|
|
|
# ensure overwrite works when file exists as read-only (there is some
|
|
|
|
# special processing when this is encountered because some environments do
|
|
|
|
# not properly overwrite read-only files)
|
2023-07-10 15:43:17 -04:00
|
|
|
output_file.write("existing")
|
2021-02-28 11:55:37 -05:00
|
|
|
output_file.chmod(0o400)
|
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2019-12-04 23:18:55 -05:00
|
|
|
set_awk
|
2019-10-01 09:12:18 -04:00
|
|
|
local_class="{LOCAL_CLASS}"
|
2021-01-10 16:38:42 -05:00
|
|
|
local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
|
2021-12-23 16:42:43 -05:00
|
|
|
local_arch="{LOCAL_ARCH}"
|
2019-10-01 09:12:18 -04:00
|
|
|
local_system="{LOCAL_SYSTEM}"
|
|
|
|
local_host="{LOCAL_HOST}"
|
|
|
|
local_user="{LOCAL_USER}"
|
|
|
|
local_distro="{LOCAL_DISTRO}"
|
2022-01-17 14:46:31 -05:00
|
|
|
local_distro_family="{LOCAL_DISTRO_FAMILY}"
|
2019-10-30 18:29:17 -04:00
|
|
|
template_default "{input_file}" "{output_file}"
|
2019-10-01 09:12:18 -04:00
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
assert output_file.read() == EXPECTED
|
2020-07-11 15:13:24 -04:00
|
|
|
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|
2019-10-10 09:09:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_source(runner, yadm, tmpdir):
|
|
|
|
"""Test yadm.source"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
input_file = tmpdir.join("input")
|
|
|
|
input_file.write("{{yadm.source}}", ensure=True)
|
2020-07-11 15:13:24 -04:00
|
|
|
input_file.chmod(FILE_MODE)
|
2023-07-10 15:43:17 -04:00
|
|
|
output_file = tmpdir.join("output")
|
2019-10-10 09:09:31 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2019-12-04 23:18:55 -05:00
|
|
|
set_awk
|
2019-10-30 18:29:17 -04:00
|
|
|
template_default "{input_file}" "{output_file}"
|
2019-10-10 09:09:31 -04:00
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2019-10-10 09:09:31 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-10 09:09:31 -04:00
|
|
|
assert output_file.read().strip() == str(input_file)
|
2020-07-11 15:13:24 -04:00
|
|
|
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|
2020-10-09 09:36:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_include(runner, yadm, tmpdir):
|
|
|
|
"""Test include"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
empty_file = tmpdir.join("empty")
|
|
|
|
empty_file.write("", ensure=True)
|
2020-10-09 09:36:58 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
basic_file = tmpdir.join("basic")
|
2020-10-09 09:36:58 -04:00
|
|
|
basic_file.write(INCLUDE_BASIC)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
variables_file = tmpdir.join(f"variables.{LOCAL_SYSTEM}")
|
2020-10-09 09:36:58 -04:00
|
|
|
variables_file.write(INCLUDE_VARIABLES)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
nested_file = tmpdir.join("dir").join("nested")
|
2020-10-09 09:36:58 -04:00
|
|
|
nested_file.write(INCLUDE_NESTED, ensure=True)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
input_file = tmpdir.join("input")
|
2020-10-09 09:36:58 -04:00
|
|
|
input_file.write(TEMPLATE_INCLUDE)
|
|
|
|
input_file.chmod(FILE_MODE)
|
2023-07-10 15:43:17 -04:00
|
|
|
output_file = tmpdir.join("output")
|
2020-10-09 09:36:58 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
set_awk
|
|
|
|
local_class="{LOCAL_CLASS}"
|
|
|
|
local_system="{LOCAL_SYSTEM}"
|
|
|
|
template_default "{input_file}" "{output_file}"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2020-10-09 09:36:58 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2020-10-09 09:36:58 -04:00
|
|
|
assert output_file.read() == EXPECTED_INCLUDE
|
|
|
|
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|
2022-01-24 16:49:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_env(runner, yadm, tmpdir):
|
|
|
|
"""Test env"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
input_file = tmpdir.join("input")
|
|
|
|
input_file.write("{{env.PWD}}", ensure=True)
|
2022-01-24 16:49:46 -05:00
|
|
|
input_file.chmod(FILE_MODE)
|
2023-07-10 15:43:17 -04:00
|
|
|
output_file = tmpdir.join("output")
|
2022-01-24 16:49:46 -05:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
set_awk
|
|
|
|
template_default "{input_file}" "{output_file}"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2022-01-24 16:49:46 -05:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert output_file.read().strip() == os.environ["PWD"]
|