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
This commit is contained in:
Tom Barron 2016-03-04 12:07:48 -05:00
parent 77f48dae33
commit 466a19f18f
2 changed files with 27 additions and 3 deletions

View File

@ -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

View File

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