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.
373 lines
16 KiB
Python
373 lines
16 KiB
Python
"""Run real rendered Fabric route and boot-nft readback programs."""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import re
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.fabric import (
|
|
FIREWALL_MODE_NATIVE,
|
|
_BOOT_HELPER,
|
|
_FABRIC_NFT_VALIDATION_SH,
|
|
_apply_script,
|
|
_nft_rules_text,
|
|
_observe_script,
|
|
parse_fabric_plan,
|
|
)
|
|
|
|
|
|
def _plan():
|
|
return parse_fabric_plan({
|
|
"fabric_id": "route-test", "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"},
|
|
],
|
|
})
|
|
|
|
|
|
def _render_apply(mode="ifupdown"):
|
|
plan = _plan()
|
|
keys = {node.host_id: base64.b64encode(bytes([index]) * 32).decode()
|
|
for index, node in enumerate(plan.nodes, 1)}
|
|
return _apply_script(plan, plan.nodes[0], keys, deployment_mode=mode,
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
|
|
|
|
def _assignment(script, name, start=0):
|
|
begin = script.index(name + "=$(printf", start)
|
|
end = script.index("')", begin) + 2
|
|
return script[begin:end]
|
|
|
|
|
|
def _route_program(variant):
|
|
if variant.startswith("apply_"):
|
|
script = _render_apply()
|
|
# Embedded helper is intentionally not the program under test.
|
|
start = script.index("MANAGED_ROUTES=")
|
|
name = {"apply_preflight": "route_conflict",
|
|
"apply_final": "final_route_extra",
|
|
"apply_seen": "final_route_seen"}[variant]
|
|
if variant == "apply_seen":
|
|
start = script.index("for final_expected_route in", start)
|
|
else:
|
|
script = _BOOT_HELPER
|
|
start = 0
|
|
name = {"boot_preflight": "route_conflict",
|
|
"boot_final": "route_extra",
|
|
"boot_seen": "route_seen"}[variant]
|
|
if variant == "boot_preflight":
|
|
start = script.index("if ! fabric_routes=")
|
|
if variant == "boot_seen":
|
|
start = script.index("for expected_route in")
|
|
return _assignment(script, name, start) + f'\nprintf "%s" "${name}"\n'
|
|
|
|
|
|
def _run_route(variant, routes, vpc_file="/dev/null"):
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", _route_program(variant)],
|
|
capture_output=True, text=True, check=False,
|
|
env={"IFACE": "wg-fabric0", "overlay": "100.64.1.1",
|
|
"allowed_routes": "100.64.1.2/32",
|
|
"MANAGED_ROUTES": "100.64.1.2/32", "OLD_MANAGED_ROUTES": "",
|
|
"fabric_routes": routes, "actual_fabric_routes": routes,
|
|
"final_fabric_routes": routes, "VPC_ROUTE_FILE": str(vpc_file),
|
|
"expected_route": "100.64.1.2/32",
|
|
"final_expected_route": "100.64.1.2/32"},
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
return result.stdout
|
|
|
|
|
|
EXTRA_VARIANTS = ("apply_preflight", "apply_final",
|
|
"boot_preflight", "boot_final")
|
|
OWN_ROUTE = ("local 100.64.1.1 dev wg-fabric0 table local proto kernel "
|
|
"scope host src 100.64.1.1\n")
|
|
|
|
|
|
@pytest.mark.parametrize("variant", EXTRA_VARIANTS)
|
|
@pytest.mark.parametrize("prefix", ["100.64.1.2", "100.64.1.2/32"])
|
|
@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""])
|
|
def test_real_host_route_and_owned_local_route_are_equivalent(
|
|
variant, prefix, device_field):
|
|
routes = f"{prefix}{device_field} scope link metric 50\n" + OWN_ROUTE.replace(
|
|
" dev wg-fabric0", device_field)
|
|
assert _run_route(variant, routes) == ""
|
|
|
|
|
|
@pytest.mark.parametrize("variant", ["apply_seen", "boot_seen"])
|
|
@pytest.mark.parametrize("prefix", ["100.64.1.2", "100.64.1.2/32"])
|
|
@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""])
|
|
def test_real_host_route_positive_readback(variant, prefix, device_field):
|
|
assert _run_route(variant, f"{prefix}{device_field} scope link\n") == "1"
|
|
assert _run_route(variant, OWN_ROUTE) == ""
|
|
|
|
|
|
@pytest.mark.parametrize("variant", EXTRA_VARIANTS)
|
|
@pytest.mark.parametrize("foreign", [
|
|
"100.64.1.2/24 dev wg-fabric0 scope link",
|
|
"100.64.1.3 dev wg-fabric0 scope link",
|
|
"100.64.1.02 dev wg-fabric0 scope link",
|
|
"100.64.1.256 dev wg-fabric0 scope link",
|
|
"100.64.1.2 dev br0 scope link",
|
|
"100.64.1.2 scope link dev",
|
|
"100.64.1.2 dev br0 dev wg-fabric0 scope link",
|
|
"100.64.1.2 dev wg-fabric0 dev wg-fabric0 scope link",
|
|
"100.64.1.2 dev wg-fabric0 table 100 scope link",
|
|
"local 100.64.1.1 dev br0 table local proto kernel scope host",
|
|
"local 100.64.1.1 dev wg-fabric0 table 100 proto kernel scope host",
|
|
"default dev wg-fabric0",
|
|
"100.64.1.2 dev wg-fabric0\n100.64.1.2/32 dev wg-fabric0",
|
|
"blackhole 100.64.1.2",
|
|
"unreachable 100.64.1.2",
|
|
"prohibit 100.64.1.2",
|
|
"throw 100.64.1.2",
|
|
"broadcast 100.64.1.2 table local proto kernel scope link",
|
|
"100.64.1.2 nhid 123 scope link metric 50",
|
|
"100.64.1.2 nexthop dev wg-fabric0 weight 1",
|
|
"100.64.1.2 encap mpls 100 dev wg-fabric0",
|
|
"local 100.64.1.1 table main proto kernel scope host src 100.64.1.1",
|
|
"local 100.64.1.1 table local proto static scope host src 100.64.1.1",
|
|
"local 100.64.1.1 table local proto kernel scope link src 100.64.1.1",
|
|
"local 100.64.1.1 table local proto kernel scope host src 100.64.1.3",
|
|
"local 100.64.1.1 table local proto kernel scope host",
|
|
"local 100.64.1.1 table local scope host src 100.64.1.1",
|
|
])
|
|
def test_host_canonicalization_keeps_foreign_and_duplicate_routes_blocked(
|
|
variant, foreign):
|
|
assert _run_route(variant, foreign + "\n") in ("foreign", "extra")
|
|
|
|
|
|
@pytest.mark.parametrize("variant", EXTRA_VARIANTS)
|
|
@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""])
|
|
def test_vpc_host_route_requires_exact_owned_tuple_and_protocol(
|
|
tmp_path, variant, device_field):
|
|
vpc_file = tmp_path / "routes"
|
|
vpc_file.write_text(
|
|
"10.1.0.3/32|wg-fabric0|100|100.64.1.2|100\n", encoding="utf-8")
|
|
normal = "100.64.1.2 dev wg-fabric0 scope link\n" + OWN_ROUTE
|
|
vpc = ("10.1.0.3 via 100.64.1.2 dev wg-fabric0 table 100 "
|
|
"proto 186 metric 100 scope link\n").replace(
|
|
" dev wg-fabric0", device_field)
|
|
assert _run_route(variant, normal + vpc, vpc_file) == ""
|
|
for changed in (vpc.replace("proto 186", "proto static"),
|
|
vpc.replace("metric 100", "metric 101"),
|
|
vpc.replace("table 100", "table 101"),
|
|
vpc.replace("via 100.64.1.2", "via 100.64.1.3")):
|
|
assert _run_route(variant, normal + changed, vpc_file) in ("foreign", "extra")
|
|
|
|
|
|
def _boot_nft_program():
|
|
start = _BOOT_HELPER.index("validate_nft_table() {")
|
|
end = _BOOT_HELPER.index("\nfabric_acl_ready()", start)
|
|
return (_FABRIC_NFT_VALIDATION_SH + "\n" + _BOOT_HELPER[start:end]
|
|
+ '\ntable=$(cat)\nvalidate_nft_table "$table"\n')
|
|
|
|
|
|
@pytest.mark.parametrize("indent", ["spaces", "tabs"])
|
|
@pytest.mark.parametrize("named_output", [False, True])
|
|
def test_boot_nft_owner_readback_ignores_only_serialization_indentation(
|
|
indent, named_output):
|
|
plan = _plan()
|
|
rules = _nft_rules_text(plan.digest)
|
|
if indent == "tabs":
|
|
rules = re.sub(r"^( )+", lambda match: "\t" * (len(match[0]) // 4),
|
|
rules, flags=re.MULTILINE)
|
|
if named_output:
|
|
rules = rules.replace("hook output priority -100", "hook output priority dstnat")
|
|
result = subprocess.run(["/bin/sh", "-eu", "-c", _boot_nft_program()],
|
|
input=rules, capture_output=True, text=True,
|
|
env={"EXPECTED_DIGEST": plan.digest}, check=False)
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
|
@pytest.mark.parametrize("drift", ["digest", "verdict", "extra_rule", "extra_chain"])
|
|
def test_boot_nft_whitespace_fix_keeps_complete_ownership_validation(drift):
|
|
plan = _plan()
|
|
rules = _nft_rules_text(plan.digest)
|
|
if drift == "digest":
|
|
rules = rules.replace(plan.digest, "a" * 64)
|
|
elif drift == "verdict":
|
|
rules = rules.replace("counter drop", "counter accept", 1)
|
|
elif drift == "extra_rule":
|
|
rules = rules.replace(" chain output {", (
|
|
' chain output {\n oifname "wg-fabric0" accept'))
|
|
else:
|
|
rules = rules.replace(" chain output {", " chain foreign {\n }\n chain output {")
|
|
result = subprocess.run(["/bin/sh", "-eu", "-c", _boot_nft_program()],
|
|
input=rules, capture_output=True, text=True,
|
|
env={"EXPECTED_DIGEST": plan.digest}, check=False)
|
|
assert result.returncode != 0
|
|
|
|
|
|
def _observe_route_program():
|
|
current = _plan()
|
|
script = _observe_script(("100.64.1.2",), "100.64.1.1",
|
|
endpoint_ips=("203.0.113.2",),
|
|
deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE,
|
|
expected_digest=current.digest)
|
|
start = script.index('awk -v vpc_file="${VPC_ROUTE_FILE:-}"')
|
|
program = script[start:script.index("\n }'", start) + len("\n }'")]
|
|
return program.replace('"${VPC_ROUTE_FILE:-}"', '"$VPC_ROUTE_FILE"')
|
|
|
|
|
|
@pytest.mark.parametrize("device_field", [" dev wg-fabric0", ""])
|
|
def test_observe_route_awk_runs_begin_outside_record_action(tmp_path, device_field):
|
|
program = _observe_route_program()
|
|
vpc_file = tmp_path / "routes"
|
|
vpc_file.write_text("10.1.0.3/32|wg-fabric0|100|100.64.1.2|100\n",
|
|
encoding="utf-8")
|
|
routes = ("100.64.1.2 dev wg-fabric0 scope link\n" + OWN_ROUTE
|
|
+ "10.1.0.3 via 100.64.1.2 dev wg-fabric0 table 100 proto 186 metric 100\n").replace(
|
|
" dev wg-fabric0", device_field)
|
|
result = subprocess.run(["/bin/sh", "-eu", "-c", program], input=routes,
|
|
capture_output=True, text=True, check=False,
|
|
env={"VPC_ROUTE_FILE": str(vpc_file), "VPC_STATE_STATUS": "valid"})
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout.splitlines() == [
|
|
"__VC_FABRIC_ROUTE__=100.64.1.2/32|main|wg-fabric0",
|
|
"__VC_FABRIC_ROUTE__=100.64.1.1/32|local|wg-fabric0",
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("device_field", [" dev br0", " dev", " dev br0 dev wg-fabric0"])
|
|
def test_observe_explicit_device_drift_remains_visible_to_readiness(device_field):
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", _observe_route_program()],
|
|
input="100.64.1.2" + device_field + "\n", capture_output=True,
|
|
text=True, check=False, env={"VPC_ROUTE_FILE": "/dev/null"},
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout.strip() == (
|
|
"__VC_FABRIC_ROUTE__=100.64.1.2/32|main|__invalid_device__")
|
|
|
|
|
|
@pytest.mark.parametrize("route", [
|
|
"blackhole 100.64.1.2",
|
|
"unreachable 100.64.1.2",
|
|
"prohibit 100.64.1.2",
|
|
"throw 100.64.1.2",
|
|
"100.64.1.2 nhid 123",
|
|
"100.64.1.2 nexthop dev wg-fabric0 weight 1",
|
|
"100.64.1.2 encap mpls 100 dev wg-fabric0",
|
|
])
|
|
def test_observe_no_oif_route_forms_cannot_bypass_readiness(route):
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", _observe_route_program()],
|
|
input=route + "\n", capture_output=True, text=True, check=False,
|
|
env={"VPC_ROUTE_FILE": "/dev/null"},
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout.strip() == (
|
|
"__VC_FABRIC_ROUTE__=100.64.1.2/32|main|__invalid_device__")
|
|
|
|
|
|
@pytest.mark.parametrize("variant", ["apply_seen", "boot_seen"])
|
|
@pytest.mark.parametrize("device_field", [" dev br0", " dev", " dev br0 dev wg-fabric0"])
|
|
def test_positive_filtered_readback_rejects_explicit_device_drift(variant, device_field):
|
|
assert _run_route(variant, "100.64.1.2" + device_field + "\n") == ""
|
|
|
|
|
|
def test_unfiltered_exact_and_overlap_parsers_do_not_infer_device():
|
|
boot_start = _BOOT_HELPER.index(
|
|
'route_conflict=$(printf \'%s\\n\' "$existing_route" | awk')
|
|
exact = _assignment(_BOOT_HELPER, "route_conflict", boot_start)
|
|
assert "filtered_route_device" not in exact
|
|
cases = [("100.64.1.2 scope link\n", "foreign"),
|
|
("100.64.1.2 dev br0 scope link\n", "foreign"),
|
|
("100.64.1.2 dev wg-fabric0 scope link\n", "")]
|
|
for routes, expected in cases:
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", exact + '\nprintf "%s" "$route_conflict"'],
|
|
env={"existing_route": routes, "IFACE": "wg-fabric0"},
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout == expected
|
|
|
|
apply = _render_apply()
|
|
start = apply.index("route_overlaps_target() {")
|
|
end = apply.index("\nfor route in $MANAGED_ROUTES", start)
|
|
overlap = apply[start:end]
|
|
assert "filtered_route_device" not in overlap
|
|
capture = 'mock_ip() { printf "%s\\n" "$VC_ROUTES"; }; IP=mock_ip\n'
|
|
for routes, expected in cases:
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", capture + overlap + "\nroute_overlaps_target 100.64.1.2/32"],
|
|
env={"VC_ROUTES": routes, "IFACE": "wg-fabric0",
|
|
"OLD_MANAGED_ROUTES": "100.64.1.2/32"},
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout.strip() == ("conflict" if expected else "")
|
|
|
|
|
|
@pytest.mark.parametrize("route,expected", [
|
|
("10.1.0.3 table 100 proto 186 metric 100", 0),
|
|
("10.1.0.3 dev vcv-01234567890 table 100 proto 186 metric 100", 0),
|
|
("10.1.0.3 dev br0 table 100 proto 186 metric 100", 1),
|
|
("10.1.0.3 table 101 proto 186 metric 100", 1),
|
|
("10.1.0.3 table 100 proto static metric 100", 1),
|
|
("blackhole 10.1.0.3 table 100 proto 186 metric 100", 1),
|
|
("10.1.0.3 nhid 123 table 100 proto 186 metric 100", 1),
|
|
])
|
|
def test_boot_vpc_bridge_filtered_inventory_keeps_table_proto_device_fences(route, expected):
|
|
start = _BOOT_HELPER.index('printf \'%s\\n\' "$vpc_bridge_routes" | awk')
|
|
end = _BOOT_HELPER.index("' || exit 71", start) + 1
|
|
program = _BOOT_HELPER[start:end]
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", program],
|
|
env={"vpc_bridge_routes": route, "vpc_table": "100",
|
|
"vpc_bridge": "vcv-01234567890"},
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert result.returncode == expected, result.stderr
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["ifupdown", "networkmanager_nmcli"])
|
|
def test_apply_and_boot_replace_the_same_route_priority_as_selected_manager(mode):
|
|
script = _render_apply(mode)
|
|
generated = re.findall(r"^\$IP route replace [^\n]+$", script, re.MULTILINE)
|
|
assert len(generated) == 1
|
|
# Inspect the rendered keyfile rather than invent a second expected metric.
|
|
keyfile_metric = re.search(r"^route-metric=([0-9]+)$", script, re.MULTILINE)[1]
|
|
metric = keyfile_metric if mode == "networkmanager_nmcli" else "0"
|
|
assert (" metric " + keyfile_metric in generated[0]) == (
|
|
mode == "networkmanager_nmcli")
|
|
|
|
boot_start = _BOOT_HELPER.index(
|
|
"while IFS='|' read -r kind a b c d; do\n"
|
|
" [ \"$kind\" = route ] || continue\n",
|
|
_BOOT_HELPER.index('"$WG" set "$IFACE" listen-port "$listen"'),
|
|
)
|
|
boot_end = _BOOT_HELPER.index("done < \"$CONF\"", boot_start) + len('done < "$CONF"')
|
|
# Run actual shell commands with an argv-only IP double. This test has no
|
|
# route mutation privileges; it verifies both sources address the same
|
|
# kernel route key, independently of optional host integration tests.
|
|
capture = 'mock_ip() { printf "%s\\n" "$*"; }; IP=mock_ip\n'
|
|
apply = subprocess.run(["/bin/sh", "-eu", "-c", capture + generated[0]],
|
|
capture_output=True, text=True,
|
|
env={"IFACE": "wg-fabric0"}, check=False)
|
|
boot = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", capture + _BOOT_HELPER[boot_start:boot_end]],
|
|
input="route|100.64.1.2/32\n", capture_output=True, text=True,
|
|
env={"IFACE": "wg-fabric0", "DEPLOYMENT_MODE": mode, "CONF": "/dev/stdin"},
|
|
check=False,
|
|
)
|
|
assert apply.returncode == boot.returncode == 0
|
|
assert apply.stdout == boot.stdout
|
|
args = apply.stdout.split()
|
|
replacement_metric = args[args.index("metric") + 1] if "metric" in args else "0"
|
|
assert replacement_metric == metric
|
|
# The documented kernel key includes priority. Existing NM priority50 or
|
|
# ifupdown priority0 gets replaced, not duplicated by a different key.
|
|
route_keys = {("100.64.1.2/32", "main", metric)}
|
|
route_keys.add((args[2], args[4], replacement_metric))
|
|
assert len(route_keys) == 1
|