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

160 lines
6.1 KiB
Python

"""VPC apply diagnostics must stay useful without exposing remote output."""
from __future__ import annotations
import os
from pathlib import Path
import shlex
import subprocess
import tempfile
import unittest
from virsh_panel.vpc import VPC_SCHEMA_VERSION
from virsh_panel.vpc_dataplane import (
VPC_APPLY_SAFE_LOG_SH,
parse_vpc_dataplane_plan,
render_apply_script,
)
_SECRET = "NEVER_LOG_vpc_private_fixture_or_sensitive_path"
def _plan():
return parse_vpc_dataplane_plan({
"schema_version": VPC_SCHEMA_VERSION,
"vpc_id": "vpc-safe-log",
"region": "hk",
"fabric_id": "fabric-hk",
"fabric_revision": 3,
"cidr": "10.42.0.0/24",
"desired_revision": 1,
"operation_id": "vpc-op-safe-log-0001",
"subnets": [{"subnet_id": "subnet-a", "cidr": "10.42.0.0/25"}],
"hosts": [{
"host_id": "host-a",
"members": [{
"member_id": "member-a",
"instance_uuid": "11111111-1111-4111-8111-111111111111",
"private_ip_cidr": "10.42.0.10/32",
"subnet_id": "subnet-a",
}],
}],
"routes": [],
"acls": [],
"fabric_peers": [],
}, "host-a", fabric_plan_digest="ab" * 32)
class VpcApplySafeLogTest(unittest.TestCase):
def _run_logger(self, body: str):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
output = root / "journal"
logger = root / "logger"
logger.write_text(
"#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$VC_VPC_TEST_LOG\"\n",
encoding="utf-8",
)
logger.chmod(0o700)
script = "\n".join((
"set -eu",
VPC_APPLY_SAFE_LOG_SH,
f"LOGGER={shlex.quote(str(logger))}",
body,
))
result = subprocess.run(
["/bin/sh", "-c", script],
env={**os.environ, "VC_VPC_TEST_LOG": str(output)},
capture_output=True,
text=True,
check=False,
)
lines = output.read_text(encoding="utf-8").splitlines() \
if output.exists() else []
return result, lines
def test_exit_trap_preserves_status_and_logs_only_fixed_fields(self):
result, lines = self._run_logger(
"VC_VPC_APPLY_PHASE=post_activation_sysctl\nexit 72")
self.assertEqual(result.returncode, 72)
self.assertEqual(result.stdout, "")
self.assertEqual(result.stderr, "")
self.assertEqual(lines, [
"-t virt-cloud-vpc -- apply_state=failed "
"apply_phase=post_activation_sysctl exit_status=72",
])
def test_unknown_phase_and_status_are_sanitized(self):
result, lines = self._run_logger("\n".join((
"VC_VPC_APPLY_PHASE=" + shlex.quote("bad\n" + _SECRET),
f"vc_vpc_apply_failure_log {shlex.quote(_SECRET)}",
)))
self.assertEqual(result.returncode, 0)
self.assertEqual(lines, [
"-t virt-cloud-vpc -- apply_state=failed "
"apply_phase=unknown exit_status=1",
])
self.assertNotIn(_SECRET, "\n".join(lines))
def test_success_does_not_create_failure_record(self):
result, lines = self._run_logger("VC_VPC_APPLY_PHASE=commit\nexit 0")
self.assertEqual(result.returncode, 0)
self.assertEqual(lines, [])
def test_term_signal_uses_nonzero_status_and_safe_phase(self):
result, lines = self._run_logger(
'VC_VPC_APPLY_PHASE=routes\nkill -TERM "$$"\nexit 99')
self.assertEqual(result.returncode, 143)
self.assertEqual(result.stdout, "")
self.assertEqual(result.stderr, "")
self.assertEqual(lines, [
"-t virt-cloud-vpc -- apply_state=failed "
"apply_phase=routes exit_status=143",
])
def test_rendered_phases_bracket_activation_and_post_activation_work(self):
script = render_apply_script(_plan(), deployment_mode="networkmanager_nmcli")
rollback_start = script.index("rollback() {")
rollback_end = script.index("\ntrap rollback EXIT HUP INT TERM", rollback_start)
rollback = script[rollback_start:rollback_end]
disable_traps = rollback.index("trap - EXIT HUP INT TERM")
safe_log = rollback.index('vc_vpc_apply_failure_log "$status"')
rollback_mutations = rollback.index("set +e")
self.assertLess(disable_traps, safe_log)
self.assertLess(safe_log, rollback_mutations)
persistence = script.index("VC_VPC_APPLY_PHASE=persistence")
activation = script.index("VC_VPC_APPLY_PHASE=activation", persistence)
nm_up = script.index(
'"$NMCLI" connection up uuid "$NM_PROFILE_UUID"', activation)
post_activation = script.index(
"VC_VPC_APPLY_PHASE=post_activation_sysctl", nm_up)
rp_filter = script.index("vc_vpc_rp_filter_set || exit 72", post_activation)
runtime = script.index("VC_VPC_APPLY_PHASE=runtime", rp_filter)
proxy = script.index("VC_VPC_APPLY_PHASE=proxy", runtime)
neighbors = script.index("VC_VPC_APPLY_PHASE=neighbors", proxy)
routes = script.index("VC_VPC_APPLY_PHASE=routes", neighbors)
policy = script.index("VC_VPC_APPLY_PHASE=policy_rules", routes)
fabric_acl = script.index("VC_VPC_APPLY_PHASE=fabric_acl", policy)
wireguard = script.index("VC_VPC_APPLY_PHASE=wireguard", fabric_acl)
final = script.index("VC_VPC_APPLY_PHASE=final_readback", wireguard)
commit = script.index("VC_VPC_APPLY_PHASE=commit", final)
self.assertLess(activation, nm_up)
self.assertLess(nm_up, post_activation)
self.assertLess(post_activation, rp_filter)
self.assertEqual(
sorted((runtime, proxy, neighbors, routes, policy, fabric_acl,
wireguard, final, commit)),
[runtime, proxy, neighbors, routes, policy, fabric_acl,
wireguard, final, commit],
)
if __name__ == "__main__":
unittest.main()