1
0
Fork 0
mirror of synced 2024-07-26 18:11:09 -04:00
yadm/test/test_unit_set_local_alt_values.py
Erik Flodin 42c74efbac
Add support for multiple local classes
A local class is set with:
$ yadm config local.class cls1

More classes can be added with:
$ yadm config --add local.class cls2
$ yadm config --add local.class cls3

Any of cls1, cls2 and cls3 can be used in an alternate condition.

For templates, the existing variable yadm.class/YADM_CLASS is set to
the last class (i.e. cls3) to remain compatible with how it works
today and with what the following command gives:
$ yadm config local.class

For the default template processor there is no explicit yadm.classes
variable. Instead a yadm.class condition will check against all
classes.

For the other processors, a new template variable YADM_CLASSES will be
set to all classes separated by newline. For jinja2 templates a class
can be checked with: {%- if "cls" in YADM_CLASSES.split("\n") %}

For esh templates the logic is a bit more complex, but it is possible
to do.

Fixes #185.
2021-12-27 21:14:09 +01:00

82 lines
2.1 KiB
Python

"""Unit tests: set_local_alt_values"""
import pytest
import utils
@pytest.mark.parametrize(
'override', [
False,
'class',
'os',
'hostname',
'user',
],
ids=[
'no-override',
'override-class',
'override-os',
'override-hostname',
'override-user',
]
)
@pytest.mark.usefixtures('ds1_copy')
def test_set_local_alt_values(
runner, yadm, paths, tst_sys, tst_host, tst_user, override):
"""Use issue_legacy_path_warning"""
script = f"""
YADM_TEST=1 source {yadm} &&
set_operating_system &&
YADM_DIR={paths.yadm} YADM_DATA={paths.data} configure_paths &&
set_local_alt_values
echo "class='$local_class'"
echo "os='$local_system'"
echo "host='$local_host'"
echo "user='$local_user'"
"""
if override == 'class':
utils.set_local(paths, override, 'first')
utils.set_local(paths, override, 'override', add=True)
elif override:
utils.set_local(paths, override, 'override')
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
if override == 'class':
assert "class='override'" in run.out
else:
assert "class=''" in run.out
if override == 'os':
assert "os='override'" in run.out
else:
assert f"os='{tst_sys}'" in run.out
if override == 'hostname':
assert "host='override'" in run.out
else:
assert f"host='{tst_host}'" in run.out
if override == 'user':
assert "user='override'" in run.out
else:
assert f"user='{tst_user}'" in run.out
def test_distro(runner, yadm):
"""Assert that local_distro is set"""
script = f"""
YADM_TEST=1 source {yadm}
function config() {{ echo "$1"; }}
function query_distro() {{ echo "testdistro"; }}
set_local_alt_values
echo "distro='$local_distro'"
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert run.out.strip() == "distro='testdistro'"