Skip to content

Commit ad9bdfc

Browse files
committed
T5195: add timeout argument to process_named_running()
Smoketests heavily rely on process_named_running() so in order to "relax" system constraints during a test we will add a timeout of 10 seconds for every testcase provided by base_interfaces_test.py
1 parent da65cbf commit ad9bdfc

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

python/vyos/utils/process.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,32 @@ def process_running(pid_file):
204204
pid = f.read().strip()
205205
return pid_exists(int(pid))
206206

207-
def process_named_running(name, cmdline: str=None):
207+
def process_named_running(name, cmdline: str=None, timeout=0):
208208
""" Checks if process with given name is running and returns its PID.
209209
If Process is not running, return None
210210
"""
211211
from psutil import process_iter
212-
for p in process_iter(['name', 'pid', 'cmdline']):
213-
if cmdline:
214-
if p.info['name'] == name and cmdline in p.info['cmdline']:
212+
def check_process(name, cmdline):
213+
for p in process_iter(['name', 'pid', 'cmdline']):
214+
if cmdline:
215+
if name in p.info['name'] and cmdline in p.info['cmdline']:
216+
return p.info['pid']
217+
elif name in p.info['name']:
215218
return p.info['pid']
216-
elif p.info['name'] == name:
217-
return p.info['pid']
219+
return None
220+
if timeout:
221+
import time
222+
time_expire = time.time() + timeout
223+
while True:
224+
tmp = check_process(name, cmdline)
225+
if not tmp:
226+
if time.time() > time_expire:
227+
break
228+
time.sleep(0.100) # wait 250ms
229+
continue
230+
return tmp
231+
else:
232+
return check_process(name, cmdline)
218233
return None
219234

220235
def is_systemd_service_active(service):

smoketest/scripts/cli/base_interfaces_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ def tearDown(self):
127127
# by also checking the cmd arguments passed to the daemon
128128
if self._interfaces:
129129
for tmp in self._interfaces:
130-
self.assertFalse(process_named_running(daemon, tmp))
130+
self.assertFalse(process_named_running(daemon, tmp, timeout=10))
131131
else:
132-
self.assertFalse(process_named_running(daemon))
132+
self.assertFalse(process_named_running(daemon, timeout=10))
133133

134134
def test_dhcp_disable_interface(self):
135135
if not self._test_dhcp:
@@ -179,7 +179,7 @@ def test_dhcp_client_options(self):
179179

180180
for interface in self._interfaces:
181181
# Check if dhclient process runs
182-
dhclient_pid = process_named_running(dhclient_process_name, cmdline=interface)
182+
dhclient_pid = process_named_running(dhclient_process_name, cmdline=interface, timeout=10)
183183
self.assertTrue(dhclient_pid)
184184

185185
dhclient_config = read_file(f'{dhclient_base_dir}/dhclient_{interface}.conf')
@@ -216,7 +216,7 @@ def test_dhcp_vrf(self):
216216
self.assertEqual(tmp, vrf_name)
217217

218218
# Check if dhclient process runs
219-
dhclient_pid = process_named_running(dhclient_process_name, cmdline=interface)
219+
dhclient_pid = process_named_running(dhclient_process_name, cmdline=interface, timeout=10)
220220
self.assertTrue(dhclient_pid)
221221
# .. inside the appropriate VRF instance
222222
vrf_pids = cmd(f'ip vrf pids {vrf_name}')
@@ -251,7 +251,7 @@ def test_dhcpv6_vrf(self):
251251
self.assertEqual(tmp, vrf_name)
252252

253253
# Check if dhclient process runs
254-
tmp = process_named_running(dhcp6c_process_name, cmdline=interface)
254+
tmp = process_named_running(dhcp6c_process_name, cmdline=interface, timeout=10)
255255
self.assertTrue(tmp)
256256
# .. inside the appropriate VRF instance
257257
vrf_pids = cmd(f'ip vrf pids {vrf_name}')
@@ -945,7 +945,7 @@ def test_dhcpv6_client_options(self):
945945
duid_base += 1
946946

947947
# Better ask the process about it's commandline in the future
948-
pid = process_named_running(dhcp6c_process_name, cmdline=interface)
948+
pid = process_named_running(dhcp6c_process_name, cmdline=interface, timeout=10)
949949
self.assertTrue(pid)
950950

951951
dhcp6c_options = read_file(f'/proc/{pid}/cmdline')
@@ -1004,7 +1004,7 @@ def test_dhcpv6pd_auto_sla_id(self):
10041004
address = str(int(address) + 1)
10051005

10061006
# Check for running process
1007-
self.assertTrue(process_named_running(dhcp6c_process_name, cmdline=interface))
1007+
self.assertTrue(process_named_running(dhcp6c_process_name, cmdline=interface, timeout=10))
10081008

10091009
for delegatee in delegatees:
10101010
# we can already cleanup the test delegatee interface here
@@ -1070,7 +1070,7 @@ def test_dhcpv6pd_manual_sla_id(self):
10701070
address = str(int(address) + 1)
10711071

10721072
# Check for running process
1073-
self.assertTrue(process_named_running(dhcp6c_process_name, cmdline=interface))
1073+
self.assertTrue(process_named_running(dhcp6c_process_name, cmdline=interface, timeout=10))
10741074

10751075
for delegatee in delegatees:
10761076
# we can already cleanup the test delegatee interface here

0 commit comments

Comments
 (0)