import copy import pytest from app.graph import build_graph, parse_vless_route from .fixtures import make_hosts, make_nodes, make_profile def by_id(g): return {n["id"]: n for n in g["nodes"]} def targets(g, src): return [e["target"] for e in g["edges"] if e["source"] == src] @pytest.fixture def g(): return build_graph(make_profile(), make_hosts(), make_nodes()) def test_parse_vless_route(): assert parse_vless_route(None) is None assert parse_vless_route(5) == {5} assert parse_vless_route("8-14") == set(range(8, 15)) assert parse_vless_route("1,3,8-10") == {1, 3, 8, 9, 10} def test_all_bridges_resolve(g): assert not [n for n in g["nodes"] if n["type"] == "unresolved"] expected = {"BRIDGE_1": "FR-01", "BRIDGE_2": "NL-01", "BRIDGE_3": "FI-01", "BRIDGE_4": "NL-04", "BRIDGE_5": "CH-01", "BRIDGE_6": "BR-01"} for out, node in expected.items(): assert targets(g, f"out:{out}") == [f"node:{node}"] assert "node:US-01" not in by_id(g) def test_shadowed_rules(g): shadowed = [n["data"]["index"] for n in g["nodes"] if n["type"] == "rule" and n["data"]["shadowed"]] assert shadowed == list(range(14, 22)) assert sum("недостижимо" in w for w in g["warnings"]) == 8 def test_host_trace(g): b = by_id(g) assert b["host:h3"]["data"]["trace"][-1] == "rule:8" # route 3 -> BRIDGE_3 assert b["host:h7"]["data"]["trace"][-1] == "rule:12" # route 7 -> BRIDGE_AUTO assert b["host:h8"]["data"]["trace"][-1] == "rule:13" # route 8 упирается в catch-all assert b["host:hn"]["data"]["trace"][-1] == "rule:13" assert "host:hx" not in b # хост другого профиля def test_no_secrets_in_output(g): dump = str(g) assert "SECRET" not in dump def test_disabled_and_offline_nodes(): nodes = make_nodes() for n in nodes: if n["name"] == "NL-04": n["isDisabled"] = True if n["name"] == "BR-01": n["isConnected"] = False nodes.insert(0, {"name": "OLD-FR", "address": "100.106.180.60", "isDisabled": True, "ips": [{"ip": "100.106.180.60", "status": "MANAGEMENT"}], "system": None}) g = build_graph(make_profile(), make_hosts(), nodes) b = by_id(g) assert targets(g, "out:BRIDGE_1") == ["node:FR-01"] # живая нода выигрывает у дубля assert b["node:NL-04"]["data"]["state"] == "disabled" assert b["node:BR-01"]["data"]["state"] == "offline" assert b["bal:BRIDGE_AUTO"]["data"]["aliveMembers"] == ["BRIDGE_1", "BRIDGE_2", "BRIDGE_3", "BRIDGE_5"] dead = {e["source"] for e in g["edges"] if e["kind"] == "link" and e["data"].get("dead")} assert dead == {"out:BRIDGE_4", "out:BRIDGE_6"} def test_all_nodes_down_warns(): nodes = make_nodes() for n in nodes: n["isDisabled"] = True g = build_graph(make_profile(), make_hosts(), nodes) assert any("нет ни одного живого члена" in w for w in g["warnings"]) assert by_id(g)["host:h3"]["data"]["exitAlive"] is False def test_unresolved_outbound(): nodes = [n for n in make_nodes() if n["name"] != "NL-04"] g = build_graph(make_profile(), make_hosts(), nodes) assert "unresolved:100.106.134.18" in by_id(g) assert any("не найден" in w for w in g["warnings"]) def test_input_not_mutated(): profile, hosts, nodes = make_profile(), make_hosts(), make_nodes() snapshot = copy.deepcopy((profile, hosts, nodes)) build_graph(profile, hosts, nodes) assert (profile, hosts, nodes) == snapshot