2019-10-01 09:12:18 -04:00
|
|
|
"""Unit tests: template_j2cli & template_envtpl"""
|
2020-07-11 15:13:24 -04:00
|
|
|
import os
|
2019-10-01 09:12:18 -04:00
|
|
|
import pytest
|
|
|
|
|
2020-07-11 15:13:24 -04:00
|
|
|
FILE_MODE = 0o754
|
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
LOCAL_CLASS = "j2_Test+@-!^Class"
|
2021-01-10 16:38:42 -05:00
|
|
|
LOCAL_CLASS2 = "j2_Test+@-|^2nd_Class withSpace"
|
2021-12-23 16:42:43 -05:00
|
|
|
LOCAL_ARCH = "j2_Test+@-!^Arch"
|
2019-10-01 09:12:18 -04:00
|
|
|
LOCAL_SYSTEM = "j2_Test+@-!^System"
|
|
|
|
LOCAL_HOST = "j2_Test+@-!^Host"
|
|
|
|
LOCAL_USER = "j2_Test+@-!^User"
|
|
|
|
LOCAL_DISTRO = "j2_Test+@-!^Distro"
|
2022-01-17 14:46:31 -05:00
|
|
|
LOCAL_DISTRO_FAMILY = "j2_Test+@-!^Family"
|
2019-10-01 09:12:18 -04:00
|
|
|
TEMPLATE = f'''
|
|
|
|
start of template
|
2022-01-17 14:46:31 -05:00
|
|
|
j2 class = >{{{{YADM_CLASS}}}}<
|
|
|
|
j2 arch = >{{{{YADM_ARCH}}}}<
|
|
|
|
j2 os = >{{{{YADM_OS}}}}<
|
|
|
|
j2 host = >{{{{YADM_HOSTNAME}}}}<
|
|
|
|
j2 user = >{{{{YADM_USER}}}}<
|
|
|
|
j2 distro = >{{{{YADM_DISTRO}}}}<
|
|
|
|
j2 distro_family = >{{{{YADM_DISTRO_FAMILY}}}}<
|
2022-02-21 12:31:44 -05:00
|
|
|
j2 classes = >{{{{YADM_CLASSES}}}}<
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- if YADM_CLASS == "wrongclass1" %}}
|
|
|
|
wrong class 1
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_CLASS == "{LOCAL_CLASS}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for class = \
|
|
|
|
{{{{YADM_CLASS}}}} ({{{{YADM_CLASS}}}} repeated)
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_CLASS == "wrongclass2" %}}
|
|
|
|
wrong class 2
|
|
|
|
{{%- endif %}}
|
2021-01-10 16:38:42 -05:00
|
|
|
{{%- if "{LOCAL_CLASS2}" in YADM_CLASSES.split("\\n") %}}
|
2022-01-22 19:14:23 -05:00
|
|
|
Included j2 section for second class
|
2021-01-10 16:38:42 -05:00
|
|
|
{{%- endif %}}
|
2021-12-23 16:42:43 -05:00
|
|
|
{{%- if YADM_ARCH == "wrongarch1" %}}
|
|
|
|
wrong arch 1
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_ARCH == "{LOCAL_ARCH}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for arch = {{{{YADM_ARCH}}}} ({{{{YADM_ARCH}}}} repeated)
|
2021-12-23 16:42:43 -05:00
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_ARCH == "wrongarch2" %}}
|
|
|
|
wrong arch 2
|
|
|
|
{{%- endif %}}
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- if YADM_OS == "wrongos1" %}}
|
|
|
|
wrong os 1
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_OS == "{LOCAL_SYSTEM}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for os = {{{{YADM_OS}}}} ({{{{YADM_OS}}}} repeated)
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_OS == "wrongos2" %}}
|
|
|
|
wrong os 2
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_HOSTNAME == "wronghost1" %}}
|
|
|
|
wrong host 1
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_HOSTNAME == "{LOCAL_HOST}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for host = \
|
|
|
|
{{{{YADM_HOSTNAME}}}} ({{{{YADM_HOSTNAME}}}} again)
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_HOSTNAME == "wronghost2" %}}
|
|
|
|
wrong host 2
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_USER == "wronguser1" %}}
|
|
|
|
wrong user 1
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_USER == "{LOCAL_USER}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for user = {{{{YADM_USER}}}} ({{{{YADM_USER}}}} repeated)
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_USER == "wronguser2" %}}
|
|
|
|
wrong user 2
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_DISTRO == "wrongdistro1" %}}
|
|
|
|
wrong distro 1
|
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_DISTRO == "{LOCAL_DISTRO}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for distro = \
|
|
|
|
{{{{YADM_DISTRO}}}} ({{{{YADM_DISTRO}}}} again)
|
2019-10-01 09:12:18 -04:00
|
|
|
{{%- endif %}}
|
|
|
|
{{%- if YADM_DISTRO == "wrongdistro2" %}}
|
|
|
|
wrong distro 2
|
|
|
|
{{%- 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}" %}}
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for distro_family = \
|
2022-01-17 14:46:31 -05:00
|
|
|
{{{{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
|
|
|
|
'''
|
|
|
|
EXPECTED = f'''
|
|
|
|
start of template
|
2022-01-17 14:46:31 -05:00
|
|
|
j2 class = >{LOCAL_CLASS}<
|
|
|
|
j2 arch = >{LOCAL_ARCH}<
|
|
|
|
j2 os = >{LOCAL_SYSTEM}<
|
|
|
|
j2 host = >{LOCAL_HOST}<
|
|
|
|
j2 user = >{LOCAL_USER}<
|
|
|
|
j2 distro = >{LOCAL_DISTRO}<
|
|
|
|
j2 distro_family = >{LOCAL_DISTRO_FAMILY}<
|
2022-02-21 12:31:44 -05:00
|
|
|
j2 classes = >{LOCAL_CLASS2}
|
|
|
|
{LOCAL_CLASS}<
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
2022-01-22 19:14:23 -05:00
|
|
|
Included j2 section for second class
|
2022-01-17 15:27:52 -05:00
|
|
|
Included j2 section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
|
|
|
|
Included j2 section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
|
|
|
Included j2 section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
|
|
|
Included j2 section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
|
|
|
Included j2 section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
|
|
|
|
Included j2 section for distro_family = \
|
2022-01-17 14:46:31 -05:00
|
|
|
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
|
2019-10-01 09:12:18 -04:00
|
|
|
end of template
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('processor', ('j2cli', 'envtpl'))
|
|
|
|
def test_template_j2(runner, yadm, tmpdir, processor):
|
|
|
|
"""Test processing by j2cli & envtpl"""
|
|
|
|
|
|
|
|
input_file = tmpdir.join('input')
|
|
|
|
input_file.write(TEMPLATE, ensure=True)
|
2020-07-11 15:13:24 -04:00
|
|
|
input_file.chmod(FILE_MODE)
|
2019-10-01 09:12:18 -04:00
|
|
|
output_file = tmpdir.join('output')
|
|
|
|
|
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)
|
|
|
|
output_file.write('existing')
|
|
|
|
output_file.chmod(0o400)
|
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
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-01 09:12:18 -04:00
|
|
|
template_{processor} "{input_file}" "{output_file}"
|
|
|
|
"""
|
|
|
|
run = runner(command=['bash'], inp=script)
|
|
|
|
assert run.success
|
|
|
|
assert run.err == ''
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('processor', ('j2cli', 'envtpl'))
|
|
|
|
def test_source(runner, yadm, tmpdir, processor):
|
|
|
|
"""Test YADM_SOURCE"""
|
|
|
|
|
|
|
|
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)
|
2019-10-10 09:09:31 -04:00
|
|
|
output_file = tmpdir.join('output')
|
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
template_{processor} "{input_file}" "{output_file}"
|
|
|
|
"""
|
|
|
|
run = runner(command=['bash'], inp=script)
|
|
|
|
assert run.success
|
|
|
|
assert run.err == ''
|
|
|
|
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
|