|
|
@ -5,6 +5,7 @@ import os
|
|
|
|
import random
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
import urlparse
|
|
|
|
import urlparse
|
|
|
@ -15,9 +16,6 @@ import mitogen.core
|
|
|
|
import mitogen.master
|
|
|
|
import mitogen.master
|
|
|
|
import mitogen.utils
|
|
|
|
import mitogen.utils
|
|
|
|
|
|
|
|
|
|
|
|
if mitogen.is_master: # TODO: shouldn't be necessary.
|
|
|
|
|
|
|
|
import docker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
|
|
|
|
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
|
|
|
|
sys.path.append(DATA_DIR)
|
|
|
|
sys.path.append(DATA_DIR)
|
|
|
@ -34,6 +32,19 @@ def data_path(suffix):
|
|
|
|
return path
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def subprocess__check_output(*popenargs, **kwargs):
|
|
|
|
|
|
|
|
# Missing from 2.6.
|
|
|
|
|
|
|
|
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
|
|
|
|
|
|
|
|
output, _ = process.communicate()
|
|
|
|
|
|
|
|
retcode = process.poll()
|
|
|
|
|
|
|
|
if retcode:
|
|
|
|
|
|
|
|
cmd = kwargs.get("args")
|
|
|
|
|
|
|
|
if cmd is None:
|
|
|
|
|
|
|
|
cmd = popenargs[0]
|
|
|
|
|
|
|
|
raise subprocess.CalledProcessError(retcode, cmd, output=output)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_port(
|
|
|
|
def wait_for_port(
|
|
|
|
host,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
port,
|
|
|
@ -162,11 +173,12 @@ class TestCase(unittest2.TestCase):
|
|
|
|
assert 0, '%r did not raise %r' % (func, exc)
|
|
|
|
assert 0, '%r did not raise %r' % (func, exc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_docker_host(docker):
|
|
|
|
def get_docker_host():
|
|
|
|
if docker.api.base_url == 'http+docker://localunixsocket':
|
|
|
|
url = os.environ.get('DOCKER_HOST')
|
|
|
|
|
|
|
|
if url in (None, 'http+docker://localunixsocket'):
|
|
|
|
return 'localhost'
|
|
|
|
return 'localhost'
|
|
|
|
|
|
|
|
|
|
|
|
parsed = urlparse.urlparse(docker.api.base_url)
|
|
|
|
parsed = urlparse.urlparse(url)
|
|
|
|
return parsed.netloc.partition(':')[0]
|
|
|
|
return parsed.netloc.partition(':')[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -179,29 +191,47 @@ class DockerizedSshDaemon(object):
|
|
|
|
self.image = 'mitogen/%s-test' % (distro,)
|
|
|
|
self.image = 'mitogen/%s-test' % (distro,)
|
|
|
|
return self.image
|
|
|
|
return self.image
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# 22/tcp -> 0.0.0.0:32771
|
|
|
|
self.docker = docker.from_env(version='auto')
|
|
|
|
PORT_RE = re.compile(r'([^/]+)/([^ ]+) -> ([^:]+):(.*)')
|
|
|
|
self.container_name = 'mitogen-test-%08x' % (random.getrandbits(64),)
|
|
|
|
port = None
|
|
|
|
self.container = self.docker.containers.run(
|
|
|
|
|
|
|
|
image=self.get_image(),
|
|
|
|
def _get_container_port(self):
|
|
|
|
detach=True,
|
|
|
|
s = subprocess__check_output(['docker', 'port', self.container_name])
|
|
|
|
privileged=True,
|
|
|
|
for line in s.splitlines():
|
|
|
|
publish_all_ports=True,
|
|
|
|
dport, proto, baddr, bport = self.PORT_RE.match(line).groups()
|
|
|
|
)
|
|
|
|
if dport == '22' and proto == 'tcp':
|
|
|
|
self.container.reload()
|
|
|
|
self.port = int(bport)
|
|
|
|
self.port = (self.container.attrs['NetworkSettings']['Ports']
|
|
|
|
|
|
|
|
['22/tcp'][0]['HostPort'])
|
|
|
|
|
|
|
|
self.host = self.get_host()
|
|
|
|
self.host = self.get_host()
|
|
|
|
|
|
|
|
if self.port is None:
|
|
|
|
|
|
|
|
raise ValueError('could not find SSH port in: %r' % (s,))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_container(self):
|
|
|
|
|
|
|
|
self.container_name = 'mitogen-test-%08x' % (random.getrandbits(64),)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
|
|
|
|
'docker',
|
|
|
|
|
|
|
|
'run',
|
|
|
|
|
|
|
|
'--detach',
|
|
|
|
|
|
|
|
'--privileged',
|
|
|
|
|
|
|
|
'--publish-all',
|
|
|
|
|
|
|
|
'--name', self.container_name,
|
|
|
|
|
|
|
|
self.get_image()
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
subprocess__check_output(args)
|
|
|
|
|
|
|
|
self._get_container_port()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.start_container()
|
|
|
|
|
|
|
|
|
|
|
|
def get_host(self):
|
|
|
|
def get_host(self):
|
|
|
|
return get_docker_host(self.docker)
|
|
|
|
return get_docker_host()
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_sshd(self):
|
|
|
|
def wait_for_sshd(self):
|
|
|
|
wait_for_port(self.get_host(), int(self.port), pattern='OpenSSH')
|
|
|
|
wait_for_port(self.get_host(), self.port, pattern='OpenSSH')
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
def close(self):
|
|
|
|
self.container.stop()
|
|
|
|
args = ['docker', 'rm', '-f', self.container_name]
|
|
|
|
self.container.remove()
|
|
|
|
subprocess__check_output(args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BrokerMixin(object):
|
|
|
|
class BrokerMixin(object):
|
|
|
|