Skip tests when DNS can't resolve a dotted FQDN
MODIFY
test/__init__.py
test/__init__.py
@@ -39,6 +39,21 @@ SHORT_TIMEOUT = 0.001
LONG_TIMEOUT = 0.5 if os.environ.get("CI") else 0.01
+def _can_resolve(host):
+ """ Returns True if the system can resolve host to an address. """
+ try:
+ socket.getaddrinfo(host, None, socket.AF_UNSPEC)
+ return True
+ except socket.gaierror:
+ return False
+
+
+# Some systems might not resolve "localhost." correctly.
+# See https://github.com/urllib3/urllib3/issues/1809 and
+# https://github.com/urllib3/urllib3/pull/1475#issuecomment-440788064.
+RESOLVES_LOCALHOST_FQDN = _can_resolve("localhost.")
+
+
def clear_warnings(cls=HTTPWarning):
new_filters = []
for f in warnings.filters:
@@ -200,23 +215,6 @@ def requires_ssl_context_keyfile_password(test):
return wrapper
-def fails_on_travis_gce(test):
- """Expect the test to fail on Google Compute Engine instances for Travis.
- Travis uses GCE for its sudo: enabled builds.
-
- Reason for this decorator:
- https://github.com/urllib3/urllib3/pull/1475#issuecomment-440788064
- """
-
- @six.wraps(test)
- def wrapper(*args, **kwargs):
- if os.environ.get("TRAVIS_INFRA") in ("gce", "unknown"):
- pytest.xfail("%s is expected to fail on Travis GCE builds" % test.__name__)
- return test(*args, **kwargs)
-
- return wrapper
-
-
def requiresTLSv1():
"""Test requires TLSv1 available"""
return pytest.mark.skipif(
@@ -245,6 +243,18 @@ def requiresTLSv1_3():
)
+def resolvesLocalhostFQDN(test):
+ """Test requires successful resolving of 'localhost.'"""
+
+ @six.wraps(test)
+ def wrapper(*args, **kwargs):
+ if not RESOLVES_LOCALHOST_FQDN:
+ pytest.skip("Can't resolve localhost.")
+ return test(*args, **kwargs)
+
+ return wrapper
+
+
class _ListHandler(logging.Handler):
def __init__(self):
super(_ListHandler, self).__init__()

MODIFY
test/test_poolmanager.py
test/test_poolmanager.py
@@ -6,9 +6,11 @@ from urllib3.poolmanager import PoolKey, key_fn_by_scheme, PoolManager
from urllib3 import connection_from_url
from urllib3.exceptions import ClosedPoolError, LocationValueError
from urllib3.util import retry, timeout
+from test import resolvesLocalhostFQDN
class TestPoolManager(object):
+ @resolvesLocalhostFQDN
def test_same_url(self):
# Convince ourselves that normally we don't get the same object
conn1 = connection_from_url("http://localhost:8081/foo")

MODIFY
test/with_dummyserver/test_https.py
test/with_dummyserver/test_https.py
@@ -26,7 +26,6 @@ from test import (
notOpenSSL098,
requires_network,
requires_ssl_context_keyfile_password,
- fails_on_travis_gce,
requiresTLSv1,
requiresTLSv1_1,
requiresTLSv1_2,
@@ -34,6 +33,7 @@ from test import (
TARPIT_HOST,
SHORT_TIMEOUT,
LONG_TIMEOUT,
+ resolvesLocalhostFQDN,
)
from urllib3 import HTTPSConnectionPool
from urllib3.connection import VerifiedHTTPSConnection, RECENT_DATE
@@ -133,7 +133,7 @@ class TestHTTPS(HTTPSDummyServerTestCase):
r = https_pool.request("GET", "/")
assert r.status == 200, r.data
- @fails_on_travis_gce
+ @resolvesLocalhostFQDN
def test_dotted_fqdn(self):
with HTTPSConnectionPool(
self.host + ".", self.port, ca_certs=DEFAULT_CA

MODIFY
test/with_dummyserver/test_socketlevel.py
test/with_dummyserver/test_socketlevel.py
@@ -50,11 +50,11 @@ import pytest
import trustme
from test import (
- fails_on_travis_gce,
requires_ssl_context_keyfile_password,
SHORT_TIMEOUT,
LONG_TIMEOUT,
notPyPy2,
+ resolvesLocalhostFQDN,
)
# Retry failed tests
@@ -1507,7 +1507,7 @@ class TestHeaders(SocketDummyServerTestCase):
pool.request("GET", "/", headers=OrderedDict(expected_request_headers))
assert expected_request_headers == actual_request_headers
- @fails_on_travis_gce
+ @resolvesLocalhostFQDN
def test_request_host_header_ignores_fqdn_dot(self):
received_headers = []

Skip tests when DNS can't resolve a dotted FQDN