test(molecule): derive the order test from the duration list
Part of #432. The start-order test now reads its scenarios and their seconds from the run script's list, so a refreshed list does not break it; the empty-lanes test only asserts the value is accepted, and the script's header and duration-list comments are rewrapped and say the list is keyed by scenario directory. Assisted-by: Claude Code
This commit is contained in:
1 parent
ef840c8952
commit
e34a0eb2e0
2 files changed
+33
-23
No files matched your search
@@ -286,48 +286,42 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_the_longest_scenarios_start_first(self):
|
||||
# an unlisted scenario counts as longest; the groups keep the selection order
|
||||
listed = {
|
||||
"roles/sshd/molecule/default": "sshd-instance",
|
||||
"roles/ufw/molecule/default": "ufw-instance",
|
||||
}
|
||||
for scenario, name in listed.items():
|
||||
seconds = listed_seconds()
|
||||
roles = [d for d in seconds if d.startswith("roles/")]
|
||||
shortest, longest = min(roles, key=seconds.get), max(roles, key=seconds.get)
|
||||
for scenario in (shortest, longest):
|
||||
d = self.root / scenario
|
||||
d.mkdir(parents=True)
|
||||
platforms = [{"name": f"{RUN_ID}-{name}", "image": "${MOLECULE_IMAGE}"}]
|
||||
name = f"{RUN_ID}-{scenario.replace('/', '-')}"
|
||||
platforms = [{"name": name, "image": "${MOLECULE_IMAGE}"}]
|
||||
(d / "molecule.yml").write_text(yaml.safe_dump({"platforms": platforms}))
|
||||
order = [
|
||||
"extensions/molecule/image-smoke",
|
||||
"roles/sshd/molecule/default",
|
||||
"roles/alpha/molecule/default",
|
||||
"roles/ufw/molecule/default",
|
||||
]
|
||||
smoke = "extensions/molecule/image-smoke"
|
||||
order = [smoke, shortest, "roles/alpha/molecule/default", longest]
|
||||
listed = sorted((smoke, shortest, longest), key=seconds.get, reverse=True)
|
||||
proc = self.run_script(order, MOLECULE_LANES="1")
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
spans = self.events()
|
||||
self.assertEqual(
|
||||
sorted(spans, key=lambda label: spans[label][0]),
|
||||
["alpha/default", "ufw/default", "sshd/default", "image-smoke"],
|
||||
[label(d) for d in ["roles/alpha/molecule/default", *listed]],
|
||||
)
|
||||
groups = re.findall(
|
||||
r"^::group::molecule test: (\S+) ", proc.stdout, re.MULTILINE
|
||||
)
|
||||
self.assertEqual(
|
||||
groups, ["image-smoke", "sshd/default", "alpha/default", "ufw/default"]
|
||||
)
|
||||
self.assertEqual(groups, [label(d) for d in order])
|
||||
|
||||
def test_the_duration_list_names_existing_scenarios(self):
|
||||
# a renamed scenario would otherwise drop out of the list unnoticed
|
||||
listed = re.findall(r"^ \[([^]]+)\]=\d+$", SCRIPT.read_text(), re.MULTILINE)
|
||||
listed = listed_seconds()
|
||||
self.assertTrue(listed)
|
||||
for scenario in listed:
|
||||
self.assertTrue((REPO / scenario / "molecule.yml").is_file(), scenario)
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_empty_lanes_mean_the_default(self):
|
||||
def test_empty_lanes_are_accepted(self):
|
||||
# the workflow passes an empty value outside a manual dispatch
|
||||
proc = self.run_script(list(PLATFORMS), MOLECULE_LANES="")
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
self.assertIn("Scenarios run: 4", proc.stdout)
|
||||
|
||||
def test_lanes_must_be_a_positive_integer(self):
|
||||
for lanes in ("0", "-1", "two", "04"):
|
||||
@@ -395,6 +389,20 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
self.assertEqual(self.log.read_text(), "")
|
||||
|
||||
|
||||
def listed_seconds():
|
||||
"""{scenario directory: seconds} from the run script's duration list."""
|
||||
pairs = re.findall(r"^ \[([^]]+)\]=(\d+)$", SCRIPT.read_text(), re.MULTILINE)
|
||||
return {d: int(s) for d, s in pairs}
|
||||
|
||||
|
||||
def label(scenario):
|
||||
"""The run script's label of a scenario directory, as its log groups print it."""
|
||||
if scenario.startswith("extensions/"):
|
||||
return scenario.rsplit("/", 1)[1]
|
||||
parts = scenario.split("/")
|
||||
return f"{parts[1]}/{parts[3]}"
|
||||
|
||||
|
||||
def alive(pid):
|
||||
"""Whether pid is a running process, not gone or a zombie."""
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user