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."""