mitogen/tests/testlib.py

75 lines
1.8 KiB
Python
Raw Normal View History

2017-09-15 06:24:41 +00:00
import os
2017-09-16 23:57:14 +00:00
import random
2017-09-15 06:24:41 +00:00
import sys
import unittest
2017-09-16 23:57:14 +00:00
import urlparse
2017-09-15 06:24:41 +00:00
import mock
import mitogen.master
2017-09-16 23:57:14 +00:00
import docker
2017-09-15 06:24:41 +00:00
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
sys.path.append(DATA_DIR)
def set_debug():
import logging
logging.getLogger('mitogen').setLevel(logging.DEBUG)
def data_path(suffix):
return os.path.join(DATA_DIR, suffix)
2017-09-16 23:57:14 +00:00
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):
2017-09-15 06:24:41 +00:00
broker_class = mitogen.master.Broker
2017-09-16 23:57:14 +00:00
router_class = mitogen.master.Router
2017-09-15 06:24:41 +00:00
def setUp(self):
2017-09-16 23:57:14 +00:00
super(RouterMixin, self).setUp()
2017-09-15 06:24:41 +00:00
self.broker = self.broker_class()
2017-09-16 23:57:14 +00:00
self.router = self.router_class(self.broker)
2017-09-15 06:24:41 +00:00
def tearDown(self):
self.broker.shutdown()
self.broker.join()
2017-09-16 23:57:14 +00:00
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()