/
test_nec_agent.py
366 lines (304 loc) · 14.9 KB
/
test_nec_agent.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2013 NEC Corporation. All rights reserved.
#
# 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.
import contextlib
import copy
import itertools
import time
import mock
from oslo.config import cfg
from six import moves
import testtools
from neutron.agent.linux import ovs_lib
from neutron.extensions import securitygroup as ext_sg
from neutron.plugins.nec.agent import nec_neutron_agent
from neutron.tests import base
DAEMON_LOOP_COUNT = 10
OVS_DPID = '00000629355b6943'
OVS_DPID_0X = '0x' + OVS_DPID
class TestNecAgentBase(base.BaseTestCase):
def setUp(self):
super(TestNecAgentBase, self).setUp()
cfg.CONF.set_default('firewall_driver',
'neutron.agent.firewall.NoopFirewallDriver',
group='SECURITYGROUP')
cfg.CONF.set_override('rpc_backend',
'neutron.openstack.common.rpc.impl_fake')
cfg.CONF.set_override('host', 'dummy-host')
with contextlib.nested(
mock.patch.object(ovs_lib.OVSBridge, 'get_datapath_id',
return_value=OVS_DPID),
mock.patch('socket.gethostname', return_value='dummy-host'),
mock.patch('neutron.openstack.common.loopingcall.'
'FixedIntervalLoopingCall'),
mock.patch('neutron.agent.rpc.PluginReportStateAPI')
) as (get_datapath_id, gethostname,
loopingcall, state_rpc_api):
kwargs = {'integ_br': 'integ_br',
'root_helper': 'dummy_wrapper',
'polling_interval': 1}
self.agent = nec_neutron_agent.NECNeutronAgent(**kwargs)
self.loopingcall = loopingcall
self.state_rpc_api = state_rpc_api
class TestNecAgent(TestNecAgentBase):
def _setup_mock(self):
vif_ports = [ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1',
self.agent.int_br),
ovs_lib.VifPort('port2', '2', 'id-2', 'mac-2',
self.agent.int_br)]
self.get_vif_ports = mock.patch.object(
ovs_lib.OVSBridge, 'get_vif_ports',
return_value=vif_ports).start()
self.update_ports = mock.patch.object(
nec_neutron_agent.NECPluginApi, 'update_ports').start()
self.prepare_devices_filter = mock.patch.object(
self.agent.sg_agent, 'prepare_devices_filter').start()
self.remove_devices_filter = mock.patch.object(
self.agent.sg_agent, 'remove_devices_filter').start()
def _test_single_loop(self, with_exc=False, need_sync=False):
self.agent.cur_ports = ['id-0', 'id-1']
self.agent.need_sync = need_sync
self.agent.loop_handler()
if with_exc:
self.assertEqual(self.agent.cur_ports, ['id-0', 'id-1'])
self.assertTrue(self.agent.need_sync)
else:
self.assertEqual(self.agent.cur_ports, ['id-1', 'id-2'])
self.assertFalse(self.agent.need_sync)
def test_single_loop_normal(self):
self._setup_mock()
self._test_single_loop()
agent_id = 'nec-q-agent.dummy-host'
self.update_ports.assert_called_once_with(
mock.ANY, agent_id, OVS_DPID_0X,
[{'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
['id-0'])
self.prepare_devices_filter.assert_called_once_with(['id-2'])
self.remove_devices_filter.assert_called_once_with(['id-0'])
def test_single_loop_need_sync(self):
self._setup_mock()
self._test_single_loop(need_sync=True)
agent_id = 'nec-q-agent.dummy-host'
self.update_ports.assert_called_once_with(
mock.ANY, agent_id, OVS_DPID_0X,
[{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'},
{'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
[])
self.prepare_devices_filter.assert_called_once_with(['id-1', 'id-2'])
self.assertFalse(self.remove_devices_filter.call_count)
def test_single_loop_with_sg_exception_remove(self):
self._setup_mock()
self.update_ports.side_effect = Exception()
self._test_single_loop(with_exc=True)
def test_single_loop_with_sg_exception_prepare(self):
self._setup_mock()
self.prepare_devices_filter.side_effect = Exception()
self._test_single_loop(with_exc=True)
def test_single_loop_with_update_ports_exception(self):
self._setup_mock()
self.remove_devices_filter.side_effect = Exception()
self._test_single_loop(with_exc=True)
def test_daemon_loop(self):
def state_check(index):
self.assertEqual(len(self.vif_ports_scenario[index]),
len(self.agent.cur_ports))
# Fake time.sleep to stop the infinite loop in daemon_loop()
self.sleep_count = 0
def sleep_mock(*args, **kwargs):
state_check(self.sleep_count)
self.sleep_count += 1
if self.sleep_count >= DAEMON_LOOP_COUNT:
raise RuntimeError()
vif_ports = [ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1',
self.agent.int_br),
ovs_lib.VifPort('port2', '2', 'id-2', 'mac-2',
self.agent.int_br)]
self.vif_ports_scenario = [[], [], vif_ports[0:1], vif_ports[0:2],
vif_ports[1:2], []]
# Ensure vif_ports_scenario is longer than DAEMON_LOOP_COUNT
if len(self.vif_ports_scenario) < DAEMON_LOOP_COUNT:
self.vif_ports_scenario.extend(
[] for _i in moves.xrange(DAEMON_LOOP_COUNT -
len(self.vif_ports_scenario)))
with contextlib.nested(
mock.patch.object(time, 'sleep', side_effect=sleep_mock),
mock.patch.object(ovs_lib.OVSBridge, 'get_vif_ports'),
mock.patch.object(nec_neutron_agent.NECPluginApi, 'update_ports'),
mock.patch.object(self.agent.sg_agent, 'prepare_devices_filter'),
mock.patch.object(self.agent.sg_agent, 'remove_devices_filter')
) as (sleep, get_vif_potrs, update_ports,
prepare_devices_filter, remove_devices_filter):
get_vif_potrs.side_effect = self.vif_ports_scenario
with testtools.ExpectedException(RuntimeError):
self.agent.daemon_loop()
self.assertEqual(update_ports.call_count, 4)
self.assertEqual(sleep.call_count, DAEMON_LOOP_COUNT)
agent_id = 'nec-q-agent.dummy-host'
expected = [
mock.call(mock.ANY, agent_id, OVS_DPID_0X,
[{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'}],
[]),
mock.call(mock.ANY, agent_id, OVS_DPID_0X,
[{'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
[]),
mock.call(mock.ANY, agent_id, OVS_DPID_0X,
[], ['id-1']),
mock.call(mock.ANY, agent_id, OVS_DPID_0X,
[], ['id-2'])
]
update_ports.assert_has_calls(expected)
expected = [mock.call(['id-1']),
mock.call(['id-2'])]
self.assertEqual(prepare_devices_filter.call_count, 2)
prepare_devices_filter.assert_has_calls(expected)
self.assertEqual(remove_devices_filter.call_count, 2)
remove_devices_filter.assert_has_calls(expected)
sleep.assert_called_with(self.agent.polling_interval)
def test_report_state_installed(self):
self.loopingcall.assert_called_once_with(self.agent._report_state)
instance = self.loopingcall.return_value
self.assertTrue(instance.start.called)
def _check_report_state(self, cur_ports, num_ports, fail_mode,
first=False):
self.assertEqual(first or fail_mode,
'start_flag' in self.agent.agent_state)
self.agent.cur_ports = cur_ports
self.agent._report_state()
self.assertEqual(fail_mode,
'start_flag' in self.agent.agent_state)
self.assertEqual(self.agent.
agent_state['configurations']['devices'],
num_ports)
self.num_ports_hist.append(num_ports)
def _test_report_state(self, fail_mode):
log_mocked = mock.patch.object(nec_neutron_agent, 'LOG')
log_patched = log_mocked.start()
def record_state(*args, **kwargs):
self.record_calls.append(copy.deepcopy(args))
if fail_mode:
raise Exception()
self.record_calls = []
self.num_ports_hist = []
state_rpc = self.state_rpc_api.return_value
state_rpc.report_state.side_effect = record_state
dummy_vif = ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1', None)
self.state_rpc_api.assert_called_once_with('q-plugin')
self.assertIn('start_flag', self.agent.agent_state)
self._check_report_state([], 0, fail_mode, first=True)
self._check_report_state([dummy_vif] * 2, 2, fail_mode)
self._check_report_state([dummy_vif] * 5, 5, fail_mode)
self._check_report_state([], 0, fail_mode)
# Since loopingcall start is mocked, call_count is same as
# the call count of check_report_state.
self.assertEqual(state_rpc.report_state.call_count, 4)
self.assertEqual(len(self.record_calls), 4)
for i, x in enumerate(itertools.izip(self.record_calls,
self.num_ports_hist)):
rec, num_ports = x
expected_state = {
'binary': 'neutron-nec-agent',
'host': 'dummy-host',
'topic': 'N/A',
'configurations': {'devices': 0},
'agent_type': 'NEC plugin agent'}
expected_state['configurations']['devices'] = num_ports
if i == 0 or fail_mode:
expected_state['start_flag'] = True
self.assertEqual(expected_state, rec[1])
self.assertEqual(fail_mode, log_patched.exception.called)
def test_report_state(self):
self._test_report_state(fail_mode=False)
def test_report_state_fail(self):
self._test_report_state(fail_mode=True)
class TestNecAgentCallback(TestNecAgentBase):
def test_port_update(self):
with contextlib.nested(
mock.patch.object(ovs_lib.OVSBridge, 'get_vif_port_by_id'),
mock.patch.object(self.agent.sg_agent, 'refresh_firewall')
) as (get_vif_port_by_id, refresh_firewall):
context = mock.Mock()
vifport = ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1',
self.agent.int_br)
# The OVS port does not exist.
get_vif_port_by_id.return_value = None
port = {'id': 'update-port-1'}
self.agent.callback_nec.port_update(context, port=port)
self.assertEqual(get_vif_port_by_id.call_count, 1)
self.assertFalse(refresh_firewall.call_count)
# The OVS port exists but no security group is associated.
get_vif_port_by_id.return_value = vifport
port = {'id': 'update-port-1'}
self.agent.callback_nec.port_update(context, port=port)
self.assertEqual(get_vif_port_by_id.call_count, 2)
self.assertFalse(refresh_firewall.call_count)
# The OVS port exists but a security group is associated.
get_vif_port_by_id.return_value = vifport
port = {'id': 'update-port-1',
ext_sg.SECURITYGROUPS: ['default']}
self.agent.callback_nec.port_update(context, port=port)
self.assertEqual(get_vif_port_by_id.call_count, 3)
self.assertEqual(refresh_firewall.call_count, 1)
get_vif_port_by_id.return_value = None
port = {'id': 'update-port-1',
ext_sg.SECURITYGROUPS: ['default']}
self.agent.callback_nec.port_update(context, port=port)
self.assertEqual(get_vif_port_by_id.call_count, 4)
self.assertEqual(refresh_firewall.call_count, 1)
class TestNecAgentPluginApi(TestNecAgentBase):
def _test_plugin_api(self, expected_failure=False):
with contextlib.nested(
mock.patch.object(nec_neutron_agent.NECPluginApi, 'make_msg'),
mock.patch.object(nec_neutron_agent.NECPluginApi, 'call'),
mock.patch.object(nec_neutron_agent, 'LOG')
) as (make_msg, apicall, log):
agent_id = 'nec-q-agent.dummy-host'
if expected_failure:
apicall.side_effect = Exception()
self.agent.plugin_rpc.update_ports(
mock.sentinel.ctx, agent_id, OVS_DPID_0X,
# port_added
[{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'},
{'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
# port_removed
['id-3', 'id-4', 'id-5'])
make_msg.assert_called_once_with(
'update_ports', topic='q-agent-notifier',
agent_id=agent_id, datapath_id=OVS_DPID_0X,
port_added=[{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'},
{'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
port_removed=['id-3', 'id-4', 'id-5'])
apicall.assert_called_once_with(mock.sentinel.ctx,
make_msg.return_value)
self.assertTrue(log.info.called)
if expected_failure:
self.assertTrue(log.warn.called)
def test_plugin_api(self):
self._test_plugin_api()
class TestNecAgentMain(base.BaseTestCase):
def test_main(self):
with contextlib.nested(
mock.patch.object(nec_neutron_agent, 'NECNeutronAgent'),
mock.patch.object(nec_neutron_agent, 'logging_config'),
mock.patch.object(nec_neutron_agent, 'config')
) as (agent, logging_config, cfg):
cfg.OVS.integration_bridge = 'br-int-x'
cfg.AGENT.root_helper = 'dummy-helper'
cfg.AGENT.polling_interval = 10
nec_neutron_agent.main()
self.assertTrue(logging_config.setup_logging.called)
agent.assert_has_calls([
mock.call('br-int-x', 'dummy-helper', 10),
mock.call().daemon_loop()
])