[PATCH 1 of 4] runtests: add a function to test if IPv6 is available

Jun Wu quark at fb.com
Thu Feb 16 00:50:14 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1487204311 28800
#      Wed Feb 15 16:18:31 2017 -0800
# Node ID a70fa1e0fcdb11980338d72dde33dfe047bda7c2
# Parent  e5363cb96233861fc99f7e9b85d7884d3121558c
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r a70fa1e0fcdb
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.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -113,4 +113,27 @@ else:
 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"""


More information about the Mercurial-devel mailing list