diff --git a/tests/run-multitests.py b/tests/run-multitests.py index 20ef4a7aa6..f55d52b6e9 100755 --- a/tests/run-multitests.py +++ b/tests/run-multitests.py @@ -66,7 +66,7 @@ class multitest: import network ip = network.WLAN().ifconfig()[0] except: - ip = "127.0.0.1" + ip = HOST_IP return ip {} @@ -87,6 +87,20 @@ IGNORE_OUTPUT_MATCHES = ( ) +def get_host_ip(_ip_cache=[]): + if not _ip_cache: + try: + import socket + + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("8.8.8.8", 80)) + _ip_cache.append(s.getsockname()[0]) + s.close() + except: + _ip_cache.append("127.0.0.1") + return _ip_cache[0] + + class PyInstance: def __init__(self): pass @@ -274,6 +288,13 @@ def run_test_on_instances(test_file, num_instances, instances): injected_globals = "" output = [[] for _ in range(num_instances)] + # If the test calls get_network_ip() then inject HOST_IP so that devices can know + # the IP address of the host. Do this lazily to not require a TCP/IP connection + # on the host if it's not needed. + with open(test_file, "rb") as f: + if b"get_network_ip" in f.read(): + injected_globals += "HOST_IP = '" + get_host_ip() + "'\n" + if cmd_args.trace_output: print("TRACE {}:".format("|".join(str(i) for i in instances)))