1 # Copyright (c) 2012 OpenStack Foundation.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
18 from oslo_context import context as oslo_context
21 from neutron.agent import rpc
22 from neutron.tests import base
25 class AgentRPCPluginApi(base.BaseTestCase):
26 def _test_rpc_call(self, method):
27 agent = rpc.PluginApi('fake_topic')
28 ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
30 with mock.patch.object(agent.client, 'call') as mock_call,\
31 mock.patch.object(agent.client, 'prepare') as mock_prepare:
32 mock_prepare.return_value = agent.client
33 mock_call.return_value = expect_val
34 func_obj = getattr(agent, method)
35 if method == 'tunnel_sync':
36 actual_val = func_obj(ctxt, 'fake_tunnel_ip')
38 actual_val = func_obj(ctxt, 'fake_device', 'fake_agent_id')
39 self.assertEqual(actual_val, expect_val)
41 def test_get_device_details(self):
42 self._test_rpc_call('get_device_details')
44 def test_get_devices_details_list(self):
45 self._test_rpc_call('get_devices_details_list')
47 def test_devices_details_list_unsupported(self):
48 agent = rpc.PluginApi('fake_topic')
49 ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
50 expect_val_get_device_details = 'foo'
51 expect_val = [expect_val_get_device_details]
52 with mock.patch.object(agent.client, 'call') as mock_call, \
53 mock.patch.object(agent.client, 'prepare') as mock_prepare:
54 mock_prepare.return_value = agent.client
55 mock_call.side_effect = [oslo_messaging.UnsupportedVersion('1.2'),
56 expect_val_get_device_details]
57 func_obj = getattr(agent, 'get_devices_details_list')
58 actual_val = func_obj(ctxt, ['fake_device'], 'fake_agent_id')
59 self.assertEqual(actual_val, expect_val)
61 def test_update_device_down(self):
62 self._test_rpc_call('update_device_down')
64 def test_tunnel_sync(self):
65 self._test_rpc_call('tunnel_sync')
68 class AgentPluginReportState(base.BaseTestCase):
69 def test_plugin_report_state_use_call(self):
71 reportStateAPI = rpc.PluginReportStateAPI(topic)
72 expected_agent_state = {'agent': 'test'}
73 with mock.patch.object(reportStateAPI.client, 'call') as mock_call, \
74 mock.patch.object(reportStateAPI.client, 'cast'), \
75 mock.patch.object(reportStateAPI.client, 'prepare'
77 mock_prepare.return_value = reportStateAPI.client
78 ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
79 reportStateAPI.report_state(ctxt, expected_agent_state,
81 self.assertEqual(mock_call.call_args[0][0], ctxt)
82 self.assertEqual(mock_call.call_args[0][1], 'report_state')
83 self.assertEqual(mock_call.call_args[1]['agent_state'],
84 {'agent_state': expected_agent_state})
85 self.assertIsInstance(mock_call.call_args[1]['time'], str)
87 def test_plugin_report_state_cast(self):
89 reportStateAPI = rpc.PluginReportStateAPI(topic)
90 expected_agent_state = {'agent': 'test'}
91 with mock.patch.object(reportStateAPI.client, 'call'), \
92 mock.patch.object(reportStateAPI.client, 'cast'
94 mock.patch.object(reportStateAPI.client, 'prepare'
96 mock_prepare.return_value = reportStateAPI.client
97 ctxt = oslo_context.RequestContext('fake_user', 'fake_project')
98 reportStateAPI.report_state(ctxt, expected_agent_state)
99 self.assertEqual(mock_cast.call_args[0][0], ctxt)
100 self.assertEqual(mock_cast.call_args[0][1], 'report_state')
101 self.assertEqual(mock_cast.call_args[1]['agent_state'],
102 {'agent_state': expected_agent_state})
103 self.assertIsInstance(mock_cast.call_args[1]['time'], str)
105 def test_plugin_report_state_microsecond_is_0(self):
107 expected_time = datetime.datetime(2015, 7, 27, 15, 33, 30, 0)
108 expected_time_str = '2015-07-27T15:33:30.000000'
109 expected_agent_state = {'agent': 'test'}
110 with mock.patch('neutron.agent.rpc.datetime') as mock_datetime:
111 reportStateAPI = rpc.PluginReportStateAPI(topic)
112 mock_datetime.utcnow.return_value = expected_time
113 with mock.patch.object(reportStateAPI.client, 'call'), \
114 mock.patch.object(reportStateAPI.client, 'cast'
116 mock.patch.object(reportStateAPI.client, 'prepare'
118 mock_prepare.return_value = reportStateAPI.client
119 ctxt = oslo_context.RequestContext('fake_user',
121 reportStateAPI.report_state(ctxt, expected_agent_state)
122 self.assertEqual(expected_time_str,
123 mock_cast.call_args[1]['time'])
126 class AgentRPCMethods(base.BaseTestCase):
128 def _test_create_consumers(
129 self, endpoints, method, expected, topics, listen):
130 call_to_patch = 'neutron.common.rpc.create_connection'
131 with mock.patch(call_to_patch) as create_connection:
132 rpc.create_consumers(
133 endpoints, method, topics, start_listening=listen)
134 create_connection.assert_has_calls(expected)
136 def test_create_consumers_start_listening(self):
137 endpoints = [mock.Mock()]
140 mock.call().create_consumer('foo-topic-op', endpoints,
142 mock.call().consume_in_threads()
145 topics = [('topic', 'op')]
146 self._test_create_consumers(
147 endpoints, method, expected, topics, True)
149 def test_create_consumers_do_not_listen(self):
150 endpoints = [mock.Mock()]
153 mock.call().create_consumer('foo-topic-op', endpoints,
157 topics = [('topic', 'op')]
158 self._test_create_consumers(
159 endpoints, method, expected, topics, False)
161 def test_create_consumers_with_node_name(self):
162 endpoints = [mock.Mock()]
165 mock.call().create_consumer('foo-topic-op', endpoints,
167 mock.call().create_consumer('foo-topic-op.node1', endpoints,
169 mock.call().consume_in_threads()
172 call_to_patch = 'neutron.common.rpc.create_connection'
173 with mock.patch(call_to_patch) as create_connection:
174 rpc.create_consumers(endpoints, 'foo', [('topic', 'op', 'node1')])
175 create_connection.assert_has_calls(expected)