]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Check for service existance in capabilities API
authorIvan Kolodyazhny <e0ne@e0ne.info>
Sat, 23 Jan 2016 11:35:15 +0000 (13:35 +0200)
committerYuriy Nesenenko <ynesenenko@mirantis.com>
Fri, 26 Feb 2016 15:10:34 +0000 (17:10 +0200)
We have to return 404 error code if requested backend
is not found and 502 if request failed with timeout.

APIImpact

Change-Id: Ia392973121800278751642f0a22ee817b3a69b74
Closes-Bug: #1520077

cinder/api/contrib/capabilities.py
cinder/exception.py
cinder/tests/unit/api/contrib/test_capabilities.py

index 177d73aa5b061e6a82dfecac0f5c438588346217..087aad68324569ab51c8a34139a3bf17e5010984 100644 (file)
 #    under the License.
 
 from oslo_log import log as logging
+import oslo_messaging
 
 from cinder.api import extensions
 from cinder.api.openstack import wsgi
 from cinder.api.views import capabilities as capabilities_view
+from cinder import exception
+from cinder.i18n import _
+from cinder import objects
 from cinder.volume import rpcapi
 
 
@@ -41,7 +45,15 @@ class CapabilitiesController(wsgi.Controller):
         """Return capabilities list of given backend."""
         context = req.environ['cinder.context']
         authorize(context, 'capabilities')
-        capabilities = self.volume_api.get_capabilities(context, id, False)
+        filters = {'host': id, 'binary': 'cinder-volume'}
+        service = objects.ServiceList.get_all(context, filters)
+        if not service:
+            msg = (_("Can't find service: %s") % id)
+            raise exception.NotFound(msg)
+        try:
+            capabilities = self.volume_api.get_capabilities(context, id, False)
+        except oslo_messaging.MessagingTimeout:
+            raise exception.RPCTimeout(service=id)
         return self._view_builder.summary(req, capabilities, id)
 
 
index 6b3f940ef2640ab0bb40a53f9e3524e0ff0483e5..87a296942c1b04a4074ce962306b1b38de8e5cf1 100644 (file)
@@ -256,6 +256,12 @@ class APITimeout(APIException):
     message = _("Timeout while requesting %(service)s API.")
 
 
+class RPCTimeout(CinderException):
+    message = _("Timeout while requesting capabilities from backend "
+                "%(service)s.")
+    code = 502
+
+
 class NotFound(CinderException):
     message = _("Resource could not be found.")
     code = 404
index c5467e749b12e5d368492714d14fb4ed1fe17ec3..e02dbb211c6c6547a94110eed3fb09e14d12a14a 100644 (file)
 
 import mock
 
+import oslo_messaging
+
 from cinder.api.contrib import capabilities
 from cinder import context
+from cinder import exception
 from cinder import test
 from cinder.tests.unit.api import fakes
 
@@ -54,8 +57,6 @@ def rpcapi_get_capabilities(self, context, host, discover):
     return capabilities
 
 
-@mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities',
-            rpcapi_get_capabilities)
 class CapabilitiesAPITest(test.TestCase):
     def setUp(self):
         super(CapabilitiesAPITest, self).setUp()
@@ -63,7 +64,11 @@ class CapabilitiesAPITest(test.TestCase):
         self.controller = capabilities.CapabilitiesController()
         self.ctxt = context.RequestContext('admin', 'fake', True)
 
-    def test_capabilities_summary(self):
+    @mock.patch('cinder.db.service_get_all')
+    @mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities',
+                rpcapi_get_capabilities)
+    def test_capabilities_summary(self, mock_services):
+        mock_services.return_value = [{'name': 'fake'}]
         req = fakes.HTTPRequest.blank('/fake/capabilities/fake')
         req.environ['cinder.context'] = self.ctxt
         res = self.controller.show(req, 'fake')
@@ -100,3 +105,23 @@ class CapabilitiesAPITest(test.TestCase):
         }
 
         self.assertDictMatch(expected, res)
+
+    @mock.patch('cinder.db.service_get_all')
+    @mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities')
+    def test_get_capabilities_rpc_timeout(self, mock_rpc, mock_services):
+        mock_rpc.side_effect = oslo_messaging.MessagingTimeout
+        mock_services.return_value = [{'name': 'fake'}]
+
+        req = fakes.HTTPRequest.blank('/fake/capabilities/fake')
+        req.environ['cinder.context'] = self.ctxt
+        self.assertRaises(exception.RPCTimeout,
+                          self.controller.show, req, 'fake')
+
+    @mock.patch('cinder.db.service_get_all')
+    def test_get_capabilities_service_not_found(self, mock_services):
+        mock_services.return_value = []
+
+        req = fakes.HTTPRequest.blank('/fake/capabilities/fake')
+        req.environ['cinder.context'] = self.ctxt
+        self.assertRaises(exception.NotFound,
+                          self.controller.show, req, 'fake')