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_libvirt_indepen...

294 lines
14 KiB
Python

"""New networking leaves libvirt units alone and retires only exact old gates."""
import shlex
import subprocess
import sys
import pytest
from virsh_panel.fabric import (
FABRIC_LIBVIRT_GATE_UNITS, _BOOT_HELPER, _LEGACY_LIBVIRT_GATE_VALIDATION_SH,
_LIBVIRT_GATE_GENERATOR, _REPAIR_UNIT, _UNIT, _apply_script, _observe_script,
)
from test_fabric import KEY1, KEY2, plan
def old_generator(*, sockets=False):
units = FABRIC_LIBVIRT_GATE_UNITS
template = _LIBVIRT_GATE_GENERATOR
if sockets:
units = tuple(unit for daemon in ("libvirtd", "virtqemud", "virtproxyd")
for unit in (f"{daemon}.service", *(f"{daemon}{suffix}.socket"
for suffix in ("", "-ro", "-admin", "-tcp", "-tls")))) + (
"libvirt-guests.service",)
old_case = " " + "|\\\n ".join(
"|".join(units[index:index + 6]) for index in range(0, 18, 6))
old_case += "|\\\n libvirt-guests.service) : ;;"
template = template.replace(
" libvirtd.service|virtqemud.service|virtproxyd.service|libvirt-guests.service) : ;;",
old_case)
return template.replace("__VC_LIBVIRT_GATE_UNITS__", " ".join(units)).replace(
"__VC_FABRIC_DIGEST__", "a" * 64) + "\n"
def root_tools(tmp_path):
stat = tmp_path / "stat"
stat.write_text(f"#!{sys.executable}\n" + '''import os, stat, sys
s = os.stat(sys.argv[-1])
fields = {'%u': '0', '%g': '0', '%a': oct(stat.S_IMODE(s.st_mode))[2:], '%s': str(s.st_size)}
value = sys.argv[-2]
for key, replacement in fields.items(): value = value.replace(key, replacement)
print(value)
''')
stat.chmod(0o700)
sha = tmp_path / "sha256sum"
sha.write_text(f"#!{sys.executable}\nimport hashlib,sys\nprint(hashlib.sha256(sys.stdin.buffer.read()).hexdigest())\n")
sha.chmod(0o700)
return f"STAT={shlex.quote(str(stat))}\nSHA256={shlex.quote(str(sha))}\nCMP=/usr/bin/cmp\n"
@pytest.mark.parametrize("sockets", [False, True])
@pytest.mark.parametrize("rollback", [False, True])
def test_exact_published_gate_migration_is_journaled_and_reversible(tmp_path, sockets, rollback):
current = plan()
script = _apply_script(current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2})
begin = script.index("snapshot_file() {")
functions = script[begin:script.index("\nrollback_sysctl_file() {", begin)]
begin = script.index("# Retire only the exact platform generator")
retirement = script[begin:script.index("vc_fabric_operation_checkpoint network_activate", begin)]
txn = tmp_path / "txn"
txn.mkdir(mode=0o700)
gate = tmp_path / "virt-cloud-wg-fabric-gate"
original = old_generator(sockets=sockets)
gate.write_text(original)
gate.chmod(0o755)
admin_unit = tmp_path / "libvirtd.service"
admin_unit.write_text("[Unit]\nAfter=operator.service\n[Service]\nExecStart=/host/libvirtd\n")
admin_original = admin_unit.read_bytes()
harness = "set -eu\numask 077\n" + root_tools(tmp_path) + f'''
TXN={shlex.quote(str(txn))}
GATE_GENERATOR={shlex.quote(str(gate))}
ROLLBACK_FAILED=no
_tmp_restore=
{_LEGACY_LIBVIRT_GATE_VALIDATION_SH}
{functions}
vc_legacy_libvirt_gate_owned "$GATE_GENERATOR"
snapshot_file "$GATE_GENERATOR" gate_generator
{retirement}
'''
if rollback:
harness += 'restore_file "$GATE_GENERATOR" gate_generator\n[ "$ROLLBACK_FAILED" = no ]\n'
result = subprocess.run(["/bin/sh", "-s"], input=harness, text=True, capture_output=True)
assert result.returncode == 0, result.stderr
assert (txn / "gate_generator").read_text() == original
assert (txn / "gate_generator.present").exists()
assert (txn / "gate_generator.desired.absent").exists()
if rollback:
assert gate.read_text() == original
else:
assert not gate.exists()
assert admin_unit.read_bytes() == admin_original
@pytest.mark.parametrize("kind", ["changed_body", "fake_marker", "wrong_mode", "symlink"])
def test_unknown_or_modified_gate_is_preserved(tmp_path, kind):
gate = tmp_path / "gate"
target = tmp_path / "external"
content = old_generator()
if kind == "changed_body":
content += "echo operator configuration\n"
elif kind == "fake_marker":
content = "\n".join(content.splitlines()[:4]) + "\n"
target.write_text(content)
target.chmod(0o755)
if kind == "symlink":
gate.symlink_to(target)
else:
gate.write_text(content)
gate.chmod(0o644 if kind == "wrong_mode" else 0o755)
before = target.read_bytes()
harness = "set -eu\n" + root_tools(tmp_path) + _LEGACY_LIBVIRT_GATE_VALIDATION_SH
harness += f'\nvc_legacy_libvirt_gate_owned {shlex.quote(str(gate))} || exit 71\n'
harness += f'rm -f {shlex.quote(str(gate))}\n'
result = subprocess.run(["/bin/sh", "-s"], input=harness, text=True, capture_output=True)
assert result.returncode == 71
assert gate.exists()
assert target.read_bytes() == before
assert gate.read_text() == content
@pytest.mark.parametrize("mode", ["ifupdown", "networkmanager_nmcli"])
def test_new_scripts_neither_install_gate_nor_control_libvirt_units(mode):
current = plan()
keys = {"host-a": KEY1, "host-b": KEY2}
apply = _apply_script(current, current.nodes[0], keys, deployment_mode=mode)
observe = _observe_script((current.nodes[1].overlay_ip,), current.nodes[0].overlay_ip,
deployment_mode=mode, expected_digest=current.digest)
for script in (apply, observe, _BOOT_HELPER, _UNIT, _REPAIR_UNIT):
assert "Before=libvirtd" not in script
assert "Requires=virt-cloud-wg-fabric.service" not in script
assert "gate_units=" not in script
assert "vc_gate_retry_start_claimed" not in script
assert "__VC_FABRIC_LIBVIRT_DEPENDENCIES__=" not in script
assert 'publish_desired_file "$_tmp_gate"' not in apply
assert "mkdir -p /usr/local/libexec /usr/local/lib/systemd/system-generators" not in apply
assert 'record_desired_absent gate_generator' in apply
assert '"$SYSTEMCTL" daemon-reload' in apply
assert "vpc_state_load" in _BOOT_HELPER
assert "nft_plan_owned" in apply
def startup_files(root, current, *, legacy=False, gate=True,
legacy_neighbors=False, legacy_routes=False):
from virsh_panel.fabric import FABRIC_STARTUP_ISOLATION_MARKER
root.mkdir(parents=True, exist_ok=True)
for name, content, mode in (("helper", _BOOT_HELPER, 0o700), ("unit", _UNIT, 0o644)):
content = content.replace("__VC_FABRIC_DIGEST__", current.digest)
if legacy:
content = content.replace(FABRIC_STARTUP_ISOLATION_MARKER + "\n", "")
if legacy_neighbors and name == "helper":
content = content.replace("# vpc-local-neighbor-contract-v1\n", "")
if legacy_routes and name == "helper":
content = content.replace("# vpc-state-route-scope-contract-v4\n", "")
target = root / name
target.write_text(content)
target.chmod(mode)
target = root / "generator"
if target.exists() or target.is_symlink():
target.unlink()
if gate:
target.write_text(old_generator().replace("a" * 64, current.digest))
target.chmod(0o755)
def startup_readback(root, current):
script = _observe_script((), expected_digest=current.digest)
start = script.index("artifact_state() {")
function = script[start:script.index("helper_marker=$(artifact_state", start)]
commands = root_tools(root) + function + f'''
UNIT={shlex.quote(str(root / 'unit'))}
GATE_GENERATOR={shlex.quote(str(root / 'generator'))}
artifact_state {shlex.quote(str(root / 'helper'))} helper {shlex.quote(current.digest)}
printf '\\n'
artifact_state "$UNIT" unit {shlex.quote(current.digest)}
'''
result = subprocess.run(["/bin/sh", "-eu", "-c", commands],
capture_output=True, text=True)
assert result.returncode == 0, result.stderr
return result.stdout.splitlines()
@pytest.mark.parametrize("legacy,gate,expected", [
(True, True, ["foreign", "foreign"]),
(True, False, ["foreign", "foreign"]),
(False, True, ["foreign", "owned"]),
(False, False, ["owned", "owned"]),
])
def test_startup_readback_requires_upgrade_marker_and_absent_legacy_generator(
tmp_path, legacy, gate, expected):
current = plan()
startup_files(tmp_path, current, legacy=legacy, gate=gate)
assert startup_readback(tmp_path, current) == expected
def test_dangling_generator_still_prevents_exact_startup_readback(tmp_path):
current = plan()
startup_files(tmp_path, current, gate=False)
(tmp_path / "generator").symlink_to(tmp_path / "missing")
assert startup_readback(tmp_path, current) == ["foreign", "owned"]
@pytest.mark.parametrize("upgrade", ["neighbors", "routes"])
def test_same_plan_legacy_helper_requires_upgrade_without_changing_unit(tmp_path, upgrade):
current = plan()
startup_files(tmp_path, current, gate=False,
legacy_neighbors=upgrade == "neighbors",
legacy_routes=upgrade == "routes")
assert startup_readback(tmp_path, current) == ["foreign", "owned"]
@pytest.mark.parametrize("upgrade", ["startup", "neighbors", "routes"])
def test_explicit_reconcile_upgrades_same_plan_then_skips_current_hosts(tmp_path, monkeypatch, upgrade):
import asyncio
from virsh_panel.fabric import FabricCoordinator, FABRIC_STARTUP_ISOLATION_MARKER
from test_fabric import AsyncCoordinatorAdapter, MemoryStore, Registry, _async_components, plan_payload
current = plan()
components = _async_components(current)
for host in current.nodes:
components[host.host_id].manager.startup_root = tmp_path / host.host_id
startup_files(tmp_path / host.host_id, current, gate=False)
class StartupAdapter(AsyncCoordinatorAdapter):
def observe(self, actual_plan, host, keys):
result = super().observe(actual_plan, host, keys)
markers = startup_readback(self.manager.startup_root, actual_plan)
if markers != ["owned", "owned"]:
result.update(state="degraded", config_ok=False, transport_verified=False)
return result
def dispatch_apply(self, actual_plan, host, keys, operation_id=None):
rendered = _apply_script(actual_plan, host, keys)
assert FABRIC_STARTUP_ISOLATION_MARKER in rendered
assert "# vpc-local-neighbor-contract-v1" in rendered
assert "# vpc-state-route-scope-contract-v4" in rendered
assert 'record_desired_absent gate_generator' in rendered
startup_files(self.manager.startup_root, actual_plan, gate=False)
return super().dispatch_apply(actual_plan, host, keys, operation_id)
monkeypatch.setattr("virsh_panel.fabric.FabricHostAdapter", StartupAdapter)
monkeypatch.setattr("virsh_panel.fabric.FABRIC_OPERATION_POLL_INTERVAL_SECONDS", 0)
coordinator = FabricCoordinator(MemoryStore(), Registry(components))
async def wait():
task = coordinator._operation_tasks_by_fabric[current.fabric_id]
await asyncio.wait_for(task, 20)
assert coordinator.get_status(current.fabric_id)["operation"]["status"] == "succeeded"
async def run():
initial = await coordinator.enqueue_apply(plan_payload(current))
await wait()
# Reproduce an upgrade: identical persisted successful plan and keys,
# but both hosts still have the prior helper/unit/generator contract.
for component in components.values():
startup_files(component.manager.startup_root, current,
legacy=upgrade == "startup", gate=upgrade == "startup",
legacy_neighbors=upgrade == "neighbors",
legacy_routes=upgrade == "routes")
previous_helpers = {
host_id: (component.manager.startup_root / "helper").read_bytes()
for host_id, component in components.items()
}
identity_calls = {
host_id: component.manager.async_identity_calls
for host_id, component in components.items()
}
# The ordinary observation loop reports the stale contract without
# publishing a helper or dispatching an implicit upgrade.
await coordinator.reconcile_all()
observed = coordinator.get_status(current.fabric_id)
assert all(node["state"] == "degraded" for node in observed["nodes"])
for host_id, component in components.items():
manager = component.manager
assert manager.async_dispatch_calls == 1
assert manager.async_identity_calls == identity_calls[host_id]
assert (manager.startup_root / "helper").read_bytes() == previous_helpers[host_id]
admitted = await coordinator.enqueue_reconcile(current.fabric_id)
assert admitted["operation"]["id"] != initial["operation"]["id"]
await wait()
final = coordinator.get_status(current.fabric_id)
assert final["revision"] == current.revision
assert final["plan_digest"] == current.digest
assert all(node["operation"]["status"] == "succeeded" for node in final["nodes"])
assert all(component.manager.async_dispatch_calls == 2 for component in components.values())
assert all(not (component.manager.startup_root / "generator").exists()
for component in components.values())
await coordinator.enqueue_reconcile(current.fabric_id)
await wait()
final = coordinator.get_status(current.fabric_id)
assert all(node["operation"]["status"] == "skipped" for node in final["nodes"])
assert all(component.manager.async_dispatch_calls == 2 for component in components.values())
coordinator.close()
asyncio.run(run())