import json import subprocess from scripts import failover_monitor as monitor def test_check_health_reports_online(monkeypatch): def fake_check_call(cmd, stdout=None): assert cmd[:4] == ["ping", "-c", "1", "-W"] return 0 monkeypatch.setattr(monitor.subprocess, "check_call", fake_check_call) assert monitor.check_health("1.2.3.4") == "ONLINE" def test_check_health_reports_offline(monkeypatch): def fake_check_call(cmd, stdout=None): raise subprocess.CalledProcessError(returncode=1, cmd=cmd) monkeypatch.setattr(monitor.subprocess, "check_call", fake_check_call) assert monitor.check_health("1.2.3.4") == "OFFLINE" def test_main_writes_status_file_and_prints(tmp_path, monkeypatch, capsys): monkeypatch.setattr(monitor, "STATUS_FILE", tmp_path / "failover_status.json") monkeypatch.setattr(monitor, "FLEET", {"ezra": "1.1.1.1", "bezalel": "2.2.2.2"}) monkeypatch.setattr(monitor.time, "time", lambda: 1713148800.0) monkeypatch.setattr( monitor, "check_health", lambda host: "ONLINE" if host == "1.1.1.1" else "OFFLINE", ) monitor.main() payload = json.loads(monitor.STATUS_FILE.read_text()) assert payload == { "timestamp": 1713148800.0, "fleet": {"ezra": "ONLINE", "bezalel": "OFFLINE"}, } captured = capsys.readouterr() assert "ALLEGRO FAILOVER MONITOR" in captured.out.upper() assert "EZRA: ONLINE" in captured.out assert "BEZALEL: OFFLINE" in captured.out