Skip to content

Commit 4cf6197

Browse files
committed
refactor: one hypervisor preparation pipeline
The integration test carried two exclusive branches: the image_tag path prepared the HyperVisor pair inline, the sysext path consumed a prepared directory, and the guest configuration was written twice. The pipeline now generates either variant into one directory, and setup consumes only that, so the guest configuration has one home and a missing piece fails in one place. The test mode input disappears with the last caller that needed it. Signed-off-by: Fabian Wiesel <fabian.wiesel@sap.com>
1 parent 80964d0 commit 4cf6197

2 files changed

Lines changed: 210 additions & 16 deletions

File tree

‎.github/scripts/prepare-sysext-hv.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ EOF
334334
cat >"$out/metadata.env" <<EOF
335335
HV_DIR=$out
336336
HV_SSH_KEY=$key
337+
HV_NETWORK=${SCI_HV_NETWORK:-default}
337338
HV_RAWS_DIR=$out/extensions
338339
HV_KERNEL=$out/vmlinuz
339340
EOF

‎extensions/tests/test_workflows.py‎

Lines changed: 209 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
SYSEXT = REPO_ROOT / ".github" / "workflows" / "sysext.yml"
1212
TEST = REPO_ROOT / ".github" / "workflows" / "test.yml"
1313
SETUP = REPO_ROOT / ".github" / "actions" / "test" / "integration" / "setup" / "action.yml"
14-
BOOT_STEP = "Boot the sysext HyperVisors and activate the extensions"
14+
BUILD = REPO_ROOT / ".github" / "actions" / "test" / "integration" / "build" / "action.yml"
15+
SETUP_STEP = "Boot the HyperVisors and configure the guests"
1516
RAWS_STAGING = ".build/delivery/staging-raws"
1617
RAWS_ARTIFACT = "sysext-raws"
1718
VEHICLE = "sci-delivery.sh"
@@ -66,22 +67,37 @@ def remote_uses(data):
6667

6768

6869
def test_setup_grants_qemu_traversal_through_the_runner_home():
69-
lines = run_text(step_named(load(SETUP)["runs"], BOOT_STEP)).splitlines()
70+
lines = run_text(step_named(load(SETUP)["runs"], SETUP_STEP)).splitlines()
7071
starts = [i for i, l in enumerate(lines) if "virsh start" in l]
7172
chmods = [i for i, l in enumerate(lines) if "chmod o+x" in l and "$HOME" in l]
7273
assert starts and chmods and chmods[0] < starts[0], (
73-
"the sysext setup step must 'sudo chmod o+x $HOME' before the first 'virsh start'")
74+
"the setup step must 'sudo chmod o+x $HOME' before the first 'virsh start'")
7475

7576

7677
def guest_script():
77-
lines = run_text(step_named(load(SETUP)["runs"], BOOT_STEP)).splitlines()
78-
a = next((i for i, l in enumerate(lines) if l.endswith("bash -s <<'EOF'")), None)
79-
assert a is not None, "the sysext step no longer pipes the guest script via \"bash -s <<'EOF'\""
78+
lines = run_text(step_named(load(SETUP)["runs"], SETUP_STEP)).splitlines()
79+
a = next((i for i, l in enumerate(lines)
80+
if l.endswith("<<'EOF'") and "bash -s" in l), None)
81+
assert a is not None, "the setup step no longer pipes the guest script via \"bash -s <<'EOF'\""
8082
b = next((i for i in range(a + 1, len(lines)) if lines[i] == "EOF"), None)
81-
assert b is not None, "the closing 'EOF' of the sysext heredoc is missing"
83+
assert b is not None, "the closing 'EOF' of the guest heredoc is missing"
8284
return "\n".join(lines[a + 1:b])
8385

8486

87+
def guest_env(tmp_path, stubs):
88+
stub_dir = tmp_path / "stubs"
89+
stub_dir.mkdir()
90+
for name, text in stubs.items():
91+
(stub_dir / name).write_text(text, encoding="utf-8")
92+
(stub_dir / name).chmod(0o755)
93+
return {"PATH": f"{stub_dir}:{os.environ.get('PATH', '/usr/bin:/bin')}"}
94+
95+
96+
def run_guest(script, sb, env):
97+
return subprocess.run(["bash", "-s"], input=script, text=True, capture_output=True,
98+
cwd=str(sb), env=env, timeout=60)
99+
100+
85101
@pytest.mark.parametrize("dropped", [[], ["sci-21-network"]])
86102
def test_sysext_setup_guest_script_guards_the_exact_extension_set(tmp_path, dropped):
87103
"""The merged extension set must equal the manifest's exact set, or the
@@ -103,8 +119,6 @@ def test_sysext_setup_guest_script_guards_the_exact_extension_set(tmp_path, drop
103119
(share / f"{name}.raw").write_bytes(b"signed extension raw")
104120
for p in ("/root/", "/opt/", "/var/log/libvirt/", "/var/lib/libvirt/", "/run/extensions"):
105121
script = script.replace(p, str(sb) + p)
106-
stub_dir = tmp_path / "stubs"
107-
stub_dir.mkdir()
108122
merged = [n for n in names if n not in dropped]
109123
status = '[{"hierarchy":"system","extensions":[' + ",".join(f'"{n}"' for n in merged) + "]}]"
110124
stubs = {
@@ -115,12 +129,9 @@ def test_sysext_setup_guest_script_guards_the_exact_extension_set(tmp_path, drop
115129
"chmod": "#!/bin/sh\nexit 0\n",
116130
"virsh": f"#!/bin/sh\nprintf '%s\\n' \"$*\" >> {sb / 'virsh.log'}\nexit 0\n",
117131
}
118-
for name, text in stubs.items():
119-
(stub_dir / name).write_text(text, encoding="utf-8")
120-
(stub_dir / name).chmod(0o755)
121-
env = {"PATH": f"{stub_dir}:{os.environ.get('PATH', '/usr/bin:/bin')}"}
122-
res = subprocess.run(["bash", "-s"], input=script, text=True, capture_output=True,
123-
cwd=str(sb), env=env, timeout=60)
132+
env = guest_env(tmp_path, stubs)
133+
env["HV_EXPECT_EXTENSIONS"] = "1"
134+
res = run_guest(script, sb, env)
124135
log = (sb / "virsh.log").read_text(encoding="utf-8") if (sb / "virsh.log").is_file() else ""
125136
post = ("pool-define-as default", "pool-start default", "net-define", "net-start default",
126137
"net-autostart default")
@@ -138,6 +149,188 @@ def test_sysext_setup_guest_script_guards_the_exact_extension_set(tmp_path, drop
138149
assert (sb / "root" / ".ssh" / "config").is_file()
139150

140151

152+
def test_setup_ssh_invocation_passes_the_extension_expectation():
153+
text = run_text(step_named(load(SETUP)["runs"], SETUP_STEP))
154+
ssh_lines = [l for l in text.splitlines() if "ssh " in l and "bash -s" in l]
155+
assert len(ssh_lines) == 1, f"exactly one guest SSH invocation expected, found {ssh_lines}"
156+
assert "HV_EXPECT_EXTENSIONS=${HV_RAWS_DIR:+1}" in ssh_lines[0], (
157+
"a dropped extension wiring would silently skip the sysext guard: "
158+
f"{ssh_lines[0]!r}")
159+
160+
161+
def test_setup_guest_script_runs_common_configuration_without_extension_checks(tmp_path):
162+
"""Without HV_EXPECT_EXTENSIONS the common pool, network, and SSH setup
163+
still runs; the extension checks are not a hidden prerequisite."""
164+
script = guest_script()
165+
sb = tmp_path / "sb"
166+
(sb / "root" / ".ssh").mkdir(parents=True)
167+
for p in ("/root/", "/opt/", "/var/log/libvirt/", "/var/lib/libvirt/", "/run/extensions"):
168+
script = script.replace(p, str(sb) + p)
169+
marker = sb / "extension-check-ran"
170+
stubs = {
171+
"systemctl": "#!/bin/sh\nexit 0\n",
172+
"chmod": "#!/bin/sh\nexit 0\n",
173+
"virsh": f"#!/bin/sh\nprintf '%s\\n' \"$*\" >> {sb / 'virsh.log'}\nexit 0\n",
174+
"findmnt": f"#!/bin/sh\n: > {marker}\nexit 1\n",
175+
"systemd-sysext": f"#!/bin/sh\n: > {marker}\nexit 1\n",
176+
}
177+
env = guest_env(tmp_path, stubs)
178+
assert "HV_EXPECT_EXTENSIONS" not in env
179+
res = run_guest(script, sb, env)
180+
log = (sb / "virsh.log").read_text(encoding="utf-8") if (sb / "virsh.log").is_file() else ""
181+
assert res.returncode == 0, (
182+
f"the common configuration failed without HV_EXPECT_EXTENSIONS: {res.stderr.strip()[:200]}")
183+
post = ("pool-define-as default", "pool-start default", "net-define", "net-start default",
184+
"net-autostart default")
185+
skipped = [c for c in post if c not in log]
186+
assert not skipped, f"post-setup commands were skipped: {skipped}"
187+
assert (sb / "root" / ".ssh" / "config").is_file(), (
188+
"the SSH config was not written without HV_EXPECT_EXTENSIONS")
189+
assert not marker.exists(), "the extension checks ran although HV_EXPECT_EXTENSIONS is unset"
190+
191+
192+
def test_setup_consumes_only_the_prepared_directory():
193+
inputs = load(SETUP).get("inputs") or {}
194+
assert "mode" not in inputs, "setup must not branch on a test mode"
195+
assert set(inputs) == {"hv_dir"}, f"setup must take one input, found {sorted(inputs)}"
196+
assert inputs["hv_dir"].get("required") is True, "hv_dir must be required"
197+
assert inputs["hv_dir"].get("default") == "", "hv_dir must default to empty and be validated"
198+
199+
200+
def test_build_generates_the_hypervisor_directory_and_test_passes_it():
201+
text = "\n".join(run_text(s) for s in steps_of(load(BUILD)["runs"]))
202+
assert "metadata.env" in text, "the build action must write the hypervisor metadata"
203+
assert re.search(r"^HV_DIR=", text, re.M), "metadata.env must carry HV_DIR"
204+
assert re.search(r"^HV_SSH_KEY=/opt/ssh_host_ed25519_key$", text, re.M), (
205+
"metadata.env must point at the shared test key")
206+
assert re.search(r"^HV_NETWORK=default$", text, re.M), "metadata.env must carry HV_NETWORK"
207+
assert "HV_RAWS_DIR" not in text, "the image path has no virtiofs raws directory"
208+
setup = step_named(job(load(TEST), "test"), "Setup")
209+
assert with_values(setup) == {"hv_dir": "${{ env.HV_DIR }}"}, (
210+
f"the setup step must pass the generated directory and nothing else: {with_values(setup)}")
211+
212+
213+
def test_build_renders_both_hypervisor_variants_and_installs_their_files(tmp_path):
214+
"""The image-tag variant must fill one prepared directory with both domain
215+
XML files and install both disks and both ignition files. The literal
216+
placeholders must not survive, and each variant gets its own name, MAC,
217+
disk, and ignition target.
218+
"""
219+
text = run_text(step_named(load(BUILD)["runs"], "Prepare the HyperVisor directory"))
220+
calls = dict(re.findall(r"^\s*render_hv\s+(\S+)\s+(\S+)\s*$", text, re.M))
221+
assert calls == {"HV1": "52:54:00:12:34:56", "HV2": "52:54:00:65:43:21"}, calls
222+
assert re.search(r's\|HV_NAME_GOES_HERE\|[^|]*\|g', text), (
223+
"the domain XML placeholder for the name must be substituted")
224+
assert re.search(r's\|MAC_GOES_HERE\|[^|]*\|g', text), (
225+
"the domain XML placeholder for the MAC must be substituted")
226+
assert "$HV_DIR/$name.xml" in text, "the rendered domain XML must land in HV_DIR"
227+
targets = re.findall(r"/var/lib/libvirt/images/(HV[12]\.(?:qcow2|ign))", text)
228+
assert sorted(targets) == ["HV1.ign", "HV1.qcow2", "HV2.ign", "HV2.qcow2"], targets
229+
230+
sb = tmp_path / "sb"
231+
(sb / "opt").mkdir(parents=True)
232+
(sb / "var" / "lib" / "libvirt" / "images").mkdir(parents=True)
233+
tag = "test-image"
234+
source = {
235+
f"{tag}.qcow2": b"qcow2 disk",
236+
f"{tag}-hv1.ign": b"ignition one",
237+
f"{tag}-hv2.ign": b"ignition two",
238+
}
239+
for name, payload in source.items():
240+
(sb / "opt" / name).write_bytes(payload)
241+
(sb / "opt" / "ssh_host_ed25519_key").write_bytes(b"test key")
242+
metadata = sb / "github_env"
243+
metadata.write_text("", encoding="utf-8")
244+
245+
script = text.replace("/var/lib/libvirt/images",
246+
str(sb / "var" / "lib" / "libvirt" / "images"))
247+
script = script.replace("/opt/", str(sb / "opt") + "/")
248+
env = guest_env(tmp_path, {"sudo": '#!/bin/sh\nexec "$@"\n'})
249+
env.update({"TAG": tag, "GITHUB_ENV": str(metadata)})
250+
res = subprocess.run(["bash", "-s"], input=script, text=True, capture_output=True,
251+
cwd=str(REPO_ROOT), env=env, timeout=60)
252+
assert res.returncode == 0, (res.returncode, res.stderr[-500:])
253+
254+
hv = sb / "opt" / "hv"
255+
for name, mac in (("HV1", "52:54:00:12:34:56"), ("HV2", "52:54:00:65:43:21")):
256+
xml = (hv / f"{name}.xml").read_text(encoding="utf-8")
257+
assert "HV_NAME_GOES_HERE" not in xml and "MAC_GOES_HERE" not in xml
258+
assert f"<name>{name}</name>" in xml
259+
assert f"mac address='{mac}'" in xml
260+
for suffix in ("qcow2", "ign"):
261+
assert f"/var/lib/libvirt/images/{name}.{suffix}" in xml
262+
images = sb / "var" / "lib" / "libvirt" / "images"
263+
assert (images / "HV1.qcow2").read_bytes() == source[f"{tag}.qcow2"]
264+
assert (images / "HV2.qcow2").read_bytes() == source[f"{tag}.qcow2"]
265+
assert (images / "HV1.ign").read_bytes() == source[f"{tag}-hv1.ign"]
266+
assert (images / "HV2.ign").read_bytes() == source[f"{tag}-hv2.ign"]
267+
assert "HV_DIR=" in metadata.read_text(encoding="utf-8")
268+
269+
270+
def network_decision_block():
271+
lines = run_text(step_named(load(SETUP)["runs"], SETUP_STEP)).splitlines()
272+
start = next((i for i, l in enumerate(lines) if l.lstrip().startswith("NETWORK=")), None)
273+
assert start is not None, "the setup step no longer decides the libvirt network"
274+
indent = len(lines[start]) - len(lines[start].lstrip())
275+
end = next((i for i in range(start + 1, len(lines))
276+
if lines[i].strip() == "fi"
277+
and len(lines[i]) - len(lines[i].lstrip()) == indent), None)
278+
assert end is not None, "the network decision block has no closing fi"
279+
return "\n".join(l[indent:] for l in lines[start:end + 1])
280+
281+
282+
@pytest.mark.parametrize("network,netinfo,rc,required,forbidden", [
283+
(None, "missing", 0, ("net-define", "net-start default"), ()),
284+
("hv-test", "missing", 1, (), ("net-define", "net-start")),
285+
("default", "inactive", 0, ("net-start default",), ("net-define",)),
286+
("default", "active", 0, (), ("net-define", "net-start")),
287+
])
288+
def test_setup_network_decision_uses_the_active_state(
289+
tmp_path, network, netinfo, rc, required, forbidden):
290+
"""Four network states decide differently: an undefined default is defined
291+
and started, an undefined non-default fails, an inactive network is only
292+
started, and an active one is left alone. The system daemon is stubbed.
293+
"""
294+
sb = tmp_path / "sb"
295+
sb.mkdir()
296+
log = sb / "virsh.log"
297+
stubs = {
298+
"sudo": '#!/bin/sh\nexec "$@"\n',
299+
"virsh": (
300+
"#!/bin/sh\n"
301+
f"printf '%s\\n' \"$*\" >> {log}\n"
302+
"if [ \"$1\" = net-info ]; then\n"
303+
" case \"$VIRSH_NETINFO\" in\n"
304+
" missing) exit 1 ;;\n"
305+
" inactive) printf 'Name: %s\\nActive: no\\n' \"$2\" ;;\n"
306+
" active) printf 'Name: %s\\nActive: yes\\n' \"$2\" ;;\n"
307+
" esac\n"
308+
"fi\n"
309+
"exit 0\n"
310+
),
311+
}
312+
env = guest_env(tmp_path, stubs)
313+
env["VIRSH_NETINFO"] = netinfo
314+
if network is not None:
315+
env["HV_NETWORK"] = network
316+
res = subprocess.run(["bash", "-c", network_decision_block()], text=True,
317+
capture_output=True, cwd=str(sb), env=env, timeout=60)
318+
ran = log.read_text(encoding="utf-8") if log.is_file() else ""
319+
assert res.returncode == rc, (res.returncode, res.stdout, res.stderr, ran)
320+
for wanted in required:
321+
assert wanted in ran, f"expected {wanted!r} in virsh calls: {ran!r}"
322+
for unwanted in forbidden:
323+
assert unwanted not in ran, f"unexpected {unwanted!r} in virsh calls: {ran!r}"
324+
if rc != 0:
325+
assert "does not exist" in res.stderr, res.stderr
326+
327+
328+
def test_prepare_writes_the_network_into_the_metadata():
329+
text = PREPARE.read_text(encoding="utf-8")
330+
assert re.search(r"^HV_NETWORK=\$\{SCI_HV_NETWORK:-default\}$", text, re.M), (
331+
"prepare-sysext-hv.sh must write HV_NETWORK into metadata.env")
332+
333+
141334
def test_quick_suite_triggers_and_pr_gates():
142335
data = load(SYSEXT)
143336
on = data["on"] if "on" in data else data[True]
@@ -373,5 +566,5 @@ def test_consumer_restores_cert_and_prepares_toolchain_and_image_in_order():
373566
assert not bad, "\n".join(bad)
374567
assert (build_data["inputs"]["image_tag"] or {}).get("required") is True, (
375568
"image_tag must be a required build input")
376-
assert "$HV_SSH_KEY" in run_text(step_named(load(SETUP)["runs"], BOOT_STEP)), (
569+
assert "$HV_SSH_KEY" in run_text(step_named(load(SETUP)["runs"], SETUP_STEP)), (
377570
"setup must use the key from prepare's metadata, not a helper-generated one")

0 commit comments

Comments
 (0)