-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathppybot.py
436 lines (338 loc) · 14.2 KB
/
ppybot.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import sys
import os
import time
import subprocess
import ppybot_config as config
import re
import xunitparser
from os import listdir
from os.path import isfile, join
from optparse import OptionParser
# encapsulate a running pybot process
class Pybot():
pid = None
process = None
started = False
ini_time = None
end_time = None
outFile = None
logFile = None
xunitFile = None
test_result = None
test_suite = None
attempt = 0
def __init__(self, folder, filename, options):
self.folder = folder
self.filename = filename
self.name = os.path.splitext(filename)[0]
self.options = options
self.outFile = "%s.out.xml" % os.path.join(options.logs_folder, self.name)
self.logFile = "%s.log" % os.path.join(options.logs_folder, self.name)
self.xunitFile = "%s.xunit.xml" % os.path.join(options.logs_folder, self.name)
def __del__(self):
self._on_finish()
def __str__(self):
if self.pid == None:
return "pybot %s" % (self.name)
else:
return "pybot %s (pid: %d)" % (self.name, self.pid)
def _init(self):
self.end_time = None
self.test_result = None
self.test_suite = None
def start(self, runfailed=False):
self._init()
commands = []
commands.append(self.options.pybot_cmd)
commands.append("-x")
commands.append(self.xunitFile)
commands.append("-l")
commands.append("NONE")
commands.append("-r")
commands.append("NONE")
if runfailed == True:
commands.append("--rerunfailed")
commands.append(self.outFile)
self.outFileRunfailed = "%s.rerun.%d.xml" % (os.path.join(self.options.logs_folder, self.name), self.attempt)
commands.append("-o")
commands.append(self.outFileRunfailed)
else:
commands.append("-o")
commands.append(self.outFile)
update_options(commands, self.options.pybot_opts)
commands.append(self.filename)
self.log = open(self.logFile, "w")
self.process = subprocess.Popen(commands, cwd=self.folder, stdout=self.log, stderr=self.log)
self.pid = self.process.pid
if self.options.verbose: print str(self)+" started!"
self.started = True
self.ini_time = time.time()
def elapsed(self):
if self.end_time == None:
if self.ini_time == None:
return 0
else:
return time.time() - self.ini_time
else:
return self.end_time - self.ini_time
def wait(self, timeout):
self.process.wait()
self._on_finish();
def isRunning(self):
running = self.process != None and self.process.poll() == None
if not running:
if self.options.verbose: print str(self)+" finished!"
self._on_finish();
return running
def isDone(self):
return self.started == True and (not self.isRunning())
def kill(self):
if self.isRunning():
self.process.kill()
self.process.terminate()
self.process.wait()
self.process = None
self._on_finish()
print "%s was killed after %d elapsed seconds" % (str(self), self.elapsed())
def _on_finish(self):
try:
self._on_finish_unsafe()
except:
None
def _on_finish_unsafe(self):
if hasattr(self, 'log') and self.log != None:
self.log.close()
self.log=None
if self.end_time == None:
self.end_time = time.time()
self.pid = None
self.attempt = self.attempt + 1
self.load_xunit_results()
def load_xunit_results(self):
if self.xunitFile != None and os.path.isfile(self.xunitFile):
if xunitparser != None:
with open(self.xunitFile, 'r') as xunit:
self.test_suite, self.test_result = xunitparser.parse(xunit)
def was_successful(self):
if self.test_result == None:
return False
else:
return len(self.test_result.errors) == 0 and len(self.test_result.failures) == 0
def output_file(self):
return self.outFile
def attempts(self):
return self.attempt
#
# support functions
#
def update_options(commands, optstring):
if optstring != None:
opts = optstring.split()
for opt in opts:
commands.append(opt)
def isWindows():
return sys.platform.startswith("win")
def run_bots(title, options, folder, bot_before, bot_after, bots, run_failed=False):
torun_bots = remove_successful_bots(bots)
if len(torun_bots) == 0:
return
print "\n\n================================================================================="
print "Starting bots! Session: "+title
print
print "- source folder: "+folder
print "- results folder: "+options.logs_folder
print "- max parallel pybots (processes): "+str(options.max_parallel_tests)
print "- max time allowed per bot (seconds): "+str(options.max_execution_time)
if options.max_execution_time_total:
print "- max total time of execution (seconds): "+str(options.max_execution_time_total)
print "\n=================================================================================\n"
if (bot_before != None):
print "Running BEFORE fixture..."
bot_before.start()
bot_before.wait(60)
print "Done!\n"
start_time=time.time()
last_dump=time.time()
done_bots = []
all_bots = torun_bots[:]
running_bots = []
while len(all_bots) > 0 or len(running_bots) > 0:
time.sleep(1)
for bot in running_bots:
if bot.isDone():
running_bots.remove(bot)
done_bots.append(bot)
if bot.elapsed() > options.max_execution_time:
running_bots.remove(bot)
bot.kill()
if time.time() > last_dump + options.advertise_time:
last_dump = time.time()
dump_bots(running_bots, all_bots, done_bots)
if options.max_execution_time_total and (time.time() > start_time + options.max_execution_time_total):
for bot in running_bots:
bot.kill()
running_bots = []
break
if len(running_bots) == options.max_parallel_tests:
continue
if len(all_bots) > 0:
bot = all_bots[0];
all_bots.remove(bot)
running_bots.append(bot)
bot.start(run_failed)
dump_bots(running_bots, all_bots, done_bots)
if (bot_after != None):
print "Running AFTER fixture..."
bot_after.start()
bot_after.wait(60)
print "Done!\n"
return done_bots
def remove_successful_bots(bots):
torun_bots = []
for bot in bots:
if not bot.was_successful():
torun_bots.append(bot)
return torun_bots
def get_bot_results(bot):
tc = bot.test_result
if tc == None:
return "not executed"
else:
return "%2.2d/%2.2d" % (tc.testsRun-len(tc.errors)-len(tc.failures), tc.testsRun)
def dump_bots(running_bots, queued_bots, done_bots):
print "\n-------------------------------------------------"
if len(running_bots) > 0:
print "Running tests:"
for bot in running_bots:
print "- %s since %d seconds ago" % (str(bot), bot.elapsed())
if len(queued_bots) > 0:
print "Queued tests:"
for bot in queued_bots:
print "- %s " % (str(bot))
if len(done_bots) > 0:
print "Done tests: (success/total)"
for bot in done_bots:
result = "" if bot.was_successful() else "FAILED"
print "- %s %s (elapsed: %d seconds) %s" % (get_bot_results(bot), str(bot), bot.elapsed(), result)
print "-------------------------------------------------\n"
def parse_args():
usage = "usage: %prog [options] folder_with_tests"
parser = OptionParser(usage=usage)
parser.add_option("-t", "--timeout", dest="max_execution_time", type="int", default=config.MAX_EXECUTION_TIME,
help="maximum time in seconds spent in executing one test before aborting", metavar="SECONDS")
parser.add_option("--timeout-total", dest="max_execution_time_total", type="int", default=config.MAX_EXECUTION_TIME_TOTAL,
help="maximum time in seconds spent in executing all tests", metavar="SECONDS")
parser.add_option("-r", "--reruns", dest="max_attempts", type="int", default=config.FAILED_RERUNS,
help="maximum number of reruns executed on failed tests", metavar="NUM")
parser.add_option("-p", "--parallels", dest="max_parallel_tests", type="int", default=config.MAX_PARALLEL_TESTS,
help="maximum number of parallel test running at the same time", metavar="NUM")
parser.add_option("-l", "--logs", dest="logs_folder", default=".",
help="folder where logs are generated, must exist", metavar="FOLDER")
parser.add_option("-a", "--advertise", dest="advertise_time", default=config.ADVERTISE_TIME, type="int",
help="the seconds to elapse before advertising the current status", metavar="SECONDS")
parser.add_option("--pybot", dest="pybot_cmd", default=config.PYBOT_CMD,
help="the command to execute to invoke pybot i.e. /usr/local/bin/pybot", metavar="PATH")
parser.add_option("--rebot", dest="rebot_cmd", default=config.REBOT_CMD,
help="the command to execute to invoke pybot i.e. /usr/local/bin/rebot", metavar="PATH")
parser.add_option("-f", "--failed-only", action="store_true", dest="failed_only", default=False, help="runs only the failed tests")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose mode, more details about wassup")
parser.add_option("-x", "--no-fixtures", action="store_true", dest="no_fixtures", default=False, help="no fixture code will be run before / after executions")
parser.add_option("-s", "--select-from", dest="tests_file", type="string", help="select the files that will be run from a file")
parser.add_option("--pybot-opts", dest="pybot_opts", default=None, help="additional commands to send to pybot")
parser.add_option("--rebot-opts", dest="rebot_opts", default=None, help="additional commands to send to rebot")
return parser.parse_args()
def makeFixtureBot(folder, filename, options):
if isfile(join(folder,filename)):
return Pybot(folder, filename, options)
else:
return None
def runtime_okay(options):
return is_executable(options.pybot_cmd) and is_executable(options.rebot_cmd)
def is_executable(runtime):
try:
commands = []
commands.append(runtime)
commands.append("--version")
process = subprocess.Popen(commands)
process.wait()
return True
except:
print "Cannot execute "+runtime+" - please check your configuration / parameters"
return False
def get_test_files(folder, options):
if options.tests_file != None:
tests = [line.rstrip('\n') for line in open(options.tests_file)]
elif config.TEST_REGEXP != None:
tests = [f for f in listdir(folder) if isfile(join(folder, f)) and re.match(config.TEST_REGEXP, f)]
else:
tests = []
return tests
#
# Main execution script
#
def main():
(options, args) = parse_args()
if len(args) == 0:
print "A destination folder must be specified!"
os.abort()
if runtime_okay(options) == False:
print "The runtime is not correctly configured - I cannot run :("
os.abort()
else:
if options.verbose: print "Runtime succesfully verified \n"
folder = args[0]
if options.logs_folder == ".":
options.logs_folder = folder
tests = get_test_files(folder, options)
if len(tests) == 0:
print "No tests found or selected"
os.abort()
all_bots = []
if options.verbose: print "Tests found:"
for test in tests:
all_bots.append(Pybot(folder, test, options))
if options.verbose: print "- "+test;
if options.no_fixtures == False:
bot_before = makeFixtureBot(folder,config.BEFORE_RUN, options)
bot_after = makeFixtureBot(folder,config.AFTER_RUN, options)
else:
bot_before = None
bot_after = None
start_time = time.time()
if options.failed_only == False:
run_bots("main", options, folder, bot_before, bot_after, all_bots, False)
else:
for bot in all_bots:
bot.load_xunit_results()
all_bots = remove_successful_bots(all_bots)
for i in range (1, 1+options.max_attempts):
run_bots("failed #"+str(i), options, folder, bot_before, bot_after, all_bots, True)
print "\n\n================================================================================="
print "TEST FINISHED (elapsed: %d seconds)" % (time.time() - start_time)
print
for bot in all_bots:
result = " ok " if bot.was_successful() else "FAIL"
print "- %s %s %s (attempts %d, elapsed %d seconds)" % (result, get_bot_results(bot), str(bot), bot.attempts(), bot.elapsed())
print "\n=================================================================================\n"
if options.failed_only == False:
print "\nNow running rebot..."
commands = []
commands.append(options.rebot_cmd)
commands.append("--name")
commands.append(config.REPORT_NAME)
commands.append("--output")
commands.append(config.REPORT_FILENAME)
commands.append("*.out.xml")
update_options(commands, options.rebot_opts)
cli = ""
for cmd in commands:
cli = cli + cmd + " "
print cli
print
subprocess.call(commands, cwd=options.logs_folder)
print "\nDone!"
#
# script launch support
#
if __name__ == "__main__":
main()