diff --git a/dev_requirements.txt b/dev_requirements.txt new file mode 100644 index 00000000..c102b4d0 --- /dev/null +++ b/dev_requirements.txt @@ -0,0 +1 @@ +docker[tls]==2.5.1 diff --git a/tests/testlib.py b/tests/testlib.py index dcde32e4..9d49b5a2 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -1,11 +1,14 @@ import os +import random import sys import unittest +import urlparse import mock import mitogen.master +import docker DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') @@ -21,14 +24,51 @@ def data_path(suffix): return os.path.join(DATA_DIR, suffix) -class BrokerMixin(object): +class DockerizedSshDaemon(object): + def __init__(self): + self.docker = docker.from_env() + self.container_name = 'mitogen-test-%08x' % (random.getrandbits(64),) + self.container = self.docker.containers.run( + image='d2mw/mitogen-test', + detach=True, + remove=True, + publish_all_ports=True, + ) + self.container.reload() + self.port = (self.container.attrs['NetworkSettings']['Ports'] + ['22/tcp'][0]['HostPort']) + self.host = self.get_host() + + def get_host(self): + parsed = urlparse.urlparse(self.docker.api.base_url) + return parsed.netloc.partition(':')[0] + + def close(self): + self.container.stop() + + +class RouterMixin(object): broker_class = mitogen.master.Broker + router_class = mitogen.master.Router def setUp(self): - super(BrokerMixin, self).setUp() + super(RouterMixin, self).setUp() self.broker = self.broker_class() + self.router = self.router_class(self.broker) def tearDown(self): self.broker.shutdown() self.broker.join() - super(BrokerMixin, self).tearDown() + super(RouterMixin, self).tearDown() + + +class DockerMixin(RouterMixin): + @classmethod + def setUpClass(cls): + super(DockerMixin, cls).setUpClass() + cls.dockerized_ssh = DockerizedSshDaemon() + + @classmethod + def tearDownClass(cls): + cls.dockerized_ssh.close() + super(DockerMixin, cls).tearDownClass()