Skip to content

Commit 7ca073c

Browse files
committed
issue #482: ci: add stray process checks to all jobs
List of interesting processes can probably expand more over time.
1 parent 1e3621a commit 7ca073c

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

.ci/ansible_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ def pause_if_interactive():
2020
signal.pause()
2121

2222

23+
interesting = ci_lib.get_interesting_procs()
24+
25+
2326
with ci_lib.Fold('unit_tests'):
2427
os.environ['SKIP_MITOGEN'] = '1'
2528
ci_lib.run('./run_tests -v')
2629

2730

31+
ci_lib.check_stray_processes(interesting)
32+
33+
2834
with ci_lib.Fold('docker_setup'):
2935
containers = ci_lib.make_containers()
3036
ci_lib.start_containers(containers)
@@ -75,4 +81,7 @@ def pause_if_interactive():
7581
pause_if_interactive()
7682
raise
7783

84+
85+
ci_lib.check_stray_processes(interesting, containers)
86+
7887
pause_if_interactive()

.ci/ci_lib.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,44 @@ def make_containers(name_prefix='', port_offset=0):
215215
return lst
216216

217217

218+
INTERESTING_COMMS = ('python', 'ssh', 'sudo', 'su', 'doas')
219+
220+
221+
def proc_is_docker(pid):
222+
try:
223+
fp = open('/proc/%s/cgroup' % (pid,), 'rb')
224+
except IOError:
225+
return False
226+
227+
try:
228+
return 'docker' in fp.read()
229+
finally:
230+
fp.close()
231+
232+
233+
def get_interesting_procs(container_name=None):
234+
args = ['ps', '-a', '-x', '-oppid=', '-opid=', '-ocomm=', '-ocommand=']
235+
if container_name is not None:
236+
args = ['docker', 'exec', container_name] + args
237+
238+
out = []
239+
for line in subprocess__check_output(args).splitlines():
240+
ppid, pid, comm, rest = line.split(None, 3)
241+
if (
242+
(
243+
any(comm.startswith(s) for s in INTERESTING_COMMS) or
244+
'mitogen:' in rest
245+
) and
246+
(
247+
container_name is not None or
248+
(not proc_is_docker(pid))
249+
)
250+
):
251+
out.append((int(pid), line))
252+
253+
return sorted(out)
254+
255+
218256
def start_containers(containers):
219257
if os.environ.get('KEEP'):
220258
return
@@ -236,9 +274,44 @@ def start_containers(containers):
236274
]
237275
for container in containers
238276
])
277+
278+
for container in containers:
279+
container['interesting'] = get_interesting_procs(container['name'])
280+
239281
return containers
240282

241283

284+
def verify_procs(hostname, old, new):
285+
oldpids = set(pid for pid, _ in old)
286+
if any(pid not in oldpids for pid, _ in new):
287+
print('%r had stray processes running:' % (hostname,))
288+
for pid, line in new:
289+
if pid not in oldpids:
290+
print('New process:', line)
291+
292+
print()
293+
return False
294+
295+
return True
296+
297+
298+
def check_stray_processes(old, containers=None):
299+
ok = True
300+
301+
new = get_interesting_procs()
302+
if old is not None:
303+
ok &= verify_procs('test host machine', old, new)
304+
305+
for container in containers or ():
306+
ok &= verify_procs(
307+
container['name'],
308+
container['interesting'],
309+
get_interesting_procs(container['name'])
310+
)
311+
312+
assert ok, 'stray processes were found'
313+
314+
242315
def dump_file(path):
243316
print()
244317
print('--- %s ---' % (path,))

.ci/debops_common_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@
6868
os.environ['ANSIBLE_HOST_KEY_CHECKING'] = 'False'
6969

7070

71+
interesting = ci_lib.get_interesting_procs()
72+
7173
with ci_lib.Fold('first_run'):
7274
ci_lib.run('debops common %s', ' '.join(sys.argv[1:]))
7375

76+
ci_lib.check_stray_processes(interesting, containers)
77+
7478

7579
with ci_lib.Fold('second_run'):
7680
ci_lib.run('debops common %s', ' '.join(sys.argv[1:]))
81+
82+
ci_lib.check_stray_processes(interesting, containers)

.ci/mitogen_install.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
'docker pull %s' % (ci_lib.image_for_distro(ci_lib.DISTRO),),
1515
])
1616

17+
1718
ci_lib.run_batches(batches)

.ci/mitogen_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
if not ci_lib.have_docker():
1515
os.environ['SKIP_DOCKER_TESTS'] = '1'
1616

17+
interesting = ci_lib.get_interesting_procs()
1718
ci_lib.run('./run_tests -v')
19+
ci_lib.check_stray_processes(interesting)

0 commit comments

Comments
 (0)