Debian Codegraph @

vif_plug_ovs/tests/unit/test_linux_net.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 import glob
14 import os.path
15
16 from unittest import mock
17
18 import testtools
19
20 from os_vif.internal.ip.api import ip as ip_lib
21
22 from vif_plug_ovs import exception
23 from vif_plug_ovs import linux_net
24 from vif_plug_ovs import privsep
25
26
27 class LinuxNetTest(testtools.TestCase):
28
29 def setUp(self):
30 super(LinuxNetTest, self).setUp()
31
32 privsep.vif_plug.set_client_mode(False)
33
34 @mock.patch.object(linux_net, "_arp_filtering")
35 @mock.patch.object(linux_net, "set_interface_state")
36 @mock.patch.object(linux_net, "_disable_ipv6")
37 @mock.patch.object(ip_lib, "add")
38 @mock.patch.object(ip_lib, "exists", return_value=False)
39 def test_ensure_bridge(self, mock_dev_exists, mock_add,
40 mock_disable_ipv6, mock_set_state,
41 mock_arp_filtering):
42 linux_net.ensure_bridge("br0")
43
44 mock_dev_exists.assert_called_once_with("br0")
45 mock_add.assert_called_once_with("br0", "bridge", ageing=0)
46 mock_disable_ipv6.assert_called_once_with("br0")
47 mock_set_state.assert_called_once_with("br0", "up")
48 mock_arp_filtering.assert_called_once_with("br0")
49
50 @mock.patch.object(linux_net, "_arp_filtering")
51 @mock.patch.object(linux_net, "set_interface_state")
52 @mock.patch.object(linux_net, "_disable_ipv6")
53 @mock.patch.object(ip_lib, "add")
54 @mock.patch.object(ip_lib, "exists", return_value=True)
55 def test_ensure_bridge_exists(self, mock_dev_exists, mock_add,
56 mock_disable_ipv6, mock_set_state,
57 mock_arp_filtering):
58 linux_net.ensure_bridge("br0")
59
60 mock_dev_exists.assert_called_once_with("br0")
61 mock_add.assert_not_called()
62 mock_disable_ipv6.assert_called_once_with("br0")
63 mock_set_state.assert_called_once_with("br0", "up")
64 mock_arp_filtering.assert_called_once_with("br0")
65
66 @mock.patch('builtins.open')
67 @mock.patch("os.path.exists")
68 def test__disable_ipv6(self, mock_exists, mock_open):
69
70 exists_path = "/proc/sys/net/ipv6/conf/br0/disable_ipv6"
71 mock_exists.return_value = False
72
73 linux_net._disable_ipv6("br0")
74 mock_exists.assert_called_once_with(exists_path)
75 mock_open.assert_not_called()
76
77 mock_exists.reset_mock()
78 mock_exists.return_value = True
79 linux_net._disable_ipv6("br0")
80 mock_exists.assert_called_once_with(exists_path)
81 mock_open.assert_called_once_with(exists_path, 'w')
82
83 @mock.patch.object(os.path, 'exists', return_value=True)
84 @mock.patch('builtins.open')
85 def test__arp_filtering(self, mock_open, *args):
86 mock_open.side_effect = mock.mock_open()
87 linux_net._arp_filtering("br0")
88
89 mock_open.assert_has_calls([
90 mock.call('/proc/sys/net/ipv4/conf/br0/arp_ignore', 'w'),
91 mock.call('/proc/sys/net/ipv4/conf/br0/arp_announce', 'w')])
92 mock_open.side_effect.return_value.write.assert_has_calls([
93 mock.call('1'),
94 mock.call('2')])
95
96 @mock.patch.object(ip_lib, "delete")
97 @mock.patch.object(ip_lib, "exists", return_value=False)
98 def test_delete_bridge_none(self, mock_dev_exists, mock_delete):
99 linux_net.delete_bridge("br0", "vnet1")
100
101 mock_delete.assert_not_called()
102 mock_dev_exists.assert_called_once_with("br0")
103
104 @mock.patch.object(linux_net, "set_interface_state")
105 @mock.patch.object(ip_lib, "delete")
106 @mock.patch.object(ip_lib, "exists", return_value=True)
107 def test_delete_bridge_exists(self, mock_dev_exists, mock_delete,
108 mock_set_state):
109 linux_net.delete_bridge("br0", "vnet1")
110
111 mock_dev_exists.assert_has_calls([mock.call("br0"),
112 mock.call("vnet1")])
113 mock_delete.assert_called_once_with("br0", check_exit_code=[0, 2, 254])
114 mock_set_state.assert_called_once_with("vnet1", "down")
115
116 @mock.patch.object(linux_net, "set_interface_state")
117 @mock.patch.object(ip_lib, "delete")
118 @mock.patch.object(ip_lib, "exists")
119 def test_delete_interface_not_present(self, mock_dev_exists, mock_delete,
120 mock_set_state):
121 mock_dev_exists.return_value = next(lambda: (yield True),
122 (yield False))
123
124 linux_net.delete_bridge("br0", "vnet1")
125
126 mock_dev_exists.assert_has_calls([mock.call("br0"),
127 mock.call("vnet1")])
128 mock_delete.assert_called_once_with("br0", check_exit_code=[0, 2, 254])
129 mock_set_state.assert_not_called()
130
131 @mock.patch.object(ip_lib, "set")
132 def test_add_bridge_port(self, mock_set):
133 linux_net.add_bridge_port("br0", "vnet1")
134 mock_set.assert_called_once_with("vnet1", master="br0")
135
136 @mock.patch.object(linux_net, '_get_phys_switch_id')
137 def test_is_switchdev_ioerror(self, mock__get_phys_switch_id):
138 mock__get_phys_switch_id.side_effect = ([IOError()])
139 test_switchdev = linux_net._is_switchdev('pf_ifname')
140 self.assertEqual(test_switchdev, False)
141
142 @mock.patch.object(linux_net, '_get_phys_switch_id')
143 def test_is_switchdev_empty(self, mock__get_phys_switch_id):
144 mock__get_phys_switch_id.return_value = ''
145 test_switchdev = linux_net._is_switchdev('pf_ifname')
146 self.assertEqual(test_switchdev, False)
147
148 @mock.patch.object(linux_net, '_get_phys_switch_id')
149 def test_is_switchdev_positive(self, mock__get_phys_switch_id):
150 mock__get_phys_switch_id.return_value = 'pf_sw_id'
151 test_switchdev = linux_net._is_switchdev('pf_ifname')
152 self.assertEqual(test_switchdev, True)
153
154 def test_parse_vf_number(self):
155 self.assertEqual(linux_net._parse_vf_number("0"), "0")
156 self.assertEqual(linux_net._parse_vf_number("pf13vf42"), "42")
157 self.assertEqual(linux_net._parse_vf_number("VF19@PF13"), "19")
158 self.assertIsNone(linux_net._parse_vf_number("p7"))
159 self.assertIsNone(linux_net._parse_vf_number("pf31"))
160 self.assertIsNone(linux_net._parse_vf_number("g4rbl3d"))
161
162 def test_parse_pf_number(self):
163 self.assertIsNone(linux_net._parse_pf_number("0"))
164 self.assertEqual(linux_net._parse_pf_number("pf13vf42"), "13")
165 self.assertEqual(linux_net._parse_pf_number("VF19@PF13"), "13")
166 self.assertIsNone(linux_net._parse_pf_number("p7"))
167 self.assertEqual(linux_net._parse_pf_number("pf31"), "31")
168 self.assertIsNone(linux_net._parse_pf_number("g4rbl3d"))
169
170 @mock.patch.object(os, 'listdir')
171 @mock.patch.object(linux_net, "_get_pf_func")
172 @mock.patch.object(linux_net, "_get_phys_port_name")
173 @mock.patch.object(linux_net, '_get_phys_switch_id')
174 def test_get_representor_port(self, mock__get_phys_switch_id,
175 mock__get_phys_port_name,
176 mock__get_pf_func,
177 mock_listdir):
178 mock_listdir.return_value = [
179 'pf_ifname', 'rep_vf_1', 'rep_vf_2'
180 ]
181 mock__get_phys_switch_id.return_value = 'pf_sw_id'
182 mock__get_pf_func.return_value = "0"
183 mock__get_phys_port_name.side_effect = (['1', "pf0vf1", "pf0vf2"])
184 ifname = linux_net.get_representor_port('pf_ifname', '2')
185 self.assertEqual('rep_vf_2', ifname)
186
187 @mock.patch.object(os, 'listdir')
188 @mock.patch.object(linux_net, "_get_pf_func")
189 @mock.patch.object(linux_net, "_get_phys_port_name")
190 @mock.patch.object(linux_net, "_get_phys_switch_id")
191 def test_get_representor_port_2_pfs(
192 self, mock__get_phys_switch_id, mock__get_phys_port_name,
193 mock__get_pf_func, mock_listdir):
194 mock_listdir.return_value = [
195 'pf_ifname1', 'pf_ifname2', 'rep_pf1_vf_1', 'rep_pf1_vf_2',
196 'rep_pf2_vf_1', 'rep_pf2_vf_2',
197 ]
198 mock__get_phys_switch_id.return_value = 'pf_sw_id'
199 mock__get_pf_func.return_value = "2"
200 mock__get_phys_port_name.side_effect = (
201 ["p1", "p2", "VF1@PF1", "pf2vf1", "vf2@pf1", "pf2vf2"])
202 ifname = linux_net.get_representor_port('pf_ifname2', '2')
203 self.assertEqual('rep_pf2_vf_2', ifname)
204
205 @mock.patch.object(os, 'listdir')
206 @mock.patch.object(linux_net, "_get_pf_func")
207 @mock.patch.object(linux_net, "_get_phys_switch_id")
208 @mock.patch.object(linux_net, "_get_phys_port_name")
209 def test_get_representor_port_not_found(
210 self, mock__get_phys_port_name, mock__get_phys_switch_id,
211 mock__get_pf_func, mock_listdir):
212 mock_listdir.return_value = [
213 'pf_ifname', 'rep_vf_1', 'rep_vf_2'
214 ]
215 mock__get_phys_switch_id.return_value = 'pf_sw_id'
216 mock__get_pf_func.return_value = "0"
217 mock__get_phys_port_name.side_effect = (
218 ["p0", "1", "2"])
219 self.assertRaises(
220 exception.RepresentorNotFound,
221 linux_net.get_representor_port,
222 'pf_ifname', '3'),
223
224 @mock.patch.object(os, 'listdir')
225 @mock.patch.object(linux_net, "_get_pf_func")
226 @mock.patch.object(linux_net, "_get_phys_port_name")
227 @mock.patch.object(linux_net, "_get_phys_switch_id")
228 def test_get_representor_port_exception_io_error(
229 self, mock__get_phys_switch_id, mock__get_phys_port_name,
230 mock__get_pf_func, mock_listdir):
231 mock_listdir.return_value = [
232 'pf_ifname', 'rep_vf_1', 'rep_vf_2'
233 ]
234 mock__get_phys_switch_id.side_effect = (
235 ['pf_sw_id', 'pf_sw_id', IOError(), 'pf_sw_id', '2'])
236 mock__get_pf_func.return_value = "0"
237 mock__get_phys_port_name.side_effect = (
238 ["p0", "pf0vf0", "pf0vf1"])
239 self.assertRaises(
240 exception.RepresentorNotFound,
241 linux_net.get_representor_port,
242 'pf_ifname', '3')
243
244 @mock.patch.object(os, 'listdir')
245 @mock.patch.object(linux_net, "_get_pf_func")
246 @mock.patch.object(linux_net, "_get_phys_port_name")
247 @mock.patch.object(linux_net, "_get_phys_switch_id")
248 def test_get_representor_port_exception_value_error(
249 self, mock__get_phys_switch_id, mock__get_phys_port_name,
250 mock__get_pf_func, mock_listdir):
251 mock_listdir.return_value = [
252 'pf_ifname', 'rep_vf_1', 'rep_vf_2'
253 ]
254 mock__get_phys_switch_id.return_value = 'pf_sw_id'
255 mock__get_phys_port_name.side_effect = (['p0', '1', 'a'])
256 mock__get_pf_func.return_value = "0"
257 self.assertRaises(
258 exception.RepresentorNotFound,
259 linux_net.get_representor_port,
260 'pf_ifname', '3')
261
262 @mock.patch.object(os, 'listdir')
263 @mock.patch.object(linux_net, '_get_phys_switch_id')
264 @mock.patch.object(linux_net, "_get_phys_port_name")
265 def test_physical_function_interface_name(
266 self, mock__get_phys_port_name, mock__get_phys_switch_id,
267 mock_listdir):
268 mock_listdir.return_value = ['foo', 'bar']
269 mock__get_phys_switch_id.side_effect = (
270 ['', 'valid_switch'])
271 mock__get_phys_port_name.side_effect = (["p1"])
272 ifname = linux_net.get_ifname_by_pci_address(
273 '0000:00:00.1', pf_interface=True, switchdev=False)
274 self.assertEqual(ifname, 'foo')
275
276 @mock.patch.object(os, 'listdir')
277 @mock.patch.object(linux_net, "_get_phys_switch_id")
278 @mock.patch.object(linux_net, "_get_phys_port_name")
279 def test_physical_function_interface_name_with_switchdev(
280 self, mock__get_phys_port_name, mock__get_phys_switch_id,
281 mock_listdir):
282 mock_listdir.return_value = ['foo', 'bar']
283 mock__get_phys_switch_id.side_effect = (
284 ['', 'valid_switch'])
285 mock__get_phys_port_name.side_effect = (["p1s0"])
286 ifname = linux_net.get_ifname_by_pci_address(
287 '0000:00:00.1', pf_interface=True, switchdev=True)
288 self.assertEqual(ifname, 'bar')
289
290 @mock.patch.object(os, 'listdir')
291 @mock.patch.object(linux_net, "_get_phys_switch_id")
292 @mock.patch.object(linux_net, "_get_phys_port_name")
293 def test_physical_function_interface_name_with_representors(
294 self, mock__get_phys_port_name, mock__get_phys_switch_id,
295 mock_listdir):
296 # Get the PF that matches the phys_port_name regex
297 mock_listdir.return_value = ['enp2s0f0_0', 'enp2s0f0_1', 'enp2s0f0']
298 mock__get_phys_switch_id.side_effect = (
299 ['valid_switch', 'valid_switch', 'valid_switch'])
300 mock__get_phys_port_name.side_effect = (["pf0vf0", "pf0vf1", "p0"])
301 ifname = linux_net.get_ifname_by_pci_address(
302 '0000:00:00.1', pf_interface=True, switchdev=True)
303 self.assertEqual(ifname, 'enp2s0f0')
304
305 @mock.patch.object(os, 'listdir')
306 @mock.patch.object(linux_net, "_get_phys_switch_id")
307 @mock.patch.object(linux_net, "_get_phys_port_name")
308 def test_physical_function_interface_name_with_fallback_To_first_netdev(
309 self, mock__get_phys_port_name, mock__get_phys_switch_id,
310 mock_listdir):
311 # Try with switchdev mode to get PF but fail because there is no match
312 # for the phys_port_name then fallback to first interface found
313 mock_listdir.return_value = ['enp2s0f0_0', 'enp2s0f0_1', 'enp2s0f0']
314 mock__get_phys_switch_id.side_effect = (['valid_switch',
315 'valid_switch',
316 'valid_switch'])
317 mock__get_phys_port_name.side_effect = (["pf0vf0", "pf0vf1", "pf0vf2"])
318 ifname = linux_net.get_ifname_by_pci_address(
319 '0000:00:00.1', pf_interface=True, switchdev=True)
320 self.assertEqual(ifname, 'enp2s0f0_0')
321
322 @mock.patch.object(os, 'listdir')
323 def test_get_ifname_by_pci_address_exception(self, mock_listdir):
324 mock_listdir.side_effect = OSError('No such file or directory')
325 self.assertRaises(
326 exception.PciDeviceNotFoundById,
327 linux_net.get_ifname_by_pci_address,
328 '0000:00:00.1'
329 )
330
331 @mock.patch.object(os, 'readlink')
332 @mock.patch.object(glob, 'iglob')
333 def test_vf_number_found(self, mock_iglob, mock_readlink):
334 mock_iglob.return_value = [
335 '/sys/bus/pci/devices/0000:00:00.1/physfn/virtfn3',
336 ]
337 mock_readlink.return_value = '../../0000:00:00.1'
338 vf_num = linux_net.get_vf_num_by_pci_address('0000:00:00.1')
339 self.assertEqual(vf_num, '3')
340
341 @mock.patch.object(os, 'readlink')
342 @mock.patch.object(glob, 'iglob')
343 def test_vf_number_not_found(self, mock_iglob, mock_readlink):
344 mock_iglob.return_value = [
345 '/sys/bus/pci/devices/0000:00:00.1/physfn/virtfn3',
346 ]
347 mock_readlink.return_value = '../../0000:00:00.2'
348 self.assertRaises(
349 exception.PciDeviceNotFoundById,
350 linux_net.get_vf_num_by_pci_address,
351 '0000:00:00.1'
352 )
353
354 @mock.patch.object(os, 'readlink')
355 @mock.patch.object(glob, 'iglob')
356 def test_get_vf_num_by_pci_address_exception(
357 self, mock_iglob, mock_readlink):
358 mock_iglob.return_value = [
359 '/sys/bus/pci/devices/0000:00:00.1/physfn/virtfn3',
360 ]
361 mock_readlink.side_effect = OSError('No such file or directory')
362 self.assertRaises(
363 exception.PciDeviceNotFoundById,
364 linux_net.get_vf_num_by_pci_address,
365 '0000:00:00.1'
366 )
367
368 @mock.patch('builtins.open')
369 @mock.patch.object(os.path, 'isfile')
370 def test__get_phys_port_name(self, mock_isfile, mock_open):
371 mock_open.return_value.__enter__ = lambda s: s
372 readline_mock = mock_open.return_value.readline
373 readline_mock.return_value = 'pf0vf0'
374 mock_isfile.return_value = True
375 phys_port_name = linux_net._get_phys_port_name("vf_ifname")
376 self.assertEqual(phys_port_name, 'pf0vf0')
377
378 @mock.patch.object(os.path, 'isfile')
379 def test__get_phys_port_name_not_found(self, mock_isfile):
380 mock_isfile.return_value = False
381 phys_port_name = linux_net._get_phys_port_name("vf_ifname")
382 self.assertIsNone(phys_port_name)
383
384 @mock.patch('builtins.open')
385 @mock.patch.object(os.path, 'isfile')
386 def test__get_phys_switch_id(self, mock_isfile, mock_open):
387 mock_open.return_value.__enter__ = lambda s: s
388 readline_mock = mock_open.return_value.readline
389 readline_mock.return_value = '66e40000039b0398'
390 mock_isfile.return_value = True
391 phys_port_name = linux_net._get_phys_switch_id("ifname")
392 self.assertEqual(phys_port_name, '66e40000039b0398')
393
394 @mock.patch.object(os.path, 'isfile')
395 def test__get_phys_switch_id_not_found(self, mock_isfile):
396 mock_isfile.return_value = False
397 phys_port_name = linux_net._get_phys_switch_id("ifname")
398 self.assertIsNone(phys_port_name)
399
400 @mock.patch.object(linux_net, "_update_device_mtu")
401 @mock.patch.object(ip_lib, "set")
402 @mock.patch.object(ip_lib, "add")
403 def test_create_tap(self, mock_add, mock_set, mock_update_mtu):
404 """Test basic tap device creation."""
405 linux_net.create_tap("tap0", 1500, "aa:bb:cc:dd:ee:ff",
406 multiqueue=False)
407
408 mock_add.assert_called_once_with("tap0", "tuntap", mode="tap",
409 multiqueue=False,
410 check_exit_code=[0, 17])
411 mock_set.assert_called_once_with("tap0", state="up",
412 address="aa:bb:cc:dd:ee:ff",
413 check_exit_code=[0, 2, 254])
414 mock_update_mtu.assert_called_once_with("tap0", 1500)
415
416 @mock.patch.object(linux_net, "_update_device_mtu")
417 @mock.patch.object(ip_lib, "set")
418 @mock.patch.object(ip_lib, "add")
419 def test_create_tap_with_multiqueue(self, mock_add, mock_set,
420 mock_update_mtu):
421 """Test tap device creation with multiqueue enabled."""
422 linux_net.create_tap("tap0", 1500, "aa:bb:cc:dd:ee:ff",
423 multiqueue=True)
424
425 mock_add.assert_called_once_with("tap0", "tuntap", mode="tap",
426 multiqueue=True,
427 check_exit_code=[0, 17])
428 mock_set.assert_called_once_with("tap0", state="up",
429 address="aa:bb:cc:dd:ee:ff",
430 check_exit_code=[0, 2, 254])
431 mock_update_mtu.assert_called_once_with("tap0", 1500)
432
433 @mock.patch.object(linux_net, "_update_device_mtu")
434 @mock.patch.object(ip_lib, "set")
435 @mock.patch.object(ip_lib, "add")
436 def test_create_tap_no_mtu(self, mock_add, mock_set, mock_update_mtu):
437 """Test tap device creation without MTU."""
438 linux_net.create_tap("tap0", None, "aa:bb:cc:dd:ee:ff")
439
440 mock_add.assert_called_once()
441 mock_set.assert_called_once()
442 mock_update_mtu.assert_called_once_with("tap0", None)