~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to nova/tests/rpc/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:
25
25
import nose
26
26
 
27
27
from nova import context
 
28
from nova import exception
 
29
from nova import flags
28
30
from nova import log as logging
29
31
from nova.rpc import amqp as rpc_amqp
30
32
from nova.rpc import common as rpc_common
 
33
from nova.rpc import dispatcher as rpc_dispatcher
31
34
from nova import test
32
35
 
33
36
 
 
37
FLAGS = flags.FLAGS
34
38
LOG = logging.getLogger(__name__)
35
39
 
36
40
 
37
41
class BaseRpcTestCase(test.TestCase):
38
42
    def setUp(self, supports_timeouts=True):
39
43
        super(BaseRpcTestCase, self).setUp()
40
 
        self.conn = self.rpc.create_connection(True)
41
 
        self.receiver = TestReceiver()
42
 
        self.conn.create_consumer('test', self.receiver, False)
43
 
        self.conn.consume_in_thread()
 
44
        self.supports_timeouts = supports_timeouts
44
45
        self.context = context.get_admin_context()
45
 
        self.supports_timeouts = supports_timeouts
 
46
        if self.rpc:
 
47
            self.conn = self.rpc.create_connection(FLAGS, True)
 
48
            receiver = TestReceiver()
 
49
            self.dispatcher = rpc_dispatcher.RpcDispatcher([receiver])
 
50
            self.conn.create_consumer('test', self.dispatcher, False)
 
51
            self.conn.consume_in_thread()
46
52
 
47
53
    def tearDown(self):
48
 
        self.conn.close()
 
54
        if self.rpc:
 
55
            self.conn.close()
49
56
        super(BaseRpcTestCase, self).tearDown()
50
57
 
51
58
    def test_call_succeed(self):
 
59
        if not self.rpc:
 
60
            raise nose.SkipTest('rpc driver not available.')
 
61
 
52
62
        value = 42
53
 
        result = self.rpc.call(self.context, 'test', {"method": "echo",
54
 
                                                 "args": {"value": value}})
 
63
        result = self.rpc.call(FLAGS, self.context, 'test',
 
64
                               {"method": "echo", "args": {"value": value}})
55
65
        self.assertEqual(value, result)
56
66
 
57
67
    def test_call_succeed_despite_multiple_returns_yield(self):
 
68
        if not self.rpc:
 
69
            raise nose.SkipTest('rpc driver not available.')
 
70
 
58
71
        value = 42
59
 
        result = self.rpc.call(self.context, 'test',
 
72
        result = self.rpc.call(FLAGS, self.context, 'test',
60
73
                          {"method": "echo_three_times_yield",
61
74
                           "args": {"value": value}})
62
75
        self.assertEqual(value + 2, result)
63
76
 
64
77
    def test_multicall_succeed_once(self):
 
78
        if not self.rpc:
 
79
            raise nose.SkipTest('rpc driver not available.')
 
80
 
65
81
        value = 42
66
 
        result = self.rpc.multicall(self.context,
 
82
        result = self.rpc.multicall(FLAGS, self.context,
67
83
                              'test',
68
84
                              {"method": "echo",
69
85
                               "args": {"value": value}})
73
89
            self.assertEqual(value + i, x)
74
90
 
75
91
    def test_multicall_three_nones(self):
 
92
        if not self.rpc:
 
93
            raise nose.SkipTest('rpc driver not available.')
 
94
 
76
95
        value = 42
77
 
        result = self.rpc.multicall(self.context,
 
96
        result = self.rpc.multicall(FLAGS, self.context,
78
97
                              'test',
79
98
                              {"method": "multicall_three_nones",
80
99
                               "args": {"value": value}})
84
103
        self.assertEqual(i, 2)
85
104
 
86
105
    def test_multicall_succeed_three_times_yield(self):
 
106
        if not self.rpc:
 
107
            raise nose.SkipTest('rpc driver not available.')
 
108
 
87
109
        value = 42
88
 
        result = self.rpc.multicall(self.context,
 
110
        result = self.rpc.multicall(FLAGS, self.context,
89
111
                              'test',
90
112
                              {"method": "echo_three_times_yield",
91
113
                               "args": {"value": value}})
93
115
            self.assertEqual(value + i, x)
94
116
 
95
117
    def test_context_passed(self):
 
118
        if not self.rpc:
 
119
            raise nose.SkipTest('rpc driver not available.')
 
120
 
96
121
        """Makes sure a context is passed through rpc call."""
97
122
        value = 42
98
 
        result = self.rpc.call(self.context,
 
123
        result = self.rpc.call(FLAGS, self.context,
99
124
                          'test', {"method": "context",
100
125
                                   "args": {"value": value}})
101
126
        self.assertEqual(self.context.to_dict(), result)
102
127
 
103
 
    def test_call_exception(self):
104
 
        """Test that exception gets passed back properly.
105
 
 
106
 
        rpc.call returns a RemoteError object.  The value of the
107
 
        exception is converted to a string, so we convert it back
108
 
        to an int in the test.
109
 
 
110
 
        """
111
 
        value = 42
112
 
        self.assertRaises(rpc_common.RemoteError,
113
 
                          self.rpc.call,
114
 
                          self.context,
115
 
                          'test',
116
 
                          {"method": "fail",
117
 
                           "args": {"value": value}})
118
 
        try:
119
 
            self.rpc.call(self.context,
120
 
                     'test',
121
 
                     {"method": "fail",
122
 
                      "args": {"value": value}})
123
 
            self.fail("should have thrown RemoteError")
124
 
        except rpc_common.RemoteError as exc:
125
 
            self.assertEqual(int(exc.value), value)
126
 
 
127
128
    def test_nested_calls(self):
 
129
        if not self.rpc:
 
130
            raise nose.SkipTest('rpc driver not available.')
 
131
 
128
132
        """Test that we can do an rpc.call inside another call."""
129
133
        class Nested(object):
130
134
            @staticmethod
135
139
                # TODO(comstud):
136
140
                # so, it will replay the context and use the same REQID?
137
141
                # that's bizarre.
138
 
                ret = self.rpc.call(context,
 
142
                ret = self.rpc.call(FLAGS, context,
139
143
                               queue,
140
144
                               {"method": "echo",
141
145
                                "args": {"value": value}})
143
147
                return value
144
148
 
145
149
        nested = Nested()
146
 
        conn = self.rpc.create_connection(True)
147
 
        conn.create_consumer('nested', nested, False)
 
150
        dispatcher = rpc_dispatcher.RpcDispatcher([nested])
 
151
        conn = self.rpc.create_connection(FLAGS, True)
 
152
        conn.create_consumer('nested', dispatcher, False)
148
153
        conn.consume_in_thread()
149
154
        value = 42
150
 
        result = self.rpc.call(self.context,
 
155
        result = self.rpc.call(FLAGS, self.context,
151
156
                          'nested', {"method": "echo",
152
157
                                     "args": {"queue": "test",
153
158
                                              "value": value}})
155
160
        self.assertEqual(value, result)
156
161
 
157
162
    def test_call_timeout(self):
 
163
        if not self.rpc:
 
164
            raise nose.SkipTest('rpc driver not available.')
 
165
 
158
166
        """Make sure rpc.call will time out"""
159
167
        if not self.supports_timeouts:
160
168
            raise nose.SkipTest(_("RPC backend does not support timeouts"))
162
170
        value = 42
163
171
        self.assertRaises(rpc_common.Timeout,
164
172
                          self.rpc.call,
165
 
                          self.context,
 
173
                          FLAGS, self.context,
166
174
                          'test',
167
175
                          {"method": "block",
168
176
                           "args": {"value": value}}, timeout=1)
169
177
        try:
170
 
            self.rpc.call(self.context,
 
178
            self.rpc.call(FLAGS, self.context,
171
179
                     'test',
172
180
                     {"method": "block",
173
181
                      "args": {"value": value}},
181
189
    """Base test class for all AMQP-based RPC tests"""
182
190
    def test_proxycallback_handles_exceptions(self):
183
191
        """Make sure exceptions unpacking messages don't cause hangs."""
 
192
        if not self.rpc:
 
193
            raise nose.SkipTest('rpc driver not available.')
 
194
 
184
195
        orig_unpack = rpc_amqp.unpack_context
185
196
 
186
197
        info = {'unpacked': False}
192
203
        self.stubs.Set(rpc_amqp, 'unpack_context', fake_unpack_context)
193
204
 
194
205
        value = 41
195
 
        self.rpc.cast(self.context, 'test', {"method": "echo",
196
 
                                             "args": {"value": value}})
 
206
        self.rpc.cast(FLAGS, self.context, 'test',
 
207
                      {"method": "echo", "args": {"value": value}})
197
208
 
198
209
        # Wait for the cast to complete.
199
210
        for x in xrange(50):
208
219
        self.stubs.Set(rpc_amqp, 'unpack_context', orig_unpack)
209
220
 
210
221
        value = 42
211
 
        result = self.rpc.call(self.context, 'test',
 
222
        result = self.rpc.call(FLAGS, self.context, 'test',
212
223
                {"method": "echo",
213
224
                 "args": {"value": value}})
214
225
        self.assertEqual(value, result)
220
231
    Uses static methods because we aren't actually storing any state.
221
232
 
222
233
    """
223
 
 
224
234
    @staticmethod
225
235
    def echo(context, value):
226
236
        """Simply returns whatever value is sent in."""
248
258
    @staticmethod
249
259
    def fail(context, value):
250
260
        """Raises an exception with the value sent in."""
251
 
        raise Exception(value)
 
261
        raise NotImplementedError(value)
 
262
 
 
263
    @staticmethod
 
264
    def fail_converted(context, value):
 
265
        """Raises an exception with the value sent in."""
 
266
        raise exception.ConvertedException(explanation=value)
252
267
 
253
268
    @staticmethod
254
269
    def block(context, value):