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.
1962 lines
74 KiB
Python
1962 lines
74 KiB
Python
"""Firewalld/nft ownership contracts for the VPC host transaction."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.vpc import VPC_SCHEMA_VERSION
|
|
from virsh_panel.fabric import _NMCLI_FILENAME_RESOLVER_SH, _nft_rules_text
|
|
from virsh_panel.vpc_dataplane import (
|
|
FIREWALL_MODE_FIREWALLD,
|
|
FIREWALL_MODE_NATIVE,
|
|
FIREWALL_ZONE_OWNERSHIP_SH,
|
|
FIREWALL_ZONE_VALIDATION_SH,
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
NFT_MANIFEST_VALIDATION_SH,
|
|
VpcDataplaneError,
|
|
VpcPrivateHostAdapter,
|
|
parse_vpc_dataplane_plan,
|
|
render_apply_script,
|
|
render_observe_script,
|
|
render_remove_script,
|
|
)
|
|
|
|
|
|
WG_KEY = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
|
|
FABRIC_DIGEST = "cd" * 32
|
|
|
|
|
|
def _fabric_guard_functions(script: str) -> str:
|
|
start = script.index("vc_fabric_nft_strict_shape() {")
|
|
end = script.index('[ -n "$IP" ]', start)
|
|
return script[start:end]
|
|
|
|
|
|
def _vpc_state_loader_functions(script: str) -> str:
|
|
start = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"')
|
|
end = script.index("vpc_state_load || exit 74", start)
|
|
return script[start:end]
|
|
|
|
|
|
def _plan():
|
|
return parse_vpc_dataplane_plan({
|
|
"schema_version": VPC_SCHEMA_VERSION,
|
|
"vpc_id": "vpc-firewalld",
|
|
"region": "hk",
|
|
"fabric_id": "fabric-hk",
|
|
"fabric_revision": 1,
|
|
"cidr": "10.44.0.0/24",
|
|
"desired_revision": 1,
|
|
"operation_id": "vpc-firewalld-op-0001",
|
|
"subnets": [{"subnet_id": "subnet-a", "cidr": "10.44.0.0/25"}],
|
|
"hosts": [
|
|
{"host_id": "host-a", "members": [{
|
|
"member_id": "member-a",
|
|
"instance_uuid": "11111111-1111-4111-8111-111111111111",
|
|
"private_ip_cidr": "10.44.0.10/32",
|
|
"subnet_id": "subnet-a",
|
|
}]},
|
|
{"host_id": "host-b", "members": [{
|
|
"member_id": "member-b",
|
|
"instance_uuid": "22222222-2222-4222-8222-222222222222",
|
|
"private_ip_cidr": "10.44.0.11/32",
|
|
"subnet_id": "subnet-a",
|
|
}]},
|
|
],
|
|
"routes": [],
|
|
"acls": [],
|
|
"fabric_peers": [{
|
|
"host_id": "host-b",
|
|
"public_key": WG_KEY,
|
|
"overlay_ip": "100.64.0.2/32",
|
|
"tenant_allowed_ips": ["10.44.0.11/32"],
|
|
}],
|
|
}, "host-a", fabric_plan_digest=FABRIC_DIGEST)
|
|
|
|
|
|
def _empty_local_plan():
|
|
plan = _plan()
|
|
return replace(
|
|
plan,
|
|
members=tuple(member for member in plan.members
|
|
if member.host_id != plan.host_id),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("firewall_mode", [
|
|
FIREWALL_MODE_NATIVE,
|
|
FIREWALL_MODE_FIREWALLD,
|
|
])
|
|
def test_apply_refuses_a_host_without_local_members(firewall_mode):
|
|
with pytest.raises(VpcDataplaneError) as rejected:
|
|
render_apply_script(
|
|
_empty_local_plan(), firewall_mode=firewall_mode)
|
|
assert rejected.value.status == 409
|
|
assert rejected.value.error_code == "vpc_apply_no_local_members"
|
|
|
|
|
|
@pytest.mark.parametrize("deployment_mode", ["networkmanager_nmcli", "ifupdown"])
|
|
def test_firewalld_scripts_are_posix_and_never_reload_global_rules(deployment_mode):
|
|
plan = _plan()
|
|
scripts = (
|
|
render_apply_script(
|
|
plan, deployment_mode=deployment_mode,
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD),
|
|
render_observe_script(plan, firewall_mode=FIREWALL_MODE_FIREWALLD),
|
|
render_remove_script(
|
|
_empty_local_plan(), deployment_mode=deployment_mode,
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD),
|
|
)
|
|
for script in scripts:
|
|
result = subprocess.run(
|
|
["/bin/sh", "-n"], input=script, text=True,
|
|
capture_output=True, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert "FIREWALL_MODE=firewalld_nft" in script
|
|
assert "/etc/virt-cloud/nftables.conf" in script
|
|
assert "# virt-cloud-nft-manifest-v1" in script
|
|
assert "--reload" not in script
|
|
assert "--runtime-to-permanent" not in script
|
|
assert '--permanent --zone=trusted --add-interface=' not in script
|
|
assert '--permanent --zone=trusted --remove-interface=' not in script
|
|
|
|
for script in (scripts[0], scripts[2]):
|
|
assert "vc_firewall_mode_ready || exit 71" in script
|
|
assert "systemctl" in script
|
|
assert "FirewallBackend" in script
|
|
assert "NftablesTableOwner" in script
|
|
assert "vc_firewall_mode_ready || firewall_mode_state=mismatch" in scripts[1]
|
|
assert "__VC_VPC_FIREWALL_STATE__=$firewall_mode_state" in scripts[1]
|
|
|
|
|
|
@pytest.mark.parametrize("transaction", ["apply", "remove"])
|
|
def test_vpc_transactions_strictly_bind_fabric_digest_and_tenant_set(
|
|
tmp_path, transaction):
|
|
plan = _plan()
|
|
script = (
|
|
render_apply_script(
|
|
plan, deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
if transaction == "apply" else
|
|
render_remove_script(
|
|
_empty_local_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
)
|
|
guard = _fabric_guard_functions(script)
|
|
loader = _vpc_state_loader_functions(script)
|
|
wrapper_only = guard[guard.index("nft_plan_owned() {"):]
|
|
expected = tmp_path / f"{transaction}.expected"
|
|
live_table = tmp_path / f"{transaction}.table"
|
|
live_set = tmp_path / f"{transaction}.set"
|
|
txn = tmp_path / f"{transaction}.txn"
|
|
txn.mkdir()
|
|
expected.write_text("10.44.0.10/32\n10.44.0.11/32\n")
|
|
|
|
def table_with(elements, *, digest=FABRIC_DIGEST):
|
|
values = ", ".join(elements)
|
|
return _nft_rules_text(digest).replace(
|
|
" flags interval;",
|
|
" flags interval;\n"
|
|
f" elements = {{ {values} }}",
|
|
1,
|
|
)
|
|
|
|
def set_with(elements):
|
|
values = ", ".join(elements)
|
|
return (
|
|
"table inet virt_cloud_wg_fabric {\n"
|
|
" set vpc_tenant_ips {\n"
|
|
" type ipv4_addr\n"
|
|
" flags interval\n"
|
|
f" elements = {{ {values} }}\n"
|
|
" }\n"
|
|
"}\n"
|
|
)
|
|
|
|
fake_nft = r'''
|
|
vc_test_nft() {
|
|
if [ "$*" = "list set inet virt_cloud_wg_fabric vpc_tenant_ips" ]; then
|
|
cat "$LIVE_SET"
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
NFT=vc_test_nft
|
|
'''.strip()
|
|
|
|
def run(table_text, set_text, *, digest=FABRIC_DIGEST,
|
|
include_validator=True):
|
|
live_table.write_text(table_text)
|
|
live_set.write_text(set_text)
|
|
functions = guard if include_validator else wrapper_only
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
f"EXPECTED_DIGEST={digest}",
|
|
f"TXN={txn}",
|
|
f"LIVE_TABLE={live_table}",
|
|
f"LIVE_SET={live_set}",
|
|
f"EXPECTED={expected}",
|
|
"STAT=/usr/bin/stat",
|
|
fake_nft,
|
|
functions,
|
|
loader,
|
|
'VPC_TENANT_FILE="$EXPECTED"',
|
|
'nft_plan_owned "$(cat "$LIVE_TABLE")" "$EXPECTED_DIGEST"',
|
|
"vpc_state_nft_set_matches",
|
|
))
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
|
|
exact = ["10.44.0.10/32", "10.44.0.11/32"]
|
|
accepted = run(table_with(exact), set_with(exact))
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
|
|
# The validator itself is a required part of the generated transaction;
|
|
# a missing definition must never degrade to comment-count ownership.
|
|
assert run(
|
|
table_with(exact), set_with(exact),
|
|
include_validator=False,
|
|
).returncode != 0
|
|
assert run(table_with(exact), set_with(exact), digest="").returncode != 0
|
|
assert run(
|
|
table_with(exact, digest="ef" * 32), set_with(exact),
|
|
).returncode != 0
|
|
assert run(
|
|
table_with(exact).replace("counter drop", "counter accept", 1),
|
|
set_with(exact),
|
|
).returncode != 0
|
|
for tenant_elements in (
|
|
["10.44.0.10/32"],
|
|
[*exact, "10.44.0.12/32"],
|
|
["10.44.0.10-10.44.0.11"],
|
|
):
|
|
assert run(
|
|
table_with(exact), set_with(tenant_elements),
|
|
).returncode != 0
|
|
|
|
expected_assignment = f"EXPECTED_DIGEST={FABRIC_DIGEST}"
|
|
assert expected_assignment in script
|
|
strict_at = script.index("vc_fabric_nft_strict_shape() {")
|
|
owner_at = script.index("nft_plan_owned() {", strict_at)
|
|
loader_at = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"')
|
|
assert script.index(expected_assignment) < strict_at < owner_at < loader_at
|
|
refresh_at = script.rindex("vpc_state_refresh_nft || exit 74")
|
|
exact_at = script.index("vpc_state_nft_set_matches || exit 74", refresh_at)
|
|
snapshot_at = script.index(
|
|
"vc_nft_table_snapshot inet virt_cloud_wg_fabric", exact_at)
|
|
final_strict = script.index(
|
|
"vc_vpc_fabric_nft_live_ready || exit 74", snapshot_at)
|
|
final_exact = script.index(
|
|
"vpc_state_nft_set_matches || exit 74", final_strict)
|
|
final_snapshot = script.index(
|
|
"vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric",
|
|
final_exact)
|
|
commit_at = script.rindex("COMMITTED=yes") if transaction == "apply" \
|
|
else script.rindex("set_phase committed")
|
|
assert refresh_at < exact_at < snapshot_at < final_strict
|
|
assert final_strict < final_exact < final_snapshot < commit_at
|
|
last_strict = script.rindex(
|
|
"vc_vpc_fabric_nft_live_ready || exit 74", 0, commit_at)
|
|
last_exact = script.index(
|
|
"vpc_state_nft_set_matches || exit 74", last_strict)
|
|
last_snapshot = script.index(
|
|
"vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric",
|
|
last_exact)
|
|
assert last_strict < last_exact < last_snapshot < commit_at
|
|
if transaction == "apply":
|
|
restore_trusted = script.rindex("connection.zone trusted")
|
|
assert last_snapshot < restore_trusted < commit_at
|
|
|
|
|
|
def test_vpc_observe_strictly_binds_fabric_digest_semantics_and_tenant_set(
|
|
tmp_path):
|
|
script = render_observe_script(
|
|
_plan(), firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
snapshot_start = script.index("vc_nft_table_absent() {")
|
|
snapshot_end = script.index(
|
|
"vc_firewalld_interface_zone() {", snapshot_start)
|
|
snapshot_functions = script[snapshot_start:snapshot_end]
|
|
guard_start = script.index("vc_fabric_nft_strict_shape() {")
|
|
loader_start = script.index(
|
|
'VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"', guard_start)
|
|
guard_functions = script[guard_start:loader_start]
|
|
loader_end = script.index("FABRIC_NFT_SNAPSHOT=", loader_start)
|
|
loader_functions = script[loader_start:loader_end]
|
|
observe_start = script.index("vc_vpc_fabric_observe_ready() {")
|
|
observe_end = script.index("firewall_mode_state=owned", observe_start)
|
|
observe_function = script[observe_start:observe_end]
|
|
|
|
live_table = tmp_path / "observe.table"
|
|
live_set = tmp_path / "observe.set"
|
|
expected = tmp_path / "observe.expected"
|
|
snapshot = tmp_path / "observe.snapshot"
|
|
expected.write_text("10.44.0.10/32\n10.44.0.11/32\n")
|
|
|
|
def table_with(elements, *, digest=FABRIC_DIGEST):
|
|
values = ", ".join(elements)
|
|
return _nft_rules_text(digest).replace(
|
|
" flags interval;",
|
|
" flags interval;\n"
|
|
f" elements = {{ {values} }}",
|
|
1,
|
|
)
|
|
|
|
def set_with(elements):
|
|
values = ", ".join(elements)
|
|
return (
|
|
"table inet virt_cloud_wg_fabric {\n"
|
|
" set vpc_tenant_ips {\n"
|
|
" type ipv4_addr\n"
|
|
" flags interval\n"
|
|
f" elements = {{ {values} }}\n"
|
|
" }\n"
|
|
"}\n"
|
|
)
|
|
|
|
fake_nft = r'''
|
|
vc_test_nft() {
|
|
if [ "$1" = -s ] && [ "$2" = list ] && [ "$3" = table ]; then
|
|
cat "$LIVE_TABLE"
|
|
return 0
|
|
fi
|
|
if [ "$1" = list ] && [ "$2" = set ]; then
|
|
cat "$LIVE_SET"
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
NFT=vc_test_nft
|
|
'''.strip()
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"CMP=/usr/bin/cmp",
|
|
"STAT=/usr/bin/stat",
|
|
f"EXPECTED_DIGEST={FABRIC_DIGEST}",
|
|
fake_nft,
|
|
snapshot_functions,
|
|
guard_functions,
|
|
loader_functions,
|
|
'VPC_TENANT_FILE="$EXPECTED"',
|
|
'FABRIC_NFT_SNAPSHOT="$SNAPSHOT"',
|
|
"vpc_state_load() { :; }",
|
|
observe_function,
|
|
"vc_vpc_fabric_observe_ready",
|
|
))
|
|
|
|
def run(table_text, set_text):
|
|
live_table.write_text(table_text)
|
|
live_set.write_text(set_text)
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, LIVE_TABLE=str(live_table),
|
|
LIVE_SET=str(live_set), EXPECTED=str(expected),
|
|
SNAPSHOT=str(snapshot)),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
exact = ["10.44.0.10/32", "10.44.0.11/32"]
|
|
accepted = run(table_with(exact), set_with(exact))
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
assert run(
|
|
table_with(exact, digest="ef" * 32), set_with(exact),
|
|
).returncode != 0
|
|
assert run(
|
|
table_with(exact).replace("counter drop", "counter accept", 1),
|
|
set_with(exact),
|
|
).returncode != 0
|
|
for tenant_elements in (
|
|
["10.44.0.10/32"],
|
|
[*exact, "10.44.0.12/32"],
|
|
["10.44.0.10-10.44.0.11"],
|
|
):
|
|
assert run(
|
|
table_with(tenant_elements), set_with(tenant_elements),
|
|
).returncode != 0
|
|
|
|
expected_assignment = f"EXPECTED_DIGEST={FABRIC_DIGEST}"
|
|
strict_at = script.index("vc_fabric_nft_strict_shape() {")
|
|
owner_at = script.index("nft_plan_owned() {", strict_at)
|
|
loader_at = script.index(
|
|
'VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"', owner_at)
|
|
ready_at = script.index("vc_vpc_fabric_observe_ready() {", loader_at)
|
|
call_at = script.rindex("if vc_vpc_fabric_observe_ready; then")
|
|
marker_at = script.index("__VC_VPC_FABRIC_NFT__", call_at)
|
|
assert script.index(expected_assignment) < strict_at < owner_at
|
|
assert owner_at < loader_at < ready_at < call_at < marker_at
|
|
assert "VPC_STATE_OBSERVE=\"yes\"" in script
|
|
assert "trap 'vc_vpc_observe_cleanup' EXIT" in script
|
|
assert '"$VPC_TENANT_FILE.nft" "$FABRIC_NFT_SNAPSHOT"' in script
|
|
assert "vpc_state_nft_set_matches || return 1" in observe_function
|
|
assert ("vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric"
|
|
in observe_function)
|
|
|
|
|
|
def test_firewalld_apply_owns_only_vpc_bridge_zone_and_manifest_include():
|
|
plan = _plan()
|
|
script = render_apply_script(
|
|
plan, deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
include = f'include "/etc/nftables.d/virt-cloud-vpc-{plan.resources.token}.nft"'
|
|
assert include in script
|
|
assert 'NFT_MAIN="$NFT_MANIFEST"' in script
|
|
assert "virt-cloud-wg-fabric[.]nft|virt-cloud-vpc-" in script
|
|
assert 'vc_firewalld_bridge_zone_expect active managed "$BRIDGE"' in script
|
|
assert '--zone=trusted --add-interface="$BRIDGE"' in script
|
|
assert '--zone=trusted --change-interface="$BRIDGE"' in script
|
|
assert '--zone=trusted --remove-interface="$BRIDGE"' in script
|
|
assert '--permanent --zone=trusted --add-interface="$BRIDGE"' not in script
|
|
assert '--permanent --zone=trusted --remove-interface="$BRIDGE"' not in script
|
|
assert "--zone=trusted --add-interface=eth" not in script
|
|
assert "--zone=trusted --add-interface=wg-fabric0" not in script
|
|
assert f"\nauto {plan.resources.bridge}\n" not in script
|
|
assert 'ifcfg-$BRIDGE' in script
|
|
assert "firewall_mode|firewalld_nft" in script
|
|
assert "ZONE_OWNERSHIP=absent" in script
|
|
assert 'ZONE_OWNERSHIP=managed' in script
|
|
assert '[ -f "$OWNER_FILE" ] && [ ! -L "$OWNER_FILE" ] &&' in script
|
|
assert 'grep -Fqx "$OWNER_MARKER" "$OWNER_FILE" || exit 71' in script
|
|
assert '[ ! -e "$IF_FRAGMENT" ] && [ ! -L "$IF_FRAGMENT" ] || exit 71' in script
|
|
assert '"$IP" link show dev "$BRIDGE" >/dev/null 2>&1 && exit 71 || true' in script
|
|
assert 'vc_firewalld_bridge_zone_expect preapply "$ZONE_OWNERSHIP" "$BRIDGE" || exit 71' in script
|
|
assert script.index('vc_firewalld_bridge_zone_expect preapply "$ZONE_OWNERSHIP"') < script.index(
|
|
'mkdir -m 700 "$TXN"')
|
|
nft_ready = script.rindex("vc_vpc_desired_nft_ready || exit 72")
|
|
assert script.index('"$NFT" -f "$NFT_BATCH"') < nft_ready
|
|
assert nft_ready < script.rindex('VC_FABRIC_APPLYING=1 "$IFUP" --force "$BRIDGE"')
|
|
assert nft_ready < script.rindex('--zone=trusted --add-interface="$BRIDGE"')
|
|
trusted_add = script.rindex('--zone=trusted --add-interface="$BRIDGE"')
|
|
assert trusted_add < script.index(
|
|
'[ -e "/etc/sysconfig/network-scripts/ifcfg-$BRIDGE" ]', trusted_add)
|
|
|
|
|
|
def test_firewalld_networkmanager_profile_is_fail_closed_on_disk_and_temporary_in_memory():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
assert "connection.autoconnect no connection.autoconnect-retries 0" in script
|
|
assert "connection.autoconnect yes" not in script
|
|
assert "GENERAL.FILENAME" not in script
|
|
assert '-f UUID,FILENAME' in script
|
|
assert script.count("vc_nm_filename_for_uuid() {") == 1
|
|
assert 'zone_count != 0' in script
|
|
assert 'substr($0, 13) != expected_auto' in script
|
|
temporary = ('"$NMCLI" connection modify --temporary uuid '
|
|
'"$NM_PROFILE_UUID" connection.zone trusted')
|
|
assert temporary in script
|
|
assert 'vc_nm_profile_disk_validate "$NM_PROFILE_FILE" "$NM_PROFILE_UUID" || exit 72' in script
|
|
fabric_ready = script.rindex("vc_vpc_fabric_nft_live_ready || exit 74")
|
|
tenant_ready = script.index(
|
|
"vpc_state_nft_set_matches || exit 74", fabric_ready)
|
|
fabric_snapshot = script.index(
|
|
"vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric",
|
|
tenant_ready)
|
|
temporary_index = script.rindex("connection.zone trusted")
|
|
blocked_up = script.rindex('"$NMCLI" connection up uuid "$NM_PROFILE_UUID"')
|
|
assert fabric_ready < tenant_ready < fabric_snapshot < temporary_index
|
|
assert blocked_up < fabric_ready
|
|
assert temporary_index < script.rindex('"$NMCLI" device reapply "$BRIDGE"')
|
|
assert 'vc_nm_profile_expect active || exit 72' in script
|
|
|
|
|
|
def test_firewalld_remove_deletes_only_exact_owned_zone_and_manifest_entry():
|
|
script = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
assert 'state_firewall_record=$(awk' in script
|
|
assert '[ "$state_firewall_record" = "1|$FIREWALL_MODE" ] || exit 71' in script
|
|
assert '--zone=trusted --remove-interface="$BRIDGE"' in script
|
|
assert '--permanent --zone=trusted --remove-interface="$BRIDGE"' not in script
|
|
assert '--permanent --zone=trusted --add-interface="$BRIDGE"' not in script
|
|
assert 'vc_firewalld_bridge_zone_expect blocked managed "$BRIDGE"' in script
|
|
assert 'ifcfg-$BRIDGE' in script
|
|
assert "grep -Eq '^[[:space:]]*(auto|allow-" in script
|
|
assert "awk -v needle=\"$NFT_INCLUDE\" '$0 != needle'" in script
|
|
assert 'vc_nft_manifest_validate "$NFT_MAIN_TMP"' in script
|
|
assert 'grep -Fqx "$NFT_INCLUDE" "$NFT_MAIN" && exit 79' in script
|
|
nm_script = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
assert "GENERAL.FILENAME" not in nm_script
|
|
assert nm_script.count("vc_nm_filename_for_uuid() {") == 1
|
|
assert ('connection modify --temporary uuid "$NM_PROFILE_UUID" '
|
|
'connection.zone ""') in nm_script
|
|
assert '"$NMCLI" device reapply "$BRIDGE"' in nm_script
|
|
assert 'vc_firewalld_bridge_zone_expect blocked absent "$BRIDGE" || exit 72' in nm_script
|
|
assert (
|
|
'[ "$(vc_firewalld_interface_zone permanent "$BRIDGE")" = none ] '
|
|
'|| exit 79'
|
|
) in script
|
|
untrust = script.index('--zone=trusted --remove-interface="$BRIDGE" >/dev/null || exit 72')
|
|
first_live_compare = script.index(
|
|
"vc_vpc_persisted_snapshots_match || exit 71")
|
|
nft_delete = script.index('"$NFT" -f "$NFT_REMOVE_BATCH"')
|
|
assert untrust < first_live_compare < nft_delete
|
|
|
|
|
|
def test_firewalld_observe_proves_manifest_zone_state_and_raw_tables():
|
|
script = render_observe_script(
|
|
_plan(), firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
assert 'vc_nft_manifest_validate "$NFT_MANIFEST"' in script
|
|
assert 'grep -Fqx "$NFT_INCLUDE" "$NFT_MAIN"' in script
|
|
assert '__VC_VPC_FIREWALL_MODE__=$FIREWALL_MODE' in script
|
|
assert '__VC_VPC_FIREWALL_ZONE__=$firewall_zone' in script
|
|
assert '__VC_VPC_FIREWALL_STATE__=$firewall_mode_state' in script
|
|
assert '[ "$firewall_mode_state" = owned ]' in script
|
|
assert '[ "$permanent_zone" = none ]' in script
|
|
assert "GENERAL.FILENAME" not in script
|
|
assert '-f UUID,FILENAME' in script
|
|
assert script.count("vc_nm_filename_for_uuid() {") == 1
|
|
assert "zone_count != 0" in script
|
|
assert "vc_nm_profile_expect active" in script
|
|
assert 'ifcfg-$BRIDGE' in script
|
|
assert "__VC_VPC_STATE_FIREWALL_MODE__=" in script
|
|
assert "state_firewall_observed=invalid" in script
|
|
assert 'list table inet "$TABLE"' in script
|
|
assert 'list table bridge "$TABLE"' in script
|
|
assert 'list table arp "$TABLE"' in script
|
|
|
|
|
|
def test_firewall_mode_is_validated_and_native_state_stays_explicit():
|
|
native = render_apply_script(_plan(), firewall_mode=FIREWALL_MODE_NATIVE)
|
|
assert "FIREWALL_MODE=native_nftables" in native
|
|
assert "firewall_mode|native_nftables" in native
|
|
assert '[ ! -e "$NFT_MANIFEST" ] && [ ! -L "$NFT_MANIFEST" ] || exit 71' in native
|
|
assert "'0|'|'1|native_nftables'" in native
|
|
native_ifup = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
assert f"\nauto {_plan().resources.bridge}\n" not in native_ifup
|
|
nft_ready = native_ifup.rindex("vc_vpc_desired_nft_ready || exit 72")
|
|
explicit_up = native_ifup.rindex(
|
|
'VC_FABRIC_APPLYING=1 "$IFUP" --force "$BRIDGE"')
|
|
assert nft_ready < explicit_up
|
|
with pytest.raises(VpcDataplaneError) as invalid:
|
|
render_apply_script(_plan(), firewall_mode="firewalld")
|
|
assert invalid.value.error_code == "vpc_dataplane_firewall_mode_conflict"
|
|
|
|
|
|
def test_manifest_validator_rejects_foreign_lines_duplicates_and_bad_mode(tmp_path):
|
|
manifest = tmp_path / "nftables.conf"
|
|
script = "\n".join((
|
|
'vc_test_stat() { printf \'0:%s\\n\' "$VC_STAT_MODE"; }',
|
|
"STAT=vc_test_stat",
|
|
NFT_MANIFEST_VALIDATION_SH,
|
|
'vc_nft_manifest_validate "$MANIFEST"',
|
|
))
|
|
env = dict(os.environ, MANIFEST=str(manifest), VC_STAT_MODE="600")
|
|
|
|
manifest.write_text(
|
|
"# virt-cloud-nft-manifest-v1\n"
|
|
'include "/etc/nftables.d/virt-cloud-wg-fabric.nft"\n'
|
|
'include "/etc/nftables.d/virt-cloud-vpc-0123456789a.nft"\n'
|
|
)
|
|
manifest.chmod(0o600)
|
|
valid = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert valid.returncode == 0, valid.stderr
|
|
|
|
invalid_payloads = (
|
|
manifest.read_text() + 'include "/etc/nftables.d/operator.nft"\n',
|
|
manifest.read_text() +
|
|
'include "/etc/nftables.d/virt-cloud-vpc-0123456789a.nft"\n',
|
|
"# foreign-owner\n"
|
|
'include "/etc/nftables.d/virt-cloud-wg-fabric.nft"\n',
|
|
)
|
|
for payload in invalid_payloads:
|
|
manifest.write_text(payload)
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0
|
|
|
|
manifest.write_text(
|
|
"# virt-cloud-nft-manifest-v1\n"
|
|
'include "/etc/nftables.d/virt-cloud-wg-fabric.nft"\n'
|
|
)
|
|
env["VC_STAT_MODE"] = "644"
|
|
wrong_mode = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert wrong_mode.returncode != 0
|
|
|
|
|
|
def test_firewalld_zone_probe_normalizes_unbound_and_detects_foreign_zone():
|
|
script = "\n".join((
|
|
r'''
|
|
vc_test_firewall() {
|
|
permanent=no
|
|
[ "${1:-}" != --permanent ] || { permanent=yes; shift; }
|
|
case "$1" in
|
|
--get-zones) printf '%s\n' 'public trusted' ;;
|
|
--zone=public)
|
|
[ "$VC_ZONE" = public ] && return 0 || return 1 ;;
|
|
--zone=trusted)
|
|
[ "$VC_ZONE" = trusted ] && return 0 || return 1 ;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
FIREWALL_CMD=vc_test_firewall
|
|
'''.strip(),
|
|
FIREWALL_ZONE_VALIDATION_SH,
|
|
'vc_firewalld_interface_zone runtime vcv-test',
|
|
))
|
|
for configured, expected in (("", "none\n"), ("trusted", "trusted\n"),
|
|
("public", "public\n")):
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script],
|
|
env=dict(os.environ, VC_ZONE=configured),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout == expected
|
|
|
|
|
|
@pytest.mark.parametrize("zones", (
|
|
"",
|
|
"public bad.zone trusted",
|
|
"x" * 65,
|
|
" ".join(f"zone{index}" for index in range(65)),
|
|
))
|
|
def test_firewalld_zone_probe_rejects_invalid_or_unbounded_zone_lists(zones):
|
|
script = "\n".join((
|
|
r'''
|
|
vc_test_firewall() {
|
|
permanent=no
|
|
[ "${1:-}" != --permanent ] || { permanent=yes; shift; }
|
|
case "$1" in
|
|
--get-zones) printf '%s\n' "$VC_ZONES" ;;
|
|
--zone=*) return 1 ;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
FIREWALL_CMD=vc_test_firewall
|
|
'''.strip(),
|
|
FIREWALL_ZONE_VALIDATION_SH,
|
|
'vc_firewalld_interface_zone runtime vcv-test',
|
|
))
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script],
|
|
env=dict(os.environ, VC_ZONES=zones),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert result.returncode != 0
|
|
|
|
|
|
def test_firewalld_zone_ownership_rejects_first_apply_prebound_trusted():
|
|
script = "\n".join((
|
|
r'''
|
|
vc_test_firewall() {
|
|
permanent=no
|
|
[ "$1" != --permanent ] || { permanent=yes; shift; }
|
|
case "$1" in
|
|
--get-zones) printf '%s\n' 'public trusted' ;;
|
|
--get-default-zone) printf '%s\n' "${VC_DEFAULT_ZONE:-public}" ;;
|
|
--zone=public|--zone=trusted)
|
|
wanted=$(printf '%s\n' "$1" | sed 's/^--zone=//')
|
|
if [ "$permanent" = yes ]; then configured=$VC_PERMANENT_ZONE
|
|
else configured=$VC_RUNTIME_ZONE
|
|
fi
|
|
[ "$configured" = "$wanted" ] && return 0 || return 1
|
|
;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
FIREWALL_CMD=vc_test_firewall
|
|
'''.strip(),
|
|
FIREWALL_ZONE_VALIDATION_SH,
|
|
FIREWALL_ZONE_OWNERSHIP_SH,
|
|
'vc_firewalld_bridge_zone_expect "$VC_EXPECTED" "$VC_OWNER" vcv-test',
|
|
))
|
|
|
|
def run(expected, owner, runtime, permanent, default="public"):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script],
|
|
env=dict(
|
|
os.environ,
|
|
VC_EXPECTED=expected,
|
|
VC_OWNER=owner,
|
|
VC_RUNTIME_ZONE=runtime,
|
|
VC_PERMANENT_ZONE=permanent,
|
|
VC_DEFAULT_ZONE=default,
|
|
),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
assert run("blocked", "absent", "", "").returncode == 0
|
|
assert run("blocked", "managed", "public", "").returncode != 0
|
|
assert run("preapply", "absent", "public", "").returncode != 0
|
|
assert run("nm-blocked", "managed-nm", "public", "").returncode == 0
|
|
assert run("preapply", "managed", "public", "").returncode != 0
|
|
assert run("preapply", "managed-nm", "public", "").returncode == 0
|
|
assert run("preapply", "absent", "trusted", "").returncode != 0
|
|
assert run("preapply", "managed", "trusted", "").returncode == 0
|
|
assert run("active", "managed", "trusted", "").returncode == 0
|
|
assert run("active", "managed", "trusted", "trusted").returncode != 0
|
|
assert run("blocked", "managed", "", "", default="trusted").returncode != 0
|
|
|
|
|
|
def test_networkmanager_default_zone_quarantine_requires_active_owned_profile(tmp_path):
|
|
profile = tmp_path / "vcv-test.nmconnection"
|
|
profile.write_text(
|
|
"[connection]\n"
|
|
"id=vcv-test\n"
|
|
"uuid=11111111-1111-4111-8111-111111111111\n"
|
|
"type=bridge\n"
|
|
"interface-name=vcv-test\n"
|
|
"stable-id=virt-cloud-vpc-dataplane-v1 vpc=vpc-test\n"
|
|
"autoconnect=false\n"
|
|
"autoconnect-retries=0\n"
|
|
"\n[ipv4]\nmethod=disabled\n",
|
|
)
|
|
profile.chmod(0o600)
|
|
helper = NETWORKMANAGER_VPC_PROFILE_SH.replace(
|
|
"/etc/NetworkManager/system-connections/", f"{tmp_path}/")
|
|
script = "\n".join((
|
|
r'''
|
|
vc_test_stat() { printf '%s\n' '0:600'; }
|
|
vc_test_nmcli() {
|
|
field=; fields=; active_query=no
|
|
while [ "$#" -gt 0 ]; do
|
|
case "$1" in
|
|
-g) shift; field=$1 ;;
|
|
-f) shift; fields=$1 ;;
|
|
--active) active_query=yes ;;
|
|
esac
|
|
shift
|
|
done
|
|
if [ "$fields:$active_query" = UUID:yes ]; then
|
|
[ "$VC_ACTIVE" = yes ] && printf '%s\n' "$NM_PROFILE_UUID"
|
|
return 0
|
|
fi
|
|
case "$field" in
|
|
connection.type) printf '%s\n' bridge ;;
|
|
connection.interface-name) printf '%s\n' "$BRIDGE" ;;
|
|
connection.stable-id) printf '%s\n' "$NM_PROFILE_MARKER" ;;
|
|
connection.autoconnect) printf '%s\n' no ;;
|
|
connection.autoconnect-retries) printf '%s\n' 0 ;;
|
|
connection.zone) printf '%s\n' "$VC_MEMORY_ZONE" ;;
|
|
ipv4.method|ipv6.method) printf '%s\n' disabled ;;
|
|
bridge.stp) printf '%s\n' no ;;
|
|
*) printf '%s\n' -- ;;
|
|
esac
|
|
}
|
|
vc_test_firewall() {
|
|
permanent=no
|
|
[ "$1" != --permanent ] || { permanent=yes; shift; }
|
|
case "$1" in
|
|
--get-zones) printf '%s\n' 'public trusted' ;;
|
|
--get-default-zone) printf '%s\n' public ;;
|
|
--zone=public|--zone=trusted)
|
|
wanted=${1#--zone=}
|
|
if [ "$permanent" = yes ]; then configured=
|
|
else configured=public
|
|
fi
|
|
[ "$configured" = "$wanted" ] && return 0 || return 1
|
|
;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
STAT=vc_test_stat
|
|
NMCLI=vc_test_nmcli
|
|
FIREWALL_CMD=vc_test_firewall
|
|
BRIDGE=vcv-test
|
|
NM_PROFILE_UUID=11111111-1111-4111-8111-111111111111
|
|
NM_PROFILE_MARKER='virt-cloud-vpc-dataplane-v1 vpc=vpc-test'
|
|
'''.strip(),
|
|
FIREWALL_ZONE_VALIDATION_SH,
|
|
FIREWALL_ZONE_OWNERSHIP_SH,
|
|
helper,
|
|
'NM_PROFILE_FILE=$PROFILE',
|
|
'vc_nm_profile_expect blocked-active',
|
|
))
|
|
base_env = dict(os.environ, PROFILE=str(profile), VC_MEMORY_ZONE="")
|
|
accepted = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script],
|
|
env=dict(base_env, VC_ACTIVE="yes"),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
inactive = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script],
|
|
env=dict(base_env, VC_ACTIVE="no"),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert inactive.returncode != 0
|
|
trusted_memory = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script],
|
|
env=dict(base_env, VC_ACTIVE="yes", VC_MEMORY_ZONE="trusted"),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert trusted_memory.returncode != 0
|
|
|
|
|
|
def test_networkmanager_disk_profile_requires_manual_boot_and_no_zone(tmp_path):
|
|
profile = tmp_path / "vcv-test.nmconnection"
|
|
profile.write_text(
|
|
"[connection]\n"
|
|
"id=vcv-test\n"
|
|
"uuid=11111111-1111-4111-8111-111111111111\n"
|
|
"type=bridge\n"
|
|
"interface-name=vcv-test\n"
|
|
"stable-id=virt-cloud-vpc-dataplane-v1 vpc=vpc-test\n"
|
|
"autoconnect=false\n"
|
|
"autoconnect-retries=0\n"
|
|
"\n[ipv4]\nmethod=disabled\n"
|
|
)
|
|
profile.chmod(0o600)
|
|
helper = NETWORKMANAGER_VPC_PROFILE_SH.replace(
|
|
"/etc/NetworkManager/system-connections/", f"{tmp_path}/")
|
|
script = "\n".join((
|
|
"vc_test_stat() { printf '%s\\n' '0:600'; }",
|
|
"STAT=vc_test_stat",
|
|
"BRIDGE=vcv-test",
|
|
"NM_PROFILE_MARKER='virt-cloud-vpc-dataplane-v1 vpc=vpc-test'",
|
|
helper,
|
|
'vc_nm_profile_disk_validate "$PROFILE" "$UUID"',
|
|
))
|
|
env = dict(
|
|
os.environ,
|
|
PROFILE=str(profile),
|
|
UUID="11111111-1111-4111-8111-111111111111",
|
|
)
|
|
|
|
accepted = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
|
|
for unsafe in ("zone=trusted\n", "autoconnect=true\n"):
|
|
content = profile.read_text()
|
|
if unsafe.startswith("autoconnect="):
|
|
content = content.replace("autoconnect=false\n", unsafe)
|
|
else:
|
|
content = content.replace("autoconnect=false\n",
|
|
"autoconnect=false\n" + unsafe)
|
|
profile.write_text(content)
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0
|
|
profile.write_text(
|
|
profile.read_text().replace("zone=trusted\n", "")
|
|
.replace("autoconnect=true\n", "autoconnect=false\n")
|
|
)
|
|
|
|
|
|
def test_firewalld_rollback_quarantines_before_one_atomic_nft_restore():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
quarantine = ('connection modify --temporary uuid "$NM_PROFILE_UUID" '
|
|
'connection.zone ""')
|
|
assert quarantine in rollback
|
|
assert rollback.index(quarantine) < rollback.index(
|
|
"while IFS='|' read -r destination device table via metric")
|
|
assert rollback.index(quarantine) < rollback.index(
|
|
'NFT_ROLLBACK="$TXN/rollback.nft"')
|
|
assert ("printf 'delete table inet virt_cloud_wg_fabric\\n' >> "
|
|
'"$NFT_ROLLBACK"') in rollback
|
|
assert 'cat "$TXN/fabric.nft.before" >> "$NFT_ROLLBACK"' in rollback
|
|
assert '"$NFT" delete table inet virt_cloud_wg_fabric' not in rollback
|
|
assert rollback.count('"$NFT" -f "$NFT_ROLLBACK"') == 1
|
|
restore_trust = ('connection modify --temporary uuid "$NM_PROFILE_UUID" '
|
|
'connection.zone trusted')
|
|
assert rollback.index('vc_vpc_fabric_nft_live_ready') < rollback.index(
|
|
restore_trust)
|
|
restore_index = rollback.index(restore_trust)
|
|
fabric_exact = rollback.rfind(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric',
|
|
0, restore_index)
|
|
assert fabric_exact >= 0
|
|
assert '"$TXN/fabric.nft.before"' in rollback[fabric_exact:restore_index]
|
|
|
|
|
|
def test_fabric_snapshot_verdict_drift_blocks_exact_restore_guard(tmp_path):
|
|
before = tmp_path / "fabric.before"
|
|
live = tmp_path / "fabric.live"
|
|
before.write_text(
|
|
'table inet virt_cloud_wg_fabric {\n'
|
|
' chain forward {\n'
|
|
' iifname "wg-fabric0" drop comment '
|
|
'"virt-cloud-wg-fabric-v1:0123456789abcdef"\n'
|
|
' }\n}\n'
|
|
)
|
|
live.write_text(before.read_text().replace(" drop comment", " accept comment"))
|
|
shell = "\n".join((
|
|
r'''
|
|
vc_test_nft() {
|
|
[ "$1" = -s ] && [ "$2" = list ] && [ "$3" = table ] || return 2
|
|
cat "$LIVE"
|
|
}
|
|
NFT=vc_test_nft
|
|
CMP=/usr/bin/cmp
|
|
'''.strip(),
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric "$BEFORE"',
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", shell],
|
|
env=dict(os.environ, LIVE=str(live), BEFORE=str(before)),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0
|
|
|
|
|
|
def test_nft_stateless_snapshot_rejects_late_table_and_semantic_drift(tmp_path):
|
|
live = tmp_path / "live"
|
|
live.mkdir()
|
|
snapshot = tmp_path / "desired"
|
|
script = "\n".join((
|
|
r'''
|
|
vc_test_nft() {
|
|
[ "$1" = -s ] || return 2
|
|
shift
|
|
[ "$1" = list ] || return 2
|
|
if [ "$2" = tables ]; then
|
|
[ "${VC_INVENTORY_FAIL:-no}" = no ] || return 3
|
|
for live_path in "$LIVE/$3-"*; do
|
|
[ -f "$live_path" ] || continue
|
|
printf 'table %s %s\n' "$3" "${live_path##*-$3-}"
|
|
done
|
|
return 0
|
|
fi
|
|
[ "$2" = table ] || return 2
|
|
live_path="$LIVE/$3-$4"
|
|
[ -f "$live_path" ] || return 1
|
|
cat "$live_path"
|
|
}
|
|
NFT=vc_test_nft
|
|
CMP=/usr/bin/cmp
|
|
'''.strip(),
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
'"$@"',
|
|
))
|
|
|
|
def run(*args):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script, "vc-test", *args],
|
|
env=dict(os.environ, LIVE=str(live)), capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
|
|
# The pre-apply view was absent. A foreign same-name table appearing
|
|
# before rollback must not be mistaken for a transaction-owned table.
|
|
captured_absent = run(
|
|
"vc_nft_table_snapshot", "inet", "virt_cloud_vpc_test",
|
|
str(snapshot),
|
|
)
|
|
assert captured_absent.returncode == 0, captured_absent.stderr
|
|
assert (tmp_path / "desired.absent").is_file()
|
|
live_table = live / "inet-virt_cloud_vpc_test"
|
|
live_table.write_text(
|
|
'table inet virt_cloud_vpc_test {\n'
|
|
' chain forward { policy accept; }\n}\n',
|
|
)
|
|
late_table = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert late_table.returncode != 0
|
|
|
|
# A point lookup error alone is never proof of absence. If the complete
|
|
# family inventory cannot be read and parsed successfully, no absent
|
|
# marker may be created.
|
|
live_table.unlink()
|
|
ambiguous = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script, "vc-test",
|
|
"vc_nft_table_snapshot", "inet", "virt_cloud_vpc_test",
|
|
str(tmp_path / "ambiguous")],
|
|
env=dict(os.environ, LIVE=str(live), VC_INVENTORY_FAIL="yes"),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert ambiguous.returncode != 0
|
|
assert not (tmp_path / "ambiguous.absent").exists()
|
|
|
|
desired = (
|
|
'table inet virt_cloud_vpc_test {\n'
|
|
' chain forward {\n'
|
|
' ip saddr 10.44.0.10 drop comment '
|
|
'"virt-cloud-vpc-dataplane-v1:test:0123456789abcdef"\n'
|
|
' }\n}\n'
|
|
)
|
|
live_table.write_text(desired)
|
|
captured = run(
|
|
"vc_nft_table_snapshot", "inet", "virt_cloud_vpc_test",
|
|
str(snapshot),
|
|
)
|
|
assert captured.returncode == 0, captured.stderr
|
|
exact = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert exact.returncode == 0, exact.stderr
|
|
|
|
# Retaining the exact ownership comment is insufficient: changing the
|
|
# verdict or deleting an expression changes the complete canonical dump.
|
|
live_table.write_text(desired.replace(" drop comment", " accept comment"))
|
|
changed_verdict = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert changed_verdict.returncode != 0
|
|
live_table.write_text(desired.replace("ip saddr 10.44.0.10 ", ""))
|
|
removed_expression = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert removed_expression.returncode != 0
|
|
|
|
|
|
def test_apply_rollback_requires_exact_post_apply_tables_before_delete():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
apply_call = script.index('"$NFT" -f "$NFT_BATCH" >/dev/null || exit 72')
|
|
applied_flag = script.index("NFT_APPLIED=yes", apply_call)
|
|
assert apply_call < applied_flag
|
|
assert "NFT_APPLIED=no" in script
|
|
assert "FABRIC_NFT_APPLIED=no" in script
|
|
assert '"$NFT" -s list table "$vc_nft_snapshot_family"' in script
|
|
assert 'vc_vpc_nft_snapshots_equal "$TXN/nft.desired"' in script
|
|
assert 'nft_snapshot|inet|/etc/virt-cloud/vpc-dataplane/' in script
|
|
assert 'nft_snapshot|bridge|/etc/virt-cloud/vpc-dataplane/' in script
|
|
assert 'nft_snapshot|arp|/etc/virt-cloud/vpc-dataplane/' in script
|
|
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
exact_check = rollback.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.desired"')
|
|
delete_emit = rollback.index(
|
|
"printf 'delete table %s %s\\n' \"$rollback_family\" \"$TABLE\"")
|
|
assert exact_check < delete_emit
|
|
assert 'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric' in rollback
|
|
assert 'NFT_ROLLBACK_SAFE=no' in rollback
|
|
assert '[ "$NFT_ROLLBACK_SAFE" = yes ] || ROLLBACK_FAILED=yes' in rollback
|
|
assert 'if "$NFT" list table inet "$TABLE"' not in rollback
|
|
|
|
|
|
def test_networkmanager_rollback_restores_nontrusted_existing_profile_state():
|
|
for firewall_mode in (FIREWALL_MODE_NATIVE, FIREWALL_MODE_FIREWALLD):
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=firewall_mode)
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
restore = rollback.index(
|
|
'[ "$NM_PROFILE_WAS_PRESENT" = yes ]')
|
|
restored_nft = rollback.index(
|
|
'if [ "$FIREWALL_ZONE_BLOCKED" = yes ]; then')
|
|
release = rollback.index("vc_nm_device_release", restore)
|
|
activate = rollback.index(
|
|
'connection up uuid "$NM_PROFILE_UUID"', release)
|
|
topology = rollback.index(
|
|
"vc_vpc_bridge_topology_matches", activate)
|
|
assert restored_nft < restore < release < activate < topology
|
|
assert '[ "$NM_WAS_ACTIVE" = yes ]' in rollback[restore:topology]
|
|
assert "vc_nm_device_expect_managed yes" in rollback[restore:]
|
|
|
|
|
|
def test_rollback_keeps_journal_when_old_trusted_acl_is_not_proven_safe():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
unsafe = rollback.index('[ "$FIREWALL_PREVIOUS_NFT_SAFE" != yes ]')
|
|
mark_failed = rollback.index("ROLLBACK_FAILED=yes", unsafe)
|
|
journal = rollback.index(
|
|
"if [ \"$ROLLBACK_FAILED\" = yes ]; then printf '%s\\n' "
|
|
"rollback_failed", mark_failed)
|
|
assert unsafe < mark_failed < journal
|
|
|
|
|
|
def test_trusted_bridge_is_quarantined_before_old_acl_readback(tmp_path):
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
quarantine_comment = script.index(
|
|
"# Emergency quarantine deliberately runs before")
|
|
hard_down = script.index(
|
|
'"$IP" link set dev "$BRIDGE" down >/dev/null || exit 72',
|
|
quarantine_comment)
|
|
remove_trust = script.index(
|
|
'--zone=trusted --remove-interface="$BRIDGE"', quarantine_comment)
|
|
fabric_owner = script.index(
|
|
'[ -f /etc/virt-cloud/wg-fabric0.owner ]', remove_trust)
|
|
manifest_check = script.index(
|
|
'vc_nft_manifest_validate "$NFT_MANIFEST" || exit 71', remove_trust)
|
|
rpdb_check = script.index("vc_ip_rule_scope_check", remove_trust)
|
|
fabric_readback = script.index(
|
|
'if "$NFT" -s list table inet virt_cloud_wg_fabric', remove_trust)
|
|
old_acl_readback = script.index(
|
|
"vc_vpc_persisted_nft_ready || exit 71", remove_trust)
|
|
assert hard_down < remove_trust
|
|
assert remove_trust < fabric_owner < manifest_check < rpdb_check
|
|
assert remove_trust < fabric_readback
|
|
assert remove_trust < old_acl_readback
|
|
|
|
def test_networkmanager_quarantine_preserves_device_until_acl_is_blocked(tmp_path):
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
early = script.index("# Emergency quarantine deliberately runs before")
|
|
quarantine_call = script.index("vc_nm_device_quarantine || exit 72", early)
|
|
mode_guard = script.index("vc_firewall_mode_ready || exit 71", quarantine_call)
|
|
remove_trust = script.index(
|
|
'--zone=trusted --remove-interface="$BRIDGE"', mode_guard)
|
|
fabric_owner = script.index(
|
|
'[ -f /etc/virt-cloud/wg-fabric0.owner ]', remove_trust)
|
|
assert quarantine_call < mode_guard < remove_trust < fabric_owner
|
|
clear_zone = NETWORKMANAGER_VPC_PROFILE_SH.index(
|
|
'connection modify --temporary uuid "$NM_PROFILE_UUID"')
|
|
unmanaged = NETWORKMANAGER_VPC_PROFILE_SH.index(
|
|
'"$NMCLI" device set "$BRIDGE" managed no', clear_zone)
|
|
hard_down = NETWORKMANAGER_VPC_PROFILE_SH.index(
|
|
'"$IP" link set dev "$BRIDGE" down', unmanaged)
|
|
assert clear_zone < unmanaged < hard_down
|
|
assert 'connection down uuid "$NM_PROFILE_UUID"' not in script
|
|
assert 'device disconnect "$BRIDGE"' not in script
|
|
|
|
zone_state = tmp_path / "zone"
|
|
managed_state = tmp_path / "managed"
|
|
link_state = tmp_path / "link"
|
|
event_log = tmp_path / "events"
|
|
zone_state.write_text("trusted\n")
|
|
managed_state.write_text("yes\n")
|
|
link_state.write_text("up\n")
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
"NM_PROFILE_UUID=11111111-1111-4111-8111-111111111111",
|
|
"FIREWALL_MODE=firewalld_nft",
|
|
f"ZONE_STATE={zone_state}",
|
|
f"MANAGED_STATE={managed_state}",
|
|
f"LINK_STATE={link_state}",
|
|
f"EVENT_LOG={event_log}",
|
|
r'''
|
|
vc_test_ip() {
|
|
if [ "$1:$2:$3" = "link:show:dev" ]; then return 0; fi
|
|
if [ "$1:$2:$3:$4:$5" = "link:set:dev:vcv-test:down" ]; then
|
|
printf '%s\n' down > "$LINK_STATE"
|
|
printf '%s\n' down >> "$EVENT_LOG"
|
|
return 0
|
|
fi
|
|
if [ "$1:$2:$3" = "-o:link:show" ]; then
|
|
[ "$(cat "$LINK_STATE")" = up ] &&
|
|
printf '%s\n' '9: vcv-test: <BROADCAST,UP,LOWER_UP>' ||
|
|
printf '%s\n' '9: vcv-test: <BROADCAST>'
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
vc_test_nmcli() {
|
|
case "$*" in
|
|
"-t --escape no -g GENERAL.CON-UUID device show vcv-test")
|
|
printf '%s\n' "$NM_PROFILE_UUID" ;;
|
|
"-t --escape no -g connection.zone connection show uuid $NM_PROFILE_UUID")
|
|
cat "$ZONE_STATE" ;;
|
|
"-t --escape no -g GENERAL.NM-MANAGED device show vcv-test")
|
|
cat "$MANAGED_STATE" ;;
|
|
"connection modify --temporary uuid $NM_PROFILE_UUID connection.zone ")
|
|
: > "$ZONE_STATE"
|
|
printf '%s\n' clear-zone >> "$EVENT_LOG" ;;
|
|
"device set vcv-test managed no")
|
|
printf '%s\n' no > "$MANAGED_STATE"
|
|
printf '%s\n' unmanaged >> "$EVENT_LOG" ;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
IP=vc_test_ip
|
|
NMCLI=vc_test_nmcli
|
|
'''.strip(),
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
"vc_nm_device_quarantine",
|
|
"false # simulate a later ACL verification failure",
|
|
))
|
|
failed = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert failed.returncode != 0
|
|
assert zone_state.read_text() == ""
|
|
assert managed_state.read_text() == "no\n"
|
|
assert link_state.read_text() == "down\n"
|
|
assert event_log.read_text().splitlines() == [
|
|
"clear-zone", "unmanaged", "down",
|
|
]
|
|
|
|
|
|
def test_networkmanager_trusted_isolated_retry_requires_exact_state():
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
"NM_PROFILE_UUID=11111111-1111-4111-8111-111111111111",
|
|
"FIREWALL_MODE=firewalld_nft",
|
|
'BRIDGE_TOPOLOGY_CAPTURED="${VC_CAPTURED:-yes}"',
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
r'''
|
|
vc_nm_profile_base_validate() { [ "$VC_BASE_VALID" = yes ]; }
|
|
vc_nm_profile_field() {
|
|
[ "$1" = connection.zone ] || return 1
|
|
printf '%s\n' "$VC_MEMORY_ZONE"
|
|
}
|
|
vc_nm_device_expect_managed() {
|
|
[ "$1" = no ] && [ "$VC_MANAGED" = no ]
|
|
}
|
|
vc_vpc_bridge_topology_matches() { [ "$VC_TOPOLOGY" = same ]; }
|
|
vc_test_nmcli() {
|
|
case "$*" in
|
|
"-t --escape no -f UUID connection show --active")
|
|
[ "$VC_ACTIVE" = no ] || printf '%s\n' "$NM_PROFILE_UUID"
|
|
;;
|
|
"-t --escape no -g GENERAL.CON-UUID device show vcv-test")
|
|
[ "${VC_DEVICE_QUERY_FAIL:-no}" = no ] || return 3
|
|
printf '%s\n' "$VC_DEVICE_UUID"
|
|
;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
vc_test_ip() {
|
|
case "$*" in
|
|
"link show dev vcv-test") return 0 ;;
|
|
"-o link show dev vcv-test")
|
|
[ "${VC_LINK_QUERY_FAIL:-no}" = no ] || return 3
|
|
case "$VC_LINK" in
|
|
up) printf '%s\n' '9: vcv-test: <BROADCAST,UP,LOWER_UP>' ;;
|
|
down) printf '%s\n' '9: vcv-test: <BROADCAST>' ;;
|
|
empty) : ;;
|
|
malformed) printf '%s\n' malformed ;;
|
|
*) return 2 ;;
|
|
esac
|
|
;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
NMCLI=vc_test_nmcli
|
|
IP=vc_test_ip
|
|
vc_nm_trusted_isolated_retry_ready
|
|
'''.strip(),
|
|
))
|
|
exact = {
|
|
"VC_BASE_VALID": "yes",
|
|
"VC_MEMORY_ZONE": "trusted",
|
|
"VC_ACTIVE": "no",
|
|
"VC_MANAGED": "no",
|
|
"VC_DEVICE_UUID": "11111111-1111-4111-8111-111111111111",
|
|
"VC_LINK": "down",
|
|
"VC_TOPOLOGY": "same",
|
|
}
|
|
|
|
def run(**changes):
|
|
values = dict(exact)
|
|
values.update(changes)
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, **values), capture_output=True, text=True,
|
|
check=False,
|
|
)
|
|
|
|
accepted = run()
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
assert run(VC_BASE_VALID="no").returncode != 0
|
|
assert run(VC_MEMORY_ZONE="").returncode != 0
|
|
assert run(VC_ACTIVE="yes").returncode != 0
|
|
assert run(VC_MANAGED="yes").returncode != 0
|
|
assert run(
|
|
VC_DEVICE_UUID="22222222-2222-4222-8222-222222222222"
|
|
).returncode != 0
|
|
assert run(VC_LINK="up").returncode != 0
|
|
assert run(VC_DEVICE_QUERY_FAIL="yes").returncode != 0
|
|
assert run(VC_LINK_QUERY_FAIL="yes").returncode != 0
|
|
assert run(VC_LINK="empty").returncode != 0
|
|
assert run(VC_LINK="malformed").returncode != 0
|
|
assert run(VC_CAPTURED="no").returncode != 0
|
|
assert run(VC_TOPOLOGY="drifted").returncode != 0
|
|
|
|
|
|
def test_vpc_bridge_topology_parser_propagates_malformed_output():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
start = script.index("vc_vpc_bridge_port_names() {")
|
|
end = script.index("# Emergency quarantine deliberately runs before", start)
|
|
helpers = script[start:end]
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
r'''
|
|
vc_test_bridge() {
|
|
[ "$*" = "link show master vcv-test" ] || return 2
|
|
printf '%s\n' "$VC_BRIDGE_OUTPUT"
|
|
}
|
|
BRIDGE_CMD=vc_test_bridge
|
|
'''.strip(),
|
|
helpers,
|
|
"vc_vpc_bridge_port_names",
|
|
))
|
|
|
|
def run(output):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, VC_BRIDGE_OUTPUT=output),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
valid = run("2: tap0@if3: <BROADCAST> master vcv-test")
|
|
assert valid.returncode == 0, valid.stderr
|
|
assert valid.stdout == "tap0\n"
|
|
assert run("malformed").returncode != 0
|
|
assert run("2: ../../foreign: <BROADCAST> master vcv-test").returncode != 0
|
|
|
|
|
|
def test_vpc_bridge_link_state_requires_one_well_formed_record():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
start = script.index("vc_vpc_bridge_link_state() {")
|
|
end = script.index(
|
|
"vc_vpc_native_ifupdown_rollback_reopen() {", start)
|
|
helper = script[start:end]
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
r'''
|
|
vc_test_ip() {
|
|
[ "$*" = "-o link show dev vcv-test" ] || return 2
|
|
[ "${VC_QUERY_FAIL:-no}" = no ] || return 3
|
|
printf '%s' "$VC_LINK_OUTPUT"
|
|
}
|
|
IP=vc_test_ip
|
|
'''.strip(),
|
|
helper,
|
|
"vc_vpc_bridge_link_state",
|
|
))
|
|
|
|
def run(output, *, fail="no"):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, VC_LINK_OUTPUT=output, VC_QUERY_FAIL=fail),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
down = run("9: vcv-test: <BROADCAST>\n")
|
|
assert down.returncode == 0 and down.stdout == "down"
|
|
up = run("9: vcv-test: <BROADCAST,UP,LOWER_UP>\n")
|
|
assert up.returncode == 0 and up.stdout == "up"
|
|
assert run("").returncode != 0
|
|
assert run("malformed\n").returncode != 0
|
|
assert run("9: foreign: <BROADCAST>\n").returncode != 0
|
|
assert run("9: vcv-test: <BROADCAST>\n", fail="yes").returncode != 0
|
|
|
|
early = script.index("# Emergency quarantine deliberately runs before")
|
|
hard_down = script.index(
|
|
'"$IP" link set dev "$BRIDGE" down >/dev/null || exit 72', early)
|
|
exact_down = script.index(
|
|
"bridge_quarantine_link_state=$(vc_vpc_bridge_link_state)",
|
|
hard_down)
|
|
assert hard_down < exact_down
|
|
|
|
|
|
def test_networkmanager_trusted_isolated_retry_is_proven_before_zone_cleanup():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
state = script.index("state_revision=$(awk")
|
|
owner = script.index(
|
|
'grep -Fqx "$OWNER_MARKER" "$OWNER_FILE" || exit 71', state)
|
|
profile = script.index("vc_nm_profile_base_validate || exit 71", owner)
|
|
candidate = script.index("NM_TRUSTED_ISOLATED_CANDIDATE=yes", profile)
|
|
ports = script.index(
|
|
"BRIDGE_PORTS_BEFORE=$(vc_vpc_bridge_port_names) || exit 71",
|
|
candidate)
|
|
exact = script.index(
|
|
"vc_nm_trusted_isolated_retry_ready || exit 71", ports)
|
|
quarantine = script.index("vc_nm_device_quarantine || exit 72", exact)
|
|
mode_guard = script.index("vc_firewall_mode_ready || exit 71", quarantine)
|
|
clear_runtime = script.index(
|
|
'if [ "$VC_VPC_ZONE_RUNTIME" = trusted ]; then', mode_guard)
|
|
assert state < owner < profile < candidate < ports < exact
|
|
assert exact < quarantine < mode_guard < clear_runtime
|
|
assert ('[ "$NM_TRUSTED_ISOLATED_CANDIDATE" = no ] ||\n'
|
|
' [ "$NM_TRUSTED_ISOLATED_RETRY" = yes ] || exit 71') in script
|
|
assert ('[ "$VC_VPC_ZONE_RUNTIME" = trusted ] &&\n'
|
|
' [ "$NM_TRUSTED_ISOLATED_RETRY" != yes ]') in script
|
|
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
assert '[ "$NM_TRUSTED_ISOLATED_RETRY" != yes ]' in rollback
|
|
|
|
|
|
def test_ifupdown_apply_rollback_never_ifdowns_a_bridge_before_port_proof():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
assert '"$IFDOWN" --force "$BRIDGE"' not in rollback
|
|
created = rollback.index('if [ "$BRIDGE_CREATED" = yes ]; then')
|
|
inspect_created = rollback.index(
|
|
'ports=$($BRIDGE_CMD link show master "$BRIDGE"', created)
|
|
delete_created = rollback.index(
|
|
'"$IP" link del dev "$BRIDGE" type bridge', inspect_created)
|
|
existing = rollback.index(
|
|
'elif [ "$BRIDGE_WAS_UP" = no ] && "$IP" link show dev "$BRIDGE"',
|
|
delete_created)
|
|
inspect_existing = rollback.index(
|
|
'ports=$($BRIDGE_CMD link show master "$BRIDGE"', existing)
|
|
down_existing = rollback.index(
|
|
'"$IP" link set dev "$BRIDGE" down', inspect_existing)
|
|
assert created < inspect_created < delete_created
|
|
assert existing < inspect_existing < down_existing
|
|
assert "IF_ACTIVATION_ATTEMPTED" not in script
|
|
assert "IF_CREATED" not in script
|
|
|
|
|
|
def test_native_ifupdown_rollback_reopens_only_exact_sampled_up_existing_bridge():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
helper_start = script.index(
|
|
"vc_vpc_native_ifupdown_rollback_reopen() {")
|
|
helper_end = script.index(
|
|
"# Emergency quarantine deliberately runs before", helper_start)
|
|
helper = script[helper_start:helper_end]
|
|
exact = helper.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.precommit.before"')
|
|
fabric = helper.index(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric', exact)
|
|
topology = helper.index("vc_vpc_bridge_topology_matches", fabric)
|
|
link_up = helper.index('"$IP" link set dev "$BRIDGE" up', topology)
|
|
verify_up = helper.index(
|
|
'vc_bridge_reopen_state=$(vc_vpc_bridge_link_state)', link_up)
|
|
hard_down = helper.index('"$IP" link set dev "$BRIDGE" down', verify_up)
|
|
assert exact < fabric < topology < link_up < verify_up < hard_down
|
|
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
restore = rollback.index(
|
|
'if [ "$MODE" = ifupdown ] &&\n'
|
|
' [ "$FIREWALL_MODE" = native_nftables ]')
|
|
restore_call = rollback.index(
|
|
"vc_vpc_native_ifupdown_rollback_reopen", restore)
|
|
assert '[ "$BRIDGE_CREATED" = no ]' in rollback[restore:restore_call]
|
|
assert '[ "$BRIDGE_WAS_UP" = yes ]' in rollback[restore:restore_call]
|
|
|
|
|
|
def test_native_ifupdown_reopen_failure_reisolates_bridge(tmp_path):
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
start = script.index("vc_vpc_native_ifupdown_rollback_reopen() {")
|
|
end = script.index(
|
|
"# Emergency quarantine deliberately runs before", start)
|
|
helper = script[start:end]
|
|
link_state = tmp_path / "link"
|
|
topology_calls = tmp_path / "topology-calls"
|
|
link_state.write_text("down\n")
|
|
topology_calls.write_text("0\n")
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
"TXN=/transaction",
|
|
f"LINK_STATE={link_state}",
|
|
f"TOPOLOGY_CALLS={topology_calls}",
|
|
r'''
|
|
vc_vpc_nft_matches_snapshot() { :; }
|
|
vc_nft_table_matches_snapshot() { :; }
|
|
vc_vpc_bridge_topology_matches() {
|
|
calls=$(cat "$TOPOLOGY_CALLS")
|
|
calls=$((calls + 1))
|
|
printf '%s\n' "$calls" > "$TOPOLOGY_CALLS"
|
|
[ "$calls" = 1 ]
|
|
}
|
|
vc_vpc_bridge_link_state() { cat "$LINK_STATE"; }
|
|
vc_test_ip() {
|
|
case "$*" in
|
|
"link set dev vcv-test up") printf '%s\n' up > "$LINK_STATE" ;;
|
|
"link set dev vcv-test down") printf '%s\n' down > "$LINK_STATE" ;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
IP=vc_test_ip
|
|
'''.strip(),
|
|
helper,
|
|
r'''
|
|
if vc_vpc_native_ifupdown_rollback_reopen; then exit 9; fi
|
|
[ "$(cat "$LINK_STATE")" = down ]
|
|
'''.strip(),
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert rejected.returncode == 0, rejected.stderr
|
|
assert topology_calls.read_text() == "2\n"
|
|
|
|
|
|
def test_networkmanager_profile_queries_propagate_command_failures():
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
"NM_PROFILE_UUID=11111111-1111-4111-8111-111111111111",
|
|
"NM_PROFILE_FILE=/owned.nmconnection",
|
|
"NM_PROFILE_MARKER='virt-cloud-vpc-dataplane-v1 vpc=vpc-test'",
|
|
r'''
|
|
vc_test_nmcli() {
|
|
field=; fields=
|
|
while [ "$#" -gt 0 ]; do
|
|
case "$1" in
|
|
-g) shift; field=$1 ;;
|
|
-f) shift; fields=$1 ;;
|
|
esac
|
|
shift
|
|
done
|
|
if [ "$fields" = NAME ]; then
|
|
[ "${VC_LIST_FAIL:-no}" = no ] || return 3
|
|
printf '%s\n' "$BRIDGE"
|
|
return 0
|
|
fi
|
|
if [ "$fields" = UUID,FILENAME ]; then
|
|
[ "${VC_FILENAME_LIST_FAIL:-no}" = no ] || return 3
|
|
if [ "${VC_FILENAME_ROWS+x}" = x ]; then
|
|
printf '%b' "$VC_FILENAME_ROWS"
|
|
else
|
|
printf '%s:%s\n' "$NM_PROFILE_UUID" "$NM_PROFILE_FILE"
|
|
fi
|
|
return 0
|
|
fi
|
|
[ "$field" != GENERAL.FILENAME ] || return 97
|
|
[ "$field" != "${VC_FAIL_FIELD:-never}" ] || return 3
|
|
case "$field" in
|
|
connection.uuid) printf '%s\n' "$NM_PROFILE_UUID" ;;
|
|
connection.type) printf '%s\n' bridge ;;
|
|
connection.interface-name) printf '%s\n' "$BRIDGE" ;;
|
|
connection.stable-id) printf '%s\n' "$NM_PROFILE_MARKER" ;;
|
|
connection.autoconnect) printf '%s\n' no ;;
|
|
connection.autoconnect-retries) printf '%s\n' 0 ;;
|
|
ipv4.method|ipv6.method) printf '%s\n' disabled ;;
|
|
bridge.stp) printf '%s\n' no ;;
|
|
*) printf '%s\n' -- ;;
|
|
esac
|
|
}
|
|
NMCLI=vc_test_nmcli
|
|
'''.strip(),
|
|
_NMCLI_FILENAME_RESOLVER_SH,
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
"vc_nm_profile_disk_validate() { :; }",
|
|
r'''
|
|
case "$VC_ACTION" in
|
|
base) vc_nm_profile_base_validate ;;
|
|
resolve_ok)
|
|
vc_nm_profile_resolve
|
|
[ "$NM_PROFILE_FILE" = "$VC_EXPECT_FILE" ]
|
|
;;
|
|
resolve)
|
|
if vc_nm_profile_resolve; then exit 9; else status=$?; fi
|
|
[ "$status" = "$VC_EXPECT_STATUS" ]
|
|
;;
|
|
*) exit 2 ;;
|
|
esac
|
|
'''.strip(),
|
|
))
|
|
|
|
def run(**values):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, **values), capture_output=True, text=True,
|
|
check=False,
|
|
)
|
|
|
|
accepted = run(VC_ACTION="base")
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
assert run(VC_ACTION="base", VC_FAIL_FIELD="ipv4.routes").returncode != 0
|
|
profile = (
|
|
"/etc/NetworkManager/system-connections/"
|
|
"bridge profile:primary.nmconnection"
|
|
)
|
|
resolved = run(
|
|
VC_ACTION="resolve_ok",
|
|
VC_EXPECT_FILE=profile,
|
|
VC_FILENAME_ROWS=(
|
|
"22222222-2222-4222-8222-222222222222:"
|
|
"/run/NetworkManager/system-connections/other.nmconnection\\n"
|
|
f"11111111-1111-4111-8111-111111111111:{profile}\\n"
|
|
),
|
|
)
|
|
assert resolved.returncode == 0, resolved.stderr
|
|
list_failure = run(
|
|
VC_ACTION="resolve", VC_LIST_FAIL="yes", VC_EXPECT_STATUS="1")
|
|
assert list_failure.returncode == 0, list_failure.stderr
|
|
filename_list_failure = run(
|
|
VC_ACTION="resolve", VC_FILENAME_LIST_FAIL="yes",
|
|
VC_EXPECT_STATUS="1",
|
|
)
|
|
assert filename_list_failure.returncode == 0, filename_list_failure.stderr
|
|
duplicate = run(
|
|
VC_ACTION="resolve", VC_EXPECT_STATUS="1",
|
|
VC_FILENAME_ROWS=(
|
|
"11111111-1111-4111-8111-111111111111:/first.nmconnection\\n"
|
|
"11111111-1111-4111-8111-111111111111:/second.nmconnection\\n"
|
|
),
|
|
)
|
|
assert duplicate.returncode == 0, duplicate.stderr
|
|
missing = run(
|
|
VC_ACTION="resolve", VC_EXPECT_STATUS="1",
|
|
VC_FILENAME_ROWS=(
|
|
"22222222-2222-4222-8222-222222222222:/other.nmconnection\\n"
|
|
),
|
|
)
|
|
assert missing.returncode == 0, missing.stderr
|
|
|
|
|
|
def test_networkmanager_created_profile_rollback_deletes_only_exact_uuid(tmp_path):
|
|
deleted = tmp_path / "deleted"
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"NM_CREATED_UUID=11111111-1111-4111-8111-111111111111",
|
|
f"DELETED={deleted}",
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
r'''
|
|
vc_nm_profile_resolve() {
|
|
if [ -f "$DELETED" ]; then return "$VC_AFTER_STATUS"; fi
|
|
NM_PROFILE_UUID=$VC_RESOLVED_UUID
|
|
return 0
|
|
}
|
|
vc_nm_profile_base_validate() { [ "${VC_BASE_VALID:-yes}" = yes ]; }
|
|
vc_test_nmcli() {
|
|
[ "$*" = "connection delete uuid $NM_CREATED_UUID" ] || return 2
|
|
printf '%s\n' "$NM_CREATED_UUID" > "$DELETED"
|
|
}
|
|
NMCLI=vc_test_nmcli
|
|
vc_nm_created_profile_remove
|
|
'''.strip(),
|
|
))
|
|
|
|
def run(resolved_uuid, after_status, *, base_valid="yes"):
|
|
deleted.unlink(missing_ok=True)
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(
|
|
os.environ,
|
|
VC_RESOLVED_UUID=resolved_uuid,
|
|
VC_AFTER_STATUS=str(after_status),
|
|
VC_BASE_VALID=base_valid,
|
|
),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
removed = deleted.read_text().strip() if deleted.exists() else None
|
|
return result, removed
|
|
|
|
exact, removed = run(
|
|
"11111111-1111-4111-8111-111111111111", 2)
|
|
assert exact.returncode == 0, exact.stderr
|
|
assert removed == "11111111-1111-4111-8111-111111111111"
|
|
|
|
foreign, removed = run(
|
|
"22222222-2222-4222-8222-222222222222", 2)
|
|
assert foreign.returncode != 0
|
|
assert removed is None
|
|
|
|
invalid_profile, removed = run(
|
|
"11111111-1111-4111-8111-111111111111", 2, base_valid="no")
|
|
assert invalid_profile.returncode != 0
|
|
assert removed is None
|
|
|
|
ambiguous, removed = run(
|
|
"11111111-1111-4111-8111-111111111111", 1)
|
|
assert ambiguous.returncode != 0
|
|
assert removed == "11111111-1111-4111-8111-111111111111"
|
|
|
|
|
|
def test_destructive_nft_batches_recheck_full_state_after_dry_run():
|
|
apply = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
baseline = apply.index(
|
|
'vc_vpc_capture_nft_precommit "$TXN/nft.precommit.before"')
|
|
apply_check = apply.index('"$NFT" -c -f "$NFT_BATCH"', baseline)
|
|
apply_precommit = apply.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.precommit.before"',
|
|
apply_check)
|
|
apply_commit = apply.index('"$NFT" -f "$NFT_BATCH"', apply_precommit)
|
|
apply_post = apply.index(
|
|
'vc_vpc_capture_nft_snapshot "$TXN/nft.desired"', apply_commit)
|
|
assert baseline < apply_check < apply_precommit < apply_commit < apply_post
|
|
|
|
reassert = apply.index('NFT_REASSERT="$TXN/reassert.nft"')
|
|
reassert_check = apply.index('"$NFT" -c -f "$NFT_REASSERT"', reassert)
|
|
reassert_precommit = apply.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.desired"', reassert_check)
|
|
reassert_commit = apply.index(
|
|
'"$NFT" -f "$NFT_REASSERT"', reassert_precommit)
|
|
reassert_post = apply.index(
|
|
'vc_vpc_capture_nft_snapshot "$TXN/nft.desired.verify"',
|
|
reassert_commit)
|
|
assert reassert_check < reassert_precommit < reassert_commit < reassert_post
|
|
|
|
rollback = apply[apply.index("rollback() {"):apply.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
rollback_check = rollback.index('"$NFT" -c -f "$NFT_ROLLBACK"')
|
|
rollback_vpc = rollback.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.desired"', rollback_check)
|
|
rollback_fabric = rollback.index(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric',
|
|
rollback_vpc)
|
|
rollback_commit = rollback.index(
|
|
'"$NFT" -f "$NFT_ROLLBACK"', rollback_fabric)
|
|
rollback_vpc_post = rollback.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.precommit.before"',
|
|
rollback_commit)
|
|
rollback_fabric_post = rollback.index(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric',
|
|
rollback_vpc_post)
|
|
assert (rollback_check < rollback_vpc < rollback_fabric <
|
|
rollback_commit < rollback_vpc_post < rollback_fabric_post)
|
|
|
|
remove = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
remove_capture = remove.index(
|
|
'vc_remove_capture_nft_precommit "$TXN/nft.remove.precommit"')
|
|
remove_check = remove.index(
|
|
'"$NFT" -c -f "$NFT_REMOVE_BATCH"', remove_capture)
|
|
remove_precommit = remove.index(
|
|
'vc_remove_nft_precommit_matches "$TXN/nft.remove.precommit"',
|
|
remove_check)
|
|
remove_commit = remove.index(
|
|
'"$NFT" -f "$NFT_REMOVE_BATCH"', remove_precommit)
|
|
remove_post = remove.index("vc_remove_nft_tables_absent", remove_commit)
|
|
assert (remove_capture < remove_check < remove_precommit < remove_commit <
|
|
remove_post)
|
|
|
|
|
|
def test_fake_nft_dry_run_replacement_never_reaches_destructive_commit(tmp_path):
|
|
apply = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
apply_start = apply.index("vc_vpc_capture_nft_precommit() {")
|
|
apply_end = apply.index("vc_vpc_nft_snapshots_equal() {", apply_start)
|
|
apply_helpers = apply[apply_start:apply_end]
|
|
|
|
remove = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
remove_start = remove.index("vc_remove_capture_nft_precommit() {")
|
|
remove_end = remove.index('case "$PHASE" in', remove_start)
|
|
remove_helpers = remove[remove_start:remove_end]
|
|
|
|
fake = r'''
|
|
vc_test_nft() {
|
|
if [ "$1" = -s ]; then
|
|
shift
|
|
[ "$1" = list ] || return 2
|
|
if [ "$2" = tables ]; then
|
|
for live_path in "$LIVE/$3-"*; do
|
|
[ -f "$live_path" ] || continue
|
|
printf 'table %s %s\n' "$3" "${live_path##*-$3-}"
|
|
done
|
|
return 0
|
|
fi
|
|
[ "$2" = table ] || return 2
|
|
live_path="$LIVE/$3-$4"
|
|
[ -f "$live_path" ] || return 1
|
|
cat "$live_path"
|
|
return 0
|
|
fi
|
|
if [ "$1" = -c ] && [ "$2" = -f ]; then
|
|
printf 'table inet %s {\n' "$RACE_TABLE" \
|
|
> "$LIVE/inet-$RACE_TABLE"
|
|
printf '%s\n' \
|
|
' chain forward { policy accept; }' '}' > \
|
|
"$LIVE/inet-$RACE_TABLE.tmp"
|
|
cat "$LIVE/inet-$RACE_TABLE.tmp" >> "$LIVE/inet-$RACE_TABLE"
|
|
rm -f "$LIVE/inet-$RACE_TABLE.tmp"
|
|
return 0
|
|
fi
|
|
if [ "$1" = -f ]; then
|
|
: > "$APPLIED"
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
NFT=vc_test_nft
|
|
CMP=/usr/bin/cmp
|
|
TABLE=virt_cloud_vpc_race
|
|
RACE_TABLE=$TABLE
|
|
'''.strip()
|
|
|
|
cases = (
|
|
(
|
|
"apply",
|
|
apply_helpers,
|
|
'vc_vpc_capture_nft_precommit "$TXN/precommit"',
|
|
'vc_vpc_nft_matches_snapshot "$TXN/precommit"',
|
|
False,
|
|
),
|
|
(
|
|
"reassert",
|
|
apply_helpers,
|
|
'vc_vpc_capture_nft_precommit "$TXN/desired"',
|
|
'vc_vpc_nft_matches_snapshot "$TXN/desired"',
|
|
True,
|
|
),
|
|
(
|
|
"remove",
|
|
remove_helpers,
|
|
'vc_remove_capture_nft_precommit "$TXN/precommit"',
|
|
'vc_remove_nft_precommit_matches "$TXN/precommit"',
|
|
True,
|
|
),
|
|
)
|
|
for name, helpers, capture, precommit, initially_present in cases:
|
|
case_dir = tmp_path / name
|
|
live = case_dir / "live"
|
|
txn = case_dir / "txn"
|
|
live.mkdir(parents=True)
|
|
txn.mkdir()
|
|
applied = case_dir / "applied"
|
|
if initially_present:
|
|
for family in ("inet", "bridge", "arp"):
|
|
(live / f"{family}-virt_cloud_vpc_race").write_text(
|
|
f"table {family} virt_cloud_vpc_race {{\n"
|
|
" chain forward { policy drop; }\n}\n"
|
|
)
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
f"LIVE={live}",
|
|
f"TXN={txn}",
|
|
f"APPLIED={applied}",
|
|
fake,
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
helpers,
|
|
capture,
|
|
'"$NFT" -c -f "$TXN/batch.nft"',
|
|
precommit,
|
|
'"$NFT" -f "$TXN/batch.nft"',
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0, name
|
|
assert not applied.exists(), name
|
|
|
|
# Rollback validates both the VPC desired snapshot and the complete Fabric
|
|
# desired snapshot after dry-run. Mutating only Fabric must also prevent
|
|
# the one restore batch from running.
|
|
case_dir = tmp_path / "rollback-fabric"
|
|
live = case_dir / "live"
|
|
txn = case_dir / "txn"
|
|
live.mkdir(parents=True)
|
|
txn.mkdir()
|
|
applied = case_dir / "applied"
|
|
for family in ("inet", "bridge", "arp"):
|
|
(live / f"{family}-virt_cloud_vpc_race").write_text(
|
|
f"table {family} virt_cloud_vpc_race {{\n"
|
|
" chain forward { policy drop; }\n}\n"
|
|
)
|
|
(live / "inet-virt_cloud_wg_fabric").write_text(
|
|
"table inet virt_cloud_wg_fabric {\n"
|
|
" chain forward { policy drop; }\n}\n"
|
|
)
|
|
fabric_harness = "\n".join((
|
|
"set -eu",
|
|
f"LIVE={live}",
|
|
f"TXN={txn}",
|
|
f"APPLIED={applied}",
|
|
fake,
|
|
"RACE_TABLE=virt_cloud_wg_fabric",
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
apply_helpers,
|
|
'vc_vpc_capture_nft_precommit "$TXN/vpc.desired"',
|
|
('vc_nft_table_snapshot inet virt_cloud_wg_fabric '
|
|
'"$TXN/fabric.desired"'),
|
|
'"$NFT" -c -f "$TXN/rollback.nft"',
|
|
'vc_vpc_nft_matches_snapshot "$TXN/vpc.desired"',
|
|
('vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric '
|
|
'"$TXN/fabric.desired"'),
|
|
'"$NFT" -f "$TXN/rollback.nft"',
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", fabric_harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0, rejected.stderr
|
|
assert not applied.exists()
|
|
|
|
|
|
def test_firewalld_remove_compares_all_canonical_tables_before_atomic_delete():
|
|
script = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
first_compare = script.index(
|
|
'vc_nft_table_matches_snapshot "$family" "$name"')
|
|
fresh_compare = script.index("vc_vpc_persisted_snapshots_match", first_compare)
|
|
apply_delete = script.index('"$NFT" -f "$NFT_REMOVE_BATCH"', fresh_compare)
|
|
assert first_compare < fresh_compare < apply_delete
|
|
assert '[ "$nft_table_count" = 3 ] || exit 71' in script
|
|
assert '"$NFT" delete table "$family" "$name"' not in script
|
|
|
|
|
|
def test_remove_acl_mismatch_fails_after_quarantining_trusted_bridge(tmp_path):
|
|
script = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_FIREWALLD)
|
|
early = script.index("# Minimum state/manager proof")
|
|
hard_down = script.index(
|
|
'"$IP" link set dev "$BRIDGE" down >/dev/null || exit 72', early)
|
|
remove_trust = script.index(
|
|
'--zone=trusted --remove-interface="$BRIDGE"', hard_down)
|
|
fabric_owner = script.index(
|
|
'[ -f /etc/virt-cloud/wg-fabric0.owner ]', remove_trust)
|
|
first_live_compare = script.index(
|
|
"vc_vpc_persisted_snapshots_match || exit 71", remove_trust)
|
|
assert hard_down < remove_trust < fabric_owner < first_live_compare
|
|
|
|
def test_adapter_normalizes_legacy_test_contract_but_rejects_unknown_mode():
|
|
adapter = VpcPrivateHostAdapter(object(), {})
|
|
adapter._deployment_contract = lambda: (
|
|
"ifupdown", "/usr/sbin/ifup", "/usr/sbin/ifdown",
|
|
"/usr/sbin/ifquery",
|
|
)
|
|
assert adapter._render_contract()[-1] == FIREWALL_MODE_NATIVE
|
|
adapter._deployment_contract = lambda: (
|
|
"ifupdown", "/usr/sbin/ifup", "/usr/sbin/ifdown",
|
|
"/usr/sbin/ifquery", "foreign",
|
|
)
|
|
with pytest.raises(VpcDataplaneError):
|
|
adapter._render_contract()
|
|
|
|
|
|
def test_adapter_reports_firewall_mode_conflict_separately(monkeypatch):
|
|
from virsh_panel import fabric
|
|
|
|
monkeypatch.setattr(
|
|
fabric, "_profile_network_mode",
|
|
lambda _profile: "networkmanager_nmcli")
|
|
|
|
def reject_firewall(_profile):
|
|
raise fabric.FabricError(
|
|
"conflict", 409, "fabric_host_firewall_mode_conflict")
|
|
|
|
monkeypatch.setattr(fabric, "_profile_firewall_mode", reject_firewall)
|
|
adapter = VpcPrivateHostAdapter(object(), {})
|
|
with pytest.raises(VpcDataplaneError) as rejected:
|
|
adapter._deployment_contract()
|
|
assert rejected.value.error_code == "vpc_dataplane_firewall_mode_conflict"
|