1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012, Red Hat, Inc.
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
18
Unit Tests for rpc.proxy
23
from nova import context
25
from nova.rpc import proxy
29
class RpcProxyTestCase(test.TestCase):
32
super(RpcProxyTestCase, self).setUp()
35
super(RpcProxyTestCase, self).tearDown()
37
def _test_rpc_method(self, rpc_method, has_timeout=False, has_retval=False,
38
server_params=None, supports_topic_override=True):
41
rpc_proxy = proxy.RpcProxy(topic, '1.0')
42
ctxt = context.RequestContext('fake_user', 'fake_project')
43
msg = {'method': 'fake_method', 'args': {'x': 'y'}}
44
expected_msg = {'method': 'fake_method', 'args': {'x': 'y'},
47
expected_retval = 'hi' if has_retval else None
50
self.fake_kwargs = None
52
def _fake_rpc_method(*args, **kwargs):
54
self.fake_kwargs = kwargs
56
return expected_retval
58
self.stubs.Set(rpc, rpc_method, _fake_rpc_method)
62
args.insert(1, server_params)
65
retval = getattr(rpc_proxy, rpc_method)(*args)
66
self.assertEqual(retval, expected_retval)
67
expected_args = [ctxt, topic, expected_msg]
69
expected_args.insert(1, server_params)
70
for arg, expected_arg in zip(self.fake_args, expected_args):
71
self.assertEqual(arg, expected_arg)
73
# overriding the version
74
retval = getattr(rpc_proxy, rpc_method)(*args, version='1.1')
75
self.assertEqual(retval, expected_retval)
76
new_msg = copy.deepcopy(expected_msg)
77
new_msg['version'] = '1.1'
78
expected_args = [ctxt, topic, new_msg]
80
expected_args.insert(1, server_params)
81
for arg, expected_arg in zip(self.fake_args, expected_args):
82
self.assertEqual(arg, expected_arg)
86
retval = getattr(rpc_proxy, rpc_method)(ctxt, msg, timeout=timeout)
87
self.assertEqual(retval, expected_retval)
88
expected_args = [ctxt, topic, expected_msg, timeout]
89
for arg, expected_arg in zip(self.fake_args, expected_args):
90
self.assertEqual(arg, expected_arg)
92
if supports_topic_override:
95
retval = getattr(rpc_proxy, rpc_method)(*args, topic=new_topic)
96
self.assertEqual(retval, expected_retval)
97
expected_args = [ctxt, new_topic, expected_msg]
99
expected_args.insert(1, server_params)
100
for arg, expected_arg in zip(self.fake_args, expected_args):
101
self.assertEqual(arg, expected_arg)
104
self._test_rpc_method('call', has_timeout=True, has_retval=True)
106
def test_multicall(self):
107
self._test_rpc_method('multicall', has_timeout=True, has_retval=True)
110
self._test_rpc_method('cast')
112
def test_fanout_cast(self):
113
self._test_rpc_method('fanout_cast', supports_topic_override=False)
115
def test_cast_to_server(self):
116
self._test_rpc_method('cast_to_server', server_params={'blah': 1})
118
def test_fanout_cast_to_server(self):
119
self._test_rpc_method('fanout_cast_to_server',
120
server_params={'blah': 1}, supports_topic_override=False)
122
def test_make_msg(self):
123
self.assertEqual(proxy.RpcProxy.make_msg('test_method', a=1, b=2),
124
{'method': 'test_method', 'args': {'a': 1, 'b': 2}})