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
This commit is contained in:
Randall Burt 2013-12-10 11:12:47 -06:00
parent c8217a39a9
commit 0583740cd4
2 changed files with 28 additions and 12 deletions

View File

@ -17,9 +17,19 @@
from heat.engine import properties from heat.engine import properties
from heat.engine import resource from heat.engine import resource
from heat.openstack.common import log as logging
from heat.openstack.common.gettextutils import _ 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): class DockerContainer(resource.Resource):
@ -128,13 +138,14 @@ class DockerContainer(resource.Resource):
'logs_tail': _('Container last logs line') 'logs_tail': _('Container last logs line')
} }
docker_client = docker.Client()
def get_client(self): def get_client(self):
client = self.docker_client client = None
if DOCKER_INSTALLED:
endpoint = self.properties.get('docker_endpoint') endpoint = self.properties.get('docker_endpoint')
if endpoint: if endpoint:
client = docker.Client(endpoint) client = docker.Client(endpoint)
else:
client = docker.Client()
return client return client
def _parse_networkinfo_ports(self, networkinfo): def _parse_networkinfo_ports(self, networkinfo):
@ -261,6 +272,11 @@ class DockerContainer(resource.Resource):
def resource_mapping(): def resource_mapping():
# only register if docker client installed
if DOCKER_INSTALLED:
return { return {
'OS::Docker::Container': DockerContainer 'OS::Docker::Container': DockerContainer
} }
else:
logger.warn("Docker plug-in loaded, but docker lib not installed.")
return {}

View File

@ -51,8 +51,8 @@ class DockerContainerTest(HeatTestCase):
def setUp(self): def setUp(self):
super(DockerContainerTest, self).setUp() super(DockerContainerTest, self).setUp()
utils.setup_dummy_db() utils.setup_dummy_db()
for res_name, res_class in docker_container.resource_mapping().items(): resource._register_class('OS::Docker::Container',
resource._register_class(res_name, res_class) docker_container.DockerContainer)
def create_container(self, resource_name): def create_container(self, resource_name):
t = template_format.parse(template) t = template_format.parse(template)