1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
from heat.common import exception as heat_exc
16
from heat.openstack.common.rpc import common as rpc_common
17
from heat.tests.common import HeatTestCase
18
from oslo.config import cfg
20
import heat.api.middleware.fault as fault
23
class FaultMiddlewareTest(HeatTestCase):
25
def test_openstack_exception_with_kwargs(self):
26
wrapper = fault.FaultWrapper(None)
27
msg = wrapper._error(heat_exc.StackNotFound(stack_name='a'))
28
expected = {'code': 404,
29
'error': {'message': 'The Stack (a) could not be found.',
30
'traceback': 'None\n',
31
'type': 'StackNotFound'},
32
'explanation': 'The resource could not be found.',
34
self.assertEqual(msg, expected)
36
def test_openstack_exception_without_kwargs(self):
37
wrapper = fault.FaultWrapper(None)
38
msg = wrapper._error(heat_exc.NoServiceEndpoint())
39
expected = {'code': 500,
40
'error': {'message': 'Response from Keystone does '
41
'not contain a Heat endpoint.',
42
'traceback': 'None\n',
43
'type': 'NoServiceEndpoint'},
44
'explanation': 'The server has either erred or is '
45
'incapable of performing the requested '
47
'title': 'Internal Server Error'}
48
self.assertEqual(msg, expected)
50
def test_remote_exception(self):
51
error = heat_exc.StackNotFound(stack_name='a')
52
exc_info = (type(error), error, None)
53
serialized = rpc_common.serialize_remote_exception(exc_info)
54
remote_error = rpc_common.deserialize_remote_exception(cfg.CONF,
56
wrapper = fault.FaultWrapper(None)
57
msg = wrapper._error(remote_error)
58
expected_message, expected_traceback = str(remote_error).split('\n', 1)
59
expected = {'code': 404,
60
'error': {'message': expected_message,
61
'traceback': expected_traceback,
62
'type': 'StackNotFound'},
63
'explanation': 'The resource could not be found.',
65
self.assertEqual(msg, expected)