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