Skip to content

Commit

Permalink
tests: Handle square bracket IPv6 from docker port
Browse files Browse the repository at this point in the history
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'
```
  • Loading branch information
moreati committed Aug 2, 2023
1 parent 47b9309 commit 1fa907d
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions tests/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,19 +499,18 @@ def get_docker_host():


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

self.host = self.get_host()
if self.port is None:
@classmethod
def get_port(cls, container):
s = subprocess.check_output(['docker', 'port', container, '22/tcp'])
m = cls.PORT_RE.search(s.decode())
if not m:
raise ValueError('could not find SSH port in: %r' % (s,))
return int(m.group('port'))

def start_container(self):
try:
Expand All @@ -530,7 +529,6 @@ def start_container(self):
self.image,
]
subprocess.check_output(args)
self._get_container_port()

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

self.image = 'public.ecr.aws/n5z0e8q9/%s-test' % (distro,)

# 22/tcp -> 0.0.0.0:32771
self.PORT_RE = re.compile(r'([^/]+)/([^ ]+) -> ([^:]+):(.*)')
self.port = None

self.start_container()
self.host = self.get_host()
self.port = self.get_port(self.container_name)

def get_host(self):
return get_docker_host()
Expand Down

0 comments on commit 1fa907d

Please sign in to comment.