Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / tests / unit / agent / test_rpc.py
1 # Copyright (c) 2012 OpenStack Foundation.
2 # All Rights Reserved.
3 #
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
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 #    under the License.
15
16 import datetime
17 import mock
18 from oslo_context import context as oslo_context
19 import oslo_messaging
20
21 from neutron.agent import rpc
22 from neutron.tests import base
23
24
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')
29         expect_val = 'foo'
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')
37             else:
38                 actual_val = func_obj(ctxt, 'fake_device', 'fake_agent_id')
39         self.assertEqual(actual_val, expect_val)
40
41     def test_get_device_details(self):
42         self._test_rpc_call('get_device_details')
43
44     def test_get_devices_details_list(self):
45         self._test_rpc_call('get_devices_details_list')
46
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)
60
61     def test_update_device_down(self):
62         self._test_rpc_call('update_device_down')
63
64     def test_tunnel_sync(self):
65         self._test_rpc_call('tunnel_sync')
66
67
68 class AgentPluginReportState(base.BaseTestCase):
69     def test_plugin_report_state_use_call(self):
70         topic = 'test'
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'
76                                   ) as mock_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,
80                                         use_call=True)
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)
86
87     def test_plugin_report_state_cast(self):
88         topic = 'test'
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'
93                                   ) as mock_cast, \
94                 mock.patch.object(reportStateAPI.client, 'prepare'
95                                   ) as mock_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)
104
105     def test_plugin_report_state_microsecond_is_0(self):
106         topic = 'test'
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'
115                                       ) as mock_cast, \
116                     mock.patch.object(reportStateAPI.client, 'prepare'
117                                       ) as mock_prepare:
118                 mock_prepare.return_value = reportStateAPI.client
119                 ctxt = oslo_context.RequestContext('fake_user',
120                                                    'fake_project')
121                 reportStateAPI.report_state(ctxt, expected_agent_state)
122                 self.assertEqual(expected_time_str,
123                                  mock_cast.call_args[1]['time'])
124
125
126 class AgentRPCMethods(base.BaseTestCase):
127
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)
135
136     def test_create_consumers_start_listening(self):
137         endpoints = [mock.Mock()]
138         expected = [
139             mock.call(),
140             mock.call().create_consumer('foo-topic-op', endpoints,
141                                         fanout=True),
142             mock.call().consume_in_threads()
143         ]
144         method = 'foo'
145         topics = [('topic', 'op')]
146         self._test_create_consumers(
147             endpoints, method, expected, topics, True)
148
149     def test_create_consumers_do_not_listen(self):
150         endpoints = [mock.Mock()]
151         expected = [
152             mock.call(),
153             mock.call().create_consumer('foo-topic-op', endpoints,
154                                         fanout=True),
155         ]
156         method = 'foo'
157         topics = [('topic', 'op')]
158         self._test_create_consumers(
159             endpoints, method, expected, topics, False)
160
161     def test_create_consumers_with_node_name(self):
162         endpoints = [mock.Mock()]
163         expected = [
164             mock.call(),
165             mock.call().create_consumer('foo-topic-op', endpoints,
166                                         fanout=True),
167             mock.call().create_consumer('foo-topic-op.node1', endpoints,
168                                         fanout=False),
169             mock.call().consume_in_threads()
170         ]
171
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)