From 466a19f18f12cb77b45aa7c7a272fae7c45c4c93 Mon Sep 17 00:00:00 2001 From: Tom Barron Date: Fri, 4 Mar 2016 12:07:48 -0500 Subject: [PATCH] Fix pylxd hard dependencies Since introduction of the LXD/LXC first-party manila driver [1], generating configuration and running unit tests fail unless pylxd is installed. This hard depency is likely unwanted as it is problematic for distributions that do not include LXD and which will not therefore distribute a packaged version of pylxd. This commit converts the pylxd dependency to a soft dependency. Config generation succeeds and lxd driver unit tests are skipped if pylxd cannot be imported. Additionally, we handle the pylxd exceptions encountered using pylxd 2.x, as reported here [1]. [1] https://review.openstack.org/#/c/289899/ Change-Id: I8436de2fd994fc73f81f6f3a43c658c874550c10 Closes-bug: #1553280 Closes-bug: #1553208 --- manila/share/drivers/lxd.py | 24 ++++++++++++++++++++++-- manila/tests/share/drivers/test_lxd.py | 6 +++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/manila/share/drivers/lxd.py b/manila/share/drivers/lxd.py index e690500e60..e118581fac 100644 --- a/manila/share/drivers/lxd.py +++ b/manila/share/drivers/lxd.py @@ -25,14 +25,30 @@ import re from oslo_config import cfg from oslo_log import log -from pylxd import api as lxd_api -from pylxd import exceptions as pylxd_exc +# NOTE(tbarron): pylxd module is unavailable in some distribtutions so +# handle this circumstance gracefully. Also handle failure to import +# pylxd_exc directly in versions >= pylxd 2. +try: + from pylxd import api as lxd_api + try: + from pylxd import exceptions as pylxd_exc + NO_LXD = False + except ImportError: + try: + # pylint: disable=E0611 + from pylxd.deprecated import exceptions as pylxd_exc + NO_LXD = False + except ImportError: + NO_LXD = True +except ImportError: + NO_LXD = True import six from manila.common import constants as const from manila import context from manila import exception from manila.i18n import _ +from manila.i18n import _LI from manila.i18n import _LW from manila.share import driver from manila import utils @@ -92,6 +108,10 @@ CONF.register_opts(lv_opts) class LXDHelper(object): def __init__(self, lxd_api, config): + if NO_LXD: + LOG.info(_LI('pylxd modules are not present on this system: LXD ' + 'driver will not function.')) + return super(LXDHelper, self).__init__() self.api = lxd_api self.conf = config diff --git a/manila/tests/share/drivers/test_lxd.py b/manila/tests/share/drivers/test_lxd.py index 5dee8b58b3..4fd4ada0bf 100644 --- a/manila/tests/share/drivers/test_lxd.py +++ b/manila/tests/share/drivers/test_lxd.py @@ -17,6 +17,7 @@ import functools import mock from oslo_config import cfg +import testtools from manila.common import constants as const from manila import context @@ -28,7 +29,6 @@ from manila.tests.db import fakes as db_fakes from manila.tests import fake_utils from manila import utils - CONF = cfg.CONF @@ -71,6 +71,7 @@ def fake_network(**kwargs): return db_fakes.FakeModel(network) +@testtools.skipIf(lxd.NO_LXD, "pylxd is unavailable") class LXDHelperTestCase(test.TestCase): """Tests LXDUnfs3Helper""" @@ -219,6 +220,7 @@ class LXDHelperTestCase(test.TestCase): fake_stream.close.assert_called_once_with() +@testtools.skipIf(lxd.NO_LXD, "pylxd is unavailable") class LXDUnfs3HelperTestCase(test.TestCase): """Tests LXDUnfs3Helper""" @@ -432,6 +434,7 @@ class LXDUnfs3HelperTestCase(test.TestCase): self.assertFalse(self.UNFS3Helper._deny_access.called) +@testtools.skipIf(lxd.NO_LXD, "pylxd is unavailable") class LXDCIFSHelperTestCase(test.TestCase): """Tests LXDCIFSHelper""" @@ -671,6 +674,7 @@ class LXDCIFSHelperTestCase(test.TestCase): self.assertFalse(self.CIFSHelper._deny_access.called) +@testtools.skipIf(lxd.NO_LXD, "pylxd is unavailable") class LXDDriverTestCase(test.TestCase): """Tests LXDDriver."""