myxce/src/xceadmin/tests/test_live_e2e_common.py
Rongsong Shen 3891bd909a
test(xceadmin): add env-var override for live E2E binary discovery
_live_e2e_common.py's CONTROLLER_CANDIDATES/XCEADMIN_CANDIDATES only ever
resolved to the three default profile-dir names (xce.build, build.release,
build.dbg), with no way to point the live E2E suite at a binary built to a
custom/explicit build dir. Adds XCE_CONTROLLER_BIN/XCEADMIN_BIN env-var
overrides via a new resolve_binary_candidates() helper -- when set, the
override becomes the sole candidate; otherwise the existing three-name
fallback list is unchanged. Mirrors the ${XCE_CONTROLLER_BIN:-default}
convention already used by clean_failover_preservation.sh/run-ha-soak.sh.

Skip messages in both live E2E test files now mention the override.

TDD: 4 new unit tests in test_live_e2e_common.py (no override, empty
override, override-becomes-sole-candidate, and an end-to-end reload proving
the real module constants respect their env vars). Full xceadmin suite
(288 tests) confirmed unaffected: 287 pass, 1 pre-existing unrelated
failure (a stale redirect-gap assertion, unrelated to this change).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AgYRU3zDTqNGV5Jk8aiKLg
2026-08-27 02:39:52 +00:00

76 lines
3.1 KiB
Python

"""Unit tests for ``_live_e2e_common``'s binary-discovery env-var override.
``CONTROLLER_CANDIDATES``/``XCEADMIN_CANDIDATES`` previously only ever
resolved to the three default profile-dir names (``xce.build``,
``build.release``, ``build.dbg``) -- no way to point the live E2E suite at
a binary built to a custom/explicit build dir (``xce-build.sh``'s
"legacy explicit build_dir" override) without editing this module directly.
This adds ``XCE_CONTROLLER_BIN``/``XCEADMIN_BIN`` env-var overrides,
consistent with the ``${XCE_CONTROLLER_BIN:-default}``-style pattern
already used by ``clean_failover_preservation.sh``/``run-ha-soak.sh``.
Tests the pure resolution helper directly (not the frozen module-level
``CONTROLLER_CANDIDATES``/``XCEADMIN_CANDIDATES`` constants, which are
computed once at import time from whatever the environment happened to be
-- re-importing the module under a patched environment is fragile/awkward
under pytest's module caching, so the override LOGIC is factored into a
directly-testable function instead).
"""
from __future__ import annotations
from pathlib import Path
from _live_e2e_common import resolve_binary_candidates
def test_no_env_override_returns_default_candidates_unchanged(monkeypatch):
monkeypatch.delenv("XCE_SOME_TEST_BIN", raising=False)
defaults = [Path("/a/xce.build/x"), Path("/a/build.release/x")]
result = resolve_binary_candidates("XCE_SOME_TEST_BIN", defaults)
assert result == defaults
def test_empty_env_override_falls_back_to_default_candidates(monkeypatch):
monkeypatch.setenv("XCE_SOME_TEST_BIN", "")
defaults = [Path("/a/xce.build/x")]
result = resolve_binary_candidates("XCE_SOME_TEST_BIN", defaults)
assert result == defaults
def test_env_override_becomes_sole_candidate(monkeypatch, tmp_path):
custom_bin = tmp_path / "my-custom-build" / "controller" / "xce_controller"
monkeypatch.setenv("XCE_SOME_TEST_BIN", str(custom_bin))
defaults = [Path("/a/xce.build/x"), Path("/a/build.dbg/x")]
result = resolve_binary_candidates("XCE_SOME_TEST_BIN", defaults)
assert result == [custom_bin]
def test_controller_and_xceadmin_candidates_respect_their_own_env_vars(monkeypatch, tmp_path):
"""End-to-end through the real module constants' construction: reload
the module with each override env var set and confirm the exported
candidate lists reduce to the single overridden path, proving the
actual production wiring (not just the helper in isolation) is correct.
"""
import importlib
import _live_e2e_common as mod
custom_controller = tmp_path / "custom" / "xce_controller"
custom_xceadmin = tmp_path / "custom" / "xceadmin"
monkeypatch.setenv("XCE_CONTROLLER_BIN", str(custom_controller))
monkeypatch.setenv("XCEADMIN_BIN", str(custom_xceadmin))
try:
importlib.reload(mod)
assert mod.CONTROLLER_CANDIDATES == [custom_controller]
assert mod.XCEADMIN_CANDIDATES == [custom_xceadmin]
finally:
monkeypatch.delenv("XCE_CONTROLLER_BIN", raising=False)
monkeypatch.delenv("XCEADMIN_BIN", raising=False)
importlib.reload(mod) # restore default candidates for any test after this one