12
12
# under the License.
13
13
14
14
15
- from time import sleep
15
+ import re
16
+ import time
16
17
17
18
import blindspin
18
19
import crayons
@@ -40,7 +41,7 @@ def wrapper(wrapped, instance, args, kwargs):
40
41
try :
41
42
return wrapped (* args , ** kwargs )
42
43
except Exception as e :
43
- sleep (config .SLEEP_TIME )
44
+ time . sleep (config .SLEEP_TIME )
44
45
exception = e
45
46
raise TimeoutException (
46
47
"""Wait time exceeded {0} sec.
@@ -55,3 +56,37 @@ def wrapper(wrapped, instance, args, kwargs):
55
56
@wait_container_is_ready ()
56
57
def wait_for (condition ):
57
58
return condition ()
59
+
60
+
61
+ def wait_for_logs (container , predicate , timeout = None , interval = 1 ):
62
+ """
63
+ Wait for the container to emit logs satisfying the predicate.
64
+
65
+ Parameters
66
+ ----------
67
+ container : DockerContainer
68
+ Container whose logs to wait for.
69
+ predicate : callable or str
70
+ Predicate that should be satisfied by the logs. If a string, the it is used as the pattern
71
+ for a multiline regular expression search.
72
+ timeout : float or None
73
+ Number of seconds to wait for the predicate to be satisfied. Defaults to wait indefinitely.
74
+ interval : float
75
+ Interval at which to poll the logs.
76
+
77
+ Returns
78
+ -------
79
+ duration : float
80
+ Number of seconds until the predicate was satisfied.
81
+ """
82
+ if isinstance (predicate , str ):
83
+ predicate = re .compile (predicate , re .MULTILINE ).search
84
+ start = time .time ()
85
+ while True :
86
+ duration = time .time () - start
87
+ if predicate (container ._container .logs ().decode ()):
88
+ return duration
89
+ if timeout and duration > timeout :
90
+ raise TimeoutError ("container did not emit logs satisfying predicate in %.3f seconds"
91
+ % timeout )
92
+ time .sleep (interval )
0 commit comments