~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/tests/rpc/test_common.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 OpenStack, LLC
 
4
#
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
"""
 
17
Unit Tests for 'common' functons used through rpc code.
 
18
"""
 
19
 
 
20
import json
 
21
import sys
 
22
 
 
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
 
29
from nova import test
 
30
from nova.tests.rpc import common
 
31
 
 
32
FLAGS = flags.FLAGS
 
33
LOG = logging.getLogger(__name__)
 
34
 
 
35
 
 
36
def raise_exception():
 
37
    raise Exception("test")
 
38
 
 
39
 
 
40
class FakeUserDefinedException(Exception):
 
41
    def __init__(self):
 
42
        Exception.__init__(self, "Test Message")
 
43
 
 
44
 
 
45
class RpcCommonTestCase(test.TestCase):
 
46
    def test_serialize_remote_exception(self):
 
47
        expected = {
 
48
            'class': 'Exception',
 
49
            'module': 'exceptions',
 
50
            'message': 'test',
 
51
        }
 
52
 
 
53
        try:
 
54
            raise_exception()
 
55
        except Exception as exc:
 
56
            failure = rpc_common.serialize_remote_exception(sys.exc_info())
 
57
 
 
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'])
 
63
 
 
64
    def test_serialize_remote_nova_exception(self):
 
65
        def raise_nova_exception():
 
66
            raise exception.NovaException("test", code=500)
 
67
 
 
68
        expected = {
 
69
            'class': 'NovaException',
 
70
            'module': 'nova.exception',
 
71
            'kwargs': {'code': 500},
 
72
            'message': 'test'
 
73
        }
 
74
 
 
75
        try:
 
76
            raise_nova_exception()
 
77
        except Exception as exc:
 
78
            failure = rpc_common.serialize_remote_exception(sys.exc_info())
 
79
 
 
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'])
 
86
 
 
87
    def test_deserialize_remote_exception(self):
 
88
        failure = {
 
89
            'class': 'NovaException',
 
90
            'module': 'nova.exception',
 
91
            'message': 'test message',
 
92
            'tb': ['raise NovaException'],
 
93
        }
 
94
        serialized = json.dumps(failure)
 
95
 
 
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))
 
101
 
 
102
    def test_deserialize_remote_exception_bad_module(self):
 
103
        failure = {
 
104
            'class': 'popen2',
 
105
            'module': 'os',
 
106
            'kwargs': {'cmd': '/bin/echo failed'},
 
107
            'message': 'foo',
 
108
        }
 
109
        serialized = json.dumps(failure)
 
110
 
 
111
        after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized)
 
112
        self.assertTrue(isinstance(after_exc, rpc_common.RemoteError))
 
113
 
 
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__])
 
117
        failure = {
 
118
            'class': 'FakeUserDefinedException',
 
119
            'module': self.__class__.__module__,
 
120
            'tb': ['raise FakeUserDefinedException'],
 
121
        }
 
122
        serialized = json.dumps(failure)
 
123
 
 
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))
 
128
 
 
129
    def test_deserialize_remote_exception_cannot_recreate(self):
 
130
        """Ensure a RemoteError is returned on initialization failure.
 
131
 
 
132
        If an exception cannot be recreated with it's original class then a
 
133
        RemoteError with the exception informations should still be returned.
 
134
 
 
135
        """
 
136
        self.flags(allowed_rpc_exception_modules=[self.__class__.__module__])
 
137
        failure = {
 
138
            'class': 'FakeIDontExistException',
 
139
            'module': self.__class__.__module__,
 
140
            'tb': ['raise FakeIDontExistException'],
 
141
        }
 
142
        serialized = json.dumps(failure)
 
143
 
 
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))