-
Notifications
You must be signed in to change notification settings - Fork 23
/
rtest.py
402 lines (360 loc) · 14.2 KB
/
rtest.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/python3
"""Copyright (C) 2021 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop-
ies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE-
CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Run tests on build"""
import re
import os
import sys
import subprocess
import shlex
import argparse
import pathlib
import platform
from genericpath import exists
from fnmatch import fnmatchcase
from xml.dom import minidom
import multiprocessing
import time
SCRIPT_VERSION = 0.1
args = {}
OS_info = {}
timeout = False
test_proc = None
stop = 0
fail_regex = r'error|fail'
test_script = [ 'cd %IDIR%', '%XML%' ]
class ArgAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs != 2:
raise ValueError("nargs must be 2")
super().__init__(option_strings, dest, nargs, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
old = getattr(namespace, self.dest, {})
if old is None:
new = {values[0]: values[1]}
else:
new = {**old, values[0]: values[1]}
setattr(namespace, self.dest, new)
def parse_args():
"""Parse command-line arguments"""
parser = argparse.ArgumentParser(description="""
Checks build arguments
""")
parser.add_argument('-t', '--test', required=True, type=str, action='append',
help='Test set to run from rtest.xml (required, e.g. osdb)')
parser.add_argument('-g', '--debug', required=False, default=False, action='store_true',
help='Test Debug build (optional, default: false)')
parser.add_argument('-o', '--output', type=str, required=False, default="xml",
help='Test output file (optional, default: test_detail.xml)')
parser.add_argument('-a', '--argument', action=ArgAction, nargs=2, metavar=('NAME', 'VALUE'), default={},
help='Arguments to substitute into the xml file (optional, multiple)')
parser.add_argument( '--install_dir', type=str, required=False, default="build",
help='Installation directory where build or release folders are (optional, default: build)')
parser.add_argument( '--fail_test', default=False, required=False, action='store_true',
help='Return as if test failed (optional, default: false)')
# parser.add_argument('-v', '--verbose', required=False, default = False, action='store_true',
# help='Verbose install (optional, default: False)')
return parser.parse_args()
def vram_detect():
global OS_info
OS_info["VRAM"] = 0
if os.name == "nt":
cmd = "hipinfo.exe"
process = subprocess.run([cmd], stdout=subprocess.PIPE)
for line_in in process.stdout.decode().splitlines():
if 'totalGlobalMem' in line_in:
OS_info["VRAM"] = float(line_in.split()[1])
break
else:
cmd = "rocminfo"
process = subprocess.run([cmd], stdout=subprocess.PIPE)
for line_in in process.stdout.decode().splitlines():
match = re.search(r'.*Size:.*([0-9]+)\(.*\).*KB', line_in, re.IGNORECASE)
if match:
OS_info["VRAM"] = float(match.group(1))/(1024*1024)
break
def os_detect():
global OS_info
if os.name == "nt":
OS_info["ID"] = platform.system()
else:
inf_file = "/etc/os-release"
if os.path.exists(inf_file):
with open(inf_file) as f:
for line in f:
if "=" in line:
k,v = line.strip().split("=")
OS_info[k] = v.replace('"','')
OS_info["NUM_PROC"] = os.cpu_count()
vram_detect()
print(OS_info)
def create_dir(dir_path):
if os.path.isabs(dir_path):
full_path = dir_path
else:
full_path = os.path.join( os.getcwd(), dir_path )
return pathlib.Path(full_path).mkdir(parents=True, exist_ok=True)
def delete_dir(dir_path) :
if (not os.path.exists(dir_path)):
return
if os.name == "nt":
return run_cmd( "RMDIR" , f"/S /Q {dir_path}")
else:
linux_path = pathlib.Path(dir_path).absolute()
return run_cmd( "rm" , f"-rf {linux_path}")
class TimerProcess(multiprocessing.Process):
def __init__(self, start, stop, kill_pid):
multiprocessing.Process.__init__(self)
self.quit = multiprocessing.Event()
self.timed_out = multiprocessing.Event()
self.start_time = start
self.max_time = stop
self.kill_pid = kill_pid
def run(self):
while not self.quit.is_set():
#print( f'time_stop {self.start_time} limit {self.max_time}')
if (self.max_time == 0):
return
t = time.monotonic()
if ( t - self.start_time > self.max_time ):
print( f'killing {self.kill_pid} t {t}')
if os.name == "nt":
cmd = ['TASKKILL', '/F', '/T', '/PID', str(self.kill_pid)]
proc = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
else:
os.kill(self.kill_pid, signal.SIGKILL)
self.timed_out.set()
self.stop()
pass
def stop(self):
self.quit.set()
def stopped(self):
return self.timed_out.is_set()
def time_stop(start, pid):
global timeout, stop
while (True):
print( f'time_stop {start} limit {stop}')
t = time.monotonic()
if (stop == 0):
return
if ( (stop > 0) and (t - start > stop) ):
print( f'killing {pid} t {t}')
if os.name == "nt":
cmd = ['TASKKILL', '/F', '/T', '/PID', str(pid)]
proc = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
else:
test_proc.kill()
timeout = True
stop = 0
time.sleep(0)
def find_cmd(cmd):
if os.name == "nt":
status, _ = subprocess.getstatusoutput(f"where {cmd}")
if status != 0:
raise RuntimeError(f"Cannot find the command or executable {cmd}")
return cmd
else:
status, _ = subprocess.getstatusoutput(f"which {cmd}")
if status == 0:
return cmd
search_paths = [
"."
]
for path_option in search_paths:
cmd_opt = f"{path_option}/{cmd}"
if os.path.isfile(cmd_opt) and os.access(cmd_opt, os.X_OK):
return cmd_opt
raise RuntimeError(f"Cannot find the command or executable {cmd}")
def run_cmd(cmd, test = False, time_limit = 0):
global args
global test_proc, timer_thread
global stop
if (cmd.startswith('cd ')):
return os.chdir(cmd[3:])
if (cmd.startswith('mkdir ')):
return create_dir(cmd[6:])
cmdline = f"{cmd}"
print(cmdline)
try:
if not test:
proc = subprocess.run(cmdline, check=True, stderr=subprocess.STDOUT, shell=True)
status = proc.returncode
else:
error = False
timeout = False
cmd_parts = shlex.split(cmdline)
cmdline = find_cmd(cmd_parts[0]) + " " + cmd[len(cmd_parts[0]):]
test_proc = subprocess.Popen(cmdline, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
if time_limit > 0:
start = time.monotonic()
#p = multiprocessing.Process(target=time_stop, args=(start, test_proc.pid))
p = TimerProcess(start, time_limit, test_proc.pid)
p.start()
while True:
output = test_proc.stdout.readline()
if output == '' and test_proc.poll() is not None:
break
elif output:
outstring = output.strip()
print (outstring)
error = error or re.search(fail_regex, outstring, re.IGNORECASE)
status = test_proc.poll()
if time_limit > 0:
p.stop()
p.join()
timeout = p.stopped()
print(f"timeout {timeout}")
if error:
status = 1
elif timeout:
status = 2
else:
status = test_proc.returncode
except:
import traceback
exc = traceback.format_exc()
print( "Python Exception: {0}".format(exc) )
status = 3
return status
def batch(script, xml):
global OS_info
global args
global fail_regex
#
cwd = pathlib.os.curdir
rtest_cwd_path = os.path.abspath( os.path.join( cwd, 'rtest.xml') )
if os.path.isfile(rtest_cwd_path) and os.path.dirname(rtest_cwd_path).endswith( "staging" ):
# if in a staging directory then test locally
test_dir = cwd
else:
build_type = "debug" if args.debug else "release"
search_paths = [
f"{args.install_dir}//{build_type}//clients//staging",
f"{args.install_dir}//{build_type}//test",
f"{args.install_dir}"
]
test_dir = ""
for path_option in search_paths:
if os.path.exists(path_option):
test_dir = path_option
break
if test_dir == "":
print(f"ERROR: Could not determine a valid test directory. Checked {', '.join(search_paths)}.")
return 2
fail = False
for i in range(len(script)):
cmdline = script[i]
xcmd = cmdline.replace('%IDIR%', test_dir)
cmd = xcmd.replace('%ODIR%', args.output)
if cmd.startswith('tdir '):
if pathlib.Path(cmd[5:]).exists():
return 0 # all further cmds skipped
else:
continue
error = False
if cmd.startswith('%XML%'):
fileversion = xml.getElementsByTagName('fileversion')
if len(fileversion) == 0:
print("INFO: Could not find the version of this xml configuration file. Version 0.1 assumed.")
elif len(fileversion) > 1:
print("WARNING: Multiple version tags found.")
else:
version = float(fileversion[0].firstChild.data)
if version > SCRIPT_VERSION:
print(f"ERROR: This file requires script version >= {version}, have version {SCRIPT_VERSION}")
exit(1)
if xml.documentElement.hasAttribute('failure-regex'):
fail_regex = xml.documentElement.getAttribute('failure-regex')
# run the matching tests listed in the xml test file
var_subs = {}
for var in xml.getElementsByTagName('var'):
name = var.getAttribute('name')
if var.hasAttribute('value'):
val = var.getAttribute('value')
elif var.firstChild is not None:
val = var.firstChild.data
else:
val = ""
var_subs[name] = val
for name, val in args.argument.items():
var_subs[name] = val
for test in xml.getElementsByTagName('test'):
sets = test.getAttribute('sets')
runset = sets.split(',')
if len([x for x in args.test if x in runset]):
for run in test.getElementsByTagName('run'):
name = run.getAttribute('name')
vram_limit = run.getAttribute('vram_min')
if vram_limit:
if OS_info["VRAM"] < float(vram_limit):
print( f'***\n*** Skipped: {name} due to VRAM req.\n***')
continue
if name:
print( f'***\n*** Running: {name}\n***')
time_limit = run.getAttribute('time_max')
if time_limit:
timeout = float(time_limit)
else:
timeout = 0
raw_cmd = run.firstChild.data
var_cmd = raw_cmd.format_map(var_subs)
error = run_cmd(var_cmd, True, timeout)
if (error == 2):
print( f'***\n*** Timed out when running: {name}\n***')
continue
else:
error = run_cmd(cmd)
fail = fail or error
if (fail):
if (cmd == "%XML%"):
print("FAILED xml test suite!")
else:
print(f"ERROR running: {cmd}")
if (os.curdir != cwd):
os.chdir( cwd )
return 1
if (os.curdir != cwd):
os.chdir( cwd )
return 0
def run_tests():
global test_script
global xmlDoc
# install
cwd = os.curdir
xmlPath = os.path.join( cwd, 'rtest.xml')
xmlDoc = minidom.parse( xmlPath )
scripts = []
scripts.append( test_script )
for i in scripts:
if (batch(i, xmlDoc)):
#print("Failure in script. ABORTING")
if (os.curdir != cwd):
os.chdir( cwd )
return 1
if (os.curdir != cwd):
os.chdir( cwd )
return 0
def main():
global args
global timer_thread
os_detect()
args = parse_args()
status = run_tests()
if args.fail_test: status = 1
if (status):
sys.exit(status)
if __name__ == '__main__':
main()