You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/engine/tests/test_fabric_first_deploy_en...

655 lines
29 KiB
Python

"""Exercise the rendered recovery helper from its real shell entry point."""
from __future__ import annotations
import base64
import json
import re
import shlex
import subprocess
import sys
from pathlib import Path
import pytest
from virsh_panel.fabric import (
FIREWALL_MODE_NATIVE,
_apply_script,
_ifupdown_fragment,
_nm_profile_template,
_nft_rules_text,
_sysctl_text,
parse_fabric_plan,
)
def _write(path: Path, content: str, mode: int = 0o600) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
path.chmod(mode)
_TOOLS = r'''import hashlib
import json
import os
import stat
import sys
from pathlib import Path
name = Path(sys.argv[0]).name
args = sys.argv[1:]
root = Path(os.environ["VC_ENTRY_ROOT"])
with (root / "calls").open("a", encoding="utf-8") as log:
log.write(json.dumps([name, *args]) + "\n")
if name == "stat":
if len(args) != 3 or args[0] != "-c":
raise SystemExit(2)
value = os.stat(args[2])
mode = format(stat.S_IMODE(value.st_mode), "o")
fields = {
"%u": "0", "%a": mode, "%u:%a": "0:" + mode,
"%u:%g:%a": "0:0:" + mode, "%s": str(value.st_size),
}
if args[1] not in fields:
raise SystemExit(2)
print(fields[args[1]])
elif name == "sha256sum":
for filename in args:
print(hashlib.sha256(Path(filename).read_bytes()).hexdigest(), filename)
elif name == "ip":
# Recovery fixtures never reached link creation. Transition fixtures
# model only the already-created, still-isolated WireGuard device; no
# production shell function or branch is replaced.
present = root / "link-present"
up = root / "link-up"
if args == ["link", "show", "dev", "wg-fabric0"]:
raise SystemExit(0 if present.exists() else 1)
elif args == ["-o", "link", "show", "dev", "wg-fabric0"]:
if not present.exists():
raise SystemExit(1)
flags = "POINTOPOINT,NOARP,UP,LOWER_UP" if up.exists() else "POINTOPOINT,NOARP"
print("12: wg-fabric0: <" + flags + "> mtu 1420")
elif args == ["link", "set", "dev", "wg-fabric0", "up"]:
if not present.exists():
raise SystemExit(1)
up.touch()
elif args == ["link", "set", "dev", "wg-fabric0", "down"]:
up.unlink(missing_ok=True)
elif args == ["link", "del", "dev", "wg-fabric0"]:
present.unlink(missing_ok=True)
up.unlink(missing_ok=True)
(root / "proc" / "ipv4" / "conf" / "wg-fabric0" /
"rp_filter").unlink(missing_ok=True)
else:
raise SystemExit(1)
elif name == "nft":
table_root = root / "tables"
if args == ["list", "tables"]:
for path in sorted(table_root.glob("*")):
family, table = path.name.split("--", 1)
print("table", family, table)
elif len(args) in (4, 5) and args[-4:-2] == ["list", "table"]:
path = table_root / (args[-2] + "--" + args[-1])
if not path.is_file():
raise SystemExit(1)
print(path.read_text(encoding="utf-8"), end="")
elif len(args) in (2, 3) and args[-2] == "-f":
batch = Path(args[-1]).read_text(encoding="utf-8")
# Recovery may delete only the completely proved interrupted Fabric
# table.
if batch != "delete table inet virt_cloud_wg_fabric\n":
raise SystemExit(2)
if "-c" not in args:
(table_root / "inet--virt_cloud_wg_fabric").unlink()
else:
raise SystemExit(2)
elif name == "systemctl":
if args[:2] == ["show", "-p"]:
if args[2] == "LoadState":
print("masked" if args[-1] == "firewalld" else
"loaded" if args[-1] == "nftables" else "not-found")
elif args[2] == "ActiveState":
print("inactive")
else:
raise SystemExit(2)
elif args[0] == "is-active" and args[1] in ("firewalld", "nftables"):
print("inactive" if args[1] == "firewalld" else "active")
elif args[0] == "is-enabled" and args[1] in ("firewalld", "nftables"):
print("masked" if args[1] == "firewalld" else "enabled")
elif args and args[0] in ("daemon-reload", "disable"):
pass
else:
raise SystemExit(2)
elif name == "nmcli":
profile = (root / "NetworkManager" / "system-connections" /
"virt-cloud-wg-fabric0.nmconnection")
identity = root / "nm-loaded-uuid"
uuid = identity.read_text().strip() if identity.is_file() else ""
if args == ["-t", "--escape", "no", "-g", "connection.uuid",
"connection", "show", "virt-cloud-wg-fabric0"]:
if uuid:
print(uuid)
elif args == ["-t", "--escape", "no", "-f", "NAME,UUID",
"connection", "show"] and profile.is_file():
print("virt-cloud-wg-fabric0:" + uuid)
elif args == ["-t", "--escape", "no", "-f", "UUID,FILENAME",
"connection", "show"] and profile.is_file():
shadow = (root / "run" / "NetworkManager" / "system-connections" /
"virt-cloud-wg-fabric0.nmconnection")
print(uuid + ":" + str(shadow if shadow.is_file() else profile))
elif args == ["-t", "--escape", "no", "-g", "GENERAL.CON-UUID",
"device", "show", "wg-fabric0"]:
if (root / "link-present").exists() and not (root / "nm-unmanaged").exists():
print(uuid)
elif args == ["-t", "--escape", "no", "-g", "GENERAL.NM-MANAGED",
"device", "show", "wg-fabric0"]:
print("no" if (root / "nm-unmanaged").exists() else "yes")
elif args == ["device", "set", "wg-fabric0", "managed", "no"]:
(root / "nm-unmanaged").touch()
elif args == ["-t", "--escape", "no", "-g", "connection.zone",
"connection", "show", "uuid", uuid] and profile.is_file():
zone = root / "nm-zone"
print(zone.read_text().strip() if zone.is_file() else "")
elif args == ["connection", "modify", "--temporary", "uuid", uuid,
"connection.zone", ""] and profile.is_file():
(root / "nm-zone").write_text("")
shadow = (root / "run" / "NetworkManager" / "system-connections" /
"virt-cloud-wg-fabric0.nmconnection")
if shadow.is_file():
shadow.write_text("\n".join(
"zone=" if line.startswith("zone=") else line
for line in shadow.read_text().splitlines()) + "\n")
elif args == ["connection", "delete", "uuid", uuid] and uuid:
identity.unlink()
shadow = (root / "run" / "NetworkManager" / "system-connections" /
"virt-cloud-wg-fabric0.nmconnection")
shadow.unlink(missing_ok=True)
else:
raise SystemExit(2)
elif name in ("wg", "bridge", "flock", "sync", "ifup", "ifquery", "ifdown"):
pass
else:
raise SystemExit(2)
'''
def _fixture(tmp_path: Path, mode: str):
plan = parse_fabric_plan({
"fabric_id": "entry-fabric", "region": "entry-region",
"overlay_cidr": "100.64.1.0/24", "revision": 1,
"nodes": [
{"host_id": "a", "overlay_ip": "100.64.1.1",
"endpoint": "203.0.113.1:51820"},
{"host_id": "b", "overlay_ip": "100.64.1.2",
"endpoint": "203.0.113.2:51820"},
],
})
public_keys = {
"a": base64.b64encode(bytes([1]) * 32).decode(),
"b": base64.b64encode(bytes([2]) * 32).decode(),
}
apply = _apply_script(
plan, plan.nodes[0], public_keys, deployment_mode=mode,
firewall_mode=FIREWALL_MODE_NATIVE,
)
start = apply.index("#!/bin/sh\n# virt-cloud-wg-fabric-v1\n")
end = re.search(r"\n__VC_FABRIC_[0-9a-f]{16}\n", apply[start:]).start() + start
helper = apply[start:end] + "\n"
assert "__VC_FABRIC_MODE__" not in helper
assert "__VC_FABRIC_NFT_RULES_SHA256__" not in helper
tools = tmp_path / "tools"
tools.mkdir()
tool_path = tools / "external-tool"
_write(tool_path, f"#!{sys.executable}\n" + _TOOLS, 0o700)
for name in ("stat", "sha256sum", "ip", "nft", "systemctl",
"nmcli", "wg", "bridge", "flock", "sync",
"ifup", "ifquery", "ifdown"):
(tools / name).symlink_to(tool_path)
roots = {
"/etc/virt-cloud": tmp_path / "etc" / "virt-cloud",
"/run/virt-cloud": tmp_path / "run" / "virt-cloud",
"/run/NetworkManager": tmp_path / "run" / "NetworkManager",
"/etc/systemd/system": tmp_path / "units",
"/usr/local/libexec": tmp_path / "libexec",
"/usr/local/lib/systemd/system-generators": tmp_path / "generators",
"/etc/NetworkManager": tmp_path / "NetworkManager",
"/etc/network": tmp_path / "network",
"/etc/sysctl.d": tmp_path / "sysctl.d",
"/etc/nftables.d": tmp_path / "nftables.d",
"/etc/nftables.conf": tmp_path / "nftables.conf",
"/proc/sys/net/ipv4": tmp_path / "proc" / "ipv4",
}
# Only redirect host resources/external tools. Keep all production shell
# control flow and variable declarations, notably TIMER_UNIT, intact.
for source, destination in roots.items():
helper = helper.replace(source, str(destination))
if source != "/etc/nftables.conf":
destination.mkdir(parents=True, exist_ok=True)
# The secure shadow ancestor check also reads /run itself; redirect only
# that exact loop token, not unrelated paths or production control flow.
helper = helper.replace("in /run ", "in " + shlex.quote(str(tmp_path / "run")) + " ")
base = roots["/etc/virt-cloud"]
runtime = roots["/run/virt-cloud"]
base.chmod(0o700)
runtime.chmod(0o700)
roots["/etc/sysctl.d"].chmod(0o755)
_write(roots["/proc/sys/net/ipv4"] / "ip_forward", "1\n")
helper = helper.replace(
"PATH=/usr/sbin:/usr/bin:/sbin:/bin",
f"PATH={shlex.quote(str(tools))}:/usr/sbin:/usr/bin:/sbin:/bin",
)
for name in ("ifup", "ifquery", "ifdown"):
helper = helper.replace(f"/usr/sbin/{name}", str(tools / name))
for name in ("wg", "ip", "nft", "stat", "sha256sum", "systemctl",
"nmcli", "bridge", "flock"):
helper = re.sub(
rf"find_tool (?:/(?:usr/)?s?bin/{re.escape(name)} ?)+",
f"find_tool {shlex.quote(str(tools / name))} ",
helper,
)
helper_path = tmp_path / "helper"
_write(helper_path, helper, 0o700)
tables = tmp_path / "tables"
tables.mkdir()
_write(roots["/etc/nftables.conf"], (
'# distribution-owned main file\n'
f'include "{roots["/etc/nftables.d"]}/*.nft"\n'
), 0o644)
return plan, helper_path, base, runtime, roots
def _journal(plan, base: Path, mode: str, helper: Path, root: Path, *,
nft_phase: str = "installing"):
journal = base / ".wg-fabric-txn"
journal.mkdir(mode=0o700)
# Read the rendered UUID; the fixture must not invent an independent plan.
uuid = re.search(
r'^CONNECTION_UUID="([^"]+)"$',
helper.read_text(encoding="utf-8"), re.MULTILINE,
).group(1)
metadata = {
"schema": "1", "deployment_mode": mode,
"firewall_mode": FIREWALL_MODE_NATIVE,
"plan_digest": plan.digest, "connection_uuid": uuid,
"nft_main": str(root / "nftables.conf"),
"iface_before": "no", "iface_up_before": "no",
"rp_filter_before": "missing", "nft_before": "no",
"service_enabled_before": "not-found",
"service_active_before": "inactive",
"timer_enabled_before": "not-found", "timer_active_before": "inactive",
}
_write(journal / "meta", "".join(
f"{key}={value}\n" for key, value in metadata.items()
))
_write(journal / "state", "ready\n")
_write(journal / "nft.phase", nft_phase + "\n")
_write(journal / "nft.table", "")
for name in ("conf", "owner", "helper", "unit", "repair", "timer",
"gate_generator", "nm_profile", "ifupdown_fragment",
"sysctl", "nft_rules"):
_write(journal / f"{name}.absent", "")
_write(journal / "nft_main.present", "")
_write(journal / "nft_main", (
'# distribution-owned main file\n'
f'include "{root / "nftables.d"}/*.nft"\n'
), 0o644)
_write(journal / "key.present", "")
_write(journal / "key", "test-private-key-snapshot\n")
_write(base / "wg-fabric0.key", "test-private-key-snapshot\n")
desired = _nft_rules_text(plan.digest)
_write(journal / "nft_rules.desired", desired)
_write(root / "tables" / "inet--virt_cloud_wg_fabric", desired)
if nft_phase == "refreshed":
_write(journal / "nft.installed", desired)
_write(journal / "nft.applied", desired)
return journal
def _run(helper: Path, root: Path, *args: str,
extra_env: dict[str, str] | None = None):
return subprocess.run(
["/bin/sh", str(helper), *args], check=False,
capture_output=True, text=True,
env={"VC_ENTRY_ROOT": str(root), "VC_FABRIC_LOCK_HELD": "1",
**(extra_env or {})},
timeout=30,
)
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
def test_first_deploy_complete_entry_recovers_absent_generator_and_timer(
tmp_path, mode):
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
journal = _journal(plan, base, mode, helper, tmp_path)
result = _run(helper, tmp_path, "--recover-installing")
assert result.returncode == 0, result.stderr
assert not journal.exists()
assert not (roots["/usr/local/lib/systemd/system-generators"] /
"virt-cloud-wg-fabric-gate").exists()
assert not (roots["/etc/systemd/system"] /
"virt-cloud-wg-fabric-reconcile.timer").exists()
assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists()
assert not (runtime / "wg-fabric-emergency.owner").exists()
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
assert ["systemctl", "show", "-p", "LoadState", "--value",
"virt-cloud-wg-fabric-reconcile.timer"] in calls
assert not any(call[0] == "systemctl" and call[1] in
("start", "enable") for call in calls)
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
@pytest.mark.parametrize("helper_published", [False, True])
def test_ready_refreshed_complete_entry_recovers_exact_first_deploy_snapshots(
tmp_path, mode, helper_published):
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
journal = _journal(plan, base, mode, helper, tmp_path,
nft_phase="refreshed")
if helper_published:
# The real first-deploy apply may publish its persistent helper before
# failing final readback. Recovery must run that helper's full entry,
# then use the cleanup_helper phase to safely remove its exact image.
content = helper.read_text(encoding="utf-8")
_write(journal / "helper.desired", content, 0o700)
helper = (roots["/usr/local/libexec"] / "virt-cloud-wg-fabric")
_write(helper, content, 0o700)
result = _run(helper, tmp_path, (
"--recover-only" if helper_published else "--recover-current"
))
assert result.returncode == 0, result.stderr
assert not journal.exists()
if helper_published:
assert not helper.exists()
assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists()
assert (base / "wg-fabric0.key").read_text(encoding="utf-8") == (
"test-private-key-snapshot\n")
assert not (runtime / "wg-fabric-emergency.owner").exists()
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
assert any(call[0] == "nft" and call[1] == "-f" for call in calls)
assert not any(call[0] == "systemctl" and call[1] in
("start", "enable") for call in calls)
assert not any(call[:5] == ["ip", "link", "set", "dev", "wg-fabric0"]
and call[-1] == "up" for call in calls)
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
def test_ready_refreshed_persistent_entry_recovers_created_link(tmp_path, mode):
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
journal = _journal(plan, base, mode, helper, tmp_path,
nft_phase="refreshed")
content = helper.read_text(encoding="utf-8")
_write(journal / "helper.desired", content, 0o700)
helper = roots["/usr/local/libexec"] / "virt-cloud-wg-fabric"
_write(helper, content, 0o700)
def publish(path: Path, name: str, value: str, permissions=0o600):
_write(journal / (name + ".desired"), value, permissions)
_write(path, value, permissions)
publish(base / "wg-fabric0.owner", "owner", "virt-cloud-wg-fabric-v1\n")
if mode == "networkmanager_nmcli":
uuid = re.search(r'^CONNECTION_UUID="([^"]+)"$', content,
re.MULTILINE).group(1)
profile = _nm_profile_template(
plan, plan.nodes[0], uuid, FIREWALL_MODE_NATIVE,
).replace("private-key=\n", "private-key=" +
base64.b64encode(bytes([7]) * 32).decode() + "\n")
persistent = (roots["/etc/NetworkManager"] / "system-connections" /
"virt-cloud-wg-fabric0.nmconnection")
publish(persistent, "nm_profile", profile)
_write(tmp_path / "nm-loaded-uuid", uuid + "\n")
_write(tmp_path / "nm-zone", "\n")
else:
fragment = _ifupdown_fragment(plan, plan.nodes[0]).replace(
"/usr/local/libexec", str(roots["/usr/local/libexec"]),
)
publish(roots["/etc/network"] / "interfaces.d" /
"virt-cloud-wg-fabric0", "ifupdown_fragment", fragment)
_write(tmp_path / "link-present", "")
_write(tmp_path / "link-up", "")
rpf = roots["/proc/sys/net/ipv4"] / "conf" / "wg-fabric0" / "rp_filter"
_write(rpf, "2\n")
result = _run(helper, tmp_path, "--recover-only")
assert result.returncode == 0, result.stderr
assert not journal.exists()
assert not helper.exists()
assert not (tmp_path / "link-present").exists()
assert not (tmp_path / "link-up").exists()
assert not rpf.exists()
assert not (base / "wg-fabric0.owner").exists()
assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists()
assert not (runtime / "wg-fabric-emergency.owner").exists()
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
assert ["ip", "link", "del", "dev", "wg-fabric0"] in calls
assert not any(call[0] == "firewall-cmd" for call in calls)
if mode == "networkmanager_nmcli":
assert ["nmcli", "device", "set", "wg-fabric0", "managed", "no"] in calls
assert ["nmcli", "connection", "delete", "uuid", uuid] in calls
assert not any(call[0] == "systemctl" and call[1] in
("start", "enable") for call in calls)
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
@pytest.mark.parametrize("failure", ["live_verdict", "applied_digest", "applied_mode"])
def test_ready_refreshed_persistent_entry_rejects_unproved_post_images(
tmp_path, mode, failure):
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
journal = _journal(plan, base, mode, helper, tmp_path,
nft_phase="refreshed")
content = helper.read_text(encoding="utf-8")
_write(journal / "helper.desired", content, 0o700)
helper = roots["/usr/local/libexec"] / "virt-cloud-wg-fabric"
_write(helper, content, 0o700)
live = tmp_path / "tables" / "inet--virt_cloud_wg_fabric"
if failure == "live_verdict":
_write(live, live.read_text(encoding="utf-8").replace(
"counter drop comment", "counter accept comment", 1,
))
elif failure == "applied_digest":
applied = journal / "nft.applied"
_write(applied, applied.read_text(encoding="utf-8").replace(
plan.digest, "f" * 64,
))
else:
(journal / "nft.applied").chmod(0o644)
before = live.read_bytes()
result = _run(helper, tmp_path, "--recover-only")
assert result.returncode == 79, result.stderr
assert journal.is_dir()
assert helper.read_text(encoding="utf-8") == content
assert live.read_bytes() == before
assert not (runtime / "wg-fabric-emergency.owner").exists()
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
assert not any(call[0] == "nft" and call[1] == "-f" for call in calls)
assert not any(call[0] == "systemctl" and call[1] in
("start", "enable") for call in calls)
def _transition_fixture(tmp_path: Path, gate_state: str):
plan, helper, base, runtime, roots = _fixture(tmp_path, "ifupdown")
_write(base / "wg-fabric0.owner", "virt-cloud-wg-fabric-v1\n")
_write(roots["/etc/sysctl.d"] / "90-virt-cloud-wg-fabric.conf",
_sysctl_text(plan), 0o644)
fragment = _ifupdown_fragment(plan, plan.nodes[0])
fragment = fragment.replace("/usr/local/libexec",
str(roots["/usr/local/libexec"]))
_write(roots["/etc/network"] / "interfaces.d" /
"virt-cloud-wg-fabric0", fragment)
_write(tmp_path / "link-present", "")
_write(roots["/proc/sys/net/ipv4"] / "conf" /
"wg-fabric0" / "rp_filter", "1\n")
if gate_state != "absent":
digest = "a" * 64 if gate_state == "wrong_digest" else plan.digest
gate = (roots["/usr/local/lib/systemd/system-generators"] /
"virt-cloud-wg-fabric-gate")
_write(gate, (
"#!/bin/sh\n# virt-cloud-wg-fabric-v1\n"
"# virt-cloud-wg-fabric-libvirt-gate-v1\n"
f"# fabric-plan-digest={digest}\n"
), 0o700 if gate_state == "insecure" else 0o755)
return plan, helper, base, runtime, roots
_TRANSITION_ENV = {
"IFUPDOWN_VC_FABRIC_LOCK_HELD": "1",
"IFUPDOWN_VC_FABRIC_APPLYING": "1",
"IFUPDOWN_VC_FABRIC_ALLOW_TRANSITION": "1",
}
@pytest.mark.parametrize("entry", ["--ifupdown-hook", "--ifupdown-configure"])
@pytest.mark.parametrize("gate_state", ["valid", "absent", "insecure", "wrong_digest"])
def test_full_ifupdown_transition_requires_transaction_published_gate(
tmp_path, entry, gate_state):
_, helper, base, runtime, roots = _transition_fixture(tmp_path, gate_state)
result = _run(helper, tmp_path, entry, extra_env=_TRANSITION_ENV)
assert result.returncode == (0 if gate_state == "valid" else 71), result.stderr
# A first deployment has no config yet; nested hooks only operate on the
# transaction-owned link and rp_filter. They must not parse stale desired
# config or start any systemd/libvirt unit.
assert not (base / "wg-fabric0.conf").exists()
assert not (runtime / "wg-fabric-emergency.owner").exists()
assert (tmp_path / "link-up").exists() == (
gate_state == "valid" and entry == "--ifupdown-configure")
rpf = (roots["/proc/sys/net/ipv4"] / "conf" /
"wg-fabric0" / "rp_filter").read_text(encoding="utf-8")
assert rpf == ("2\n" if gate_state == "valid" else "1\n")
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
assert not any(call[0] == "systemctl" and call[1] in
("start", "enable") for call in calls)
def test_ifupdown_configure_without_transition_flags_cannot_activate(tmp_path):
_, helper, _, runtime, _ = _transition_fixture(tmp_path, "valid")
result = _run(helper, tmp_path, "--ifupdown-configure")
assert result.returncode != 0, result.stderr
assert not (tmp_path / "link-up").exists()
assert not (runtime / "wg-fabric-emergency.owner").exists()
@pytest.mark.parametrize("mode", ["ifupdown", "networkmanager_nmcli"])
def test_transaction_publishes_generator_before_network_activation(mode):
plan = parse_fabric_plan({
"fabric_id": "gate-order", "region": "hk",
"overlay_cidr": "100.64.1.0/24", "revision": 1,
"nodes": [
{"host_id": "a", "overlay_ip": "100.64.1.1",
"endpoint": "203.0.113.1:51820"},
{"host_id": "b", "overlay_ip": "100.64.1.2",
"endpoint": "203.0.113.2:51820"},
],
})
keys = {host.host_id: base64.b64encode(bytes([index]) * 32).decode()
for index, host in enumerate(plan.nodes, start=1)}
script = _apply_script(plan, plan.nodes[0], keys, deployment_mode=mode,
firewall_mode=FIREWALL_MODE_NATIVE)
# Inspect the real transaction after the embedded helper here-document.
transaction = script.index("COMMITTED=no\nROLLBACK_FAILED=no")
snapshot = script.index('snapshot_file "$GATE_GENERATOR" gate_generator',
transaction)
helper_publish = script.index(
'publish_desired_file "$_tmp_helper" "$HELPER" helper', snapshot)
gate_publish = script.index(
'publish_desired_file "$_tmp_gate" "$GATE_GENERATOR" gate_generator',
helper_publish)
activation = script.index(
"vc_fabric_operation_checkpoint network_activate", helper_publish)
ifup_call = script.index('"$IFUP" --force "$IFACE"', activation)
persistence = script.index(
"vc_fabric_operation_checkpoint persistence", ifup_call)
reload = script.index('"$SYSTEMCTL" daemon-reload', persistence)
assert (snapshot < helper_publish < gate_publish < activation
< ifup_call < persistence < reload)
assert script.count(
'publish_desired_file "$_tmp_gate" "$GATE_GENERATOR" gate_generator') == 1
@pytest.mark.parametrize("field", ["fabric", "network"])
def test_normal_complete_entry_rejects_config_outside_rendered_plan(tmp_path, field):
plan, helper, base, runtime, roots = _fixture(tmp_path, "ifupdown")
gate = (roots["/usr/local/lib/systemd/system-generators"] /
"virt-cloud-wg-fabric-gate")
_write(gate, (
"#!/bin/sh\n# virt-cloud-wg-fabric-v1\n"
"# virt-cloud-wg-fabric-libvirt-gate-v1\n"
f"# fabric-plan-digest={plan.digest}\n"
), 0o755)
_write(base / "wg-fabric0.owner", "virt-cloud-wg-fabric-v1\n")
_write(roots["/etc/sysctl.d"] / "90-virt-cloud-wg-fabric.conf",
_sysctl_text(plan), 0o644)
fabric = "other-fabric" if field == "fabric" else plan.fabric_id
network = "100.64.2.0/24" if field == "network" else plan.overlay_cidr
overlay = "100.64.2.1" if field == "network" else "100.64.1.1"
peer_ip = "100.64.2.2" if field == "network" else "100.64.1.2"
key = base64.b64encode(bytes([2]) * 32).decode()
_write(base / "wg-fabric0.conf", (
f"schema|1\nfabric|{fabric}\nrevision|1\ndigest|{plan.digest}\n"
"firewall|native_nftables\n"
f"network|{network}\noverlay|{overlay}\nlisten|51820\n"
f"peer|b|{key}|203.0.113.2:51820|{peer_ip}/32\nroute|{peer_ip}/32\n"
))
result = _run(helper, tmp_path)
assert result.returncode == 72, result.stderr
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
assert not any(call[-3:] == ["table", "inet", "firewalld"] for call in calls)
# Both otherwise syntactically valid configurations fail at their exact
# identity/network assertion, before any route or link activation.
assert not any(call[0] == "ip" and "route" in call for call in calls)
assert not (runtime / "wg-fabric-emergency.owner").exists()
assert not any(call[0] == "systemctl" and call[1] in
("start", "enable") for call in calls)
@pytest.mark.parametrize("failure", ["foreign_gate", "plan_digest"])
def test_first_deploy_complete_entry_rejects_unproved_artifacts(
tmp_path, failure):
plan, helper, base, runtime, roots = _fixture(tmp_path, "ifupdown")
journal = _journal(plan, base, "ifupdown", helper, tmp_path)
if failure == "foreign_gate":
gate = (roots["/usr/local/lib/systemd/system-generators"] /
"virt-cloud-wg-fabric-gate")
_write(gate, "foreign-generator\n", 0o755)
else:
metadata = (journal / "meta").read_text(encoding="utf-8")
_write(journal / "meta", metadata.replace(plan.digest, "a" * 64))
before = (tmp_path / "tables" /
"inet--virt_cloud_wg_fabric").read_bytes()
result = _run(helper, tmp_path, "--recover-installing")
assert result.returncode == 79, result.stderr
assert journal.is_dir()
assert (tmp_path / "tables" /
"inet--virt_cloud_wg_fabric").read_bytes() == before
assert not (runtime / "wg-fabric-emergency.owner").exists()
@pytest.mark.parametrize("gate_state", ["absent", "insecure", "wrong_digest", "valid"])
def test_normal_complete_entry_still_validates_generator(tmp_path, gate_state):
plan, helper, base, runtime, roots = _fixture(tmp_path, "ifupdown")
gate = (roots["/usr/local/lib/systemd/system-generators"] /
"virt-cloud-wg-fabric-gate")
if gate_state != "absent":
digest = "a" * 64 if gate_state == "wrong_digest" else plan.digest
_write(gate, (
"#!/bin/sh\n# virt-cloud-wg-fabric-v1\n"
"# virt-cloud-wg-fabric-libvirt-gate-v1\n"
f"# fabric-plan-digest={digest}\n"
), 0o700 if gate_state == "insecure" else 0o755)
result = _run(helper, tmp_path)
# The valid case proceeds to the next required owner/sysctl contract.
assert result.returncode == 71, result.stderr
calls = [json.loads(line) for line in
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
sysctl_dir_checked = ["stat", "-c", "%u:%a",
str(roots["/etc/sysctl.d"])] in calls
assert sysctl_dir_checked == (gate_state == "valid")
assert not (runtime / "wg-fabric-emergency.owner").exists()