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.
123 lines
4.0 KiB
Python
123 lines
4.0 KiB
Python
"""Execute the rendered persistence gate under real POSIX shell semantics."""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import shlex
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from test_vpc_dataplane import _plan
|
|
from virsh_panel.vpc_dataplane import render_observe_script
|
|
|
|
|
|
def _persistence_gate(**overrides: str) -> str:
|
|
script = render_observe_script(_plan())
|
|
match = re.search(
|
|
r'(?m)^if \[ "\$firewall_mode_state" = owned \] && '
|
|
r'\[ "\$NFT_MAIN_COUNT" = 1 \] &&\n.*?^fi$',
|
|
script,
|
|
re.S,
|
|
)
|
|
assert match is not None, "The production persistence gate was not found."
|
|
values = {
|
|
"MODE": "networkmanager_nmcli",
|
|
"firewall_mode_state": "owned",
|
|
"NFT_MAIN_COUNT": "1",
|
|
"persistence_target": "yes",
|
|
"opposite_clean": "yes",
|
|
"nft_file_ok": "yes",
|
|
"sysctl_file_ok": "yes",
|
|
"nft_snapshot_state": "yes",
|
|
"nm_owned": "yes",
|
|
"nm_present": "yes",
|
|
"nm_profile_query": "ok",
|
|
"nm_profile_count": "1",
|
|
"ifup_owned": "no",
|
|
"ifup_profile_query": "ok",
|
|
"ifup_profile_count": "0",
|
|
"persistence": "no",
|
|
}
|
|
values.update(overrides)
|
|
rendered = "set -eu\n" + "\n".join(
|
|
f"{key}={shlex.quote(value)}" for key, value in values.items()
|
|
) + "\n" + match.group(0) + '\nprintf "%s\\n" "$persistence"\n'
|
|
result = subprocess.run(
|
|
["/bin/sh", "-c", rendered], check=False,
|
|
capture_output=True, text=True, timeout=5,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
return result.stdout.strip()
|
|
|
|
|
|
@pytest.mark.parametrize("owners", [
|
|
{
|
|
"MODE": "networkmanager_nmcli",
|
|
"nm_owned": "yes", "nm_present": "yes",
|
|
"nm_profile_query": "ok", "nm_profile_count": "1",
|
|
"ifup_owned": "no",
|
|
"ifup_profile_query": "ok", "ifup_profile_count": "0",
|
|
},
|
|
{
|
|
"MODE": "ifupdown",
|
|
"nm_owned": "no", "nm_present": "no",
|
|
"nm_profile_query": "not_applicable", "nm_profile_count": "0",
|
|
"ifup_owned": "yes",
|
|
"ifup_profile_query": "ok", "ifup_profile_count": "1",
|
|
},
|
|
])
|
|
def test_persistence_accepts_each_exact_exclusive_owner(owners):
|
|
assert _persistence_gate(**owners) == "yes"
|
|
|
|
|
|
@pytest.mark.parametrize("owners", [
|
|
{"nm_owned": "yes", "nm_present": "yes", "ifup_owned": "yes"},
|
|
{"nm_owned": "no", "nm_present": "yes", "ifup_owned": "yes"},
|
|
{"nm_owned": "no", "nm_present": "yes", "ifup_owned": "no"},
|
|
{"nm_owned": "no", "nm_present": "no", "ifup_owned": "no"},
|
|
{"nm_owned": "no", "nm_present": "invalid", "ifup_owned": "yes"},
|
|
])
|
|
def test_persistence_rejects_dual_foreign_missing_or_unknown_owners(owners):
|
|
assert _persistence_gate(**owners) == "no"
|
|
|
|
|
|
@pytest.mark.parametrize("inventory", [
|
|
{"nm_profile_query": "invalid"},
|
|
{"nm_profile_count": "2"},
|
|
{"ifup_profile_query": "invalid"},
|
|
{"ifup_profile_count": "1"},
|
|
])
|
|
def test_networkmanager_persistence_rejects_ambiguous_or_opposite_inventory(
|
|
inventory):
|
|
assert _persistence_gate(**inventory) == "no"
|
|
|
|
|
|
@pytest.mark.parametrize("field,value", [
|
|
("firewall_mode_state", "mismatch"),
|
|
("NFT_MAIN_COUNT", "0"),
|
|
("NFT_MAIN_COUNT", "2"),
|
|
("persistence_target", "no"),
|
|
("opposite_clean", "no"),
|
|
("nft_file_ok", "no"),
|
|
("sysctl_file_ok", "no"),
|
|
("nft_snapshot_state", "no"),
|
|
])
|
|
@pytest.mark.parametrize("owners", [
|
|
{
|
|
"MODE": "networkmanager_nmcli",
|
|
"nm_owned": "yes", "nm_present": "yes",
|
|
"nm_profile_query": "ok", "nm_profile_count": "1",
|
|
"ifup_owned": "no",
|
|
"ifup_profile_query": "ok", "ifup_profile_count": "0",
|
|
},
|
|
{
|
|
"MODE": "ifupdown",
|
|
"nm_owned": "no", "nm_present": "no",
|
|
"nm_profile_query": "not_applicable", "nm_profile_count": "0",
|
|
"ifup_owned": "yes",
|
|
"ifup_profile_query": "ok", "ifup_profile_count": "1",
|
|
},
|
|
])
|
|
def test_each_persistence_prerequisite_remains_required(field, value, owners):
|
|
assert _persistence_gate(**owners, **{field: value}) == "no"
|