]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix usage of novaclient
authorIvan Kolodyazhny <e0ne@e0ne.info>
Fri, 11 Sep 2015 11:23:02 +0000 (14:23 +0300)
committerIvan Kolodyazhny <e0ne@e0ne.info>
Mon, 14 Sep 2015 17:03:35 +0000 (20:03 +0300)
We should use 'client interface' for initialization of novaclient
instead of direct import of some specific client.

Change-Id: I51254e20151fe1d6771897e64a8d0bdf2d674d03
Closes-Bug: #1493576

cinder/compute/nova.py
cinder/tests/unit/compute/test_nova.py
cinder/tests/unit/scheduler/fakes.py
cinder/tests/unit/scheduler/test_host_filters.py

index 3508ff5ae18a2b4448bd06d8f63472efc0632be9..58a349da5f32458cbc12e47966a8de083ec4173b 100644 (file)
@@ -17,12 +17,9 @@ Handles all requests to Nova.
 """
 
 
+from novaclient import client as nova_client
 from novaclient import exceptions as nova_exceptions
-from novaclient import extension
 from novaclient import service_catalog
-from novaclient.v2 import client as nova_client
-from novaclient.v2.contrib import assisted_volume_snapshots
-from novaclient.v2.contrib import list_extensions
 from oslo_config import cfg
 from oslo_log import log as logging
 from requests import exceptions as request_exceptions
@@ -65,8 +62,11 @@ CONF.register_opts(nova_opts)
 
 LOG = logging.getLogger(__name__)
 
-nova_extensions = (assisted_volume_snapshots,
-                   extension.Extension('list_extensions', list_extensions))
+# TODO(e0ne): Make Nova version configurable in Mitaka.
+NOVA_API_VERSION = 2
+
+nova_extensions = [ext for ext in nova_client.discover_extensions(2)
+                   if ext.name == "assisted_volume_snapshots"]
 
 
 def novaclient(context, admin_endpoint=False, privileged_user=False,
@@ -137,7 +137,8 @@ def novaclient(context, admin_endpoint=False, privileged_user=False,
 
         LOG.debug('Nova client connection created using URL: %s', url)
 
-    c = nova_client.Client(context.user_id,
+    c = nova_client.Client(NOVA_API_VERSION,
+                           context.user_id,
                            context.auth_token,
                            context.project_name,
                            auth_url=url,
@@ -161,12 +162,7 @@ class API(base.Base):
 
     def has_extension(self, context, extension, timeout=None):
         try:
-            client = novaclient(context, timeout=timeout)
-
-            # Pylint gives a false positive here because the 'list_extensions'
-            # method is not explicitly declared. Overriding the error.
-            # pylint: disable-msg=E1101
-            nova_exts = client.list_extensions.show_all()
+            nova_exts = nova_client.discover_extensions(NOVA_API_VERSION)
         except request_exceptions.Timeout:
             raise exception.APITimeout(service='Nova')
         return extension in [e.name for e in nova_exts]
index bd3cc5cdfceedd742193a67ef9b19fa31fbf8bcd..e9e44f6a1afc4cd8a89c64a0042c0b997d0bbf23 100644 (file)
@@ -38,49 +38,54 @@ class NovaClientTestCase(test.TestCase):
         self.override_config('os_privileged_user_name', 'adminuser')
         self.override_config('os_privileged_user_password', 'strongpassword')
 
-    @mock.patch('novaclient.v2.client.Client')
+    @mock.patch('novaclient.client.Client')
     def test_nova_client_regular(self, p_client):
         nova.novaclient(self.ctx)
         p_client.assert_called_once_with(
+            nova.NOVA_API_VERSION,
             'regularuser', 'token', None, region_name=None,
             auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
             insecure=False, endpoint_type='publicURL', cacert=None,
             timeout=None, extensions=nova.nova_extensions)
 
-    @mock.patch('novaclient.v2.client.Client')
+    @mock.patch('novaclient.client.Client')
     def test_nova_client_admin_endpoint(self, p_client):
         nova.novaclient(self.ctx, admin_endpoint=True)
         p_client.assert_called_once_with(
+            nova.NOVA_API_VERSION,
             'regularuser', 'token', None, region_name=None,
             auth_url='http://novaadmhost:4778/v2/e3f0833dc08b4cea',
             insecure=False, endpoint_type='adminURL', cacert=None,
             timeout=None, extensions=nova.nova_extensions)
 
-    @mock.patch('novaclient.v2.client.Client')
+    @mock.patch('novaclient.client.Client')
     def test_nova_client_privileged_user(self, p_client):
         nova.novaclient(self.ctx, privileged_user=True)
         p_client.assert_called_once_with(
+            nova.NOVA_API_VERSION,
             'adminuser', 'strongpassword', None, region_name=None,
             auth_url='http://keystonehost:5000/v2.0',
             insecure=False, endpoint_type='publicURL', cacert=None,
             timeout=None, extensions=nova.nova_extensions)
 
-    @mock.patch('novaclient.v2.client.Client')
+    @mock.patch('novaclient.client.Client')
     def test_nova_client_privileged_user_custom_auth_url(self, p_client):
         self.override_config('os_privileged_user_auth_url',
                              'http://privatekeystonehost:5000/v2.0')
         nova.novaclient(self.ctx, privileged_user=True)
         p_client.assert_called_once_with(
+            nova.NOVA_API_VERSION,
             'adminuser', 'strongpassword', None, region_name=None,
             auth_url='http://privatekeystonehost:5000/v2.0',
             insecure=False, endpoint_type='publicURL', cacert=None,
             timeout=None, extensions=nova.nova_extensions)
 
-    @mock.patch('novaclient.v2.client.Client')
+    @mock.patch('novaclient.client.Client')
     def test_nova_client_custom_region(self, p_client):
         self.override_config('os_region_name', 'farfaraway')
         nova.novaclient(self.ctx)
         p_client.assert_called_once_with(
+            nova.NOVA_API_VERSION,
             'regularuser', 'token', None, region_name='farfaraway',
             auth_url='http://novahost:8774/v2/e3f0833dc08b4cea',
             insecure=False, endpoint_type='publicURL', cacert=None,
index 071eb64e67901ffc3a0d290765aecfc63b9c203c..58b77437482ed48e8b5c4d70560964a3c031a429 100644 (file)
@@ -139,7 +139,7 @@ class FakeNovaClient(object):
 
     def __init__(self, ext_srv_attr=True):
         self.servers = FakeNovaClient.ServerManager()
-        self.list_extensions = FakeNovaClient.ListExtManager(
+        self.discover_extensions = FakeNovaClient.ListExtManager(
             ext_srv_attr=ext_srv_attr)
 
 
index ec4f013d8e4ccae8638ae5a937f8015687eb82a5..659119dff87e775297ed2460314d5dbd7dd31a04 100644 (file)
@@ -902,9 +902,12 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase):
              {'type': 'identity', 'name': 'keystone', 'endpoints':
               [{'publicURL': 'http://keystonehost:5000/v2.0'}]}]
 
+    @mock.patch('novaclient.client.discover_extensions')
     @mock.patch('cinder.compute.nova.novaclient')
-    def test_same_host(self, _mock_novaclient):
+    def test_same_host(self, _mock_novaclient, fake_extensions):
         _mock_novaclient.return_value = fakes.FakeNovaClient()
+        fake_extensions.return_value = (
+            fakes.FakeNovaClient().discover_extensions.show_all())
         filt_cls = self.class_map['InstanceLocalityFilter']()
         host = fakes.FakeHostState('host1', {})
         uuid = nova.novaclient().servers.create('host1')
@@ -913,9 +916,12 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase):
                              'scheduler_hints': {'local_to_instance': uuid}}
         self.assertTrue(filt_cls.host_passes(host, filter_properties))
 
+    @mock.patch('novaclient.client.discover_extensions')
     @mock.patch('cinder.compute.nova.novaclient')
-    def test_different_host(self, _mock_novaclient):
+    def test_different_host(self, _mock_novaclient, fake_extensions):
         _mock_novaclient.return_value = fakes.FakeNovaClient()
+        fake_extensions.return_value = (
+            fakes.FakeNovaClient().discover_extensions.show_all())
         filt_cls = self.class_map['InstanceLocalityFilter']()
         host = fakes.FakeHostState('host1', {})
         uuid = nova.novaclient().servers.create('host2')
@@ -966,10 +972,13 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase):
         filter_properties = {'context': self.context, 'size': 100}
         self.assertTrue(filt_cls.host_passes(host, filter_properties))
 
+    @mock.patch('novaclient.client.discover_extensions')
     @mock.patch('requests.request')
-    def test_nova_timeout(self, _mock_request):
+    def test_nova_timeout(self, _mock_request, fake_extensions):
         # Simulate a HTTP timeout
         _mock_request.side_effect = request_exceptions.Timeout
+        fake_extensions.return_value = (
+            fakes.FakeNovaClient().discover_extensions.show_all())
 
         filt_cls = self.class_map['InstanceLocalityFilter']()
         host = fakes.FakeHostState('host1', {})