-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathtest_util.py
More file actions
228 lines (197 loc) · 7.97 KB
/
test_util.py
File metadata and controls
228 lines (197 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import io
import signal
import subprocess
import sys
import time
import traceback
global cl
class Colorizer(object):
RED = "\033[31;1m"
GREEN = "\033[32;1m"
YELLOW = "\033[33;1m"
CYAN = "\033[36;1m"
RESET = "\033[0m"
NEWLINE = "\n"
@classmethod
def _colorize(cls, string, color):
return getattr(cls, color) + string + cls.RESET + cls.NEWLINE
@classmethod
def red(cls, string):
return cls._colorize(string, "RED")
@classmethod
def green(cls, string):
return cls._colorize(string, "GREEN")
@classmethod
def yellow(cls, string):
return cls._colorize(string, "YELLOW")
@classmethod
def cyan(cls, string):
return cls._colorize(string, "CYAN")
class FakeStdout(io.StringIO):
"""Fake class to mimic stdout. We can't just use io.StringIO because we need
to fake the ability to write binary files to sys.stdout.buffer (thus this
class has a "buffer" attribute that behaves the same way).
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.buffer = io.BytesIO()
def getvalue(self):
"""
If self.buffer has a non-unicode value, return that value.
Otherwise, decode the self.buffer value and append it
to self.getvalue().
This is because this function is mimicking the behavior of `sys.stdout`.
`sys.stdout` can be read as either a string or bytes.
When a string is written to `sys.stdout`, it returns a string when doing `getvalue()`.
When bytes are written to `sys.stdout` (by writing to `sys.stdout.buffer`),
it returns bytes when doing `getvalue()`. The reason we need to account for this
case is that there are tests in which a binary file is uploaded, then it is
printed out (by writing to `sys.stdout.buffer`), and then the test reads what's
printed out and makes sure it matches the original file.
"""
try:
buffer_value = self.buffer.getvalue().decode()
except UnicodeDecodeError:
return self.buffer.getvalue()
return super().getvalue() + buffer_value
def run_command(
args,
expected_exit_code=0,
max_output_chars=1024,
env=None,
include_stderr=False,
binary=False,
force_subprocess=False,
cwd=None,
):
# We import the following imports here because codalab_service.py imports TestModule from
# this file. If we kept the imports at the top, then anyone who ran codalab_service.py
# would also have to install all the dependencies that BundleCLI and CodaLabManager use.
from codalab.lib.bundle_cli import BundleCLI
from codalab.lib.codalab_manager import CodaLabManager
def sanitize(string, max_chars=256):
# Sanitize and truncate output so it can be printed on the command line.
# Don't print out binary.
if isinstance(string, bytes):
string = '<binary>'
if len(string) > max_chars:
string = string[:max_chars] + ' (...more...)'
return string
# If we don't care about the exit code, set `expected_exit_code` to None.
print(">>", *map(str, args), sep=" ", end="\t")
sys.stdout.flush()
try:
kwargs = dict(env=env)
if not binary:
kwargs = dict(kwargs, encoding="utf-8")
if include_stderr:
kwargs = dict(kwargs, stderr=subprocess.STDOUT)
if cwd:
kwargs = dict(kwargs, cwd=cwd)
if not force_subprocess:
# In this case, run the Codalab CLI directly, which is much faster
# than opening a new subprocess to do so.
stderr = io.StringIO() # Not used; we just don't want to redirect cli.stderr to stdout.
stdout = FakeStdout()
cli = BundleCLI(CodaLabManager(), stdout=stdout, stderr=stderr)
try:
cli.do_command(args[1:])
exitcode = 0
except SystemExit as e:
exitcode = e.code
output = stdout.getvalue()
else:
output = subprocess.check_output([a.encode() for a in args], **kwargs)
exitcode = 0
except subprocess.CalledProcessError as e:
output = e.output
exitcode = e.returncode
except Exception:
output = traceback.format_exc()
exitcode = 1
if expected_exit_code is not None and exitcode != expected_exit_code:
colorize = Colorizer.red
extra = ' BAD'
else:
colorize = Colorizer.cyan
extra = ''
print(
colorize(" (exit code %s, expected %s%s)" % (exitcode, expected_exit_code, extra)).strip(),
end="\t",
)
print(sanitize(output, max_output_chars).strip())
sys.stdout.flush()
assert (
expected_exit_code == exitcode
), f'Exit codes don\'t match: got {exitcode}, expected {expected_exit_code}'
return output.rstrip()
def cleanup(cl, tag, should_wait=True):
'''
Removes all bundles and worksheets with the specified tag.
:param cl: str
Path to CodaLab command line.
:param tag: str
Specific tag use to search for bundles and worksheets to delete.
:param should_wait: boolean
Whether to wait for a bundle to finish running before deleting (default is true).
:return:
'''
print('Cleaning up bundles and worksheets tagged with {}...'.format(tag))
# Clean up tagged bundles
bundles_removed = 0
while True:
# Query 1000 bundles at a time for removal
query_result = run_command([cl, 'search', 'tags=%s' % tag, '.limit=1000', '--uuid-only'])
if len(query_result) == 0:
break
for uuid in query_result.split('\n'):
if should_wait:
# Wait until the bundle finishes and then delete it
run_command([cl, 'wait', uuid])
run_command([cl, 'rm', uuid, '--force'])
bundles_removed += 1
# Clean up tagged worksheets
worksheets_removed = 0
while True:
query_result = run_command([cl, 'wsearch', 'tag=%s' % tag, '.limit=1000', '--uuid-only'])
if len(query_result) == 0:
break
for uuid in query_result.split('\n'):
run_command([cl, 'wrm', uuid, '--force'])
worksheets_removed += 1
print('Removed {} bundles and {} worksheets.'.format(bundles_removed, worksheets_removed))
class Timer:
"""
Class that uses signal to interrupt functions while they're running
if they run for longer than timeout_seconds.
Can also be used to time how long functions take within its context manager.
Used for the timing tests.
"""
def __init__(self, timeout_seconds=1, handle_timeouts=True, uuid=None):
"""
A class that can be used as a context manager to ensure that code within that context manager times out
after timeout_seconds time and which times the execution of code within the context manager.
Parameters:
timeout_seconds (float): Amount of time before execution in context manager is interrupted for timeout
handle_timeouts (bool): If True, do not timeout, only return the time taken for execution in context manager.
uuid (str): Uuid of bundles running within context manager.
"""
self.handle_timeouts = handle_timeouts
self.timeout_seconds = timeout_seconds
self.uuid = None
def handle_timeout(self, signum, frame):
timeout_message = "Timeout ocurred"
if self.uuid:
timeout_message += " while waiting for %s to run" % self.uuid
raise TimeoutError(timeout_message)
def time_elapsed(self):
return time.time() - self.start_time
def __enter__(self):
self.start_time = time.time()
if self.handle_timeouts:
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.timeout_seconds)
def __exit__(self, type, value, traceback):
self.time_elapsed = time.time() - self.start_time
if self.handle_timeouts:
signal.alarm(0)