From ed08ed4a454d81daecb28e4077e7bb456485cced Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 15 Oct 2021 01:36:08 +0900 Subject: [PATCH 1/3] test-network: use constant variables in dnsmasq command --- test/test-network/systemd-networkd-tests.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index 27ccc3ce23..3f6027b115 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -21,8 +21,9 @@ networkd_ci_path='/run/networkd-ci' network_sysctl_ipv6_path='/proc/sys/net/ipv6/conf' network_sysctl_ipv4_path='/proc/sys/net/ipv4/conf' -dnsmasq_pid_file='/run/networkd-ci/test-test-dnsmasq.pid' -dnsmasq_log_file='/run/networkd-ci/test-dnsmasq-log-file' +dnsmasq_pid_file='/run/networkd-ci/test-dnsmasq.pid' +dnsmasq_log_file='/run/networkd-ci/test-dnsmasq.log' +dnsmasq_lease_file='/run/networkd-ci/test-dnsmasq.lease' systemd_lib_paths=['/usr/lib/systemd', '/lib/systemd'] which_paths=':'.join(systemd_lib_paths + os.getenv('PATH', os.defpath).lstrip(':').split(':')) @@ -474,7 +475,7 @@ def remove_networkd_conf_dropin(dropins): os.remove(os.path.join(networkd_conf_dropin_path, dropin)) def start_dnsmasq(additional_options='', ipv4_range='192.168.5.10,192.168.5.200', ipv6_range='2600::10,2600::20', lease_time='1h'): - dnsmasq_command = f'dnsmasq -8 /var/run/networkd-ci/test-dnsmasq-log-file --log-queries=extra --log-dhcp --pid-file=/var/run/networkd-ci/test-test-dnsmasq.pid --conf-file=/dev/null --interface=veth-peer --enable-ra --dhcp-range={ipv6_range},{lease_time} --dhcp-range={ipv4_range},{lease_time} -R --dhcp-leasefile=/var/run/networkd-ci/lease --dhcp-option=26,1492 --dhcp-option=option:router,192.168.5.1 --port=0 ' + additional_options + dnsmasq_command = f'dnsmasq -8 {dnsmasq_log_file} --log-queries=extra --log-dhcp --pid-file={dnsmasq_pid_file} --conf-file=/dev/null --interface=veth-peer --enable-ra --dhcp-range={ipv6_range},{lease_time} --dhcp-range={ipv4_range},{lease_time} -R --dhcp-leasefile={dnsmasq_lease_file} --dhcp-option=26,1492 --dhcp-option=option:router,192.168.5.1 --port=0 ' + additional_options check_output(dnsmasq_command) def stop_dnsmasq(pid_file): @@ -498,11 +499,11 @@ def search_words_in_dnsmasq_log(words, show_all=False): return True return False -def remove_lease_file(): - if os.path.exists(os.path.join(networkd_ci_path, 'lease')): - os.remove(os.path.join(networkd_ci_path, 'lease')) +def remove_dnsmasq_lease_file(): + if os.path.exists(dnsmasq_lease_file): + os.remove(dnsmasq_lease_file) -def remove_log_file(): +def remove_dnsmasq_log_file(): if os.path.exists(dnsmasq_log_file): os.remove(dnsmasq_log_file) @@ -4040,8 +4041,8 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): def tearDown(self): stop_dnsmasq(dnsmasq_pid_file) - remove_lease_file() - remove_log_file() + remove_dnsmasq_lease_file() + remove_dnsmasq_log_file() remove_links(self.links) remove_unit_from_networkd_path(self.units) stop_networkd(show_logs=True) @@ -4839,7 +4840,6 @@ class NetworkdIPv6PrefixTests(unittest.TestCase, Utilities): stop_networkd(show_logs=False) def tearDown(self): - remove_log_file() remove_links(self.links) remove_unit_from_networkd_path(self.units) stop_networkd(show_logs=True) @@ -4928,7 +4928,6 @@ class NetworkdMTUTests(unittest.TestCase, Utilities): stop_networkd(show_logs=False) def tearDown(self): - remove_log_file() remove_links(self.links) remove_unit_from_networkd_path(self.units) stop_networkd(show_logs=True) From 888f57c11c2d93c6b8f677419f3d98c3a0159ac8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 15 Oct 2021 01:54:11 +0900 Subject: [PATCH 2/3] test-network: drop pid_file argument from stop_dnsmasq() --- test/test-network/systemd-networkd-tests.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index 3f6027b115..dca101f39e 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -478,7 +478,7 @@ def start_dnsmasq(additional_options='', ipv4_range='192.168.5.10,192.168.5.200' dnsmasq_command = f'dnsmasq -8 {dnsmasq_log_file} --log-queries=extra --log-dhcp --pid-file={dnsmasq_pid_file} --conf-file=/dev/null --interface=veth-peer --enable-ra --dhcp-range={ipv6_range},{lease_time} --dhcp-range={ipv4_range},{lease_time} -R --dhcp-leasefile={dnsmasq_lease_file} --dhcp-option=26,1492 --dhcp-option=option:router,192.168.5.1 --port=0 ' + additional_options check_output(dnsmasq_command) -def stop_dnsmasq(pid_file): +def stop_by_pid_file(pid_file): if os.path.exists(pid_file): with open(pid_file, 'r') as f: pid = f.read().rstrip(' \t\r\n\0') @@ -486,6 +486,9 @@ def stop_dnsmasq(pid_file): os.remove(pid_file) +def stop_dnsmasq(): + stop_by_pid_file(dnsmasq_pid_file) + 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: @@ -4035,12 +4038,12 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): 'static.network'] def setUp(self): - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() remove_links(self.links) stop_networkd(show_logs=False) def tearDown(self): - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() remove_dnsmasq_lease_file() remove_dnsmasq_log_file() remove_links(self.links) @@ -4093,7 +4096,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, r'192.168.5.6 proto dhcp scope link src 192.168.5.181 metric 1024') self.assertRegex(output, r'192.168.5.7 proto dhcp scope link src 192.168.5.181 metric 1024') - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() start_dnsmasq(additional_options='--dhcp-option=option:dns-server,192.168.5.1,192.168.5.7,192.168.5.8', lease_time='2m') # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120 @@ -4331,7 +4334,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, r'192.168.6.0/24 proto static') self.assertRegex(output, r'192.168.7.0/24 proto static') - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() start_dnsmasq(ipv4_range='192.168.5.210,192.168.5.220', lease_time='2m') # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120 @@ -4364,7 +4367,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, r'2600::/64 proto ra metric 1024') self.assertRegex(output, r'2600:0:0:1::/64 proto static metric 1024 pref medium') - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() start_dnsmasq(ipv6_range='2600::30,2600::40', lease_time='2m') # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120 @@ -4394,7 +4397,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, r'192.168.5.*') # Stopping dnsmasq as networkd won't be allowed to renew the DHCP lease. - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120 print('Wait for the dynamic address to be expired') @@ -4444,7 +4447,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): print(output) self.assertRegex(output, r'192.168.5.*') - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() check_output('systemctl stop systemd-networkd.socket') check_output('systemctl stop systemd-networkd.service') @@ -4687,7 +4690,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, f'default via 192.168.5.1 proto dhcp src {address1} metric 1024') self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address1} metric 1024') - stop_dnsmasq(dnsmasq_pid_file) + stop_dnsmasq() start_dnsmasq(ipv4_range='192.168.5.200,192.168.5.250', lease_time='2m') print('Wait for the dynamic address to be expired') From a4a1385e078b46ac2adbbe5f649b23450670c85e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 15 Oct 2021 02:00:27 +0900 Subject: [PATCH 3/3] test-network: cleanup dnsmasq related file on setup Just for safety. --- test/test-network/systemd-networkd-tests.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index dca101f39e..733a8d5aa5 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -4039,6 +4039,8 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): def setUp(self): stop_dnsmasq() + remove_dnsmasq_lease_file() + remove_dnsmasq_log_file() remove_links(self.links) stop_networkd(show_logs=False)