Debian Codegraph @

os_vif/tests/functional/internal/command/ip/test_impl_pyroute2.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 os
14 import re
15 import time
16
17 from oslo_concurrency import processutils
18 from oslo_utils import excutils
19
20 from os_vif.internal.ip.api import ip as ip_lib
21 from os_vif.tests.functional import base
22 from os_vif.tests.functional import privsep
23
24
25 @privsep.os_vif_pctxt.entrypoint
26 def _execute_command(*args):
27 return processutils.execute(*args)
28
29
30 class ShellIpCommands(object):
31
32 def add_device(self, device, dev_type, peer=None, link=None,
33 vlan_id=None):
34 if 'vlan' == dev_type:
35 _execute_command('ip', 'link', 'add', 'link', link,
36 'name', device, 'type', dev_type, 'vlan', 'id',
37 vlan_id)
38 elif 'veth' == dev_type:
39 _execute_command('ip', 'link', 'add', device, 'type', dev_type,
40 'peer', 'name', peer)
41 elif 'dummy' == dev_type:
42 _execute_command('ip', 'link', 'add', device, 'type', dev_type)
43 # ensure that the device exists to prevent racing
44 # with other ip commands
45 for _ in range(10):
46 if self.exist_device(device):
47 return
48 time.sleep(0.1)
49
50 def del_device(self, device):
51 if self.exist_device(device):
52 _execute_command('ip', 'link', 'del', device)
53
54 def set_status_up(self, device):
55 _execute_command('ip', 'link', 'set', device, 'up')
56
57 def set_status_down(self, device):
58 _execute_command('ip', 'link', 'set', device, 'down')
59
60 def set_device_mtu(self, device, mtu):
61 _execute_command('ip', 'link', 'set', device, 'mtu', mtu)
62
63 def show_device(self, device):
64 val, err = _execute_command('ip', 'link', 'show', device)
65 return val.splitlines()
66
67 def exist_device(self, device):
68 try:
69 _execute_command('ip', 'link', 'show', device)
70 return True
71 except processutils.ProcessExecutionError as e:
72 with excutils.save_and_reraise_exception() as saved_exception:
73 if e.exit_code == 1:
74 saved_exception.reraise = False
75 return False
76
77 def show_state(self, device):
78 regex = re.compile(r".*state (?P<state>\w+)")
79 match = regex.match(self.show_device(device)[0])
80 if match is None:
81 return
82 return match.group('state')
83
84 def is_admin_up(self, device):
85 """Check if device is administratively up (UP flag set).
86
87 This differs from show_state() which returns the operational state.
88 TAP devices without a carrier will have state=DOWN but can still
89 be administratively UP (UP in flags like <NO-CARRIER,BROADCAST,UP>).
90 """
91 # Flags are in angle brackets on the first line, e.g. <NO-CARRIER,UP>
92 first_line = self.show_device(device)[0]
93 regex = re.compile(r".*<(?P<flags>[^>]+)>")
94 match = regex.match(first_line)
95 if match is None:
96 return False
97 flags = match.group('flags').split(',')
98 return 'UP' in flags
99
100 def show_promisc(self, device):
101 regex = re.compile(r".*(PROMISC)")
102 match = regex.match(self.show_device(device)[0])
103 return True if match else False
104
105 def show_mac(self, device):
106 exp = r".*link/ether (?P<mac>([0-9A-Fa-f]{2}[:]){5}[0-9A-Fa-f]{2})"
107 regex = re.compile(exp)
108 match = regex.match(self.show_device(device)[1])
109 if match is None:
110 return
111 return match.group('mac')
112
113 def show_mtu(self, device):
114 regex = re.compile(r".*mtu (?P<mtu>\d+)")
115 match = regex.match(self.show_device(device)[0])
116 if match is None:
117 return
118 return int(match.group('mtu'))
119
120
121 @privsep.os_vif_pctxt.entrypoint
122 def _ip_cmd_set(*args, **kwargs):
123 ip_lib.set(*args, **kwargs)
124
125
126 @privsep.os_vif_pctxt.entrypoint
127 def _ip_cmd_add(*args, **kwargs):
128 ip_lib.add(*args, **kwargs)
129
130
131 @privsep.os_vif_pctxt.entrypoint
132 def _ip_cmd_delete(*args, **kwargs):
133 ip_lib.delete(*args, **kwargs)
134
135
136 @privsep.os_vif_pctxt.entrypoint
137 def _ip_cmd_exists(*args, **kwargs):
138 return ip_lib.exists(*args, **kwargs)
139
140
141 class TestIpCommand(ShellIpCommands, base.BaseFunctionalTestCase):
142
143 def setUp(self):
144 super(TestIpCommand, self).setUp()
145
146 def test_set_state(self):
147 device1 = "test_dev_1"
148 device2 = "test_dev_2"
149 self.addCleanup(self.del_device, device1)
150 self.add_device(device1, 'veth', peer=device2)
151 _ip_cmd_set(device1, state='up')
152 _ip_cmd_set(device2, state='up')
153 self.assertEqual('UP', self.show_state(device1))
154 self.assertEqual('UP', self.show_state(device2))
155 _ip_cmd_set(device1, state='down')
156 _ip_cmd_set(device2, state='down')
157 self.assertEqual('DOWN', self.show_state(device1))
158 self.assertEqual('DOWN', self.show_state(device2))
159
160 def test_set_mtu(self):
161 device = "test_dev_3"
162 self.addCleanup(self.del_device, device)
163 self.add_device(device, 'dummy')
164 _ip_cmd_set(device, mtu=1200)
165 self.assertEqual(1200, self.show_mtu(device))
166 _ip_cmd_set(device, mtu=900)
167 self.assertEqual(900, self.show_mtu(device))
168
169 def test_set_address(self):
170 device = "test_dev_4"
171 address1 = "36:a7:e4:f9:01:01"
172 address2 = "36:a7:e4:f9:01:01"
173 self.addCleanup(self.del_device, device)
174 self.add_device(device, 'dummy')
175 _ip_cmd_set(device, address=address1)
176 self.assertEqual(address1, self.show_mac(device))
177 _ip_cmd_set(device, address=address2)
178 self.assertEqual(address2, self.show_mac(device))
179
180 def test_set_promisc(self):
181 device = "test_dev_5"
182 self.addCleanup(self.del_device, device)
183 self.add_device(device, 'dummy')
184 _ip_cmd_set(device, promisc=True)
185 self.assertTrue(self.show_promisc(device))
186 _ip_cmd_set(device, promisc=False)
187 self.assertFalse(self.show_promisc(device))
188
189 def test_add_vlan(self):
190 device = "test_dev_6"
191 link = "test_devlink"
192 self.addCleanup(self.del_device, device)
193 self.addCleanup(self.del_device, link)
194 self.add_device(link, 'dummy')
195 _ip_cmd_add(device, 'vlan', link=link, vlan_id=100)
196 self.assertTrue(self.exist_device(device))
197
198 def test_add_veth(self):
199 device = "test_dev_7"
200 peer = "test_devpeer"
201 self.addCleanup(self.del_device, device)
202 _ip_cmd_add(device, 'veth', peer=peer)
203 self.assertTrue(self.exist_device(device))
204 self.assertTrue(self.exist_device(peer))
205
206 def test_delete(self):
207 device = "test_dev_8"
208 self.addCleanup(self.del_device, device)
209 self.add_device(device, 'dummy')
210 self.assertTrue(self.exist_device(device))
211 _ip_cmd_delete(device)
212 self.assertFalse(self.exist_device(device))
213
214 def test_iproute_object_closes_correctly(self):
215 # NOTE(ralonsoh): check https://bugs.launchpad.net/os-vif/+bug/1807949
216 device = "test_dev_9"
217 link = "test_devlink_2"
218 self.add_device(link, 'dummy')
219 self.addCleanup(self.del_device, device)
220 self.addCleanup(self.del_device, link)
221 for _ in range(300):
222 _ip_cmd_add(device, 'vlan', link=link, vlan_id=100)
223 _ip_cmd_delete(device)
224
225 def test_exists(self):
226 device = "test_dev_10"
227 self.addCleanup(self.del_device, device)
228 self.add_device(device, 'dummy')
229 self.assertTrue(_ip_cmd_exists(device))
230 self.del_device(device)
231 self.assertFalse(_ip_cmd_exists(device))
232
233 def test_add_bridge(self):
234 device = "test_dev_11"
235 self.addCleanup(self.del_device, device)
236 _ip_cmd_add(device, 'bridge')
237 self.assertTrue(self.exist_device(device))
238 base_path = "/sys/class/net/test_dev_11/bridge/%s"
239 with open(base_path % "forward_delay", "r") as f:
240 self.assertEqual("0", f.readline().rstrip('\n'))
241 with open(base_path % "stp_state", "r") as f:
242 self.assertEqual("0", f.readline().rstrip('\n'))
243 with open(base_path % "multicast_snooping", "r") as f:
244 self.assertEqual("0", f.readline().rstrip('\n'))
245 with open(base_path % "ageing_time", "r") as f:
246 value = int(f.readline().rstrip('\n'))
247 # NOTE(sean-k-mooney): IEEE 8021-Q recommends that the default
248 # ageing should be 300 and the linux kernel defaults to 300
249 # via an unconditional define. As such we expect this to be
250 # 300 however since services like network-manager could change
251 # the default on bridge creation we check that if it is not 300
252 # then the value should not be 0.
253 self.assertTrue(300 == value or value != 0)
254
255 def test_add_bridge_with_mac_ageing_0(self):
256 device = "test_dev_12"
257 self.addCleanup(self.del_device, device)
258 _ip_cmd_add(device, 'bridge', ageing=0)
259 self.assertTrue(self.exist_device(device))
260 base_path = "/sys/class/net/test_dev_12/bridge/%s"
261 with open(base_path % "forward_delay", "r") as f:
262 self.assertEqual("0", f.readline().rstrip('\n'))
263 with open(base_path % "stp_state", "r") as f:
264 self.assertEqual("0", f.readline().rstrip('\n'))
265 with open(base_path % "ageing_time", "r") as f:
266 self.assertEqual("0", f.readline().rstrip('\n'))
267 with open(base_path % "multicast_snooping", "r") as f:
268 self.assertEqual("0", f.readline().rstrip('\n'))
269
270 def test_add_port_to_bridge(self):
271 device = "test_dev_13"
272 bridge = "test_dev_14"
273 self.addCleanup(self.del_device, device)
274 self.addCleanup(self.del_device, bridge)
275 self.add_device(device, 'dummy')
276 _ip_cmd_add(bridge, 'bridge')
277 self.assertTrue(self.exist_device(device))
278 self.assertTrue(self.exist_device(bridge))
279 _ip_cmd_set(device, master=bridge)
280 path = "/sys/class/net/{}/brif/{}".format(bridge, device)
281 self.assertTrue(os.path.exists(path))
282
283 def test_add_tap(self):
284 """Test creating a tap device."""
285 device = "test_tap_1"
286 self.addCleanup(self.del_device, device)
287 _ip_cmd_add(device, 'tuntap', mode='tap')
288 self.assertTrue(self.exist_device(device))
289
290 # Verify it's a tap device by checking /sys/class/net/<dev>/tun_flags
291 tun_flags_path = "/sys/class/net/{}/tun_flags".format(device)
292 if os.path.exists(tun_flags_path):
293 with open(tun_flags_path, 'r') as f:
294 flags_hex = f.read().strip()
295 flags = int(flags_hex, 16)
296 # IFF_TAP = 0x0002
297 IFF_TAP = 0x0002
298 self.assertTrue(flags & IFF_TAP,
299 "TAP flag not set. Flags: {}".format(flags_hex))
300
301 def test_add_tap_with_multiqueue(self):
302 """Test creating a tap device with multiqueue parameter.
303
304 Note: The IFF_MULTI_QUEUE flag (0x0100) cannot be set via netlink.
305 It is only set when a process opens /dev/net/tun with the
306 IFF_MULTI_QUEUE flag in the ioctl. Libvirt sets this when attaching
307 to the TAP device. This test verifies that:
308 1. The TAP device can be created with multiqueue=True without error
309 2. The device is properly created as a TAP device
310 """
311 device = "test_tap_mq"
312 self.addCleanup(self.del_device, device)
313 _ip_cmd_add(device, 'tuntap', mode='tap', multiqueue=True)
314 self.assertTrue(self.exist_device(device))
315
316 # Verify it's a tap device by checking /sys/class/net/<dev>/tun_flags
317 tun_flags_path = "/sys/class/net/{}/tun_flags".format(device)
318 if os.path.exists(tun_flags_path):
319 with open(tun_flags_path, 'r') as f:
320 flags_hex = f.read().strip()
321 flags = int(flags_hex, 16)
322 # IFF_TAP = 0x0002 - verify it's a TAP device
323 IFF_TAP = 0x0002
324 self.assertTrue(
325 flags & IFF_TAP,
326 "TAP flag not set. Flags: %s" % flags_hex)
327
328 def test_add_tap_idempotent(self):
329 """Test tap device creation is idempotent with exit code handling."""
330 device = "test_tap_idemp"
331 self.addCleanup(self.del_device, device)
332
333 # Create the device
334 _ip_cmd_add(device, 'tuntap', mode='tap')
335 self.assertTrue(self.exist_device(device))
336
337 # Try to create again with EEXIST in check_exit_code (17)
338 # This should not raise an exception
339 _ip_cmd_add(device, 'tuntap', mode='tap', check_exit_code=[0, 17])
340 self.assertTrue(self.exist_device(device))
341
342 def test_add_tun_device(self):
343 """Test creating a tun device (not tap)."""
344 device = "test_tun_1"
345 self.addCleanup(self.del_device, device)
346 _ip_cmd_add(device, 'tuntap', mode='tun')
347 self.assertTrue(self.exist_device(device))
348
349 # Verify it's a tun device
350 tun_flags_path = "/sys/class/net/{}/tun_flags".format(device)
351 if os.path.exists(tun_flags_path):
352 with open(tun_flags_path, 'r') as f:
353 flags_hex = f.read().strip()
354 flags = int(flags_hex, 16)
355 # IFF_TUN = 0x0001
356 IFF_TUN = 0x0001
357 self.assertTrue(flags & IFF_TUN,
358 "TUN flag not set. Flags: {}".format(flags_hex))
359
360 def test_tap_set_properties(self):
361 """Test setting properties on a tap device."""
362 device = "test_tap_props"
363 mac = "36:a7:e4:f9:01:02"
364 self.addCleanup(self.del_device, device)
365
366 # Create tap device
367 _ip_cmd_add(device, 'tuntap', mode='tap')
368 self.assertTrue(self.exist_device(device))
369
370 # Set MAC address and state
371 _ip_cmd_set(device, address=mac, state='up')
372 # TAP devices without a carrier will show state=DOWN but should be
373 # administratively UP (UP flag in interface flags)
374 self.assertTrue(self.is_admin_up(device),
375 "Device should be administratively UP")
376 self.assertEqual(mac, self.show_mac(device))