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

146 lines
5.7 KiB
Python

"""Run the rendered helper's real failure trap and bounded journal records."""
from __future__ import annotations
import shlex
import pytest
from test_fabric_first_deploy_entry import (
_TRANSITION_ENV,
_fixture,
_journal,
_run,
_transition_fixture,
_write,
)
_SECRET = "DO_NOT_LOG_private_key_or_raw_config"
_PHASES = (
"dispatch", "quarantine", "recovery", "gate", "sysctl", "manager",
"config", "underlay", "routes", "nft", "activation", "vpc", "final",
"complete",
)
def _line(phase: str, status: int) -> str:
return ("virt-cloud-fabric helper_state=failed "
f"helper_phase={phase} exit_status={status}\n")
def _assert_failed(result, phase: str, status: int, root):
assert result.returncode == status
assert result.stdout == ""
assert result.stderr == _line(phase, status)
assert _SECRET not in result.stderr
assert str(root) not in result.stderr
def _inject_failure_point(helper, body: str):
content = helper.read_text(encoding="utf-8")
marker = "trap cleanup EXIT HUP INT TERM\n"
assert content.count(marker) == 1
# Only add a controlled early failure point. The rendered logger, trap,
# cleanup implementation, and original exit status remain unmodified.
_write(helper, content.replace(marker, marker + body + "\n", 1), 0o700)
@pytest.mark.parametrize("phase", _PHASES)
def test_rendered_helper_failure_trap_logs_each_fixed_phase(tmp_path, phase):
_, helper, _, _, _ = _fixture(tmp_path, "ifupdown")
_inject_failure_point(helper, (
f"VC_FABRIC_HELPER_PHASE={shlex.quote(phase)}\n"
f"vc_private_fixture={shlex.quote(_SECRET)}\nexit 71"
))
_assert_failed(_run(helper, tmp_path), phase, 71, tmp_path)
@pytest.mark.parametrize("status", [1, 70, 71, 79, 127, 143, 255])
def test_rendered_helper_cleanup_preserves_original_failure_status(tmp_path, status):
_, helper, _, _, _ = _fixture(tmp_path, "ifupdown")
_inject_failure_point(helper, f"VC_FABRIC_HELPER_PHASE=final\nexit {status}")
_assert_failed(_run(helper, tmp_path), "final", status, tmp_path)
@pytest.mark.parametrize("phase", [
"", "not-a-phase", _SECRET, "/run/" + _SECRET,
"routes\nexit_status=0\n" + _SECRET,
])
def test_rendered_helper_failure_trap_sanitizes_unknown_phase(tmp_path, phase):
_, helper, _, _, _ = _fixture(tmp_path, "ifupdown")
_inject_failure_point(helper, (
f"VC_FABRIC_HELPER_PHASE={shlex.quote(phase)}\nexit 79"
))
_assert_failed(_run(helper, tmp_path), "unknown", 79, tmp_path)
@pytest.mark.parametrize("status", ["", "-1", "256", "999999999999999999999", _SECRET])
def test_rendered_helper_logger_sanitizes_invalid_status_without_echo(tmp_path, status):
_, helper, _, _, _ = _fixture(tmp_path, "ifupdown")
_inject_failure_point(helper, (
"VC_FABRIC_HELPER_PHASE=final\n"
f"vc_fabric_helper_failure_log {shlex.quote(status)}\nexit 0"
))
# Call the real rendered logger with controlled invalid arguments, then
# succeed so the actual EXIT trap must not produce a duplicate record.
result = _run(helper, tmp_path)
assert result.returncode == 0
assert result.stdout == ""
assert result.stderr == _line("final", 1)
assert _SECRET not in result.stderr
assert str(tmp_path) not in result.stderr
@pytest.mark.parametrize("failure", ["base_symlink", "emergency_owner", "gate", "sysctl", "journal"])
def test_complete_helper_entry_records_real_safe_failure_phase(tmp_path, failure):
if failure == "sysctl":
_, helper, _, _, roots = _transition_fixture(tmp_path, "valid")
_write(roots["/etc/sysctl.d"] / "90-virt-cloud-wg-fabric.conf", _SECRET + "\n")
phase, status = "sysctl", 71
else:
plan, helper, base, runtime, _ = _fixture(tmp_path, "ifupdown")
if failure == "base_symlink":
target = tmp_path / "external-base"
base.rename(target)
base.symlink_to(target, target_is_directory=True)
phase, status = "dispatch", 71
elif failure == "emergency_owner":
_write(runtime / "wg-fabric-emergency.owner", _SECRET + "\n")
phase, status = "quarantine", 71
elif failure == "journal":
journal = _journal(plan, base, "ifupdown", helper, tmp_path,
nft_phase="refreshed")
_write(journal / "nft.applied", _SECRET + "\n")
phase, status = "recovery", 79
else:
phase, status = "gate", 71
_assert_failed(_run(helper, tmp_path), phase, status, tmp_path)
@pytest.mark.parametrize("entry", ["--ifupdown-hook", "--ifupdown-configure"])
def test_complete_helper_successful_transition_is_quiet(tmp_path, entry):
_, helper, _, _, _ = _transition_fixture(tmp_path, "valid")
result = _run(helper, tmp_path, entry, extra_env=_TRANSITION_ENV)
assert result.returncode == 0, result.stderr
assert result.stdout == ""
assert result.stderr == ""
@pytest.mark.parametrize("needs_repair", [False, True])
def test_complete_helper_normal_repair_probe_is_quiet(tmp_path, needs_repair):
_, helper, _, runtime, _ = _fixture(tmp_path, "ifupdown")
if not needs_repair:
(runtime / "wg-fabric-emergency.owner").unlink()
for family in ("inet", "bridge"):
(tmp_path / "tables" / f"{family}--virt_cloud_fabric_emergency").unlink()
result = _run(helper, tmp_path, "--needs-repair")
assert result.returncode == (0 if needs_repair else 1), result.stderr
assert result.stdout == ""
assert result.stderr == ""
def test_complete_helper_fatal_repair_probe_is_not_hidden(tmp_path):
_, helper, _, _, _ = _fixture(tmp_path, "ifupdown")
(tmp_path / "tools" / "wg").unlink()
_assert_failed(_run(helper, tmp_path, "--needs-repair"), "dispatch", 70, tmp_path)