"""Tests for agent-reported primary IP (task 274).""" from plugins.host_agent.routes import pick_host_address from plugins.host_agent import agent as a # ── pick_host_address: fill only when blank, never clobber ──────────────────── def test_pick_fills_when_current_blank(): assert pick_host_address("", "10.0.0.5") == "10.0.0.5" assert pick_host_address(None, "10.0.0.5") == "10.0.0.5" assert pick_host_address(" ", "10.0.0.5") == "10.0.0.5" def test_pick_keeps_admin_value(): # A non-blank current address is never overwritten. assert pick_host_address("monitor.example.com", "10.0.0.5") is None assert pick_host_address("192.168.1.9", "10.0.0.5") is None def test_pick_no_reported_ip_is_noop(): assert pick_host_address("", None) is None assert pick_host_address("", "") is None assert pick_host_address(None, None) is None # ── detect_primary_ip: never raises; None or a non-loopback string ──────────── def test_detect_primary_ip_never_raises_and_is_sane(): ip = a.detect_primary_ip() assert ip is None or isinstance(ip, str) if ip is not None: assert not ip.startswith("127.") assert ip # non-empty # ── host_ip rides in the metadata bag the agent sends ───────────────────────── def test_collect_metadata_includes_host_ip_key(): md = a.collect_metadata() assert "host_ip" in md # present even if None when offline assert set(md) >= {"kernel", "distro", "arch", "host_ip"}