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_observer_optional_...

125 lines
5.0 KiB
Python

"""Run optional/empty host observations under the production shell options."""
from __future__ import annotations
import os
from pathlib import Path
import shlex
import subprocess
import pytest
from test_vpc_nft_dataplane import _plan
from virsh_panel.vpc_dataplane import render_observe_script
SHELLS = tuple(path for path in ("/bin/sh", "/bin/dash", "/bin/bash")
if Path(path).is_file())
NM_PATHS = ("/usr/bin/nmcli", "/usr/sbin/nmcli", "/bin/nmcli", "/sbin/nmcli")
def _fragment(start: str, end: str) -> str:
script = render_observe_script(_plan())
first = script.index(start)
return script[first:script.index(end, first)]
def _run(shell: str, script: str, env: dict[str, str]):
return subprocess.run(
[shell, "-eu", "-c", script], text=True, capture_output=True,
check=False, env=os.environ | env,
)
@pytest.mark.parametrize("shell", SHELLS)
@pytest.mark.parametrize("scenario,expected", [
("absent", "yes"),
("nonexecutable", "yes"),
("available_ifupdown", "yes"),
("owned_nm", "yes"),
("duplicate_nm", "no"),
("foreign_nm", "no"),
("invalid_ifupdown", "no"),
("both_managers", "no"),
])
def test_observer_optional_nmcli_preserves_manager_ownership(
tmp_path, shell, scenario, expected):
plan = _plan()
body = _fragment("nm_owned=no\n", "for artifact_spec in ")
candidates = [tmp_path / f"nmcli-{index}" for index in range(4)]
for original, candidate in zip(NM_PATHS, candidates):
body = body.replace(original, shlex.quote(str(candidate)))
# Exercise the final fallback path as well as normal first-path discovery.
candidate = candidates[-1] if scenario == "owned_nm" else candidates[0]
if scenario not in {"absent", "invalid_ifupdown"}:
candidate.write_text('''#!/bin/sh
case "$*" in
'-t --escape no -f NAME connection show')
case "$SCENARIO" in
available_ifupdown) : ;;
duplicate_nm) printf '%s\\n' "$BRIDGE" "$BRIDGE" ;;
*) printf '%s\\n' "$BRIDGE" ;;
esac ;;
*'-g connection.stable-id '*)
if [ "$SCENARIO" = foreign_nm ]; then printf foreign; else printf '%s' "$NM_PROFILE_MARKER"; fi ;;
*'-g connection.type '*) printf bridge ;;
*'-g connection.interface-name '*) printf '%s' "$BRIDGE" ;;
*'-g ipv4.method '*|*'-g ipv6.method '*) printf disabled ;;
*'-g connection.zone '*) : ;;
*) exit 98 ;;
esac
''')
candidate.chmod(0o600 if scenario == "nonexecutable" else 0o700)
fragment = tmp_path / "interfaces"
if scenario != "owned_nm":
owner = (f"# virt-cloud-vpc-dataplane-v1 vpc={plan.vpc.vpc_id} "
f"revision={plan.desired_revision} digest={plan.desired_digest}")
fragment.write_text(
owner + "\n" + f"iface {plan.resources.bridge} inet manual\n"
+ (f"auto {plan.resources.bridge}\n" if scenario == "invalid_ifupdown" else ""))
fragment.chmod(0o600)
harness = '''
# Remote artifacts are root-owned; only the stat identity is simulated here.
stat() { [ "$1:$2:$3" = "-c:%u:%a:$IF_FRAGMENT" ] || return 98; printf '0:600'; }
vc_nm_profile_resolve() { return 0; }
vc_nm_profile_expect() { [ "$1" = active ]; }
firewall_mode_state=owned; NFT_MAIN_COUNT=1; persistence_target=yes
opposite_clean=yes; nft_file_ok=yes; sysctl_file_ok=yes; persistence=no
'''
result = _run(shell, harness + body + '\nprintf "AFTER=%s\\n" "$NMCLI"\n', {
"SCENARIO": scenario, "BRIDGE": plan.resources.bridge,
"NM_PROFILE_MARKER": f"virt-cloud-vpc-dataplane-v1 vpc={plan.vpc.vpc_id}",
"IF_FRAGMENT": str(fragment), "FIREWALL_MODE": "native_nftables",
"NFT_SNAPSHOT_PREFIX": str(tmp_path / "snapshot"),
})
assert result.returncode == 0, result.stderr
assert f"__VC_VPC_PERSISTENCE__={expected}\n" in result.stdout
selected = "" if scenario in {"absent", "nonexecutable", "invalid_ifupdown"} else str(candidate)
assert f"AFTER={selected}\n" in result.stdout
@pytest.mark.parametrize("shell", SHELLS)
@pytest.mark.parametrize("actual,expected", [
("", "missing"),
("10.44.0.10\n10.44.0.11", "owned"),
("10.44.0.10", "missing"),
])
def test_observer_empty_proxy_set_reports_missing_without_aborting(
tmp_path, shell, actual, expected):
body = _fragment("proxy_records=", "# vc-local-neighbor-observe-begin")
proxy_file = tmp_path / "proxy"
proxy_file.write_text("# virt-cloud-vpc-proxy-v1\n10.44.0.10\n10.44.0.11\n")
result = _run(shell, '''
mock_ip() { printf '%s\\n' "$ACTUAL_PROXY"; }
IP=mock_ip
''' + body + '\nprintf "AFTER=yes\\n"\n', {
"ACTUAL_PROXY": actual, "PROXY_FILE": str(proxy_file),
"BRIDGE": _plan().resources.bridge,
})
assert result.returncode == 0, result.stderr
assert f"__VC_VPC_PROXY_STATE__={expected}\n" in result.stdout
assert "AFTER=yes\n" in result.stdout
actual_markers = [line.removeprefix("__VC_VPC_PROXY_IP__=")
for line in result.stdout.splitlines()
if line.startswith("__VC_VPC_PROXY_IP__=")]
assert actual_markers == actual.splitlines()