"""Unit tests for mapping config-defined SNMP devices onto a Steward host.""" from plugins.snmp.routes import _devices_for_host _CFG = [ {"name": "core-switch", "host": "192.168.1.1"}, {"name": "ups", "host": "192.168.1.10"}, {"name": "by-name", "host": "edge-router"}, "not-a-dict", # tolerated, skipped {"name": "no-host"}, # no host → never matches ] def test_matches_by_address(): out = _devices_for_host(_CFG, "192.168.1.1", "somehost") assert [d["name"] for d in out] == ["core-switch"] def test_matches_by_name_case_insensitive(): out = _devices_for_host(_CFG, "10.0.0.9", "Edge-Router") assert [d["name"] for d in out] == ["by-name"] def test_no_match_returns_empty(): assert _devices_for_host(_CFG, "10.0.0.1", "nope") == [] def test_blank_address_and_name_match_nothing(): # A host with no address/name must not match a device with a blank host. assert _devices_for_host(_CFG, "", None) == [] def test_implicit_match_unaffected_when_host_id_passed(): # Passing the new host_id arg must not break the legacy implicit match. out = _devices_for_host(_CFG, "192.168.1.1", "somehost", host_id="h-1") assert [d["name"] for d in out] == ["core-switch"] # ── Explicit binding ───────────────────────────────────────────────────────── def test_explicit_host_id_matches_exact_uuid(): cfg = [{"name": "sw", "host": "10.0.0.5", "host_id": "uuid-abc"}] out = _devices_for_host(cfg, "10.0.0.9", "other", host_id="uuid-abc") assert [d["name"] for d in out] == ["sw"] def test_explicit_steward_host_matches_by_name_or_address(): cfg = [{"name": "pdu", "host": "10.0.0.5", "steward_host": "Switch01"}] # Bind hits the Host's name despite a different poll target / address. by_name = _devices_for_host(cfg, "10.0.0.9", "switch01", host_id="h9") by_addr = _devices_for_host(cfg, "switch01", "whatever", host_id="h9") assert [d["name"] for d in by_name] == ["pdu"] assert [d["name"] for d in by_addr] == ["pdu"] def test_explicit_binding_is_exclusive(): # Device polls 192.168.1.1 but is explicitly bound to host A by name. cfg = [{"name": "sw", "host": "192.168.1.1", "steward_host": "hostA"}] # Host B *would* match implicitly by the poll target — but must NOT, # because the explicit binding takes over and points only at host A. hostB = _devices_for_host(cfg, "192.168.1.1", "hostB", host_id="b") hostA = _devices_for_host(cfg, "10.0.0.2", "hostA", host_id="a") assert hostB == [] assert [d["name"] for d in hostA] == ["sw"] def test_explicit_host_id_wrong_uuid_does_not_match(): cfg = [{"name": "sw", "host": "10.0.0.5", "host_id": "uuid-abc"}] out = _devices_for_host(cfg, "10.0.0.5", "sw", host_id="uuid-zzz") assert out == []