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_vpc_route_readback.py

299 lines
14 KiB
Python

"""Exercise generated host programs with real iproute2 output shapes."""
import os
import re
import subprocess
import pytest
from test_vpc_dataplane import (
_active_observation_markers, _guest_network_evidence, _plan,
)
from virsh_panel.vpc import VpcCoordinator
from virsh_panel.vpc_dataplane import (
VPC_ROUTE_RECORDS_SH, VpcPrivateHostAdapter, _markers,
_render_apply_script_v2, _routes, patch_private_interface_xml,
private_guest_network_generation, private_guest_routes,
render_observe_script,
)
def _program(phase):
script = (_render_apply_script_v2(_plan()) if phase == "apply"
else render_observe_script(_plan()))
helper = re.search(r"(?ms)^vc_vpc_route_records\(\) \{.*?^\}\n", script)
assert helper is not None
if phase == "apply":
start = script.index('bridge_routes=$($IP -o -4 -N route show table all dev "$BRIDGE"')
end = script.index("bridge_routes6=", start)
else:
start = script.index('if route_inventory=$("$IP" -o -4 -N route show table "$ROUTE_TABLE"')
# Stop at the end of route/rule readback. The following state and
# shared-Fabric removal checks depend on variables and helpers defined
# by the complete observe program and are outside this fragment test.
end = script.index("vpc_state_loaded=no", start)
return helper[0] + "\n" + script[start:end]
def _run(tmp_path, phase, inventory, *, failed=False):
plan = _plan()
routes = _routes(plan)
(tmp_path / "bridge.routes.allowed").write_text("\n".join(
f"{r['destination_cidr']}|{r['device']}|{r['table']}|{r.get('via') or '-'}|{r['metric']}|186"
for r in routes if r["device"] == plan.resources.bridge) + "\n")
fake_ip = tmp_path / "ip"
fake_ip.write_text('''#!/bin/sh
case "$*" in
*'route show'*)
case " $* " in *' -N '*) : ;; *) exit 91 ;; esac
[ "$VC_FAIL" = no ] || exit 2; printf '%s\\n' "$VC_ROUTES" ;;
*'rule show'*) : ;;
*) exit 90 ;;
esac
''')
fake_ip.chmod(0o700)
return subprocess.run(
["/bin/sh", "-eu", "-c", "vc_ip_rule_read() { return 1; }\n" + _program(phase)],
capture_output=True, text=True, check=False, env=os.environ | {
"IP": str(fake_ip), "VC_ROUTES": inventory,
"VC_FAIL": "yes" if failed else "no", "TXN": str(tmp_path),
"BRIDGE": plan.resources.bridge, "ROUTE_TABLE": str(plan.resources.route_table),
})
def _inventory(*, explicit_prefix=False, omit_device=False, bridge_only=False):
plan = _plan()
lines = []
for route in _routes(plan):
if bridge_only and route["device"] != plan.resources.bridge:
continue
prefix = route["destination_cidr"]
if not explicit_prefix:
prefix = prefix.removesuffix("/32")
fields = [prefix]
if route.get("via"):
fields += ["via", route["via"]]
if not omit_device:
fields += ["dev", route["device"]]
if bridge_only:
fields += ["table", str(route["table"])]
fields += ["proto", "186", "scope", "253", "metric", str(route["metric"])]
if route.get("via"):
fields += ["onlink"]
lines.append(" ".join(fields))
return "\n".join(lines)
@pytest.mark.parametrize("explicit_prefix", [False, True])
@pytest.mark.parametrize("omit_device", [False, True])
def test_apply_accepts_exact_owned_bridge_routes_after_device_filter(
tmp_path, explicit_prefix, omit_device):
result = _run(tmp_path, "apply", _inventory(
explicit_prefix=explicit_prefix, omit_device=omit_device, bridge_only=True))
assert result.returncode == 0, result.stderr
@pytest.mark.parametrize("explicit_prefix", [False, True])
def test_observe_confirms_all_local_remote_and_service_routes(tmp_path, explicit_prefix):
result = _run(tmp_path, "observe", _inventory(explicit_prefix=explicit_prefix))
assert result.returncode == 0, result.stderr
assert "__VC_VPC_ROUTE_EXTRA__=no" in result.stdout
receipts = [line for line in result.stdout.splitlines() if line.startswith("__VC_VPC_ROUTE__=")]
assert len(receipts) == len(_routes(_plan()))
assert all(line.endswith("|owned") for line in receipts)
@pytest.mark.parametrize("phase", ["apply", "observe"])
@pytest.mark.parametrize("change", [
"foreign_device", "duplicate_device", "missing_value", "duplicate_route",
"wrong_table", "wrong_proto", "wrong_metric", "leading_zero", "invalid_ip",
"broad_prefix", "blackhole", "multipath", "duplicate_linkdown",
"dead", "unresolved", "offload", "duplicate_proto", "failed_command",
])
def test_route_normalization_never_accepts_drift_or_unknown_output(tmp_path, phase, change):
plan = _plan()
inventory = _inventory(bridge_only=phase == "apply")
first, *remaining = inventory.splitlines()
destination = first.split()[0]
changes = {
"foreign_device": first.replace(plan.resources.bridge, "br0"),
"duplicate_device": first + f" dev {plan.resources.bridge}",
"missing_value": first + " dev",
"duplicate_route": first + "\n" + first.replace(destination, destination + "/32", 1),
"wrong_table": first.replace(f"table {plan.resources.route_table}", "table 254")
if phase == "apply" else first + " table 254",
"wrong_proto": first.replace("proto 186", "proto static"),
"wrong_metric": first.replace("metric 100", "metric 101"),
"leading_zero": first.replace(destination, "010.42.0.10", 1),
"invalid_ip": first.replace(destination, "256.42.0.10", 1),
"broad_prefix": first.replace(destination, destination + "/24", 1),
"blackhole": "blackhole " + first,
"multipath": first + f" nexthop dev {plan.resources.bridge} weight 1",
"duplicate_linkdown": first + " linkdown linkdown",
"dead": first + " dead",
"unresolved": first + " unresolved",
"offload": first + " offload",
"duplicate_proto": first + " proto 186",
"failed_command": first,
}
result = _run(tmp_path, phase, "\n".join([changes[change], *remaining]),
failed=change == "failed_command")
if phase == "apply":
assert result.returncode == 71, (change, result.stdout, result.stderr)
else:
assert result.returncode == 0, result.stderr
assert "__VC_VPC_ROUTE_EXTRA__=yes" in result.stdout
def test_observe_does_not_infer_device_for_unfiltered_inventory(tmp_path):
result = _run(tmp_path, "observe", _inventory(omit_device=True))
assert result.returncode == 0, result.stderr
assert "__VC_VPC_ROUTE_EXTRA__=yes" in result.stdout
assert not any(line.endswith("|owned") for line in result.stdout.splitlines()
if line.startswith("__VC_VPC_ROUTE__="))
@pytest.mark.parametrize("explicit_prefix", [False, True])
@pytest.mark.parametrize("omit_device", [False, True])
def test_apply_accepts_exact_bridge_routes_without_carrier(
tmp_path, explicit_prefix, omit_device):
inventory = _inventory(explicit_prefix=explicit_prefix,
omit_device=omit_device, bridge_only=True)
result = _run(tmp_path, "apply", inventory + " linkdown")
assert result.returncode == 0, result.stderr
@pytest.mark.parametrize("device", ["wg-fabric0", "br0"])
def test_linkdown_is_only_admitted_for_the_explicit_private_bridge(tmp_path, device):
plan = _plan()
inventory = _inventory()
if device == "wg-fabric0":
inventory = "\n".join(
line + " linkdown" if f"dev {device} " in line else line
for line in inventory.splitlines())
else:
inventory = inventory.replace(f"dev {plan.resources.bridge}", "dev br0")
inventory = inventory.replace("dev br0", "dev br0 linkdown")
result = _run(tmp_path, "observe", inventory)
assert "__VC_VPC_ROUTE_EXTRA__=yes" in result.stdout
assert all(route["state"] == "missing"
for route in _markers(result.stdout)["routes"])
def test_route_parser_requires_explicit_carrier_exception():
plan = _plan()
record = _inventory(bridge_only=True) + " linkdown"
result = subprocess.run(
["/bin/sh", "-eu", "-c", VPC_ROUTE_RECORDS_SH
+ '\nprintf "%s\\n" "$VC_ROUTES" | vc_vpc_route_records "$BRIDGE" main'],
capture_output=True, text=True, env=os.environ | {
"VC_ROUTES": record, "BRIDGE": plan.resources.bridge,
})
assert result.returncode == 1
assert result.stdout == ""
def _observe_lifecycle(tmp_path, *, running, linkdown, guest_verified=True):
"""Run generated route parsing through the real host and VM projections.
Only the remote ip inventory, unrelated host markers and domain evidence
are simulated. This is not a Linux-network-namespace or QGA integration.
"""
plan = _plan()
member = plan.local_members[0]
routes = private_guest_routes(plan.vpc, member)
inventory = "\n".join(
line + " linkdown" if linkdown and f"dev {plan.resources.bridge} " in line
else line for line in _inventory().splitlines())
result = _run(tmp_path, "observe", inventory)
assert result.returncode == 0, result.stderr
route_prefixes = ("__VC_VPC_ROUTE__=", "__VC_VPC_ROUTE_EXTRA__=")
markers = [line for line in _active_observation_markers(plan)
if not line.startswith(route_prefixes)]
markers.extend(line for line in result.stdout.splitlines()
if line.startswith(route_prefixes))
xml = patch_private_interface_xml(
"<domain><metadata/><devices>"
"<interface type='bridge'><mac address='52:54:00:00:00:01'/>"
"<alias name='ua-public0'/><source bridge='br0'/></interface>"
"</devices></domain>", member=member, resources=plan.resources,
guest_network_generation=private_guest_network_generation(member, routes),
vpc_id=plan.vpc.vpc_id, desired_revision=plan.desired_revision,
desired_digest=plan.desired_digest)
guest = (_guest_network_evidence(member, routes) if running and guest_verified
else {"state": "unavailable" if running else "stopped",
"verified": False,
"reason_code": ("vpc_private_guest_agent_unavailable" if running
else "vpc_private_guest_stopped")})
adapter = VpcPrivateHostAdapter(object(), {})
adapter._deployment_contract = lambda: ("networkmanager", "", "", "")
adapter._run = lambda _script: "\n".join(markers) + "\n"
observed = adapter.observe(plan, xml_by_instance={member.instance_uuid: {
"inactive": xml, "live": xml if running else None,
"seed_verified": True, "guest_network": guest,
}})
return plan, member, observed
def _instance_projection(plan, member, observed):
host = dict(observed, transport_verified=True,
private_dataplane_verified=observed["state"] == "active")
return VpcCoordinator._instance_private_state(
member.instance_uuid, required=True, vpc_id=plan.vpc.vpc_id,
member_id=member.member_id, desired_state="active",
private_ip_cidr=member.private_ip + "/32", private_mac=member.private_mac,
desired_revision=plan.desired_revision, desired_digest=plan.desired_digest,
host=host, member_nic=observed["member_nics"][0])
def test_stop_last_member_retry_and_start_preserve_configuration_proof(tmp_path):
# Initial stopped attachment and stopping the last active VM both retain
# the same exact routes while the bridge has no active tap/carrier.
for running in (False, True, False, False, True):
bridge_inventory = _inventory(bridge_only=True)
if not running:
bridge_inventory += " linkdown"
applied = _run(tmp_path, "apply", bridge_inventory)
assert applied.returncode == 0, applied.stderr
plan, member, observed = _observe_lifecycle(
tmp_path, running=running, linkdown=not running)
assert observed["config_verified"] is True
assert observed["config_observed_revision"] == plan.desired_revision
assert observed["config_observed_digest"] == plan.desired_digest
private = _instance_projection(plan, member, observed)
assert private["readback"] == "confirmed"
assert private["state"] == ("active" if running else "stopped")
assert observed["state"] == ("active" if running else "applying")
bridge_route = next(route for route in observed["routes"]
if route["device"] == plan.resources.bridge)
assert bridge_route["flags"] == ([] if running else ["linkdown"])
assert all(route["flags"] == [] for route in observed["routes"]
if route["device"] == "wg-fabric0")
def test_carrier_or_qga_loss_never_promotes_running_vm_to_usable(tmp_path):
for linkdown, guest_verified in ((True, True), (True, False), (False, False)):
plan, member, observed = _observe_lifecycle(
tmp_path, running=True, linkdown=linkdown,
guest_verified=guest_verified)
assert observed["config_verified"] is True
assert observed["state"] != "active"
assert observed["observed_revision"] == 0
private = _instance_projection(plan, member, observed)
assert private["state"] == "uncertain"
assert private["readback"] != "confirmed"
def test_other_members_route_flag_does_not_erase_healthy_member_proof(tmp_path):
plan, member, observed = _observe_lifecycle(
tmp_path, running=True, linkdown=False)
# Host configuration and the member's own runtime proof remain exact even
# if a sibling's observation is unavailable. The projection is per VM.
observed["state"] = "applying"
own = next(route for route in observed["routes"]
if route["destination_cidr"] == member.private_ip + "/32")
observed["routes"].append(dict(
own, destination_cidr="10.42.0.12/32", flags=["linkdown"]))
private = _instance_projection(plan, member, observed)
assert private["state"] == "active"
assert private["readback"] == "confirmed"