2019-10-01 09:12:18 -04:00
|
|
|
"""Unit tests: score_file"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
CONDITION = {
|
2023-07-10 15:43:17 -04:00
|
|
|
"default": {
|
|
|
|
"labels": ["default"],
|
|
|
|
"modifier": 0,
|
|
|
|
},
|
|
|
|
"arch": {
|
|
|
|
"labels": ["a", "arch"],
|
|
|
|
"modifier": 1,
|
|
|
|
},
|
|
|
|
"system": {
|
|
|
|
"labels": ["o", "os"],
|
|
|
|
"modifier": 2,
|
|
|
|
},
|
|
|
|
"distro": {
|
|
|
|
"labels": ["d", "distro"],
|
|
|
|
"modifier": 4,
|
|
|
|
},
|
|
|
|
"distro_family": {
|
|
|
|
"labels": ["f", "distro_family"],
|
|
|
|
"modifier": 8,
|
|
|
|
},
|
|
|
|
"class": {
|
|
|
|
"labels": ["c", "class"],
|
|
|
|
"modifier": 16,
|
|
|
|
},
|
|
|
|
"hostname": {
|
|
|
|
"labels": ["h", "hostname"],
|
|
|
|
"modifier": 32,
|
|
|
|
},
|
|
|
|
"user": {
|
|
|
|
"labels": ["u", "user"],
|
|
|
|
"modifier": 64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
TEMPLATE_LABELS = ["t", "template", "yadm"]
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
def calculate_score(filename):
|
|
|
|
"""Calculate the expected score"""
|
|
|
|
# pylint: disable=too-many-branches
|
|
|
|
score = 0
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
_, conditions = filename.split("##", 1)
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
for condition in conditions.split(","):
|
2019-10-01 09:12:18 -04:00
|
|
|
label = condition
|
|
|
|
value = None
|
2023-07-10 15:43:17 -04:00
|
|
|
if "." in condition:
|
|
|
|
label, value = condition.split(".", 1)
|
|
|
|
if label in CONDITION["default"]["labels"]:
|
2019-10-01 09:12:18 -04:00
|
|
|
score += 1000
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in CONDITION["arch"]["labels"]:
|
|
|
|
if value == "testarch":
|
|
|
|
score += 1000 + CONDITION["arch"]["modifier"]
|
2020-02-21 20:10:39 -05:00
|
|
|
else:
|
|
|
|
score = 0
|
|
|
|
break
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in CONDITION["system"]["labels"]:
|
|
|
|
if value == "testsystem":
|
|
|
|
score += 1000 + CONDITION["system"]["modifier"]
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2019-10-06 12:04:21 -04:00
|
|
|
score = 0
|
|
|
|
break
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in CONDITION["distro"]["labels"]:
|
|
|
|
if value == "testdistro":
|
|
|
|
score += 1000 + CONDITION["distro"]["modifier"]
|
2019-10-06 12:04:21 -04:00
|
|
|
else:
|
|
|
|
score = 0
|
|
|
|
break
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in CONDITION["class"]["labels"]:
|
|
|
|
if value == "testclass":
|
|
|
|
score += 1000 + CONDITION["class"]["modifier"]
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2019-10-06 12:04:21 -04:00
|
|
|
score = 0
|
|
|
|
break
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in CONDITION["hostname"]["labels"]:
|
|
|
|
if value == "testhost":
|
|
|
|
score += 1000 + CONDITION["hostname"]["modifier"]
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2019-10-06 12:04:21 -04:00
|
|
|
score = 0
|
|
|
|
break
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in CONDITION["user"]["labels"]:
|
|
|
|
if value == "testuser":
|
|
|
|
score += 1000 + CONDITION["user"]["modifier"]
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2019-10-06 12:04:21 -04:00
|
|
|
score = 0
|
|
|
|
break
|
2019-10-01 09:12:18 -04:00
|
|
|
elif label in TEMPLATE_LABELS:
|
2019-10-06 12:04:21 -04:00
|
|
|
score = 0
|
|
|
|
break
|
2019-10-01 09:12:18 -04:00
|
|
|
return score
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("default", ["default", None], ids=["default", "no-default"])
|
|
|
|
@pytest.mark.parametrize("arch", ["arch", None], ids=["arch", "no-arch"])
|
|
|
|
@pytest.mark.parametrize("system", ["system", None], ids=["system", "no-system"])
|
|
|
|
@pytest.mark.parametrize("distro", ["distro", None], ids=["distro", "no-distro"])
|
|
|
|
@pytest.mark.parametrize("cla", ["class", None], ids=["class", "no-class"])
|
|
|
|
@pytest.mark.parametrize("host", ["hostname", None], ids=["hostname", "no-host"])
|
|
|
|
@pytest.mark.parametrize("user", ["user", None], ids=["user", "no-user"])
|
|
|
|
def test_score_values(runner, yadm, default, arch, system, distro, cla, host, user):
|
2019-10-01 09:12:18 -04:00
|
|
|
"""Test score results"""
|
|
|
|
# pylint: disable=too-many-branches
|
2023-07-10 15:43:17 -04:00
|
|
|
local_class = "testclass"
|
|
|
|
local_arch = "testarch"
|
|
|
|
local_system = "testsystem"
|
|
|
|
local_distro = "testdistro"
|
|
|
|
local_host = "testhost"
|
|
|
|
local_user = "testuser"
|
|
|
|
filenames = {"filename##": 0}
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
if default:
|
|
|
|
for filename in list(filenames):
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[default]["labels"]:
|
2019-10-01 09:12:18 -04:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
2019-10-01 09:12:18 -04:00
|
|
|
newfile += label
|
|
|
|
filenames[newfile] = calculate_score(newfile)
|
2020-02-21 20:10:39 -05:00
|
|
|
if arch:
|
|
|
|
for filename in list(filenames):
|
|
|
|
for match in [True, False]:
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[arch]["labels"]:
|
2020-02-21 20:10:39 -05:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, local_arch if match else "badarch"])
|
2020-02-21 20:10:39 -05:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
2019-10-01 09:12:18 -04:00
|
|
|
if system:
|
|
|
|
for filename in list(filenames):
|
|
|
|
for match in [True, False]:
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[system]["labels"]:
|
2019-10-01 09:12:18 -04:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, local_system if match else "badsys"])
|
2019-10-01 09:12:18 -04:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
2019-10-06 12:04:21 -04:00
|
|
|
if distro:
|
|
|
|
for filename in list(filenames):
|
|
|
|
for match in [True, False]:
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[distro]["labels"]:
|
2019-10-06 12:04:21 -04:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, local_distro if match else "baddistro"])
|
2019-10-06 12:04:21 -04:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
2019-10-01 09:12:18 -04:00
|
|
|
if cla:
|
|
|
|
for filename in list(filenames):
|
|
|
|
for match in [True, False]:
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[cla]["labels"]:
|
2019-10-01 09:12:18 -04:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, local_class if match else "badclass"])
|
2019-10-01 09:12:18 -04:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
|
|
|
if host:
|
|
|
|
for filename in list(filenames):
|
|
|
|
for match in [True, False]:
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[host]["labels"]:
|
2019-10-01 09:12:18 -04:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, local_host if match else "badhost"])
|
2019-10-01 09:12:18 -04:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
|
|
|
if user:
|
|
|
|
for filename in list(filenames):
|
|
|
|
for match in [True, False]:
|
2023-07-10 15:43:17 -04:00
|
|
|
for label in CONDITION[user]["labels"]:
|
2019-10-01 09:12:18 -04:00
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, local_user if match else "baduser"])
|
2019-10-01 09:12:18 -04:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
score=0
|
|
|
|
local_class={local_class}
|
2021-01-10 16:38:42 -05:00
|
|
|
local_classes=({local_class})
|
2020-02-21 20:10:39 -05:00
|
|
|
local_arch={local_arch}
|
2019-10-01 09:12:18 -04:00
|
|
|
local_system={local_system}
|
2019-10-06 12:04:21 -04:00
|
|
|
local_distro={local_distro}
|
2019-10-01 09:12:18 -04:00
|
|
|
local_host={local_host}
|
|
|
|
local_user={local_user}
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
expected = ""
|
2023-07-09 12:13:13 -04:00
|
|
|
for filename, score in filenames.items():
|
2019-10-01 09:12:18 -04:00
|
|
|
script += f"""
|
|
|
|
score_file "{filename}"
|
|
|
|
echo "{filename}"
|
|
|
|
echo "$score"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
expected += filename + "\n"
|
|
|
|
expected += str(score) + "\n"
|
|
|
|
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 == ""
|
2020-12-22 11:51:23 -05:00
|
|
|
assert run.out == expected
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("ext", [None, "e", "extension"])
|
2020-12-22 11:51:23 -05:00
|
|
|
def test_extensions(runner, yadm, ext):
|
|
|
|
"""Verify extensions do not effect scores"""
|
2023-07-10 15:43:17 -04:00
|
|
|
local_user = "testuser"
|
|
|
|
filename = f"filename##u.{local_user}"
|
2020-12-22 11:51:23 -05:00
|
|
|
if ext:
|
2023-07-10 15:43:17 -04:00
|
|
|
filename += f",{ext}.xyz"
|
|
|
|
expected = ""
|
2020-12-22 11:51:23 -05:00
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
score=0
|
|
|
|
local_user={local_user}
|
|
|
|
score_file "{filename}"
|
|
|
|
echo "$score"
|
|
|
|
"""
|
|
|
|
expected = f'{1000 + CONDITION["user"]["modifier"]}\n'
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2020-12-22 11:51:23 -05:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.out == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_score_values_templates(runner, yadm):
|
|
|
|
"""Test score results"""
|
2023-07-10 15:43:17 -04:00
|
|
|
local_class = "testclass"
|
|
|
|
local_arch = "arch"
|
|
|
|
local_system = "testsystem"
|
|
|
|
local_distro = "testdistro"
|
|
|
|
local_host = "testhost"
|
|
|
|
local_user = "testuser"
|
|
|
|
filenames = {"filename##": 0}
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
for filename in list(filenames):
|
|
|
|
for label in TEMPLATE_LABELS:
|
|
|
|
newfile = filename
|
2023-07-10 15:43:17 -04:00
|
|
|
if not newfile.endswith("##"):
|
|
|
|
newfile += ","
|
|
|
|
newfile += ".".join([label, "testtemplate"])
|
2019-10-01 09:12:18 -04:00
|
|
|
filenames[newfile] = calculate_score(newfile)
|
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
score=0
|
|
|
|
local_class={local_class}
|
2020-02-21 20:10:39 -05:00
|
|
|
local_arch={local_arch}
|
2019-10-01 09:12:18 -04:00
|
|
|
local_system={local_system}
|
2019-10-06 12:04:21 -04:00
|
|
|
local_distro={local_distro}
|
2019-10-01 09:12:18 -04:00
|
|
|
local_host={local_host}
|
|
|
|
local_user={local_user}
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
expected = ""
|
2023-07-09 12:13:13 -04:00
|
|
|
for filename, score in filenames.items():
|
2019-10-01 09:12:18 -04:00
|
|
|
script += f"""
|
|
|
|
score_file "{filename}"
|
|
|
|
echo "{filename}"
|
|
|
|
echo "$score"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
expected += filename + "\n"
|
|
|
|
expected += str(score) + "\n"
|
|
|
|
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 run.out == expected
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("cmd_generated", [True, False], ids=["supported-template", "unsupported-template"])
|
2019-10-01 09:12:18 -04:00
|
|
|
def test_template_recording(runner, yadm, cmd_generated):
|
|
|
|
"""Template should be recorded if choose_template_cmd outputs a command"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
mock = "function choose_template_cmd() { return; }"
|
|
|
|
expected = ""
|
2019-10-01 09:12:18 -04:00
|
|
|
if cmd_generated:
|
|
|
|
mock = 'function choose_template_cmd() { echo "test_cmd"; }'
|
2023-07-10 15:43:17 -04:00
|
|
|
expected = "template recorded"
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
function record_template() {{ echo "template recorded"; }}
|
|
|
|
{mock}
|
|
|
|
score_file "testfile##template.kind"
|
|
|
|
"""
|
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 run.out.rstrip() == expected
|
2022-09-13 13:38:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_underscores_in_distro_and_family(runner, yadm):
|
|
|
|
"""Test replacing spaces in distro / distro_family with underscores"""
|
2023-07-10 15:43:17 -04:00
|
|
|
local_distro = "test distro"
|
|
|
|
local_distro_family = "test family"
|
2022-09-13 13:38:18 -04:00
|
|
|
filenames = {
|
2023-07-10 15:43:17 -04:00
|
|
|
"filename##distro.test distro": 1004,
|
|
|
|
"filename##distro.test-distro": 0,
|
|
|
|
"filename##distro.test_distro": 1004,
|
|
|
|
"filename##distro_family.test family": 1008,
|
|
|
|
"filename##distro_family.test-family": 0,
|
|
|
|
"filename##distro_family.test_family": 1008,
|
|
|
|
}
|
2022-09-13 13:38:18 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
score=0
|
|
|
|
local_distro="{local_distro}"
|
|
|
|
local_distro_family="{local_distro_family}"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
expected = ""
|
2023-07-09 12:13:13 -04:00
|
|
|
for filename, score in filenames.items():
|
2022-09-13 13:38:18 -04:00
|
|
|
script += f"""
|
|
|
|
score_file "{filename}"
|
|
|
|
echo "{filename}"
|
|
|
|
echo "$score"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
expected += filename + "\n"
|
|
|
|
expected += str(score) + "\n"
|
|
|
|
run = runner(command=["bash"], inp=script)
|
2022-09-13 13:38:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2022-09-13 13:38:18 -04:00
|
|
|
assert run.out == expected
|