Debian Codegraph @

vif_plug_ovs/tests/unit/test_plugin.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 import testtools
16
17 from os_vif.internal.ip.api import ip as ip_lib
18 from os_vif import objects
19 from os_vif.objects import fields
20
21 from vif_plug_ovs import constants
22 from vif_plug_ovs import exception
23 from vif_plug_ovs import linux_net
24 from vif_plug_ovs import ovs
25 from vif_plug_ovs.ovsdb import ovsdb_lib
26
27
28 class PluginTest(testtools.TestCase):
29
30 def __init__(self, *args, **kwargs):
31 super(PluginTest, self).__init__(*args, **kwargs)
32
33 objects.register_all()
34
35 self.subnet_bridge_4 = objects.subnet.Subnet(
36 cidr='101.168.1.0/24',
37 dns=['8.8.8.8'],
38 gateway='101.168.1.1',
39 dhcp_server='191.168.1.1')
40
41 self.subnet_bridge_6 = objects.subnet.Subnet(
42 cidr='101:1db9::/64',
43 gateway='101:1db9::1')
44
45 self.subnets = objects.subnet.SubnetList(
46 objects=[self.subnet_bridge_4,
47 self.subnet_bridge_6])
48
49 self.network_ovs = objects.network.Network(
50 id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7',
51 bridge='br0',
52 subnets=self.subnets,
53 vlan=99)
54
55 self.network_ovs_trunk = objects.network.Network(
56 id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7',
57 bridge='%s01' % constants.TRUNK_BR_PREFIX,
58 subnets=self.subnets,
59 vlan=99)
60
61 self.network_ovs_mtu = objects.network.Network(
62 id='437c6db5-4e6f-4b43-b64b-ed6a11ee5ba7',
63 bridge='br0',
64 subnets=self.subnets,
65 vlan=99,
66 mtu=1234)
67
68 self.profile_ovs = objects.vif.VIFPortProfileOpenVSwitch(
69 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
70 datapath_type='netdev')
71
72 self.profile_ovs_system = objects.vif.VIFPortProfileOpenVSwitch(
73 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
74 datapath_type='system')
75
76 # This is used for ironic with vif_type=smartnic
77 self.profile_ovs_smart_nic = objects.vif.VIFPortProfileOpenVSwitch(
78 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
79 create_port=True)
80
81 self.profile_ovs_no_datatype = objects.vif.VIFPortProfileOpenVSwitch(
82 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
83 datapath_type='')
84
85 self.vif_ovs_hybrid = objects.vif.VIFBridge(
86 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
87 address='ca:fe:de:ad:be:ef',
88 network=self.network_ovs,
89 vif_name='tap-xxx-yyy-zzz',
90 bridge_name="qbrvif-xxx-yyy",
91 port_profile=self.profile_ovs_no_datatype)
92
93 self.vif_ovs = objects.vif.VIFOpenVSwitch(
94 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
95 address='ca:fe:de:ad:be:ef',
96 network=self.network_ovs,
97 vif_name='tap-xxx-yyy-zzz',
98 port_profile=self.profile_ovs)
99
100 self.vif_ovs_system = objects.vif.VIFOpenVSwitch(
101 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
102 address='ca:fe:de:ad:be:ef',
103 network=self.network_ovs,
104 vif_name='tap-xxx-yyy-zzz',
105 port_profile=self.profile_ovs_system)
106
107 # This is used for ironic with vif_type=smartnic
108 self.vif_ovs_smart_nic = objects.vif.VIFOpenVSwitch(
109 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
110 address='ca:fe:de:ad:be:ef',
111 network=self.network_ovs,
112 vif_name='rep0-0',
113 port_profile=self.profile_ovs_smart_nic)
114
115 self.vif_vhostuser = objects.vif.VIFVHostUser(
116 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
117 address='ca:fe:de:ad:be:ef',
118 network=self.network_ovs,
119 path='/var/run/openvswitch/vhub679325f-ca',
120 mode='client',
121 port_profile=self.profile_ovs)
122
123 self.vif_vhostuser_trunk = objects.vif.VIFVHostUser(
124 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
125 address='ca:fe:de:ad:be:ef',
126 network=self.network_ovs_trunk,
127 path='/var/run/openvswitch/vhub679325f-ca',
128 mode='client',
129 port_profile=self.profile_ovs)
130
131 self.vif_vhostuser_client = objects.vif.VIFVHostUser(
132 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
133 address='ca:fe:de:ad:be:ef',
134 network=self.network_ovs,
135 path='/var/run/openvswitch/vhub679325f-ca',
136 mode='server', # qemu server mode <=> ovs client mode
137 port_profile=self.profile_ovs)
138
139 self.vif_ovs_vf_passthrough = objects.vif.VIFHostDevice(
140 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
141 address='ca:fe:de:ad:be:ef',
142 network=self.network_ovs,
143 dev_type=fields.VIFHostDeviceDevType.ETHERNET,
144 dev_address='0002:24:12.3',
145 bridge_name='br-int',
146 port_profile=self.profile_ovs_system)
147
148 self.vif_ovs_vf_dpdk = objects.vif.VIFHostDevice(
149 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
150 address='ca:fe:de:ad:be:ef',
151 network=self.network_ovs,
152 dev_type=fields.VIFHostDeviceDevType.ETHERNET,
153 dev_address='0002:24:12.3',
154 port_profile=self.profile_ovs)
155
156 self.instance = objects.instance_info.InstanceInfo(
157 name='demo',
158 uuid='f0000000-0000-0000-0000-000000000001')
159
160 def test__get_vif_datapath_type(self):
161 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
162 dp_type = plugin._get_vif_datapath_type(
163 self.vif_ovs, datapath=constants.OVS_DATAPATH_SYSTEM)
164 self.assertEqual(self.profile_ovs.datapath_type, dp_type)
165
166 dp_type = plugin._get_vif_datapath_type(
167 self.vif_ovs_hybrid, datapath=constants.OVS_DATAPATH_SYSTEM)
168 self.assertEqual(constants.OVS_DATAPATH_SYSTEM, dp_type)
169
170 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
171 def test_create_vif_port(self, mock_create_ovs_vif_port):
172 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
173 plugin._create_vif_port(
174 self.vif_ovs, mock.sentinel.vif_name, self.instance,
175 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
176 mock_create_ovs_vif_port.assert_called_once_with(
177 self.vif_ovs.network.bridge, mock.sentinel.vif_name,
178 self.vif_ovs.port_profile.interface_id,
179 self.vif_ovs.address, self.instance.uuid,
180 mtu=plugin.config.network_device_mtu,
181 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
182
183 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
184 def test_create_vif_port_mtu_in_model(self, mock_create_ovs_vif_port):
185 self.vif_ovs.network = self.network_ovs_mtu
186 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
187 plugin._create_vif_port(
188 self.vif_ovs, mock.sentinel.vif_name, self.instance,
189 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
190 mock_create_ovs_vif_port.assert_called_once_with(
191 self.vif_ovs.network.bridge, mock.sentinel.vif_name,
192 self.vif_ovs.port_profile.interface_id,
193 self.vif_ovs.address, self.instance.uuid,
194 mtu=self.network_ovs_mtu.mtu,
195 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
196
197 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
198 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
199 def test_create_vif_port_isolate_port_no_isolate_vif_no_port(
200 self, mock_port_exists, mock_create_ovs_vif_port):
201 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
202 mock_port_exists.return_value = False
203 with mock.patch.object(plugin.config, 'isolate_vif', False):
204 plugin._create_vif_port(
205 self.vif_ovs, mock.sentinel.vif_name, self.instance,
206 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
207 mock_create_ovs_vif_port.assert_called_once_with(
208 self.vif_ovs.network.bridge, mock.sentinel.vif_name,
209 self.vif_ovs.port_profile.interface_id,
210 self.vif_ovs.address, self.instance.uuid,
211 mtu=plugin.config.network_device_mtu,
212 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
213
214 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
215 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
216 def test_create_vif_port_isolate_port_isolate_vif_no_port(
217 self, mock_port_exists, mock_create_ovs_vif_port):
218 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
219 mock_port_exists.return_value = False
220 with mock.patch.object(plugin.config, 'isolate_vif', True):
221 plugin._create_vif_port(
222 self.vif_ovs, mock.sentinel.vif_name, self.instance,
223 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
224 mock_create_ovs_vif_port.assert_called_once_with(
225 self.vif_ovs.network.bridge, mock.sentinel.vif_name,
226 self.vif_ovs.port_profile.interface_id,
227 self.vif_ovs.address, self.instance.uuid,
228 mtu=plugin.config.network_device_mtu,
229 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE,
230 tag=constants.DEAD_VLAN,
231 vlan_mode='trunk',
232 trunks=constants.DEAD_VLAN
233 )
234
235 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
236 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
237 def test_create_vif_port_isolate_port_isolate_vif_port_exists(
238 self, mock_port_exists, mock_create_ovs_vif_port):
239 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
240 mock_port_exists.return_value = True
241 with mock.patch.object(plugin.config, 'isolate_vif', True):
242 plugin._create_vif_port(
243 self.vif_ovs, mock.sentinel.vif_name, self.instance,
244 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
245 mock_create_ovs_vif_port.assert_called_once_with(
246 self.vif_ovs.network.bridge, mock.sentinel.vif_name,
247 self.vif_ovs.port_profile.interface_id,
248 self.vif_ovs.address, self.instance.uuid,
249 mtu=plugin.config.network_device_mtu,
250 interface_type=constants.OVS_VHOSTUSER_INTERFACE_TYPE)
251
252 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
253 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
254 def test_create_vif_port_qos_port_bridge_true_port_new(
255 self, mock_port_exists, mock_create_ovs_vif_port):
256 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
257 mock_port_exists.return_value = False
258 port_bridge_name = "port-bridge-xxx"
259 # _create_vif_port as from _plug_port_bridge
260 plugin._create_vif_port(
261 self.vif_ovs_system, mock.sentinel.vif_name, self.instance,
262 bridge=port_bridge_name, set_ids=False)
263 # port existence should be checked on the per-port bridge
264 mock_port_exists.assert_called_once_with(
265 mock.sentinel.vif_name, port_bridge_name)
266 # qos_type should be set for the new port
267 mock_create_ovs_vif_port.assert_called_once_with(
268 port_bridge_name, mock.sentinel.vif_name,
269 self.vif_ovs_system.port_profile.interface_id,
270 self.vif_ovs_system.address, self.instance.uuid,
271 mtu=plugin.config.network_device_mtu,
272 set_ids=False,
273 qos_type="linux-noop")
274
275 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
276 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
277 def test_create_vif_port_qos_port_bridge_true_port_exists(
278 self, mock_port_exists, mock_create_ovs_vif_port):
279 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
280 mock_port_exists.return_value = True
281 port_bridge_name = "port-bridge-xxx"
282 # _create_vif_port as from _plug_port_bridge
283 plugin._create_vif_port(
284 self.vif_ovs_system, mock.sentinel.vif_name, self.instance,
285 bridge=port_bridge_name, set_ids=False)
286 # port existence should be checked on the per-port bridge
287 mock_port_exists.assert_called_once_with(
288 mock.sentinel.vif_name, port_bridge_name)
289 # qos_type should not be set for the existing port
290 mock_create_ovs_vif_port.assert_called_once_with(
291 port_bridge_name, mock.sentinel.vif_name,
292 self.vif_ovs_system.port_profile.interface_id,
293 self.vif_ovs_system.address, self.instance.uuid,
294 mtu=plugin.config.network_device_mtu,
295 set_ids=False)
296
297 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
298 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
299 def test_create_vif_port_qos_port_bridge_false_port_new(
300 self, mock_port_exists, mock_create_ovs_vif_port):
301 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
302 mock_port_exists.return_value = False
303 # _create_vif_port as from _plug_vif_generic
304 plugin._create_vif_port(
305 self.vif_ovs_system, mock.sentinel.vif_name, self.instance)
306 mock_port_exists.assert_called_once_with(
307 mock.sentinel.vif_name, self.vif_ovs_system.network.bridge)
308 # qos_type should be set for the new port
309 mock_create_ovs_vif_port.assert_called_once_with(
310 self.vif_ovs_system.network.bridge, mock.sentinel.vif_name,
311 self.vif_ovs_system.port_profile.interface_id,
312 self.vif_ovs_system.address, self.instance.uuid,
313 mtu=plugin.config.network_device_mtu,
314 qos_type="linux-noop")
315
316 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
317 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
318 def test_create_vif_port_qos_port_bridge_false_port_exists(
319 self, mock_port_exists, mock_create_ovs_vif_port):
320 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
321 mock_port_exists.return_value = True
322 # _create_vif_port as from _plug_vif_generic
323 plugin._create_vif_port(
324 self.vif_ovs_system, mock.sentinel.vif_name, self.instance)
325 mock_port_exists.assert_called_once_with(
326 mock.sentinel.vif_name, self.vif_ovs_system.network.bridge)
327 # qos_type should not be set for the existing port
328 mock_create_ovs_vif_port.assert_called_once_with(
329 self.vif_ovs_system.network.bridge, mock.sentinel.vif_name,
330 self.vif_ovs_system.port_profile.interface_id,
331 self.vif_ovs_system.address, self.instance.uuid,
332 mtu=plugin.config.network_device_mtu)
333
334 @mock.patch.object(ovs.OvsPlugin, '_plug_vif_generic')
335 def test_plug_ovs_port_bridge_false(self, plug_vif_generic):
336 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
337 with mock.patch.object(plugin.config, 'per_port_bridge', False):
338 plugin.plug(self.vif_ovs, self.instance)
339 plug_vif_generic.assert_called_once_with(
340 self.vif_ovs, self.instance)
341
342 @mock.patch.object(ovs.OvsPlugin, '_plug_port_bridge')
343 def test_plug_ovs_port_bridge_true(self, plug_vif):
344 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
345 with mock.patch.object(plugin.config, 'per_port_bridge', True):
346 plugin.plug(self.vif_ovs, self.instance)
347 plug_vif.assert_called_once_with(self.vif_ovs, self.instance)
348
349 @mock.patch.object(ovsdb_lib.BaseOVS, 'ensure_ovs_bridge')
350 @mock.patch.object(ovs.OvsPlugin, "_create_vif_port")
351 def test_plug_vif_generic(self, create_port, ensure_bridge):
352 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
353 plugin._plug_vif_generic(self.vif_ovs, self.instance)
354 ensure_bridge.assert_called_once()
355 # NOTE(sean-k-mooney): the interface will be plugged
356 # by libvirt so we assert _create_vif_port is not called.
357 create_port.assert_not_called()
358
359 @mock.patch.object(linux_net, 'set_interface_state')
360 @mock.patch.object(ovsdb_lib.BaseOVS, 'ensure_ovs_bridge')
361 @mock.patch.object(ovs.OvsPlugin, '_update_vif_port')
362 @mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
363 @mock.patch.object(linux_net, 'add_bridge_port')
364 @mock.patch.object(linux_net, 'update_veth_pair')
365 @mock.patch.object(linux_net, 'create_veth_pair')
366 @mock.patch.object(ip_lib, 'exists')
367 @mock.patch.object(linux_net, 'ensure_bridge')
368 def test_plug_ovs_bridge(self, ensure_bridge, device_exists,
369 create_veth_pair, update_veth_pair,
370 add_bridge_port, _create_vif_port,
371 _update_vif_port, ensure_ovs_bridge,
372 set_interface_state):
373 dp_type = ovs.OvsPlugin._get_vif_datapath_type(self.vif_ovs_hybrid)
374 calls = {
375 'device_exists': [mock.call('qvob679325f-ca')],
376 'create_veth_pair': [mock.call('qvbb679325f-ca',
377 'qvob679325f-ca',
378 1500)],
379 'update_veth_pair': [mock.call('qvbb679325f-ca',
380 'qvob679325f-ca',
381 1500)],
382 'ensure_bridge': [mock.call('qbrvif-xxx-yyy')],
383 'set_interface_state': [mock.call('qbrvif-xxx-yyy',
384 'up')],
385 'add_bridge_port': [mock.call('qbrvif-xxx-yyy',
386 'qvbb679325f-ca')],
387 '_update_vif_port': [mock.call(self.vif_ovs_hybrid,
388 'qvob679325f-ca')],
389 '_create_vif_port': [mock.call(self.vif_ovs_hybrid,
390 'qvob679325f-ca',
391 self.instance)],
392 'ensure_ovs_bridge': [mock.call('br0', dp_type)]
393 }
394
395 # plugging new devices should result in devices being created
396
397 device_exists.return_value = False
398 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
399 plugin.plug(self.vif_ovs_hybrid, self.instance)
400 ensure_bridge.assert_has_calls(calls['ensure_bridge'])
401 device_exists.assert_has_calls(calls['device_exists'])
402 create_veth_pair.assert_has_calls(calls['create_veth_pair'])
403 update_veth_pair.assert_not_called()
404 _update_vif_port.assert_not_called()
405 add_bridge_port.assert_has_calls(calls['add_bridge_port'])
406 _create_vif_port.assert_has_calls(calls['_create_vif_port'])
407 ensure_ovs_bridge.assert_has_calls(calls['ensure_ovs_bridge'])
408
409 # reset call stacks
410
411 create_veth_pair.reset_mock()
412 _create_vif_port.reset_mock()
413
414 # plugging existing devices should result in devices being updated
415
416 device_exists.return_value = True
417 plugin.plug(self.vif_ovs_hybrid, self.instance)
418 create_veth_pair.assert_not_called()
419 _create_vif_port.assert_not_called()
420 update_veth_pair.assert_has_calls(calls['update_veth_pair'])
421 _update_vif_port.assert_has_calls(calls['_update_vif_port'])
422
423 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
424 @mock.patch.object(ovs.OvsPlugin, '_unplug_vif_generic')
425 def test_unplug_ovs_port_bridge_false(self, unplug,
426 delete_ovs_bridge):
427 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
428 with mock.patch.object(plugin.config, 'per_port_bridge', False):
429 plugin.unplug(self.vif_ovs, self.instance)
430 unplug.assert_called_once_with(self.vif_ovs, self.instance)
431 delete_ovs_bridge.assert_not_called()
432
433 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
434 @mock.patch.object(ovs.OvsPlugin, '_unplug_port_bridge')
435 def test_unplug_ovs_port_bridge_true(self, unplug,
436 delete_ovs_bridge):
437 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
438 with mock.patch.object(plugin.config, 'per_port_bridge', True):
439 plugin.unplug(self.vif_ovs, self.instance)
440 unplug.assert_called_once_with(self.vif_ovs, self.instance)
441 delete_ovs_bridge.assert_not_called()
442
443 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
444 @mock.patch.object(ovs.OvsPlugin, '_unplug_vif_generic')
445 def test_unplug_vif_generic(self, delete_port, delete_ovs_bridge):
446 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
447 plugin._unplug_vif_generic(self.vif_ovs, self.instance)
448 delete_port.assert_called_once()
449 delete_ovs_bridge.assert_not_called()
450
451 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
452 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
453 @mock.patch.object(linux_net, 'delete_bridge')
454 def test_unplug_ovs_bridge(self, delete_bridge,
455 delete_ovs_vif_port, delete_ovs_bridge):
456 calls = {
457 'delete_bridge': [mock.call('qbrvif-xxx-yyy', 'qvbb679325f-ca')],
458 'delete_ovs_vif_port': [mock.call(
459 'br0', 'qvob679325f-ca', qos_type='linux-noop'
460 )]
461 }
462 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
463 plugin.unplug(self.vif_ovs_hybrid, self.instance)
464 delete_bridge.assert_has_calls(calls['delete_bridge'])
465 delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])
466 delete_ovs_bridge.assert_not_called()
467
468 @mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
469 def test_plug_ovs_vhostuser(self, _create_vif_port):
470 dp_type = ovs.OvsPlugin._get_vif_datapath_type(self.vif_vhostuser)
471 calls = [mock.call(
472 self.vif_vhostuser, 'vhub679325f-ca',
473 self.instance,
474 interface_type='dpdkvhostuser',
475 datapath_type=dp_type)]
476
477 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
478 plugin.plug(self.vif_vhostuser, self.instance)
479 _create_vif_port.assert_has_calls(calls)
480
481 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
482 def test_plug_ovs_vhostuser_client(self, create_ovs_vif_port):
483 dp_type = ovs.OvsPlugin._get_vif_datapath_type(
484 self.vif_vhostuser_client)
485 calls = [
486 mock.call(
487 'br0', 'vhub679325f-ca',
488 'e65867e0-9340-4a7f-a256-09af6eb7a3aa',
489 'ca:fe:de:ad:be:ef',
490 'f0000000-0000-0000-0000-000000000001',
491 mtu=1500, interface_type='dpdkvhostuserclient',
492 vhost_server_path='/var/run/openvswitch/vhub679325f-ca',
493 datapath_type=dp_type
494 )]
495
496 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
497 plugin.plug(self.vif_vhostuser_client, self.instance)
498 create_ovs_vif_port.assert_has_calls(calls)
499
500 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
501 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
502 def test_unplug_ovs_vhostuser(self, delete_ovs_vif_port,
503 delete_ovs_bridge):
504 calls = {
505 'delete_ovs_vif_port': [mock.call('br0', 'vhub679325f-ca')]
506 }
507 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
508 plugin.unplug(self.vif_vhostuser, self.instance)
509 delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])
510 delete_ovs_bridge.assert_not_called()
511
512 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
513 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
514 def test_unplug_ovs_vhostuser_trunk(self, delete_ovs_vif_port,
515 delete_ovs_bridge):
516 bridge_name = '%s01' % constants.TRUNK_BR_PREFIX
517 calls = {
518 'delete_ovs_vif_port': [mock.call(bridge_name, 'vhub679325f-ca')]
519 }
520 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
521 plugin.unplug(self.vif_vhostuser_trunk, self.instance)
522 delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])
523 delete_ovs_bridge.assert_called_once_with(bridge_name)
524
525 @mock.patch.object(ovsdb_lib.BaseOVS, 'ensure_ovs_bridge')
526 @mock.patch.object(linux_net, 'get_ifname_by_pci_address')
527 @mock.patch.object(linux_net, 'get_vf_num_by_pci_address')
528 @mock.patch.object(linux_net, 'get_representor_port')
529 @mock.patch.object(linux_net, 'set_interface_state')
530 @mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
531 def test_plug_ovs_vf_passthrough(self, _create_vif_port,
532 set_interface_state,
533 get_representor_port,
534 get_vf_num_by_pci_address,
535 get_ifname_by_pci_address,
536 ensure_ovs_bridge):
537
538 get_ifname_by_pci_address.return_value = 'eth0'
539 get_vf_num_by_pci_address.return_value = '2'
540 get_representor_port.return_value = 'eth0_2'
541 calls = {
542
543 'ensure_ovs_bridge': [mock.call('br0',
544 constants.OVS_DATAPATH_SYSTEM)],
545 'get_ifname_by_pci_address': [mock.call('0002:24:12.3',
546 pf_interface=True,
547 switchdev=True)],
548 'get_vf_num_by_pci_address': [mock.call('0002:24:12.3')],
549 'get_representor_port': [mock.call('eth0', '2')],
550 'set_interface_state': [mock.call('eth0_2', 'up')],
551 '_create_vif_port': [mock.call(
552 self.vif_ovs_vf_passthrough, 'eth0_2',
553 self.instance)]
554 }
555
556 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
557 plugin.plug(self.vif_ovs_vf_passthrough, self.instance)
558 ensure_ovs_bridge.assert_has_calls(calls['ensure_ovs_bridge'])
559 get_ifname_by_pci_address.assert_has_calls(
560 calls['get_ifname_by_pci_address'])
561 get_vf_num_by_pci_address.assert_has_calls(
562 calls['get_vf_num_by_pci_address'])
563 get_representor_port.assert_has_calls(
564 calls['get_representor_port'])
565 set_interface_state.assert_has_calls(calls['set_interface_state'])
566 _create_vif_port.assert_has_calls(calls['_create_vif_port'])
567
568 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
569 @mock.patch.object(linux_net, 'get_ifname_by_pci_address')
570 @mock.patch.object(linux_net, 'get_vf_num_by_pci_address')
571 @mock.patch.object(linux_net, 'get_representor_port')
572 @mock.patch.object(linux_net, 'set_interface_state')
573 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
574 def test_unplug_ovs_vf_passthrough(self, delete_ovs_vif_port,
575 set_interface_state,
576 get_representor_port,
577 get_vf_num_by_pci_address,
578 get_ifname_by_pci_address,
579 delete_ovs_bridge):
580 calls = {
581
582 'get_ifname_by_pci_address': [mock.call('0002:24:12.3',
583 pf_interface=True,
584 switchdev=True)],
585 'get_vf_num_by_pci_address': [mock.call('0002:24:12.3')],
586 'get_representor_port': [mock.call('eth0', '2')],
587 'set_interface_state': [mock.call('eth0_2', 'down')],
588 'delete_ovs_vif_port': [
589 mock.call(
590 'br0', 'eth0_2', delete_netdev=False,
591 qos_type='linux-noop'
592 )
593 ]
594 }
595
596 get_ifname_by_pci_address.return_value = 'eth0'
597 get_vf_num_by_pci_address.return_value = '2'
598 get_representor_port.return_value = 'eth0_2'
599 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
600 plugin.unplug(self.vif_ovs_vf_passthrough, self.instance)
601 get_ifname_by_pci_address.assert_has_calls(
602 calls['get_ifname_by_pci_address'])
603 get_vf_num_by_pci_address.assert_has_calls(
604 calls['get_vf_num_by_pci_address'])
605 get_representor_port.assert_has_calls(
606 calls['get_representor_port'])
607 delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])
608 set_interface_state.assert_has_calls(calls['set_interface_state'])
609 delete_ovs_bridge.assert_not_called()
610
611 @mock.patch.object(ovsdb_lib.BaseOVS, 'ensure_ovs_bridge')
612 @mock.patch.object(ovs.OvsPlugin, "_create_vif_port")
613 def test_plug_vif_ovs_ironic_smart_nic(self, create_port, ensure_bridge):
614 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
615 with mock.patch.object(plugin.config, 'per_port_bridge', False):
616 plugin.plug(self.vif_ovs_smart_nic, self.instance)
617 ensure_bridge.assert_called_once()
618 create_port.assert_called_once()
619
620 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
621 @mock.patch.object(ovs.OvsPlugin, '_unplug_vif_generic')
622 def test_unplug_vif_ovs_smart_nic(self, delete_port, delete_ovs_bridge):
623 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
624 with mock.patch.object(plugin.config, 'per_port_bridge', False):
625 plugin.unplug(self.vif_ovs_smart_nic, self.instance)
626 delete_port.assert_called_once()
627 delete_ovs_bridge.assert_not_called()
628
629 @mock.patch.object(linux_net, 'get_dpdk_representor_port_name')
630 @mock.patch.object(ovsdb_lib.BaseOVS, 'ensure_ovs_bridge')
631 @mock.patch.object(linux_net, 'get_vf_num_by_pci_address')
632 @mock.patch.object(linux_net, 'get_pf_pci_from_vf')
633 @mock.patch.object(ovs.OvsPlugin, '_create_vif_port')
634 def test_plug_ovs_vf_dpdk(self, _create_vif_port,
635 get_pf_pci_from_vf,
636 get_vf_num_by_pci_address,
637 ensure_ovs_bridge,
638 get_dpdk_representor_port_name):
639
640 pf_pci = self.vif_ovs_vf_dpdk.dev_address
641 devname = 'vfrb679325f-ca'
642 get_vf_num_by_pci_address.return_value = '2'
643 get_pf_pci_from_vf.return_value = pf_pci
644 get_dpdk_representor_port_name.return_value = devname
645 calls = {
646 'ensure_ovs_bridge': [mock.call('br0',
647 constants.OVS_DATAPATH_NETDEV)],
648 'get_vf_num_by_pci_address': [mock.call('0002:24:12.3')],
649 'get_pf_pci_from_vf': [mock.call(pf_pci)],
650 'get_dpdk_representor_port_name': [mock.call(
651 self.vif_ovs_vf_dpdk.id)],
652 '_create_vif_port': [mock.call(
653 self.vif_ovs_vf_dpdk,
654 devname,
655 self.instance,
656 interface_type='dpdk',
657 pf_pci=pf_pci,
658 vf_num='2')]}
659
660 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
661 plugin.plug(self.vif_ovs_vf_dpdk, self.instance)
662 ensure_ovs_bridge.assert_has_calls(
663 calls['ensure_ovs_bridge'])
664 get_vf_num_by_pci_address.assert_has_calls(
665 calls['get_vf_num_by_pci_address'])
666 get_pf_pci_from_vf.assert_has_calls(
667 calls['get_pf_pci_from_vf'])
668 get_dpdk_representor_port_name.assert_has_calls(
669 calls['get_dpdk_representor_port_name'])
670 _create_vif_port.assert_has_calls(
671 calls['_create_vif_port'])
672
673 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
674 @mock.patch.object(linux_net, 'get_dpdk_representor_port_name')
675 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
676 def test_unplug_ovs_vf_dpdk(self, delete_ovs_vif_port,
677 get_dpdk_representor_port_name,
678 delete_ovs_bridge):
679 devname = 'vfrb679325f-ca'
680 get_dpdk_representor_port_name.return_value = devname
681 calls = {
682 'get_dpdk_representor_port_name': [mock.call(
683 self.vif_ovs_vf_dpdk.id)],
684 'delete_ovs_vif_port': [
685 mock.call(
686 'br0', devname, delete_netdev=False,
687 qos_type=None
688 )
689 ]
690 }
691 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
692 plugin.unplug(self.vif_ovs_vf_dpdk, self.instance)
693 get_dpdk_representor_port_name.assert_has_calls(
694 calls['get_dpdk_representor_port_name'])
695 delete_ovs_vif_port.assert_has_calls(calls['delete_ovs_vif_port'])
696 delete_ovs_bridge.assert_not_called()
697
698 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_patch_port_pair')
699 @mock.patch.object(ovsdb_lib.BaseOVS, 'ensure_ovs_bridge')
700 @mock.patch.object(ovs.OvsPlugin, "_create_vif_port")
701 def test_plug_port_bridge(
702 self, create_port, ensure_bridge, create_patch_port_pair):
703 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
704 plugin._plug_port_bridge(self.vif_ovs, self.instance)
705 calls = [
706 mock.call('br0', 'netdev'),
707 mock.call('pbb679325f-ca8', 'netdev')
708 ]
709 ensure_bridge.assert_has_calls(calls)
710 create_port.assert_called_once()
711 create_patch_port_pair.assert_called_once()
712
713 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
714 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
715 def test_unplug_port_bridge(
716 self, delete_ovs_bridge, delete_ovs_vif_port):
717 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
718 plugin._unplug_port_bridge(self.vif_ovs, self.instance)
719 calls = [
720 mock.call('br0', 'ibpb679325f-ca89-4ee0-a8be-6db1409b69ea'),
721 mock.call(
722 'pbb679325f-ca8', 'pbpb679325f-ca89-4ee0-a8be-6db1409b69ea'),
723 mock.call('pbb679325f-ca8', 'tap-xxx-yyy-zzz', qos_type=None)
724 ]
725 delete_ovs_vif_port.assert_has_calls(calls)
726 delete_ovs_bridge.assert_called_once_with('pbb679325f-ca8')
727
728 @mock.patch.object(ip_lib, 'exists', return_value=True)
729 @mock.patch.object(ovs.OvsPlugin, '_unplug_bridge')
730 def test_unplug_hybrid_bridge(self, m_unplug_bridge, m_ip_lib_exists):
731 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
732 plugin.unplug(self.vif_ovs, self.instance)
733 m_unplug_bridge.assert_called_once()
734
735 @mock.patch.object(ip_lib, 'exists', return_value=False)
736 @mock.patch.object(ovs.OvsPlugin, '_unplug_vif_generic')
737 def test_unplug_ovs(self, m_unplug_generic, m_ip_lib_exists):
738 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
739 plugin.unplug(self.vif_ovs, self.instance)
740 m_unplug_generic.assert_called_once()
741
742 @mock.patch.object(linux_net, 'create_tap')
743 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
744 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
745 def test_create_vif_port_with_tap_creation(
746 self, mock_port_exists, mock_create_ovs_vif_port, mock_create_tap):
747 """Test that create_tap is called when create_tap flag is set."""
748 # Create a profile with create_tap=True
749 profile_with_tap = objects.vif.VIFPortProfileOpenVSwitch(
750 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
751 create_tap=True)
752 vif_with_tap = objects.vif.VIFOpenVSwitch(
753 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
754 address='ca:fe:de:ad:be:ef',
755 network=self.network_ovs,
756 vif_name='tap-xxx-yyy-zzz',
757 port_profile=profile_with_tap)
758
759 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
760 mock_port_exists.return_value = False
761 plugin._create_vif_port(
762 vif_with_tap, 'tap-xxx-yyy-zzz', self.instance)
763
764 # Verify create_tap was called with correct parameters
765 mock_create_tap.assert_called_once_with(
766 'tap-xxx-yyy-zzz',
767 plugin.config.network_device_mtu,
768 'ca:fe:de:ad:be:ef',
769 multiqueue=False)
770
771 @mock.patch.object(linux_net, 'create_tap')
772 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
773 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
774 def test_create_vif_port_with_tap_and_multiqueue(
775 self, mock_port_exists, mock_create_ovs_vif_port, mock_create_tap):
776 """Test that create_tap is called with multiqueue when both are set."""
777 # Create a profile with create_tap=True and multiqueue=True
778 profile_with_tap_mq = objects.vif.VIFPortProfileOpenVSwitch(
779 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
780 create_tap=True,
781 multiqueue=True)
782 vif_with_tap_mq = objects.vif.VIFOpenVSwitch(
783 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
784 address='ca:fe:de:ad:be:ef',
785 network=self.network_ovs,
786 vif_name='tap-xxx-yyy-zzz',
787 port_profile=profile_with_tap_mq)
788
789 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
790 mock_port_exists.return_value = False
791 plugin._create_vif_port(
792 vif_with_tap_mq, 'tap-xxx-yyy-zzz', self.instance)
793
794 # Verify create_tap was called with multiqueue=True
795 mock_create_tap.assert_called_once_with(
796 'tap-xxx-yyy-zzz',
797 plugin.config.network_device_mtu,
798 'ca:fe:de:ad:be:ef',
799 multiqueue=True)
800
801 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
802 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
803 def test_create_vif_port_tap_not_supported_vhostuser(
804 self, mock_port_exists, mock_create_ovs_vif_port):
805 """Test that TapCreationNotSupported is raised for VIFVHostUser."""
806 # Create a VIFVHostUser with create_tap=True
807 profile_with_tap = objects.vif.VIFPortProfileOpenVSwitch(
808 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
809 create_tap=True)
810 vif_vhostuser_with_tap = objects.vif.VIFVHostUser(
811 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
812 address='ca:fe:de:ad:be:ef',
813 network=self.network_ovs,
814 path='/var/run/openvswitch/vhub679325f-ca',
815 mode='client',
816 port_profile=profile_with_tap)
817
818 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
819 mock_port_exists.return_value = False
820
821 # Verify exception is raised
822 self.assertRaises(
823 exception.TapCreationNotSupported,
824 plugin._create_vif_port,
825 vif_vhostuser_with_tap, 'vhub679325f-ca', self.instance)
826
827 @mock.patch.object(ovsdb_lib.BaseOVS, 'create_ovs_vif_port')
828 @mock.patch.object(ovsdb_lib.BaseOVS, 'port_exists')
829 def test_create_vif_port_tap_not_supported_hostdevice(
830 self, mock_port_exists, mock_create_ovs_vif_port):
831 """Test that TapCreationNotSupported is raised for VIFHostDevice."""
832 # Create a VIFHostDevice with create_tap=True
833 profile_with_tap = objects.vif.VIFPortProfileOpenVSwitch(
834 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
835 create_tap=True)
836 vif_hostdevice_with_tap = objects.vif.VIFHostDevice(
837 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
838 address='ca:fe:de:ad:be:ef',
839 network=self.network_ovs,
840 dev_type=fields.VIFHostDeviceDevType.ETHERNET,
841 dev_address='0002:24:12.3',
842 port_profile=profile_with_tap)
843
844 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
845 mock_port_exists.return_value = False
846
847 # Verify exception is raised
848 self.assertRaises(
849 exception.TapCreationNotSupported,
850 plugin._create_vif_port,
851 vif_hostdevice_with_tap, 'tap-xxx-yyy-zzz', self.instance)
852
853 @mock.patch.object(ip_lib, 'exists', return_value=True)
854 @mock.patch.object(linux_net, 'delete_net_dev')
855 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
856 def test_unplug_vif_generic_deletes_tap(
857 self, mock_delete_ovs_vif_port, mock_delete_net_dev,
858 mock_exists):
859 """Test that tap device is deleted when unplugging with
860 create_tap=True.
861 """
862 # Create a VIF with create_tap=True
863 profile_with_tap = objects.vif.VIFPortProfileOpenVSwitch(
864 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
865 create_tap=True)
866 vif_with_tap = objects.vif.VIFOpenVSwitch(
867 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
868 address='ca:fe:de:ad:be:ef',
869 network=self.network_ovs,
870 vif_name='tap-xxx-yyy-zzz',
871 port_profile=profile_with_tap)
872
873 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
874 plugin._unplug_vif_generic(vif_with_tap, self.instance)
875
876 # Verify delete_net_dev was called with vif_name
877 mock_delete_net_dev.assert_called_once_with('tap-xxx-yyy-zzz')
878
879 @mock.patch.object(linux_net, 'delete_net_dev')
880 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
881 def test_unplug_vif_generic_no_tap_deletion_when_not_created(
882 self, mock_delete_ovs_vif_port, mock_delete_net_dev):
883 """Test that tap device is not deleted when create_tap=False."""
884 # Use default vif_ovs which has create_tap=False (or unset)
885 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
886 plugin._unplug_vif_generic(self.vif_ovs, self.instance)
887
888 # Verify delete_net_dev was not called
889 mock_delete_net_dev.assert_not_called()
890
891 @mock.patch.object(ip_lib, 'exists', return_value=True)
892 @mock.patch.object(linux_net, 'delete_net_dev')
893 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_vif_port')
894 @mock.patch.object(ovsdb_lib.BaseOVS, 'delete_ovs_bridge')
895 def test_unplug_port_bridge_deletes_tap(
896 self, mock_delete_ovs_bridge, mock_delete_ovs_vif_port,
897 mock_delete_net_dev, mock_exists):
898 """Test that tap device is deleted when unplugging port bridge with
899 create_tap=True.
900 """
901 # Create a VIF with create_tap=True
902 profile_with_tap = objects.vif.VIFPortProfileOpenVSwitch(
903 interface_id='e65867e0-9340-4a7f-a256-09af6eb7a3aa',
904 create_tap=True)
905 vif_with_tap = objects.vif.VIFOpenVSwitch(
906 id='b679325f-ca89-4ee0-a8be-6db1409b69ea',
907 address='ca:fe:de:ad:be:ef',
908 network=self.network_ovs,
909 vif_name='tap-xxx-yyy-zzz',
910 port_profile=profile_with_tap)
911
912 plugin = ovs.OvsPlugin.load(constants.PLUGIN_NAME)
913 plugin._unplug_port_bridge(vif_with_tap, self.instance)
914
915 # Verify delete_net_dev was called with vif_name
916 mock_delete_net_dev.assert_called_once_with('tap-xxx-yyy-zzz')