1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
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
16 # pylint: disable=W0613
17 @Controller('foo', secure
=False)
18 class FooResource(RESTController
):
21 @handle_rados_error('foo')
22 def no_exception(self
, param1
, param2
):
23 return [param1
, param2
]
26 @handle_rados_error('foo')
27 def error_foo_controller(self
):
28 raise rados
.OSError('hi', errno
=-42)
31 @handle_send_command_error('foo')
32 def error_send_command(self
):
33 raise SendCommandError('hi', 'prefix', {}, -42)
36 def error_generic(self
):
37 raise rados
.Error('hi')
48 @handle_rados_error('foo')
50 def vc_exception(self
):
51 @ViewCache(timeout
=10)
53 raise rados
.OSError('hi', errno
=-42)
59 def internal_server_error(self
):
62 @handle_send_command_error('foo')
64 raise SendCommandError('list', 'prefix', {}, -42)
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)
74 def wait_task_exception(self
):
75 ex
, _
= TaskManager
.list('task_exceptions/task_exception')
79 # pylint: disable=C0102
84 class RESTControllerTest(ControllerTestCase
):
86 def setup_server(cls
):
87 NotificationQueue
.start_queue()
89 cls
.setup_controllers([FooResource
])
91 def test_no_exception(self
):
92 self
._get
('/foo/no_exception/a/b')
93 self
.assertStatus(200)
98 def test_error_foo_controller(self
):
99 self
._get
('/foo/error_foo_controller')
100 self
.assertStatus(400)
102 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
105 def test_error_send_command(self
):
106 self
._get
('/foo/error_send_command')
107 self
.assertStatus(400)
109 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
112 def test_error_send_command_list(self
):
114 self
.assertStatus(400)
116 {'detail': '[errno -42] list', 'code': "42", 'component': 'foo'}
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)
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})
129 def test_viewcache_exception(self
):
130 self
._get
('/foo/vc_exception')
131 self
.assertStatus(400)
133 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo'}
136 def test_task_exception(self
):
137 self
._get
('/foo/task_exception')
138 self
.assertStatus(400)
140 {'detail': '[errno -42] hi', 'code': "42", 'component': 'foo',
141 'task': {'name': 'task_exceptions/task_exception', 'metadata': {'1': 2}}}
144 self
._get
('/foo/wait_task_exception')
145 while self
.json_body():
147 self
._get
('/foo/wait_task_exception')
149 def test_internal_server_error(self
):
150 self
._get
('/foo/internal_server_error')
151 self
.assertStatus(500)
152 self
.assertIn('unexpected condition', self
.json_body()['detail'])
155 self
._get
('/foonot_found')
156 self
.assertStatus(404)
157 self
.assertIn('detail', self
.json_body())