Merge pull request #8 from NeverBehave/master

fix: resolve IPv6 container hostname
main
Alexander Graf 2 years ago
parent b711f930ef
commit 9f511faf64
No known key found for this signature in database
GPG Key ID: B8A9DC143E075629

@ -1 +1,22 @@
.DS_Store
.idea
tmp
*.bak
*~
.*.swp
__pycache__/ __pycache__/
*.pyc
*.pyo
*.egg-info/
.build
.env*
.venv
*.code-workspace
venv/
build/
dist/

@ -10,7 +10,7 @@ def resolve_hostname(hostname):
It is capable of retrying in case the host is not immediately available It is capable of retrying in case the host is not immediately available
""" """
try: try:
return socket.gethostbyname(hostname) return sorted(socket.getaddrinfo(hostname, None, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE), key=lambda s:s[0])[0][4][0]
except Exception as e: except Exception as e:
log.warn("Unable to lookup '%s': %s",hostname,e) log.warn("Unable to lookup '%s': %s",hostname,e)
raise e raise e
@ -22,6 +22,8 @@ def resolve_address(address):
""" """
hostname, *rest = address.rsplit(":", 1) hostname, *rest = address.rsplit(":", 1)
ip_address = resolve_hostname(hostname) ip_address = resolve_hostname(hostname)
if ":" in ip_address:
ip_address = "[{}]".format(ip_address)
return ip_address + "".join(":" + port for port in rest) return ip_address + "".join(":" + port for port in rest)

@ -70,15 +70,24 @@ class TestSystem(unittest.TestCase):
def test_resolve_hostname(self): def test_resolve_hostname(self):
self.assertEqual( self.assertEqual(
system.resolve_hostname("1.2.3.4.xip.io"), system.resolve_hostname("1.2.3.4.sslip.io"),
"1.2.3.4" "1.2.3.4"
) )
self.assertEqual(
system.resolve_hostname("2001-db8--f00.sslip.io"),
"2001:db8::f00"
)
def test_resolve_address(self): def test_resolve_address(self):
self.assertEqual( self.assertEqual(
system.resolve_address("1.2.3.4.xip.io:80"), system.resolve_address("1.2.3.4.sslip.io:80"),
"1.2.3.4:80" "1.2.3.4:80"
) )
self.assertEqual(
system.resolve_address("2001-db8--f00.sslip.io:80"),
"[2001:db8::f00]:80"
)
def test_get_host_address_from_environment(self): def test_get_host_address_from_environment(self):
if "TEST_ADDRESS" in os.environ: if "TEST_ADDRESS" in os.environ:
@ -87,20 +96,20 @@ class TestSystem(unittest.TestCase):
del os.environ["HOST_TEST"] del os.environ["HOST_TEST"]
# if nothing is set, the default must be resolved # if nothing is set, the default must be resolved
self.assertEqual( self.assertEqual(
system.get_host_address_from_environment("TEST", "1.2.3.4.xip.io:80"), system.get_host_address_from_environment("TEST", "1.2.3.4.sslip.io:80"),
"1.2.3.4:80" "1.2.3.4:80"
) )
# if HOST is set, the HOST must be resolved # if HOST is set, the HOST must be resolved
os.environ['HOST_TEST']="1.2.3.5.xip.io:80" os.environ['HOST_TEST']="1.2.3.5.sslip.io:80"
self.assertEqual( self.assertEqual(
system.get_host_address_from_environment("TEST", "1.2.3.4.xip.io:80"), system.get_host_address_from_environment("TEST", "1.2.3.4.sslip.io:80"),
"1.2.3.5:80" "1.2.3.5:80"
) )
# if ADDRESS is set, the ADDRESS must be returned unresolved # if ADDRESS is set, the ADDRESS must be returned unresolved
os.environ['TEST_ADDRESS']="1.2.3.6.xip.io:80" os.environ['TEST_ADDRESS']="1.2.3.6.sslip.io:80"
self.assertEqual( self.assertEqual(
system.get_host_address_from_environment("TEST", "1.2.3.4.xip.io:80"), system.get_host_address_from_environment("TEST", "1.2.3.4.sslip.io:80"),
"1.2.3.6.xip.io:80" "1.2.3.6.sslip.io:80"
) )

Loading…
Cancel
Save