]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_exceptions.py
import ceph quincy 17.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_exceptions.py
1 # -*- coding: utf-8 -*-
2
3 import time
4
5 import rados
6
7 from ..controllers import Endpoint, RESTController, Router, Task
8 from ..services.ceph_service import SendCommandError
9 from ..services.exception import handle_rados_error, \
10 handle_send_command_error, serialize_dashboard_exception
11 from ..tests import ControllerTestCase
12 from ..tools import NotificationQueue, TaskManager, ViewCache
13
14
15 # pylint: disable=W0613
16 @Router('foo', secure=False)
17 class FooResource(RESTController):
18
19 @Endpoint()
20 @handle_rados_error('foo')
21 def no_exception(self, param1, param2):
22 return [param1, param2]
23
24 @Endpoint()
25 @handle_rados_error('foo')
26 def error_foo_controller(self):
27 raise rados.OSError('hi', errno=-42)
28
29 @Endpoint()
30 @handle_send_command_error('foo')
31 def error_send_command(self):
32 raise SendCommandError('hi', 'prefix', {}, -42)
33
34 @Endpoint()
35 def error_generic(self):
36 raise rados.Error('hi')
37
38 @Endpoint()
39 def vc_no_data(self):
40 @ViewCache(timeout=0)
41 def _no_data():
42 time.sleep(0.2)
43
44 _no_data()
45 assert False
46
47 @handle_rados_error('foo')
48 @Endpoint()
49 def vc_exception(self):
50 @ViewCache(timeout=10)
51 def _raise():
52 raise rados.OSError('hi', errno=-42)
53
54 _raise()
55 assert False
56
57 @Endpoint()
58 def internal_server_error(self):
59 return 1/0
60
61 @handle_send_command_error('foo')
62 def list(self):
63 raise SendCommandError('list', 'prefix', {}, -42)
64
65 @Endpoint()
66 @Task('task_exceptions/task_exception', {1: 2}, 1.0,
67 exception_handler=serialize_dashboard_exception)
68 @handle_rados_error('foo')
69 def task_exception(self):
70 raise rados.OSError('hi', errno=-42)
71
72 @Endpoint()
73 def wait_task_exception(self):
74 ex, _ = TaskManager.list('task_exceptions/task_exception')
75 return bool(len(ex))
76
77
78 # pylint: disable=C0102
79 class Root(object):
80 foo = FooResource()
81
82
83 class RESTControllerTest(ControllerTestCase):
84 @classmethod
85 def setup_server(cls):
86 NotificationQueue.start_queue()
87 TaskManager.init()
88 cls.setup_controllers([FooResource])
89
90 @classmethod
91 def tearDownClass(cls):
92 NotificationQueue.stop()
93
94 def test_no_exception(self):
95 self._get('/foo/no_exception/a/b')
96 self.assertStatus(200)
97 self.assertJsonBody(
98 ['a', 'b']
99 )
100
101 def test_error_foo_controller(self):
102 self._get('/foo/error_foo_controller')
103 self.assertStatus(400)
104 self.assertJsonBody(
105 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
106 )
107
108 def test_error_send_command(self):
109 self._get('/foo/error_send_command')
110 self.assertStatus(400)
111 self.assertJsonBody(
112 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
113 )
114
115 def test_error_send_command_list(self):
116 self._get('/foo/')
117 self.assertStatus(400)
118 self.assertJsonBody(
119 {'detail': '[errno -42] list', 'code': "42", 'component': 'foo'}
120 )
121
122 def test_error_foo_generic(self):
123 self._get('/foo/error_generic')
124 self.assertJsonBody({'detail': 'hi', 'code': 'Error', 'component': None})
125 self.assertStatus(400)
126
127 def test_viewcache_no_data(self):
128 self._get('/foo/vc_no_data')
129 self.assertStatus(200)
130 self.assertJsonBody({'status': ViewCache.VALUE_NONE, 'value': None})
131
132 def test_viewcache_exception(self):
133 self._get('/foo/vc_exception')
134 self.assertStatus(400)
135 self.assertJsonBody(
136 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
137 )
138
139 def test_task_exception(self):
140 self._get('/foo/task_exception')
141 self.assertStatus(400)
142 self.assertJsonBody(
143 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo',
144 'task': {'name': 'task_exceptions/task_exception', 'metadata': {'1': 2}}}
145 )
146
147 self._get('/foo/wait_task_exception')
148 while self.json_body():
149 time.sleep(0.5)
150 self._get('/foo/wait_task_exception')
151
152 def test_internal_server_error(self):
153 self._get('/foo/internal_server_error')
154 self.assertStatus(500)
155 self.assertIn('unexpected condition', self.json_body()['detail'])
156
157 def test_404(self):
158 self._get('/foonot_found')
159 self.assertStatus(404)
160 self.assertIn('detail', self.json_body())