]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # -*- coding: utf-8 -*- |
2 | from __future__ import absolute_import | |
3 | ||
4 | import time | |
5 | ||
6 | import rados | |
7 | ||
8 | from . import ControllerTestCase | |
9 | from ..services.ceph_service import SendCommandError | |
10 | from ..controllers import RESTController, Controller, Task, Endpoint | |
11 | from ..services.exception import handle_rados_error, handle_send_command_error, \ | |
12 | serialize_dashboard_exception | |
13 | from ..tools import ViewCache, TaskManager, NotificationQueue | |
14 | ||
15 | ||
16 | # pylint: disable=W0613 | |
17 | @Controller('foo', secure=False) | |
18 | class 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 | |
80 | class Root(object): | |
81 | foo = FooResource() | |
82 | ||
83 | ||
84 | class RESTControllerTest(ControllerTestCase): | |
85 | @classmethod | |
86 | def setup_server(cls): | |
87 | NotificationQueue.start_queue() | |
88 | TaskManager.init() | |
89 | cls.setup_controllers([FooResource]) | |
90 | ||
91 | def test_no_exception(self): | |
92 | self._get('/foo/no_exception/a/b') | |
93 | self.assertStatus(200) | |
94 | self.assertJsonBody( | |
95 | ['a', 'b'] | |
96 | ) | |
97 | ||
98 | def test_error_foo_controller(self): | |
99 | self._get('/foo/error_foo_controller') | |
100 | self.assertStatus(400) | |
101 | self.assertJsonBody( | |
102 | {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'} | |
103 | ) | |
104 | ||
105 | def test_error_send_command(self): | |
106 | self._get('/foo/error_send_command') | |
107 | self.assertStatus(400) | |
108 | self.assertJsonBody( | |
109 | {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'} | |
110 | ) | |
111 | ||
112 | def test_error_send_command_list(self): | |
113 | self._get('/foo/') | |
114 | self.assertStatus(400) | |
115 | self.assertJsonBody( | |
116 | {'detail': '[errno -42] list', 'code': "42", 'component': 'foo'} | |
117 | ) | |
118 | ||
119 | def test_error_foo_generic(self): | |
120 | self._get('/foo/error_generic') | |
121 | self.assertJsonBody({'detail': 'hi', 'code': 'Error', 'component': None}) | |
122 | self.assertStatus(400) | |
123 | ||
124 | def test_viewcache_no_data(self): | |
125 | self._get('/foo/vc_no_data') | |
126 | self.assertStatus(200) | |
127 | self.assertJsonBody({'status': ViewCache.VALUE_NONE, 'value': None}) | |
128 | ||
129 | def test_viewcache_exception(self): | |
130 | self._get('/foo/vc_exception') | |
131 | self.assertStatus(400) | |
132 | self.assertJsonBody( | |
133 | {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'} | |
134 | ) | |
135 | ||
136 | def test_task_exception(self): | |
137 | self._get('/foo/task_exception') | |
138 | self.assertStatus(400) | |
139 | self.assertJsonBody( | |
140 | {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo', | |
141 | 'task': {'name': 'task_exceptions/task_exception', 'metadata': {'1': 2}}} | |
142 | ) | |
143 | ||
144 | self._get('/foo/wait_task_exception') | |
9f95a23c | 145 | while self.json_body(): |
11fdf7f2 TL |
146 | time.sleep(0.5) |
147 | self._get('/foo/wait_task_exception') | |
148 | ||
149 | def test_internal_server_error(self): | |
150 | self._get('/foo/internal_server_error') | |
151 | self.assertStatus(500) | |
9f95a23c | 152 | self.assertIn('unexpected condition', self.json_body()['detail']) |
11fdf7f2 TL |
153 | |
154 | def test_404(self): | |
155 | self._get('/foonot_found') | |
156 | self.assertStatus(404) | |
9f95a23c | 157 | self.assertIn('detail', self.json_body()) |