Files
pipelines/scripts/test_lan_address.py
T

43 lines
1.9 KiB
Python

import importlib.util
import json
from pathlib import Path
import subprocess
import unittest
spec = importlib.util.spec_from_file_location("lan", Path(__file__).with_name("lan-address.py"))
lan = importlib.util.module_from_spec(spec)
spec.loader.exec_module(lan)
class LanTests(unittest.TestCase):
def test_linux_prefers_default_physical_interface_not_vpn_or_docker(self):
def run(command, **kwargs):
if "route" in command:
data = [{"dev":"tailscale0", "metric":0}, {"dev":"wlan0", "metric":100}]
else:
data = [{"ifname":name, "addr_info":[{"local":ip, "scope":"global"}]}
for name,ip in [("docker0","172.17.0.1"),("tun0","10.7.0.1"),
("eth0","10.0.0.2"),("wlan0","192.168.1.25")]]
return subprocess.CompletedProcess(command, 0, stdout=json.dumps(data))
self.assertEqual(lan.detect_lan(run, "linux"), "192.168.1.25")
def test_macos_finds_wifi_even_when_default_route_is_vpn(self):
def run(command, **kwargs):
data = "interface: utun3" if "route" in command[0] else (
"utun3: flags=UP,RUNNING\n\tinet 10.7.0.1\n"
"en0: flags=UP,RUNNING\n\tinet 192.168.1.25 netmask 0xffffff00\n")
return subprocess.CompletedProcess(command, 0, stdout=data)
self.assertEqual(lan.detect_lan(run, "darwin"), "192.168.1.25")
def test_no_tools_uses_local_route_selection_without_remote_request(self):
def missing(*args, **kwargs):
raise FileNotFoundError()
self.assertEqual(lan.detect_lan(missing, "linux", lambda: "192.168.1.25"), "192.168.1.25")
for ip in ["127.0.0.1","100.85.25.33","169.254.1.2","8.8.8.8"]:
with self.assertRaises(ValueError):
lan.detect_lan(missing, "linux", lambda: ip)
if __name__ == "__main__":
unittest.main()