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
57
58
59
60
61
62
63
64
65
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
|
# Copyright 2013 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 nova.scheduler.rpcapi
"""
from unittest import mock
from oslo_utils.fixture import uuidsentinel as uuids
from nova import conf
from nova import context
from nova import exception as exc
from nova import objects
from nova.scheduler import rpcapi as scheduler_rpcapi
from nova import test
CONF = conf.CONF
class SchedulerRpcAPITestCase(test.NoDBTestCase):
def _test_scheduler_api(self, method, rpc_method, expected_args=None,
**kwargs):
ctxt = context.RequestContext('fake_user', 'fake_project')
rpcapi = scheduler_rpcapi.SchedulerAPI()
self.assertIsNotNone(rpcapi.client)
self.assertEqual(rpcapi.client.target.topic,
scheduler_rpcapi.RPC_TOPIC)
expected_retval = 'foo' if rpc_method == 'call' else None
expected_version = kwargs.pop('version', None)
expected_fanout = kwargs.pop('fanout', None)
expected_kwargs = kwargs.copy()
if expected_args:
expected_kwargs = expected_args
prepare_kwargs = {}
if method == 'select_destinations':
prepare_kwargs.update({
'call_monitor_timeout': CONF.rpc_response_timeout,
'timeout': CONF.long_rpc_timeout
})
if expected_fanout:
prepare_kwargs['fanout'] = True
if expected_version:
prepare_kwargs['version'] = expected_version
# NOTE(sbauza): We need to persist the method before mocking it
orig_prepare = rpcapi.client.prepare
def fake_can_send_version(version=None):
return orig_prepare(version=version).can_send_version()
@mock.patch.object(rpcapi.client, rpc_method,
return_value=expected_retval)
@mock.patch.object(rpcapi.client, 'prepare',
return_value=rpcapi.client)
@mock.patch.object(rpcapi.client, 'can_send_version',
side_effect=fake_can_send_version)
def do_test(mock_csv, mock_prepare, mock_rpc_method):
retval = getattr(rpcapi, method)(ctxt, **kwargs)
self.assertEqual(retval, expected_retval)
mock_prepare.assert_called_once_with(**prepare_kwargs)
mock_rpc_method.assert_called_once_with(ctxt, method,
**expected_kwargs)
do_test()
def test_select_destinations(self):
fake_spec = objects.RequestSpec()
self._test_scheduler_api('select_destinations', rpc_method='call',
expected_args={'spec_obj': fake_spec,
'instance_uuids': [uuids.instance], 'return_objects': True,
'return_alternates': True},
spec_obj=fake_spec, instance_uuids=[uuids.instance],
return_objects=True, return_alternates=True, version='4.5')
def test_select_destinations_4_4(self):
self.flags(scheduler='4.4', group='upgrade_levels')
fake_spec = objects.RequestSpec()
self._test_scheduler_api('select_destinations', rpc_method='call',
expected_args={'spec_obj': fake_spec,
'instance_uuids': [uuids.instance]}, spec_obj=fake_spec,
instance_uuids=[uuids.instance], return_objects=False,
return_alternates=False, version='4.4')
def test_select_destinations_4_3(self):
self.flags(scheduler='4.3', group='upgrade_levels')
fake_spec = objects.RequestSpec()
self._test_scheduler_api('select_destinations', rpc_method='call',
expected_args={'spec_obj': fake_spec},
spec_obj=fake_spec, instance_uuids=[uuids.instance],
return_alternates=False, version='4.3')
def test_select_destinations_old_with_new_params(self):
self.flags(scheduler='4.4', group='upgrade_levels')
fake_spec = objects.RequestSpec()
ctxt = context.RequestContext('fake_user', 'fake_project')
rpcapi = scheduler_rpcapi.SchedulerAPI()
self.assertRaises(exc.SelectionObjectsWithOldRPCVersionNotSupported,
rpcapi.select_destinations, ctxt, fake_spec, ['fake_uuids'],
return_objects=True, return_alternates=True)
self.assertRaises(exc.SelectionObjectsWithOldRPCVersionNotSupported,
rpcapi.select_destinations, ctxt, fake_spec, ['fake_uuids'],
return_objects=False, return_alternates=True)
self.assertRaises(exc.SelectionObjectsWithOldRPCVersionNotSupported,
rpcapi.select_destinations, ctxt, fake_spec, ['fake_uuids'],
return_objects=True, return_alternates=False)
@mock.patch.object(objects.RequestSpec, 'to_legacy_filter_properties_dict')
@mock.patch.object(objects.RequestSpec, 'to_legacy_request_spec_dict')
def test_select_destinations_with_old_manager(self, to_spec, to_props):
self.flags(scheduler='4.0', group='upgrade_levels')
to_spec.return_value = 'fake_request_spec'
to_props.return_value = 'fake_prop'
fake_spec = objects.RequestSpec()
self._test_scheduler_api('select_destinations', rpc_method='call',
expected_args={'request_spec': 'fake_request_spec',
'filter_properties': 'fake_prop'},
spec_obj=fake_spec, instance_uuids=[uuids.instance],
version='4.0')
def test_update_aggregates(self):
self._test_scheduler_api('update_aggregates', rpc_method='cast',
aggregates='aggregates',
version='4.1',
fanout=True)
def test_delete_aggregate(self):
self._test_scheduler_api('delete_aggregate', rpc_method='cast',
aggregate='aggregate',
version='4.1',
fanout=True)
def test_update_instance_info(self):
self._test_scheduler_api('update_instance_info', rpc_method='cast',
host_name='fake_host',
instance_info='fake_instance',
fanout=True,
version='4.2')
def test_delete_instance_info(self):
self._test_scheduler_api('delete_instance_info', rpc_method='cast',
host_name='fake_host',
instance_uuid='fake_uuid',
fanout=True,
version='4.2')
def test_sync_instance_info(self):
self._test_scheduler_api('sync_instance_info', rpc_method='cast',
host_name='fake_host',
instance_uuids=['fake1', 'fake2'],
fanout=True,
version='4.2')
|