Browse Source

Fix the raise error in list services process

Change-Id: I2d9c6f10af1fc9e707f1998867bb36ae39bfb331
liushuai 5 months ago
parent
commit
6b91d5fe2d
2 changed files with 18 additions and 8 deletions
  1. 3
    3
      karbor/api/v1/services.py
  2. 15
    5
      karbor/tests/unit/api/v1/test_services.py

+ 3
- 3
karbor/api/v1/services.py View File

@@ -88,9 +88,9 @@ class ServiceController(wsgi.Controller):
88 88
         try:
89 89
             services = objects.ServiceList.get_all_by_args(
90 90
                 context, host, binary)
91
-        except exception as e:
92
-            LOG.error('List service failed, reason: %s' % e)
93
-            raise
91
+        except Exception as e:
92
+            msg = (_('List service failed, reason: %s') % e)
93
+            raise exc.HTTPBadRequest(explanation=msg)
94 94
         return self._view_builder.detail_list(req, services)
95 95
 
96 96
     def update(self, req, id, body):

+ 15
- 5
karbor/tests/unit/api/v1/test_services.py View File

@@ -25,21 +25,30 @@ class ServiceApiTest(base.TestCase):
25 25
 
26 26
     @mock.patch('karbor.objects.service.ServiceList.get_all_by_args')
27 27
     def test_service_list_with_admin_context(self, mock_get_all_by_args):
28
-        req = fakes.HTTPRequest.blank('/v1/services?host=host1',
28
+        req = fakes.HTTPRequest.blank('/v1/os-services?host=host1',
29 29
                                       use_admin_context=True)
30 30
         self.controller.index(req)
31 31
         self.assertTrue(mock_get_all_by_args.called)
32 32
 
33 33
     def test_service_list_with_non_admin_context(self):
34
-        req = fakes.HTTPRequest.blank('/v1/services', use_admin_context=False)
34
+        req = fakes.HTTPRequest.blank('/v1/os-services',
35
+                                      use_admin_context=False)
35 36
         self.assertRaises(
36 37
             exception.PolicyNotAuthorized, self.controller.index, req)
37 38
 
39
+    @mock.patch('karbor.objects.service.ServiceList.get_all_by_args')
40
+    def test_service_list_with_invalid_services(self, mock_get_all_by_args):
41
+        req = fakes.HTTPRequest.blank('/v1/os-services',
42
+                                      use_admin_context=True)
43
+        mock_get_all_by_args.side_effect = exception.NotFound()
44
+        self.assertRaises(exc.HTTPBadRequest, self.controller.index, req)
45
+
38 46
     @mock.patch('karbor.utils.service_is_up')
39 47
     @mock.patch('karbor.objects.service.Service.get_by_id')
40 48
     def test_service_update_with_admin_context(
41 49
             self, mock_get_by_id, mock_service_is_up):
42
-        req = fakes.HTTPRequest.blank('/v1/services/1', use_admin_context=True)
50
+        req = fakes.HTTPRequest.blank('/v1/os-services/1',
51
+                                      use_admin_context=True)
43 52
         body = {
44 53
             "status": 'disabled',
45 54
             'disabled_reason': 'reason'
@@ -53,7 +62,7 @@ class ServiceApiTest(base.TestCase):
53 62
         self.assertTrue(mock_service.save.called)
54 63
 
55 64
     def test_service_update_with_non_admin_context(self):
56
-        req = fakes.HTTPRequest.blank('/v1/services/1',
65
+        req = fakes.HTTPRequest.blank('/v1/os-services/1',
57 66
                                       use_admin_context=False)
58 67
         body = {
59 68
             "status": 'disabled',
@@ -69,7 +78,8 @@ class ServiceApiTest(base.TestCase):
69 78
 
70 79
     @mock.patch('karbor.objects.service.Service.get_by_id')
71 80
     def test_update_protection_services(self, mock_get_by_id):
72
-        req = fakes.HTTPRequest.blank('/v1/services/1', use_admin_context=True)
81
+        req = fakes.HTTPRequest.blank('/v1/os-services/1',
82
+                                      use_admin_context=True)
73 83
         body = {
74 84
             "status": 'disabled',
75 85
             'disabled_reason': 'reason'

Loading…
Cancel
Save