From 0583740cd430ab2274456fe64c84d8190c12bacf Mon Sep 17 00:00:00 2001 From: Randall Burt Date: Tue, 10 Dec 2013 11:12:47 -0600 Subject: [PATCH] Allow docker plugin to run w/o deps The docker plugin tests would not run without the docker lib being installed even though the client was mocked in the tests. This patch adds conditional import of the client and registration of the plugin so that the contrib tests can be run in the gates without having to hack requirements or add more third party libs. Change-Id: Ia114f8ebc0cf0b72af8e6e5d2c3264fb7952d7f9 Partial-Bug: #1251380 --- .../docker-plugin/plugin/docker_container.py | 36 +++++++++++++------ .../tests/test_docker_container.py | 4 +-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/contrib/docker-plugin/plugin/docker_container.py b/contrib/docker-plugin/plugin/docker_container.py index ff4dc9cec4..84b182be14 100644 --- a/contrib/docker-plugin/plugin/docker_container.py +++ b/contrib/docker-plugin/plugin/docker_container.py @@ -17,9 +17,19 @@ from heat.engine import properties from heat.engine import resource +from heat.openstack.common import log as logging from heat.openstack.common.gettextutils import _ -import docker +logger = logging.getLogger(__name__) + +DOCKER_INSTALLED = False +# conditionally import so tests can work without having the dependency +# satisfied +try: + import docker + DOCKER_INSTALLED = True +except ImportError: + docker = None class DockerContainer(resource.Resource): @@ -128,13 +138,14 @@ class DockerContainer(resource.Resource): 'logs_tail': _('Container last logs line') } - docker_client = docker.Client() - def get_client(self): - client = self.docker_client - endpoint = self.properties.get('docker_endpoint') - if endpoint: - client = docker.Client(endpoint) + client = None + if DOCKER_INSTALLED: + endpoint = self.properties.get('docker_endpoint') + if endpoint: + client = docker.Client(endpoint) + else: + client = docker.Client() return client def _parse_networkinfo_ports(self, networkinfo): @@ -261,6 +272,11 @@ class DockerContainer(resource.Resource): def resource_mapping(): - return { - 'OS::Docker::Container': DockerContainer - } + # only register if docker client installed + if DOCKER_INSTALLED: + return { + 'OS::Docker::Container': DockerContainer + } + else: + logger.warn("Docker plug-in loaded, but docker lib not installed.") + return {} diff --git a/contrib/docker-plugin/tests/test_docker_container.py b/contrib/docker-plugin/tests/test_docker_container.py index 4d72689a16..8ab79c42f5 100644 --- a/contrib/docker-plugin/tests/test_docker_container.py +++ b/contrib/docker-plugin/tests/test_docker_container.py @@ -51,8 +51,8 @@ class DockerContainerTest(HeatTestCase): def setUp(self): super(DockerContainerTest, self).setUp() utils.setup_dummy_db() - for res_name, res_class in docker_container.resource_mapping().items(): - resource._register_class(res_name, res_class) + resource._register_class('OS::Docker::Container', + docker_container.DockerContainer) def create_container(self, resource_name): t = template_format.parse(template)