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.
136 lines
5.1 KiB
Python
136 lines
5.1 KiB
Python
"""Fabric boot ordering with systemd's service/socket default dependencies.
|
|
|
|
The fixtures model Debian 13/systemd 257 and upstream libvirt socket units:
|
|
https://manpages.debian.org/trixie/systemd/systemd.service.5.en.html
|
|
https://manpages.debian.org/trixie/systemd/systemd.socket.5.en.html
|
|
https://raw.githubusercontent.com/systemd/systemd/v257/units/basic.target
|
|
https://raw.githubusercontent.com/libvirt/libvirt/master/src/remote/libvirtd.socket.in
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import shlex
|
|
import subprocess
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.fabric import (
|
|
FABRIC_LIBVIRT_GATE_UNITS,
|
|
FABRIC_SERVICE,
|
|
_LIBVIRT_GATE_GENERATOR,
|
|
_UNIT,
|
|
)
|
|
|
|
|
|
def _properties(content: str) -> dict[str, list[str]]:
|
|
properties: dict[str, list[str]] = defaultdict(list)
|
|
for line in content.splitlines():
|
|
if line and not line.startswith(("#", "[")) and "=" in line:
|
|
key, value = line.split("=", 1)
|
|
properties[key].extend(value.split())
|
|
return properties
|
|
|
|
|
|
def _ordering_edges(units: dict[str, str]) -> dict[str, set[str]]:
|
|
"""Edges point from the earlier unit to the later unit, as systemd does."""
|
|
graph: dict[str, set[str]] = defaultdict(set)
|
|
for name, content in units.items():
|
|
properties = _properties(content)
|
|
graph[name]
|
|
if properties.get("DefaultDependencies") != ["no"]:
|
|
if name.endswith(".service"):
|
|
graph["basic.target"].add(name)
|
|
graph["sysinit.target"].add(name)
|
|
elif name.endswith(".socket"):
|
|
graph[name].add("sockets.target")
|
|
graph["sysinit.target"].add(name)
|
|
for earlier in properties.get("After", []):
|
|
graph[earlier].add(name)
|
|
for later in properties.get("Before", []):
|
|
graph[name].add(later)
|
|
if name.endswith(".socket"):
|
|
# Socket units implicitly precede their activated daemon.
|
|
daemon = properties.get("Service", [name[:-7] + ".service"])[0]
|
|
graph[name].add(daemon)
|
|
return graph
|
|
|
|
|
|
def _has_cycle(graph: dict[str, set[str]]) -> bool:
|
|
visiting: set[str] = set()
|
|
visited: set[str] = set()
|
|
|
|
def visit(node: str) -> bool:
|
|
if node in visiting:
|
|
return True
|
|
if node in visited:
|
|
return False
|
|
visiting.add(node)
|
|
if any(visit(child) for child in graph.get(node, ())):
|
|
return True
|
|
visiting.remove(node)
|
|
visited.add(node)
|
|
return False
|
|
|
|
return any(visit(node) for node in list(graph))
|
|
|
|
|
|
@pytest.mark.parametrize("daemons", [
|
|
("libvirtd",),
|
|
("virtqemud", "virtproxyd"),
|
|
])
|
|
def test_socket_activation_waits_for_fabric_without_early_boot_cycle(
|
|
tmp_path: Path, daemons: tuple[str, ...],
|
|
):
|
|
installed = tmp_path / "system"
|
|
output = tmp_path / "generator"
|
|
installed.mkdir()
|
|
output.mkdir()
|
|
units = {
|
|
"basic.target": "[Unit]\nAfter=sysinit.target sockets.target\n",
|
|
"sockets.target": "[Unit]\n",
|
|
FABRIC_SERVICE: _UNIT,
|
|
"libvirt-guests.service": "[Unit]\n[Service]\nExecStart=/bin/true\n",
|
|
}
|
|
for daemon in daemons:
|
|
service = f"{daemon}.service"
|
|
units[service] = "[Unit]\n[Service]\nExecStart=/bin/true\n"
|
|
for suffix in ("", "-ro", "-admin", "-tcp", "-tls"):
|
|
socket = f"{daemon}{suffix}.socket"
|
|
units[socket] = (
|
|
"[Unit]\n[Socket]\nListenStream=/run/libvirt/test.sock\n"
|
|
f"Service={service}\n[Install]\nWantedBy=sockets.target\n"
|
|
)
|
|
for name, content in units.items():
|
|
(installed / name).write_text(content)
|
|
generator = _LIBVIRT_GATE_GENERATOR.replace(
|
|
"__VC_LIBVIRT_GATE_UNITS__", " ".join(FABRIC_LIBVIRT_GATE_UNITS),
|
|
).replace(
|
|
"/etc/systemd/system /run/systemd/system /usr/local/lib/systemd/system "
|
|
"/usr/lib/systemd/system /lib/systemd/system",
|
|
shlex.quote(str(installed)),
|
|
)
|
|
completed = subprocess.run(
|
|
["/bin/sh", "-s", "--", str(output)],
|
|
input=generator, text=True, capture_output=True, check=False,
|
|
)
|
|
assert completed.returncode == 0, completed.stderr
|
|
expected_gates = {f"{daemon}.service.d" for daemon in daemons}
|
|
expected_gates.add("libvirt-guests.service.d")
|
|
assert {path.name for path in output.iterdir()} == expected_gates
|
|
for daemon in (*daemons, "libvirt-guests"):
|
|
service = f"{daemon}.service"
|
|
gate = (output / f"{service}.d/90-virt-cloud-wg-fabric.conf").read_text()
|
|
properties = _properties(gate)
|
|
assert properties["Requires"] == [FABRIC_SERVICE]
|
|
assert properties["After"] == [FABRIC_SERVICE]
|
|
units[service] += gate
|
|
assert not any(name.endswith(".socket") for name in FABRIC_LIBVIRT_GATE_UNITS)
|
|
assert not any(name.endswith(".socket") for name in _properties(_UNIT)["Before"])
|
|
assert not _has_cycle(_ordering_edges(units))
|
|
|
|
# Verify that these default-dependency fixtures detect the old defect.
|
|
socket = f"{daemons[0]}.socket"
|
|
units[socket] += f"[Unit]\nAfter={FABRIC_SERVICE}\n"
|
|
assert _has_cycle(_ordering_edges(units))
|