runtests: add a function to test if IPv6 is available
authorJun Wu <quark@fb.com>
Wed, 15 Feb 2017 16:18:31 -0800
changeset 30984 15f9084a9a0c
parent 30983 d4825798818b
child 30985 1f803482844a
runtests: add a function to test if IPv6 is available Previously, checkportisavailable returns True if the port is free either on IPv4 or IPv6, but the hg server only uses IPv4 by default. That leads to issues when IPv4 port is not free but the IPv6 one is. To address that, run-tests should stick with either IPv4 or IPv6. This patch adds a function similar to checkportisavailable to test if IPv6 is available, and assigns the result to a variable. The new function was tested in a Linux system script with the following steps: 1. Run "ip addr del ::1/128 dev lo" to delete lo's IPv6 address, Confirm checkipv6available() returns False. 2. Run "ip addr add ::1/128 dev lo" to add back lo's IPv6 address. Confirm checkipv6available() returns True. 3. Start a web server taking the 8000 port. Confirm checkipv6available(8000) is still True.
tests/run-tests.py
--- a/tests/run-tests.py	Wed Feb 15 13:34:06 2017 -0800
+++ b/tests/run-tests.py	Wed Feb 15 16:18:31 2017 -0800
@@ -112,6 +112,29 @@
 # For Windows support
 wifexited = getattr(os, "WIFEXITED", lambda x: False)
 
+# Whether to use IPv6
+def checkipv6available(port=20058):
+    """return true if we can listen on localhost's IPv6 ports"""
+    family = getattr(socket, 'AF_INET6', None)
+    if family is None:
+        return False
+    try:
+        s = socket.socket(family, socket.SOCK_STREAM)
+        s.bind(('localhost', port))
+        s.close()
+        return True
+    except socket.error as exc:
+        if exc.errno == errno.EADDRINUSE:
+            return True
+        elif exc.errno in (errno.EADDRNOTAVAIL, errno.EPROTONOSUPPORT):
+            return False
+        else:
+            raise
+    else:
+        return False
+
+useipv6 = checkipv6available()
+
 def checkportisavailable(port):
     """return true if a port seems free to bind on localhost"""
     families = [getattr(socket, i, None)