From 004daaf5a1a67e503f7ff30e1b097e3571b9b08c Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Sat, 11 Jun 2022 17:47:52 +0200 Subject: [PATCH 1/4] test-network: override networkctl bin w/ valgrind/sanitizers --- test/test-network/systemd-networkd-tests.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index 7a962ba708..eb99e21dfc 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -321,14 +321,19 @@ def setUpModule(): '[Service]', 'Restart=no', 'ExecStart=', + 'ExecReload=', ] if use_valgrind: drop_in += [ 'ExecStart=!!valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ' + networkd_bin, + f'ExecReload=valgrind {networkctl_bin} reload', 'PrivateTmp=yes' ] else: - drop_in += ['ExecStart=!!' + networkd_bin] + drop_in += [ + 'ExecStart=!!' + networkd_bin, + f'ExecReload={networkctl_bin} reload', + ] if enable_debug: drop_in += ['Environment=SYSTEMD_LOG_LEVEL=debug'] if asan_options: From e4295d4ddc710a004b0ffb42e160c608bc5e28b1 Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Sat, 11 Jun 2022 17:54:59 +0200 Subject: [PATCH 2/4] test-network: make use of f-strings in couple more places --- test/test-network/systemd-networkd-tests.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index eb99e21dfc..e79eace97b 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -566,7 +566,7 @@ def search_words_in_dnsmasq_log(words, show_all=False): for line in contents.splitlines(): if words in line: in_file.close() - print("%s, %s" % (words, line)) + print(f"{words}, {line}") return True return False @@ -5709,8 +5709,8 @@ class NetworkdDHCPPDTests(unittest.TestCase, Utilities): print(output) self.assertRegex(output, '2001:db8:6464:[0-9a-f]+01::/64 proto kernel metric [0-9]* expires') - print('### ip -d link show dev {}'.format(tunnel_name)) - output = check_output('ip -d link show dev {}'.format(tunnel_name)) + print(f'### ip -d link show dev {tunnel_name}') + output = check_output(f'ip -d link show dev {tunnel_name}') print(output) self.assertIn('link/sit 10.100.100.', output) self.assertIn('local 10.100.100.', output) @@ -5718,14 +5718,14 @@ class NetworkdDHCPPDTests(unittest.TestCase, Utilities): self.assertIn('6rd-prefix 2001:db8::/32', output) self.assertIn('6rd-relay_prefix 10.0.0.0/8', output) - print('### ip -6 address show dev {}'.format(tunnel_name)) - output = check_output('ip -6 address show dev {}'.format(tunnel_name)) + print(f'### ip -6 address show dev {tunnel_name}') + output = check_output(f'ip -6 address show dev {tunnel_name}') print(output) self.assertRegex(output, 'inet6 2001:db8:6464:[0-9a-f]+0[23]:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*/64 (metric 256 |)scope global dynamic') self.assertRegex(output, 'inet6 ::10.100.100.[0-9]+/96 scope global') - print('### ip -6 route show dev {}'.format(tunnel_name)) - output = check_output('ip -6 route show dev {}'.format(tunnel_name)) + print(f'### ip -6 route show dev {tunnel_name}') + output = check_output(f'ip -6 route show dev {tunnel_name}') print(output) self.assertRegex(output, '2001:db8:6464:[0-9a-f]+0[23]::/64 proto kernel metric [0-9]* expires') self.assertRegex(output, '::/96 proto kernel metric [0-9]*') @@ -5734,7 +5734,7 @@ class NetworkdDHCPPDTests(unittest.TestCase, Utilities): output = check_output('ip -6 route show default') print(output) self.assertIn('default', output) - self.assertIn('via ::10.0.0.1 dev {}'.format(tunnel_name), output) + self.assertIn(f'via ::10.0.0.1 dev {tunnel_name}', output) def test_dhcp4_6rd(self): copy_unit_to_networkd_unit_path('25-veth.netdev', '25-dhcp4-6rd-server.network', '25-dhcp4-6rd-upstream.network', @@ -5768,7 +5768,7 @@ class NetworkdDHCPPDTests(unittest.TestCase, Utilities): tunnel_name = name break - self.wait_online(['{}:routable'.format(tunnel_name)]) + self.wait_online([f'{tunnel_name}:routable']) self.verify_dhcp4_6rd(tunnel_name) From d45476ef5c59ea9cd44e1e42962c960879ea3b2a Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Sat, 11 Jun 2022 18:03:28 +0200 Subject: [PATCH 3/4] test-network: explicitly set encoding when open()ing text files --- test/test-network/systemd-networkd-tests.py | 60 ++++++++++----------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index e79eace97b..126a7426f4 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -215,7 +215,7 @@ def expectedFailureIfNetdevsimWithSRIOVIsNotAvailable(): return unittest.expectedFailure(func) try: - with open('/sys/bus/netdevsim/new_device', mode='w') as f: + with open('/sys/bus/netdevsim/new_device', mode='w', encoding='utf-8') as f: f.write('99 1') except OSError: return unittest.expectedFailure(func) @@ -223,7 +223,7 @@ def expectedFailureIfNetdevsimWithSRIOVIsNotAvailable(): call('udevadm settle') call('udevadm info -w10s /sys/devices/netdevsim99/net/eni99np1', stderr=subprocess.DEVNULL) try: - with open('/sys/class/net/eni99np1/device/sriov_numvfs', mode='w') as f: + with open('/sys/class/net/eni99np1/device/sriov_numvfs', mode='w', encoding='utf-8') as f: f.write('3') except OSError: call('rmmod netdevsim', stderr=subprocess.DEVNULL) @@ -353,7 +353,7 @@ def setUpModule(): ] os.makedirs('/run/systemd/system/systemd-networkd.service.d', exist_ok=True) - with open('/run/systemd/system/systemd-networkd.service.d/00-override.conf', mode='w') as f: + with open('/run/systemd/system/systemd-networkd.service.d/00-override.conf', mode='w', encoding='utf-8') as f: f.write('\n'.join(drop_in)) drop_in = [ @@ -384,7 +384,7 @@ def setUpModule(): ] os.makedirs('/run/systemd/system/systemd-resolved.service.d', exist_ok=True) - with open('/run/systemd/system/systemd-resolved.service.d/00-override.conf', mode='w') as f: + with open('/run/systemd/system/systemd-resolved.service.d/00-override.conf', mode='w', encoding='utf-8') as f: f.write('\n'.join(drop_in)) drop_in = [ @@ -394,7 +394,7 @@ def setUpModule(): ] os.makedirs('/run/systemd/system/systemd-udevd.service.d', exist_ok=True) - with open('/run/systemd/system/systemd-udevd.service.d/00-override.conf', mode='w') as f: + with open('/run/systemd/system/systemd-udevd.service.d/00-override.conf', mode='w', encoding='utf-8') as f: f.write('\n'.join(drop_in)) check_output('systemctl daemon-reload') @@ -424,7 +424,7 @@ def tearDownModule(): check_output(f'systemctl start {u}') def read_link_attr(*args): - with open(os.path.join('/sys/class/net/', *args)) as f: + with open(os.path.join('/sys/class/net/', *args), encoding='utf-8') as f: return f.readline().strip() def read_bridge_port_attr(bridge, link, attribute): @@ -432,7 +432,7 @@ def read_bridge_port_attr(bridge, link, attribute): path_port = 'lower_' + link + '/brport' path = os.path.join(path_bridge, path_port) - with open(os.path.join(path, attribute)) as f: + with open(os.path.join(path, attribute), encoding='utf-8') as f: return f.readline().strip() def link_exists(link): @@ -477,11 +477,11 @@ def remove_l2tp_tunnels(tunnel_ids): time.sleep(1) def read_ipv6_sysctl_attr(link, attribute): - with open(os.path.join(os.path.join(network_sysctl_ipv6_path, link), attribute)) as f: + with open(os.path.join(os.path.join(network_sysctl_ipv6_path, link), attribute), encoding='utf-8') as f: return f.readline().strip() def read_ipv4_sysctl_attr(link, attribute): - with open(os.path.join(os.path.join(network_sysctl_ipv4_path, link), attribute)) as f: + with open(os.path.join(os.path.join(network_sysctl_ipv4_path, link), attribute), encoding='utf-8') as f: return f.readline().strip() def copy_unit_to_networkd_unit_path(*units, dropins=True): @@ -534,7 +534,7 @@ def start_dnsmasq(additional_options='', interface='veth-peer', ipv4_range='192. def stop_by_pid_file(pid_file): if os.path.exists(pid_file): - with open(pid_file, 'r') as f: + with open(pid_file, 'r', encoding='utf-8') as f: pid = f.read().rstrip(' \t\r\n\0') os.kill(int(pid), signal.SIGTERM) for _ in range(25): @@ -554,12 +554,12 @@ def stop_dnsmasq(): def dump_dnsmasq_log_file(): if os.path.exists(dnsmasq_log_file): - with open (dnsmasq_log_file) as in_file: + with open (dnsmasq_log_file, encoding='utf-8') as in_file: print(in_file.read()) def search_words_in_dnsmasq_log(words, show_all=False): if os.path.exists(dnsmasq_log_file): - with open (dnsmasq_log_file) as in_file: + with open (dnsmasq_log_file, encoding='utf-8') as in_file: contents = in_file.read() if show_all: print(contents) @@ -1289,7 +1289,7 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities): self.tearDown() copy_unit_to_networkd_unit_path('21-macvtap.netdev', '26-netdev-link-local-addressing-yes.network', '11-dummy.netdev', '25-macvtap.network') - with open(os.path.join(network_unit_file_path, '21-macvtap.netdev'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '21-macvtap.netdev'), mode='a', encoding='utf-8') as f: f.write('[MACVTAP]\nMode=' + mode) start_networkd() @@ -1307,7 +1307,7 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities): self.tearDown() copy_unit_to_networkd_unit_path('21-macvlan.netdev', '26-netdev-link-local-addressing-yes.network', '11-dummy.netdev', '25-macvlan.network') - with open(os.path.join(network_unit_file_path, '21-macvlan.netdev'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '21-macvlan.netdev'), mode='a', encoding='utf-8') as f: f.write('[MACVLAN]\nMode=' + mode) start_networkd() @@ -1351,7 +1351,7 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities): self.tearDown() copy_unit_to_networkd_unit_path('25-ipvlan.netdev', '26-netdev-link-local-addressing-yes.network', '11-dummy.netdev', '25-ipvlan.network') - with open(os.path.join(network_unit_file_path, '25-ipvlan.netdev'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-ipvlan.netdev'), mode='a', encoding='utf-8') as f: f.write('[IPVLAN]\nMode=' + mode + '\nFlags=' + flag) start_networkd() @@ -1369,7 +1369,7 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities): self.tearDown() copy_unit_to_networkd_unit_path('25-ipvtap.netdev', '26-netdev-link-local-addressing-yes.network', '11-dummy.netdev', '25-ipvtap.network') - with open(os.path.join(network_unit_file_path, '25-ipvtap.netdev'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-ipvtap.netdev'), mode='a', encoding='utf-8') as f: f.write('[IPVTAP]\nMode=' + mode + '\nFlags=' + flag) start_networkd() @@ -3677,7 +3677,7 @@ class NetworkdStateFileTests(unittest.TestCase, Utilities): # TODO: check json string check_output(*networkctl_cmd, '--json=short', 'status', env=env) - with open(path) as f: + with open(path, encoding='utf-8') as f: data = f.read() self.assertRegex(data, r'IPV4_ADDRESS_STATE=routable') self.assertRegex(data, r'IPV6_ADDRESS_STATE=routable') @@ -3706,7 +3706,7 @@ class NetworkdStateFileTests(unittest.TestCase, Utilities): # TODO: check json string check_output(*networkctl_cmd, '--json=short', 'status', env=env) - with open(path) as f: + with open(path, encoding='utf-8') as f: data = f.read() self.assertRegex(data, r'DNS=10.10.10.12#ccc.com 10.10.10.13 1111:2222::3333') self.assertRegex(data, r'NTP=2.fedora.pool.ntp.org 3.fedora.pool.ntp.org') @@ -3721,7 +3721,7 @@ class NetworkdStateFileTests(unittest.TestCase, Utilities): # TODO: check json string check_output(*networkctl_cmd, '--json=short', 'status', env=env) - with open(path) as f: + with open(path, encoding='utf-8') as f: data = f.read() self.assertRegex(data, r'DNS=10.10.10.12#ccc.com 10.10.10.13 1111:2222::3333') self.assertRegex(data, r'NTP=0.fedora.pool.ntp.org 1.fedora.pool.ntp.org') @@ -3736,7 +3736,7 @@ class NetworkdStateFileTests(unittest.TestCase, Utilities): # TODO: check json string check_output(*networkctl_cmd, '--json=short', 'status', env=env) - with open(path) as f: + with open(path, encoding='utf-8') as f: data = f.read() self.assertRegex(data, r'DNS=10.10.10.10#aaa.com 10.10.10.11:1111#bbb.com \[1111:2222::3333\]:1234#ccc.com') self.assertRegex(data, r'NTP=0.fedora.pool.ntp.org 1.fedora.pool.ntp.org') @@ -4168,12 +4168,12 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities): def test_sriov(self): call('modprobe netdevsim', stderr=subprocess.DEVNULL) - with open('/sys/bus/netdevsim/new_device', mode='w') as f: + with open('/sys/bus/netdevsim/new_device', mode='w', encoding='utf-8') as f: f.write('99 1') call('udevadm settle') call('udevadm info -w10s /sys/devices/netdevsim99/net/eni99np1', stderr=subprocess.DEVNULL) - with open('/sys/class/net/eni99np1/device/sriov_numvfs', mode='w') as f: + with open('/sys/class/net/eni99np1/device/sriov_numvfs', mode='w', encoding='utf-8') as f: f.write('3') copy_unit_to_networkd_unit_path('25-sriov.network') @@ -4195,7 +4195,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities): copy_unit_to_networkd_unit_path('25-sriov.link', '25-sriov-udev.network') call('udevadm control --reload') - with open('/sys/bus/netdevsim/new_device', mode='w') as f: + with open('/sys/bus/netdevsim/new_device', mode='w', encoding='utf-8') as f: f.write('99 1') start_networkd() @@ -4211,7 +4211,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities): self.assertNotIn('vf 3', output) self.assertNotIn('vf 4', output) - with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a', encoding='utf-8') as f: f.write('[Link]\nSR-IOVVirtualFunctions=4\n') call('udevadm control --reload') @@ -4227,7 +4227,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities): ) self.assertNotIn('vf 4', output) - with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a', encoding='utf-8') as f: f.write('[Link]\nSR-IOVVirtualFunctions=\n') call('udevadm control --reload') @@ -4243,7 +4243,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities): ) self.assertNotIn('vf 4', output) - with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a', encoding='utf-8') as f: f.write('[Link]\nSR-IOVVirtualFunctions=2\n') call('udevadm control --reload') @@ -4259,7 +4259,7 @@ class NetworkdSRIOVTests(unittest.TestCase, Utilities): self.assertNotIn('vf 3', output) self.assertNotIn('vf 4', output) - with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-sriov.link'), mode='a', encoding='utf-8') as f: f.write('[Link]\nSR-IOVVirtualFunctions=\n') call('udevadm control --reload') @@ -4912,7 +4912,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): print(output) self.assertRegex(output, r'192.168.5.*') - with open(os.path.join(network_unit_file_path, '25-dhcp-client-keep-configuration-dhcp.network'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-dhcp-client-keep-configuration-dhcp.network'), mode='a', encoding='utf-8') as f: f.write('[Network]\nDHCP=no\n') start_networkd() @@ -4976,7 +4976,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): remove_unit_from_networkd_path(['25-dhcp-client.network']) - with open(os.path.join(network_unit_file_path, '25-static.network'), mode='w') as f: + with open(os.path.join(network_unit_file_path, '25-static.network'), mode='w', encoding='utf-8') as f: f.write(static_network) # When networkd started, the links are already configured, so let's wait for 5 seconds @@ -5051,7 +5051,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, 'default via 192.168.5.1 proto dhcp src 192.168.5.[0-9]*') self.assertIn('10.0.0.0/8 via 192.168.5.1 proto dhcp', output) - with open(os.path.join(network_unit_file_path, '25-dhcp-client-gateway-ipv4.network'), mode='a') as f: + with open(os.path.join(network_unit_file_path, '25-dhcp-client-gateway-ipv4.network'), mode='a', encoding='utf-8') as f: f.write('[DHCPv4]\nUseGateway=no\n') rc = call(*networkctl_cmd, 'reload', env=env) From f4586b70b28be20f041dc4628a7485334cb2d5ad Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Sat, 11 Jun 2022 18:06:38 +0200 Subject: [PATCH 4/4] test-network: drop unnecessary `global` `global` is needed only when assigning a new value to the global variable; it's not necessary when modifying a mutable object (in our case we just append items to the global list). --- test/test-network/systemd-networkd-tests.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index 126a7426f4..911f79f404 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -295,9 +295,6 @@ def expectedFailureIfFQPIEIsNotAvailable(): return f def setUpModule(): - # pylint: disable=global-statement - global running_units - os.makedirs(network_unit_file_path, exist_ok=True) os.makedirs(networkd_conf_dropin_path, exist_ok=True) os.makedirs(networkd_ci_path, exist_ok=True) @@ -405,9 +402,6 @@ def setUpModule(): check_output('systemctl restart systemd-udevd') def tearDownModule(): - # pylint: disable=global-statement - global running_units - shutil.rmtree(networkd_ci_path) os.remove(os.path.join(udev_rules_dir, '00-debug-net.rules'))