Spaces:
Running
Running
Download tests/test_network.py from brand0nwalls/NetOps: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/tests/test_network.py
- Command line
-
hf download hf://spaces/brand0nwalls/NetOps/tests/test_network.py
-
curl -L -o test_network.py https://huggingface.co/spaces/brand0nwalls/NetOps/resolve/main/tests/test_network.py
3.77 kB
| """Test network topology creation, convergence, and lifecycle.""" | |
| import os | |
| import sys | |
| import time | |
| import json | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from server.network import NetworkManager | |
| from server.constants import AS_CONFIGS, EBGP_PEERS | |
| def test_network_lifecycle(): | |
| nm = NetworkManager() | |
| passed = 0 | |
| failed = 0 | |
| # --- Start --- | |
| print("[1] Starting 3-AS network...") | |
| t0 = time.time() | |
| startup_time = nm.start() | |
| print(f" Network up in {startup_time:.1f}s") | |
| # --- Router count --- | |
| print("[2] Checking router count...") | |
| routers = nm.get_all_routers() | |
| expected_routers = sum(len(c["routers"]) for c in AS_CONFIGS.values()) | |
| if len(routers) == expected_routers: | |
| print(f" PASS: {len(routers)} routers") | |
| passed += 1 | |
| else: | |
| print(f" FAIL: expected {expected_routers}, got {len(routers)}") | |
| failed += 1 | |
| # --- Host count --- | |
| print("[3] Checking host count...") | |
| hosts = nm.get_all_hosts() | |
| expected_hosts = sum(len(c["hosts"]) for c in AS_CONFIGS.values()) | |
| if len(hosts) == expected_hosts: | |
| print(f" PASS: {len(hosts)} hosts") | |
| passed += 1 | |
| else: | |
| print(f" FAIL: expected {expected_hosts}, got {len(hosts)}") | |
| failed += 1 | |
| # --- BGP sessions --- | |
| print("[4] Checking BGP sessions established...") | |
| all_established = True | |
| for r in routers: | |
| try: | |
| out = r.cmd('vtysh -c "show bgp summary json"') | |
| bgp = json.loads(out) | |
| peers = bgp.get("ipv4Unicast", {}).get("peers", {}) | |
| for peer_ip, info in peers.items(): | |
| if info.get("state") != "Established": | |
| print(f" FAIL: {r.name} peer {peer_ip} state={info.get('state')}") | |
| all_established = False | |
| except Exception as e: | |
| print(f" FAIL: {r.name} BGP read error: {e}") | |
| all_established = False | |
| if all_established: | |
| print(" PASS: all BGP sessions established") | |
| passed += 1 | |
| else: | |
| failed += 1 | |
| # --- Cross-AS ping --- | |
| print("[5] Cross-AS reachability (h1 -> h3, h1 -> h5)...") | |
| for src, dst in [("h1", "h3"), ("h1", "h5"), ("h3", "h5")]: | |
| result = nm.ping(src, dst) | |
| if result["loss_pct"] == 0: | |
| print(f" PASS: {src} -> {dst} (0% loss)") | |
| passed += 1 | |
| else: | |
| print(f" FAIL: {src} -> {dst} ({result['loss_pct']}% loss)") | |
| failed += 1 | |
| # --- Link interface lookup --- | |
| print("[6] Link interface lookup...") | |
| for a, b in EBGP_PEERS[:1]: | |
| intf = nm.find_link_interface(a, b) | |
| if intf is not None: | |
| print(f" PASS: {a}->{b} interface = {intf.name}") | |
| passed += 1 | |
| else: | |
| print(f" FAIL: no interface found for {a}->{b}") | |
| failed += 1 | |
| # --- Traceroute --- | |
| print("[7] Traceroute h1 -> h5...") | |
| tr = nm.traceroute("h1", "h5") | |
| hops = [l for l in tr.split("\n") if l.strip() and not l.startswith("traceroute")] | |
| print(f" {len(hops)} hops") | |
| if len(hops) > 0: | |
| print(" PASS: traceroute returned hops") | |
| passed += 1 | |
| else: | |
| print(" FAIL: no hops returned") | |
| failed += 1 | |
| # --- Stop --- | |
| print("[8] Stopping network...") | |
| nm.stop() | |
| if not nm.running: | |
| print(" PASS: network stopped") | |
| passed += 1 | |
| else: | |
| print(" FAIL: network still running") | |
| failed += 1 | |
| print(f"\n{'='*40}") | |
| print(f"RESULTS: {passed} passed, {failed} failed") | |
| print(f"{'='*40}") | |
| return failed == 0 | |
| if __name__ == "__main__": | |
| success = test_network_lifecycle() | |
| sys.exit(0 if success else 1) | |