Spaces:
Running
Running
Download tests/test_telemetry.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 5.2 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/tests/test_telemetry.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/tests/test_telemetry.py
-
curl -L -o test_telemetry.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/tests/test_telemetry.py
5.2 kB
| """Test all telemetry readers against a live network.""" | |
| import os | |
| import sys | |
| import json | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from server.network import NetworkManager | |
| from server import telemetry | |
| def test_telemetry(): | |
| nm = NetworkManager() | |
| passed = 0 | |
| failed = 0 | |
| print("[0] Starting network...") | |
| nm.start() | |
| routers = nm.get_all_routers() | |
| test_router = routers[0] | |
| print(f" Using router: {test_router.name}") | |
| # --- BGP summary --- | |
| print(f"[1] read_bgp_summary({test_router.name})...") | |
| try: | |
| data = telemetry.read_bgp_summary(test_router) | |
| peers = data.get("ipv4Unicast", {}).get("peers", {}) | |
| print(f" PASS: {len(peers)} peers") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- BGP neighbors detail --- | |
| print(f"[2] read_bgp_neighbors_detail({test_router.name})...") | |
| try: | |
| data = telemetry.read_bgp_neighbors_detail(test_router) | |
| assert isinstance(data, dict) | |
| print(f" PASS: {len(data)} neighbor entries") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- OSPF neighbors --- | |
| print(f"[3] read_ospf_neighbors({test_router.name})...") | |
| try: | |
| data = telemetry.read_ospf_neighbors(test_router) | |
| assert isinstance(data, dict) | |
| print(f" PASS: got OSPF data") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- OSPF interface --- | |
| print(f"[4] read_ospf_interface({test_router.name})...") | |
| try: | |
| data = telemetry.read_ospf_interface(test_router) | |
| assert isinstance(data, dict) | |
| print(f" PASS: got OSPF interface data") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- Routes --- | |
| print(f"[5] read_routes({test_router.name})...") | |
| try: | |
| data = telemetry.read_routes(test_router) | |
| assert isinstance(data, dict) | |
| print(f" PASS: {len(data)} route prefixes") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- Interfaces --- | |
| print(f"[6] read_interfaces({test_router.name})...") | |
| try: | |
| data = telemetry.read_interfaces(test_router) | |
| assert isinstance(data, list) | |
| print(f" PASS: {len(data)} interfaces") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- Queue stats --- | |
| print(f"[7] read_queue_stats({test_router.name})...") | |
| try: | |
| intfs = telemetry.read_interfaces(test_router) | |
| intf_name = None | |
| for i in intfs: | |
| name = i.get("ifname", "") | |
| if name != "lo" and not name.startswith("sit"): | |
| intf_name = name | |
| break | |
| if intf_name: | |
| data = telemetry.read_queue_stats(test_router, intf_name) | |
| print(f" PASS: {len(data.get('qdiscs', []))} qdiscs on {intf_name}") | |
| passed += 1 | |
| else: | |
| print(" SKIP: no suitable interface found") | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- Protocol counters --- | |
| print(f"[8] read_protocol_counters({test_router.name})...") | |
| try: | |
| data = telemetry.read_protocol_counters(test_router) | |
| snmp = data.get("snmp", {}) | |
| protocols = list(snmp.keys()) | |
| print(f" PASS: protocols = {protocols}") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- read_all_router_telemetry --- | |
| print(f"[9] read_all_router_telemetry({test_router.name})...") | |
| try: | |
| data = telemetry.read_all_router_telemetry(test_router) | |
| errors = [k for k in data if k.endswith("_error")] | |
| successes = [k for k in data if not k.endswith("_error")] | |
| print(f" {len(successes)} streams OK, {len(errors)} errors") | |
| if errors: | |
| for e in errors: | |
| print(f" ERROR: {e} = {data[e]}") | |
| if len(errors) == 0: | |
| print(" PASS") | |
| passed += 1 | |
| else: | |
| print(" FAIL: some streams errored") | |
| failed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| # --- OVS (may not be present) --- | |
| print("[10] OVS switch telemetry...") | |
| switches = nm.get_all_switches() | |
| if switches: | |
| try: | |
| data = telemetry.read_all_switch_telemetry(switches[0]) | |
| errors = [k for k in data if k.endswith("_error")] | |
| print(f" {len(switches)} switches, errors: {errors}") | |
| passed += 1 | |
| except Exception as e: | |
| print(f" FAIL: {e}") | |
| failed += 1 | |
| else: | |
| print(" SKIP: no OVS switches in topology") | |
| print("\n[cleanup] Stopping network...") | |
| nm.stop() | |
| print(f"\n{'='*40}") | |
| print(f"RESULTS: {passed} passed, {failed} failed") | |
| print(f"{'='*40}") | |
| return failed == 0 | |
| if __name__ == "__main__": | |
| success = test_telemetry() | |
| sys.exit(0 if success else 1) | |