1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 OpenStack, LLC
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
Unit Tests for 'common' functons used through rpc code.
23
from nova import context
24
from nova import exception
25
from nova import flags
26
from nova import log as logging
27
from nova.rpc import amqp as rpc_amqp
28
from nova.rpc import common as rpc_common
30
from nova.tests.rpc import common
33
LOG = logging.getLogger(__name__)
36
def raise_exception():
37
raise Exception("test")
40
class FakeUserDefinedException(Exception):
42
Exception.__init__(self, "Test Message")
45
class RpcCommonTestCase(test.TestCase):
46
def test_serialize_remote_exception(self):
49
'module': 'exceptions',
55
except Exception as exc:
56
failure = rpc_common.serialize_remote_exception(sys.exc_info())
58
failure = json.loads(failure)
59
#assure the traceback was added
60
self.assertEqual(expected['class'], failure['class'])
61
self.assertEqual(expected['module'], failure['module'])
62
self.assertEqual(expected['message'], failure['message'])
64
def test_serialize_remote_nova_exception(self):
65
def raise_nova_exception():
66
raise exception.NovaException("test", code=500)
69
'class': 'NovaException',
70
'module': 'nova.exception',
71
'kwargs': {'code': 500},
76
raise_nova_exception()
77
except Exception as exc:
78
failure = rpc_common.serialize_remote_exception(sys.exc_info())
80
failure = json.loads(failure)
81
#assure the traceback was added
82
self.assertEqual(expected['class'], failure['class'])
83
self.assertEqual(expected['module'], failure['module'])
84
self.assertEqual(expected['kwargs'], failure['kwargs'])
85
self.assertEqual(expected['message'], failure['message'])
87
def test_deserialize_remote_exception(self):
89
'class': 'NovaException',
90
'module': 'nova.exception',
91
'message': 'test message',
92
'tb': ['raise NovaException'],
94
serialized = json.dumps(failure)
96
after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized)
97
self.assertTrue(isinstance(after_exc, exception.NovaException))
98
self.assertTrue('test message' in unicode(after_exc))
99
#assure the traceback was added
100
self.assertTrue('raise NovaException' in unicode(after_exc))
102
def test_deserialize_remote_exception_bad_module(self):
106
'kwargs': {'cmd': '/bin/echo failed'},
109
serialized = json.dumps(failure)
111
after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized)
112
self.assertTrue(isinstance(after_exc, rpc_common.RemoteError))
114
def test_deserialize_remote_exception_user_defined_exception(self):
115
"""Ensure a user defined exception can be deserialized."""
116
self.flags(allowed_rpc_exception_modules=[self.__class__.__module__])
118
'class': 'FakeUserDefinedException',
119
'module': self.__class__.__module__,
120
'tb': ['raise FakeUserDefinedException'],
122
serialized = json.dumps(failure)
124
after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized)
125
self.assertTrue(isinstance(after_exc, FakeUserDefinedException))
126
#assure the traceback was added
127
self.assertTrue('raise FakeUserDefinedException' in unicode(after_exc))
129
def test_deserialize_remote_exception_cannot_recreate(self):
130
"""Ensure a RemoteError is returned on initialization failure.
132
If an exception cannot be recreated with it's original class then a
133
RemoteError with the exception informations should still be returned.
136
self.flags(allowed_rpc_exception_modules=[self.__class__.__module__])
138
'class': 'FakeIDontExistException',
139
'module': self.__class__.__module__,
140
'tb': ['raise FakeIDontExistException'],
142
serialized = json.dumps(failure)
144
after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized)
145
self.assertTrue(isinstance(after_exc, rpc_common.RemoteError))
146
#assure the traceback was added
147
self.assertTrue('raise FakeIDontExistException' in unicode(after_exc))