Fix real host Fabric sysctl and first deploy recovery
parent
84b1ba14c8
commit
c12e8d7b62
@ -0,0 +1,401 @@
|
||||
"""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_FIREWALLD,
|
||||
_apply_script,
|
||||
_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)
|
||||
|
||||
|
||||
def _emergency_table(family: str, token: str) -> str:
|
||||
rows = [f"table {family} virt_cloud_fabric_emergency {{"]
|
||||
for chain in ("input", "output", "forward"):
|
||||
rows.extend((f" chain {chain} {{", (
|
||||
f" type filter hook {chain} priority 300; policy accept;"
|
||||
)))
|
||||
if family == "inet":
|
||||
matches = []
|
||||
if chain in ("input", "forward"):
|
||||
matches.extend(('iifname "wg-fabric0"', 'iifname "vcv-*"'))
|
||||
if chain in ("output", "forward"):
|
||||
matches.extend(('oifname "wg-fabric0"', 'oifname "vcv-*"'))
|
||||
else:
|
||||
matches = []
|
||||
if chain in ("input", "forward"):
|
||||
matches.append('meta ibrname "vcv-*"')
|
||||
if chain in ("output", "forward"):
|
||||
matches.append('meta obrname "vcv-*"')
|
||||
for match in matches:
|
||||
rows.append(
|
||||
f' {match} counter drop comment '
|
||||
f'"virt-cloud-fabric-emergency-v2:{token}"'
|
||||
)
|
||||
rows.append(" }")
|
||||
rows.append("}")
|
||||
return "\n".join(rows) + "\n"
|
||||
|
||||
|
||||
_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":
|
||||
# The interrupted first deployment never reached link creation.
|
||||
raise SystemExit(1)
|
||||
elif name == "nft":
|
||||
table_root = root / "tables"
|
||||
if args == ["-j", "list", "table", "inet", "firewalld"]:
|
||||
print('{"nftables":[{"table":{"family":"inet",'
|
||||
'"name":"firewalld","flags":["owner","persist"]}}]}')
|
||||
elif 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. Releasing emergency isolation is a test failure.
|
||||
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("loaded" if args[-1] in ("firewalld", "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("active" if args[1] == "firewalld" else "inactive")
|
||||
elif args[0] == "is-enabled" and args[1] in ("firewalld", "nftables"):
|
||||
print("enabled" if args[1] == "firewalld" else "disabled")
|
||||
elif args and args[0] in ("daemon-reload", "disable"):
|
||||
pass
|
||||
else:
|
||||
raise SystemExit(2)
|
||||
elif name == "firewall-cmd":
|
||||
if args == ["--state"]:
|
||||
print("running")
|
||||
elif "--get-zones" in args:
|
||||
print("public trusted")
|
||||
elif any(arg.startswith("--query-interface=") for arg in args):
|
||||
raise SystemExit(1)
|
||||
elif any(arg.startswith("--query-rich-rule=") for arg in args):
|
||||
raise SystemExit(1)
|
||||
else:
|
||||
raise SystemExit(2)
|
||||
elif name == "nmcli":
|
||||
if args == ["-t", "--escape", "no", "-g", "connection.uuid",
|
||||
"connection", "show", "virt-cloud-wg-fabric0"]:
|
||||
pass
|
||||
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_FIREWALLD,
|
||||
)
|
||||
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",
|
||||
"firewall-cmd", "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",
|
||||
"/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",
|
||||
"/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))
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
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",
|
||||
"firewall-cmd", "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()
|
||||
token = "A" * 24
|
||||
_write(runtime / "wg-fabric-emergency.owner", (
|
||||
f"virt-cloud-fabric-emergency-v2:{token}\n"
|
||||
))
|
||||
for family in ("inet", "bridge"):
|
||||
_write(tables / f"{family}--virt_cloud_fabric_emergency", (
|
||||
_emergency_table(family, token)
|
||||
))
|
||||
return plan, helper_path, base, runtime, roots
|
||||
|
||||
|
||||
def _journal(plan, base: Path, mode: str, helper: Path, root: Path):
|
||||
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_FIREWALLD,
|
||||
"plan_digest": plan.digest, "connection_uuid": uuid,
|
||||
"nft_main": str(base / "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",
|
||||
"firewalld_runtime_zone_before": "none",
|
||||
"firewalld_permanent_zone_before": "none",
|
||||
"underlay_endpoint": "203.0.113.1", "underlay_port": "51820",
|
||||
"underlay_zone": "public", "underlay_rule_owner": "panel",
|
||||
"underlay_rule_runtime_before": "no",
|
||||
"underlay_rule_permanent_before": "no",
|
||||
"old_underlay_rule_remove": "no", "old_underlay_endpoint": "",
|
||||
"old_underlay_port": "", "old_underlay_zone": "",
|
||||
}
|
||||
_write(journal / "meta", "".join(
|
||||
f"{key}={value}\n" for key, value in metadata.items()
|
||||
))
|
||||
_write(journal / "state", "ready\n")
|
||||
_write(journal / "nft.phase", "installing\n")
|
||||
_write(journal / "nft.table", "")
|
||||
for name in ("conf", "owner", "helper", "unit", "repair", "timer",
|
||||
"gate_generator", "nm_profile", "ifupdown_fragment",
|
||||
"sysctl", "nft_rules", "nft_main"):
|
||||
_write(journal / f"{name}.absent", "")
|
||||
_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)
|
||||
return journal
|
||||
|
||||
|
||||
def _run(helper: Path, root: Path, *args: str):
|
||||
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"},
|
||||
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()
|
||||
# Recovery keeps independent isolation; only a complete normal apply may
|
||||
# activate interfaces or release these tables.
|
||||
assert (runtime / "wg-fabric-emergency.owner").is_file()
|
||||
for family in ("inet", "bridge"):
|
||||
assert (tmp_path / "tables" /
|
||||
f"{family}--virt_cloud_fabric_emergency").is_file()
|
||||
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("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|firewalld_nft\nunderlay|local|203.0.113.1|eth0|public\n"
|
||||
"firewall_rule|51820|panel\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 ["nft", "-j", "list", "table", "inet", "firewalld"] 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 (runtime / "wg-fabric-emergency.owner").is_file()
|
||||
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 (runtime / "wg-fabric-emergency.owner").is_file()
|
||||
|
||||
|
||||
@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 (runtime / "wg-fabric-emergency.owner").is_file()
|
||||
@ -0,0 +1,205 @@
|
||||
"""Exercise the production proc/sys reader with real POSIX shells."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import os
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from virsh_panel.fabric import (
|
||||
_BOOT_HELPER,
|
||||
_FABRIC_SYSCTL_VALIDATION_SH,
|
||||
_apply_script,
|
||||
_observe_script,
|
||||
parse_fabric_plan,
|
||||
)
|
||||
|
||||
|
||||
SHELLS = tuple(path for path in ("/bin/sh", "/bin/dash", "/bin/bash")
|
||||
if os.access(path, os.X_OK))
|
||||
|
||||
|
||||
@pytest.fixture(params=SHELLS)
|
||||
def shell(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def run_reader(shell, path, prefix="", reader="vc_read_proc_sysctl",
|
||||
source=_FABRIC_SYSCTL_VALIDATION_SH):
|
||||
return subprocess.run(
|
||||
[shell, "-c", prefix + "\n" + source
|
||||
+ f'\n{reader} "$1"\n', "sysctl-reader", str(path)],
|
||||
text=True, capture_output=True, timeout=5, check=False,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ("0", "1", "2"))
|
||||
def test_buffered_reader_accepts_one_kernel_integer_line(tmp_path, shell, value):
|
||||
path = tmp_path / "sysctl"
|
||||
path.write_text(value + "\n", encoding="ascii")
|
||||
result = run_reader(shell, path)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout == value
|
||||
|
||||
|
||||
@pytest.mark.parametrize("payload", (
|
||||
"", "1", "1\n\n", "1\n2\n", "1\ninvalid\n", " 1\n", "1 \n",
|
||||
"1\r\n", "3\n", "-1\n", "01\n", "1.0\n",
|
||||
))
|
||||
def test_buffered_reader_rejects_empty_multiline_or_malformed_values(
|
||||
tmp_path, shell, payload):
|
||||
path = tmp_path / "sysctl"
|
||||
path.write_text(payload, encoding="ascii")
|
||||
result = run_reader(shell, path)
|
||||
assert result.returncode != 0
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("target", ("missing", "directory", "partial_io"))
|
||||
def test_buffered_reader_rejects_io_failure_even_after_a_valid_digit(
|
||||
tmp_path, shell, target):
|
||||
path = tmp_path / "sysctl"
|
||||
prefix = ""
|
||||
if target == "directory":
|
||||
path.mkdir()
|
||||
elif target == "partial_io":
|
||||
path.write_text("1\n", encoding="ascii")
|
||||
# A partial read must not become valid merely because stdout already
|
||||
# contained a valid-looking value before cat reported an I/O error.
|
||||
prefix = 'cat() { command cat "$@"; return 42; }'
|
||||
result = run_reader(shell, path, prefix)
|
||||
assert result.returncode != 0
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
def test_buffered_reader_survives_proc_single_read_eof(tmp_path, shell):
|
||||
path = tmp_path / "sysctl"
|
||||
path.write_text("1\n", encoding="ascii")
|
||||
# Model the observable result of a one-byte shell read from an integer
|
||||
# proc sysctl: it assigns the digit, then sees EOF before the newline.
|
||||
# The buffered production reader must not depend on that builtin result.
|
||||
prefix = '''read() { proc_digit=1; return 1; }
|
||||
legacy_reader() {
|
||||
proc_digit=
|
||||
IFS= read -r proc_digit < "$1" || return 1
|
||||
printf '%s' "$proc_digit"
|
||||
}
|
||||
'''
|
||||
legacy = run_reader(shell, path, prefix, "legacy_reader")
|
||||
assert legacy.returncode != 0
|
||||
buffered = run_reader(shell, path, prefix)
|
||||
assert buffered.returncode == 0, buffered.stderr
|
||||
assert buffered.stdout == "1"
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def production_scripts():
|
||||
plan = parse_fabric_plan({
|
||||
"fabric_id": "proc-test", "region": "hk", "revision": 1,
|
||||
"overlay_cidr": "100.64.1.0/24", "listen_port": 51820,
|
||||
"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 = {node.host_id: base64.b64encode(bytes([index + 1]) * 32).decode()
|
||||
for index, node in enumerate(plan.nodes)}
|
||||
return {
|
||||
"apply": _apply_script(plan, plan.nodes[0], keys),
|
||||
"boot": _BOOT_HELPER,
|
||||
"observe": _observe_script(("100.64.1.2",), "100.64.1.1"),
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_name", ("apply", "boot", "observe"))
|
||||
def test_each_production_sysctl_reader_uses_buffered_io(
|
||||
tmp_path, shell, production_scripts, script_name):
|
||||
script = production_scripts[script_name]
|
||||
reader = "read_observed_sysctl" if script_name == "observe" else "read_sysctl"
|
||||
function = re.search(
|
||||
rf"^{reader}\(\) \{{\n.*?^\}}", script, re.MULTILINE | re.DOTALL)
|
||||
assert function is not None
|
||||
shared_reader = re.search(
|
||||
r"^vc_read_proc_sysctl\(\) \{\n.*?^\}",
|
||||
script, re.MULTILINE | re.DOTALL)
|
||||
assert shared_reader is not None
|
||||
path = tmp_path / "sysctl"
|
||||
path.write_text("1\n", encoding="ascii")
|
||||
result = run_reader(
|
||||
shell, path, function.group(0), reader, shared_reader.group(0))
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout == "1"
|
||||
assert 'IFS= read -r sysctl_value' not in script
|
||||
assert 'IFS= read -r observed_sysctl_value' not in script
|
||||
assert 'IFS= read -r vc_rpf_value' not in script
|
||||
|
||||
|
||||
@pytest.mark.parametrize("script_name", ("apply", "boot"))
|
||||
@pytest.mark.parametrize("value,expected_rc", (("0", 71), ("1", 0), ("2", 71)))
|
||||
def test_preflight_and_boot_still_require_exact_ip_forward_one(
|
||||
tmp_path, shell, production_scripts, script_name, value, expected_rc):
|
||||
script = production_scripts[script_name]
|
||||
check = re.search(
|
||||
r"^read_sysctl\(\) \{\n.*?^\}\n"
|
||||
r"if ! sysctl_forward=.*?^\[ \"\$sysctl_forward\" = 1 \] \|\| exit 71",
|
||||
script, re.MULTILINE | re.DOTALL)
|
||||
assert check is not None
|
||||
path = tmp_path / "sysctl"
|
||||
path.write_text(value + "\n", encoding="ascii")
|
||||
check_script = check.group(0).replace("/proc/sys/net/ipv4/ip_forward", '"$1"')
|
||||
result = subprocess.run(
|
||||
[shell, "-c", _FABRIC_SYSCTL_VALIDATION_SH + "\n" + check_script,
|
||||
"sysctl-preflight", str(path)],
|
||||
text=True, capture_output=True, timeout=5, check=False,
|
||||
)
|
||||
assert result.returncode == expected_rc, result.stderr
|
||||
|
||||
|
||||
@pytest.mark.parametrize("io_fails", (False, True))
|
||||
def test_platform_rp_filter_write_requires_buffered_exact_two_readback(
|
||||
tmp_path, shell, io_fails):
|
||||
owner = tmp_path / "owner"
|
||||
owner.write_text("virt-cloud-wg-fabric-v1\n", encoding="ascii")
|
||||
path = tmp_path / "rp_filter"
|
||||
path.write_text("1\n", encoding="ascii")
|
||||
setter = re.search(
|
||||
r"^vc_fabric_rp_filter_set\(\) \{\n.*?^\}",
|
||||
_FABRIC_SYSCTL_VALIDATION_SH, re.MULTILINE | re.DOTALL)
|
||||
assert setter is not None
|
||||
source = _FABRIC_SYSCTL_VALIDATION_SH + "\n" + setter.group(0).replace(
|
||||
'vc_rpf_path="/proc/sys/net/ipv4/conf/$IFACE/rp_filter"',
|
||||
'vc_rpf_path="$RPF_FILE"',
|
||||
)
|
||||
prefix = (f"OWNER={shlex.quote(str(owner))}\n"
|
||||
f"RPF_FILE={shlex.quote(str(path))}\nWG=:; IFACE=wg-fabric0\n")
|
||||
if io_fails:
|
||||
prefix += '''cat() {
|
||||
command cat "$@" || return 1
|
||||
[ "$1" != "$RPF_FILE" ] || return 42
|
||||
}
|
||||
'''
|
||||
result = run_reader(
|
||||
shell, path, prefix, "vc_fabric_rp_filter_set", source)
|
||||
assert path.read_text(encoding="ascii") == "2\n"
|
||||
assert result.returncode == (1 if io_fails else 0), result.stderr
|
||||
|
||||
|
||||
@pytest.mark.parametrize("path", (
|
||||
"/proc/sys/net/ipv4/ip_forward", "/proc/sys/net/ipv4/conf/all/rp_filter",
|
||||
"/proc/sys/net/ipv4/conf/default/rp_filter",
|
||||
))
|
||||
def test_buffered_reader_reads_real_linux_proc_sysctl(shell, path):
|
||||
source = Path(path)
|
||||
if not source.is_file():
|
||||
pytest.skip("real Linux proc sysctls are unavailable on this test host")
|
||||
expected = source.read_text(encoding="ascii").strip()
|
||||
assert expected in ("0", "1", "2")
|
||||
result = run_reader(shell, source)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout == expected
|
||||
Loading…
Reference in New Issue