@@ -146,6 +146,17 @@ def data_path(suffix):
146
146
return path
147
147
148
148
149
+ def retry (fn , on , max_attempts , delay ):
150
+ for i in range (max_attempts ):
151
+ try :
152
+ return fn ()
153
+ except on :
154
+ if i >= max_attempts - 1 :
155
+ raise
156
+ else :
157
+ time .sleep (delay )
158
+
159
+
149
160
def threading__thread_is_alive (thread ):
150
161
"""Return whether the thread is alive (Python version compatibility shim).
151
162
@@ -562,18 +573,24 @@ def wait_for_sshd(self):
562
573
wait_for_port (self .get_host (), self .port , pattern = 'OpenSSH' )
563
574
564
575
def check_processes (self ):
565
- args = ['docker' , 'exec' , self .container_name , 'ps' , '-o' , 'comm=' ]
576
+ # Get Accounting name (ucomm) & command line (args) of each process
577
+ # in the container. No truncation (-ww). No column headers (foo=).
578
+ ps_output = subprocess .check_output ([
579
+ 'docker' , 'exec' , self .container_name ,
580
+ 'ps' , '-w' , '-w' , '-o' , 'ucomm=' , '-o' , 'args=' ,
581
+ ])
582
+ ps_lines = ps_output .decode ().splitlines ()
583
+ processes = [tuple (line .split (None , 1 )) for line in ps_lines ]
566
584
counts = {}
567
- for comm in subprocess .check_output (args ).decode ().splitlines ():
568
- comm = comm .strip ()
569
- counts [comm ] = counts .get (comm , 0 ) + 1
585
+ for ucomm , _ in processes :
586
+ counts [ucomm ] = counts .get (ucomm , 0 ) + 1
570
587
571
588
if counts != {'ps' : 1 , 'sshd' : 1 }:
572
589
assert 0 , (
573
590
'Docker container %r contained extra running processes '
574
591
'after test completed: %r' % (
575
592
self .container_name ,
576
- counts
593
+ processes ,
577
594
)
578
595
)
579
596
@@ -630,7 +647,12 @@ def setUpClass(cls):
630
647
631
648
@classmethod
632
649
def tearDownClass (cls ):
633
- cls .dockerized_ssh .check_processes ()
650
+ retry (
651
+ cls .dockerized_ssh .check_processes ,
652
+ on = AssertionError ,
653
+ max_attempts = 5 ,
654
+ delay = 0.1 ,
655
+ )
634
656
cls .dockerized_ssh .close ()
635
657
super (DockerMixin , cls ).tearDownClass ()
636
658
0 commit comments