Skip to content
Permalink
Browse files

Make tests happy

  • Loading branch information...
anish committed Mar 27, 2017
1 parent b08a300 commit 8efde5ce6760c4d52e6f85fb01d195f079f58b11
@@ -16,6 +16,8 @@
from __future__ import absolute_import
from __future__ import print_function

version = "1.10.6"


class Client(object):
latest = None
@@ -24,6 +26,7 @@ def __init__(self, base_url):
Client.latest = self
self.call_args_create_container = []
self.call_args_create_host_config = []
self.called_class_name = None
self._images = [{'RepoTags': ['busybox:latest', 'worker:latest']}]
self._containers = {}

@@ -61,6 +64,7 @@ def create_host_config(self, *args, **kwargs):

def create_container(self, image, *args, **kwargs):
self.call_args_create_container.append(kwargs)
self.called_class_name = self.__class__.__name__
name = kwargs.get('name', None)
if 'buggy' in image:
raise Exception('we could not create this container')
@@ -83,5 +87,6 @@ def create_container(self, image, *args, **kwargs):
def remove_container(self, id, **kwargs):
del self._containers[id]


class APIClient(Client):
pass
@@ -36,6 +36,7 @@
class TestDockerLatentWorker(unittest.SynchronousTestCase):

def setupWorker(self, *args, **kwargs):
self.patch(dockerworker, 'docker', docker)
worker = dockerworker.DockerLatentWorker(*args, **kwargs)
master = fakemaster.make_master(testcase=self, wantData=True)
worker.setServiceParent(master)
@@ -86,6 +87,24 @@ def test_constructor_minimal(self):
self.assertEqual(bs.image, 'worker')
self.assertEqual(bs.command, [])

def test_contruction_minimal_docker_py(self):
docker.version = "1.10.6"
bs = self.setupWorker('bot', 'pass', 'tcp://1234:2375', 'worker')
id, name = self.successResultOf(bs.start_instance(self.build))
client = docker.APIClient.latest
self.assertEqual(client.called_class_name, "Client")
client = docker.Client.latest
self.assertNotEqual(client.called_class_name, "APIClient")

def test_contruction_minimal_docker(self):
docker.version = "2.0.0"
bs = self.setupWorker('bot', 'pass', 'tcp://1234:2375', 'worker')
id, name = self.successResultOf(bs.start_instance(self.build))
client = docker.Client.latest
self.assertEqual(client.called_class_name, "APIClient")
client = docker.APIClient.latest
self.assertNotEqual(client.called_class_name, "Client")

def test_constructor_nopassword(self):
# when no password, it is created automatically
bs = self.setupWorker('bot', None, 'tcp://1234:2375', 'worker')
@@ -37,6 +37,7 @@
from docker.errors import NotFound
_hush_pyflakes = [docker, client]
except ImportError:
docker = None
client = None


@@ -180,6 +181,13 @@ def _thd_parse_volumes(self, volumes):
binds[bind] = {'bind': volume, 'ro': ro}
return volume_list, binds

def _getDockerClient(self):
if docker.version[0] == '1':
docker_client = client.Client(**self.client_args)
else:
docker_client = client.APIClient(**self.client_args)
return docker_client

@defer.inlineCallbacks
def start_instance(self, build):
if self.instance is not None:
@@ -201,10 +209,7 @@ def _image_exists(self, client, name):
return False

def _thd_start_instance(self, image, dockerfile, volumes):
if docker.version[0] == '1':
docker_client = client.Client(**self.client_args)
else:
docker_client = client.APIClient(**self.client_args)
docker_client = self._getDockerClient()
# cleanup the old instances
instances = docker_client.containers(
all=1,
@@ -280,10 +285,7 @@ def stop_instance(self, fast=False):
return threads.deferToThread(self._thd_stop_instance, instance, fast)

def _thd_stop_instance(self, instance, fast):
if docker.version[0] == '1':
docker_client = client.Client(**self.client_args)
else:
docker_client = client.APIClient(**self.client_args)
docker_client = self._getDockerClient()
log.msg('Stopping container %s...' % instance['Id'][:6])
docker_client.stop(instance['Id'])
if not fast:

0 comments on commit 8efde5c

Please sign in to comment.
You can’t perform that action at this time.