Skip to content

Commit 3658194

Browse files
committed
tests: Handle square bracket IPv6 from docker port
Fixes ``` ====================================================================== ERROR: setUpClass (ssh_test.BannerTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/vsts/work/1/s/tests/testlib.py", line 625, in setUpClass cls.dockerized_ssh = DockerizedSshDaemon(**daemon_args) File "/home/vsts/work/1/s/tests/testlib.py", line 553, in __init__ self.start_container() File "/home/vsts/work/1/s/tests/testlib.py", line 533, in start_container self._get_container_port() File "/home/vsts/work/1/s/tests/testlib.py", line 510, in _get_container_port self.port = int(bport) ValueError: invalid literal for int() with base 10: ':]:32770' ```
1 parent 47b9309 commit 3658194

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

tests/testlib.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -499,19 +499,18 @@ def get_docker_host():
499499

500500

501501
class DockerizedSshDaemon(object):
502-
def _get_container_port(self):
503-
s = subprocess.check_output(['docker', 'port', self.container_name])
504-
for line in s.decode().splitlines():
505-
m = self.PORT_RE.match(line)
506-
if not m:
507-
continue
508-
dport, proto, _, bport = m.groups()
509-
if dport == '22' and proto == 'tcp':
510-
self.port = int(bport)
502+
PORT_RE = re.compile(
503+
# e.g. 0.0.0.0:32771, :::32771, [::]:32771'
504+
r'(?P<addr>[0-9.]+|::|\[[a-f0-9:.]+\]):(?P<port>[0-9]+)',
505+
)
511506

512-
self.host = self.get_host()
513-
if self.port is None:
507+
@classmethod
508+
def get_port(cls, container):
509+
s = subprocess.check_output(['docker', 'port', container, '22/tcp'])
510+
m = cls.PORT_RE.search(s)
511+
if not m:
514512
raise ValueError('could not find SSH port in: %r' % (s,))
513+
return int(m.group('port'))
515514

516515
def start_container(self):
517516
try:
@@ -530,7 +529,6 @@ def start_container(self):
530529
self.image,
531530
]
532531
subprocess.check_output(args)
533-
self._get_container_port()
534532

535533
def __init__(self, mitogen_test_distro=os.environ.get('MITOGEN_TEST_DISTRO', 'debian9')):
536534
if '-' in mitogen_test_distro:
@@ -545,12 +543,9 @@ def __init__(self, mitogen_test_distro=os.environ.get('MITOGEN_TEST_DISTRO', 'de
545543
self.python_path = '/usr/bin/python'
546544

547545
self.image = 'public.ecr.aws/n5z0e8q9/%s-test' % (distro,)
548-
549-
# 22/tcp -> 0.0.0.0:32771
550-
self.PORT_RE = re.compile(r'([^/]+)/([^ ]+) -> ([^:]+):(.*)')
551-
self.port = None
552-
553546
self.start_container()
547+
self.host = self.get_host()
548+
self.port = self.get_port(self.container_name)
554549

555550
def get_host(self):
556551
return get_docker_host()

0 commit comments

Comments
 (0)