1 # -*- coding: utf-8 -*-
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
15 # pylint: disable=W0613
16 @Router('foo', secure
=False)
17 class FooResource(RESTController
):
20 @handle_rados_error('foo')
21 def no_exception(self
, param1
, param2
):
22 return [param1
, param2
]
25 @handle_rados_error('foo')
26 def error_foo_controller(self
):
27 raise rados
.OSError('hi', errno
=-42)
30 @handle_send_command_error('foo')
31 def error_send_command(self
):
32 raise SendCommandError('hi', 'prefix', {}, -42)
35 def error_generic(self
):
36 raise rados
.Error('hi')
47 @handle_rados_error('foo')
49 def vc_exception(self
):
50 @ViewCache(timeout
=10)
52 raise rados
.OSError('hi', errno
=-42)
58 def internal_server_error(self
):
61 @handle_send_command_error('foo')
63 raise SendCommandError('list', 'prefix', {}, -42)
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)
73 def wait_task_exception(self
):
74 ex
, _
= TaskManager
.list('task_exceptions/task_exception')
78 # pylint: disable=C0102
83 class RESTControllerTest(ControllerTestCase
):
85 def setup_server(cls
):
86 NotificationQueue
.start_queue()
88 cls
.setup_controllers([FooResource
])
91 def tearDownClass(cls
):
92 NotificationQueue
.stop()
94 def test_no_exception(self
):
95 self
._get
('/foo/no_exception/a/b')
96 self
.assertStatus(200)
101 def test_error_foo_controller(self
):
102 self
._get
('/foo/error_foo_controller')
103 self
.assertStatus(400)
105 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
108 def test_error_send_command(self
):
109 self
._get
('/foo/error_send_command')
110 self
.assertStatus(400)
112 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
115 def test_error_send_command_list(self
):
117 self
.assertStatus(400)
119 {'detail': '[errno -42] list', 'code': "42", 'component': 'foo'}
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)
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})
132 def test_viewcache_exception(self
):
133 self
._get
('/foo/vc_exception')
134 self
.assertStatus(400)
136 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
139 def test_task_exception(self
):
140 self
._get
('/foo/task_exception')
141 self
.assertStatus(400)
143 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo',
144 'task': {'name': 'task_exceptions/task_exception', 'metadata': {'1': 2}}}
147 self
._get
('/foo/wait_task_exception')
148 while self
.json_body():
150 self
._get
('/foo/wait_task_exception')
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'])
158 self
._get
('/foonot_found')
159 self
.assertStatus(404)
160 self
.assertIn('detail', self
.json_body())