summaryrefslogtreecommitdiffstats
path: root/tests/unit/rpc/test_dispatcher.py
blob: a09d35826685bc762226e54dd81c811daa909a25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
generated by cgit  (git 2.34.1) at 2024-05-09 12:32:39 +0000
 


'#n66'>66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012, Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Unit Tests for rpc.dispatcher
"""

import mox

from openstack.common import context
from openstack.common.rpc import common as rpc_common
from openstack.common.rpc import dispatcher
from openstack.common.rpc import serializer as rpc_serializer
from tests import utils


class RpcDispatcherTestCase(utils.BaseTestCase):
    class API1(object):
        RPC_API_VERSION = '1.0'

        def __init__(self):
            self.test_method_ctxt = None
            self.test_method_arg1 = None

        def test_method(self, ctxt, arg1):
            self.test_method_ctxt = ctxt
            self.test_method_arg1 = arg1
            return 'fake-result'

    class API2(object):
        RPC_API_VERSION = '2.1'

        def __init__(self):
            self.test_method_ctxt = None
            self.test_method_arg1 = None

        def test_method(self, ctxt, arg1):
            self.test_method_ctxt = ctxt
            self.test_method_arg1 = arg1

    class API3(object):
        RPC_API_VERSION = '3.5'

        def __init__(self):
            self.test_method_ctxt = None
            self.test_method_arg1 = None

        def test_method(self, ctxt, arg1):
            self.test_method_ctxt = ctxt
            self.test_method_arg1 = arg1

    class API4(object):
        RPC_API_VERSION = '1.0'
        RPC_API_NAMESPACE = 'testapi'

        def __init__(self):
            self.test_method_ctxt = None
            self.test_method_arg1 = None

        def test_method(self, ctxt, arg1):
            self.test_method_ctxt = ctxt
            self.test_method_arg1 = arg1

    def setUp(self):
        super(RpcDispatcherTestCase, self).setUp()
        self.ctxt = context.RequestContext('fake_user', 'fake_project')
        self.mox = mox.Mox()

    def cleanUp(self):
        super(RpcDispatcherTestCase, self).setUp()
        self.mox.VerifyAll()

    def _test_dispatch(self, version, expectations):
        v2 = self.API2()
        v3 = self.API3()
        disp = dispatcher.RpcDispatcher([v2, v3])

        disp.dispatch(self.ctxt, version, 'test_method', None, arg1=1)

        self.assertEqual(v2.test_method_ctxt, expectations[0])
        self.assertEqual(v2.test_method_arg1, expectations[1])
        self.assertEqual(v3.test_method_ctxt, expectations[2])
        self.assertEqual(v3.test_method_arg1, expectations[3])

    def test_dispatch(self):
        self._test_dispatch('2.1', (self.ctxt, 1, None, None))
        self._test_dispatch('3.5', (None, None, self.ctxt, 1))

    def test_dispatch_lower_minor_version(self):
        self._test_dispatch('2.0', (self.ctxt, 1, None, None))
        self._test_dispatch('3.1', (None, None, self.ctxt, 1))

    def test_dispatch_higher_minor_version(self):
        self.assertRaises(
            rpc_common.UnsupportedRpcVersion,
            self._test_dispatch, '2.6', (None, None, None, None))
        self.assertRaises(
            rpc_common.UnsupportedRpcVersion,
            self._test_dispatch, '3.6', (None, None, None, None))

    def test_dispatch_lower_major_version(self):
        self.assertRaises(
            rpc_common.UnsupportedRpcVersion,
            self._test_dispatch, '1.0', (None, None, None, None))

    def test_dispatch_higher_major_version(self):
        self.assertRaises(
            rpc_common.UnsupportedRpcVersion,
            self._test_dispatch, '4.0', (None, None, None, None))

    def test_dispatch_no_version_uses_v1(self):
        v1 = self.API1()
        disp = dispatcher.RpcDispatcher([v1])

        disp.dispatch(self.ctxt, None, 'test_method', None, arg1=1)

        self.assertEqual(v1.test_method_ctxt, self.ctxt)
        self.assertEqual(v1.test_method_arg1, 1)

    def test_missing_method_version_match(self):
        v1 = self.API1()
        disp = dispatcher.RpcDispatcher([v1])
        self.assertRaises(AttributeError,
                          disp.dispatch,
                          self.ctxt, "1.0", "does_not_exist", None)

    def test_missing_method_version_no_match(self):
        v1 = self.API1()
        disp = dispatcher.RpcDispatcher([v1])
        self.assertRaises(rpc_common.UnsupportedRpcVersion,
                          disp.dispatch,
                          self.ctxt, "2.0", "does_not_exist", None)

    def test_method_without_namespace(self):
        v1 = self.API1()
        v4 = self.API4()
        disp = dispatcher.RpcDispatcher([v1, v4])

        disp.dispatch(self.ctxt, '1.0', 'test_method', None, arg1=1)

        self.assertEqual(v1.test_method_ctxt, self.ctxt)
        self.assertEqual(v1.test_method_arg1, 1)
        self.assertEqual(v4.test_method_ctxt, None)
        self.assertEqual(v4.test_method_arg1, None)

    def test_method_with_namespace(self):
        v1 = self.API1()
        v4 = self.API4()
        disp = dispatcher.RpcDispatcher([v1, v4])

        disp.dispatch(self.ctxt, '1.0', 'test_method', 'testapi', arg1=1)

        self.assertEqual(v1.test_method_ctxt, None)
        self.assertEqual(v1.test_method_arg1, None)
        self.assertEqual(v4.test_method_ctxt, self.ctxt)
        self.assertEqual(v4.test_method_arg1, 1)

    def test_serializer(self):
        api = self.API1()
        serializer = rpc_serializer.NoOpSerializer()

        self.mox.StubOutWithMock(serializer, 'serialize_entity')
        self.mox.StubOutWithMock(serializer, 'deserialize_entity')

        serializer.deserialize_entity(self.ctxt, 1).AndReturn(1)
        serializer.serialize_entity(self.ctxt, 'fake-result').AndReturn(
            'worked!')

        self.mox.ReplayAll()

        disp = dispatcher.RpcDispatcher([api], serializer)
        result = disp.dispatch(self.ctxt, '1.0', 'test_method',
                               None, arg1=1)
        self.assertEqual(result, 'worked!')