3 # Parallel VM test case executor
4 # Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
17 logger = logging.getLogger()
19 # Test cases that take significantly longer time to execute than average.
20 long_tests = [ "ap_roam_open",
21 "wpas_mesh_password_mismatch_retry",
22 "wpas_mesh_password_mismatch",
23 "hostapd_oom_wpa2_psk_connect",
24 "ap_hs20_fetch_osu_stop",
27 "nfc_wps_er_handover_pk_hash_mismatch_sta",
28 "go_neg_peers_force_diff_freq",
31 "ap_pmf_sta_unprot_deauth_burst",
32 "ap_bss_add_remove_during_ht_scan",
34 "autoscan_exponential",
37 "ap_inactivity_disconnect",
38 "scan_bss_expiration_age",
40 "discovery_group_client",
46 "ap_ht_40mhz_intolerant_ap",
48 "discovery_pd_retries",
49 "ap_wps_setup_locked_timeout",
54 "grpform_cred_ready_timeout",
55 "hostapd_oom_wpa2_eap_connect",
58 "hostapd_oom_wpa2_eap",
60 "proxyarp_open_ebtables",
62 "obss_scan_40_intolerant",
66 "ap_wps_iteration_error",
68 "ap_wps_http_timeout",
69 "p2p_go_move_reg_change",
72 "p2p_go_move_scm_peer_supports",
73 "p2p_go_move_scm_peer_does_not_support",
74 "p2p_go_move_scm_multi" ]
78 for i in range(num_servers):
79 failed += vm[i]['failed']
82 def vm_read_stdout(vm, i):
83 global total_started, total_passed, total_failed, total_skipped
88 out = vm['proc'].stdout.read()
91 logger.debug("VM[%d] stdout.read[%s]" % (i, out))
92 pending = vm['pending'] + out
95 pos = pending.find('\n')
98 line = pending[0:pos].rstrip()
99 pending = pending[(pos + 1):]
100 logger.debug("VM[%d] stdout full line[%s]" % (i, line))
101 if line.startswith("READY"):
103 elif line.startswith("PASS"):
106 elif line.startswith("FAIL"):
109 vals = line.split(' ')
111 logger.info("VM[%d] incomplete FAIL line: %s" % (i, line))
115 logger.debug("VM[%d] test case failed: %s" % (i, name))
116 vm['failed'].append(name)
117 elif line.startswith("NOT-FOUND"):
120 logger.info("VM[%d] test case not found" % i)
121 elif line.startswith("SKIP"):
124 elif line.startswith("START"):
126 if len(vm['failed']) == 0:
127 vals = line.split(' ')
129 vm['fail_seq'].append(vals[1])
130 vm['out'] += line + '\n'
132 vm['pending'] = pending
135 def show_progress(scr):
141 global first_run_failures
142 global total_started, total_passed, total_failed, total_skipped
144 total_tests = len(tests)
145 logger.info("Total tests: %d" % total_tests)
148 scr.addstr(0, 0, "Parallel test execution status", curses.A_BOLD)
149 for i in range(0, num_servers):
150 scr.addstr(i + 1, 0, "VM %d:" % (i + 1), curses.A_BOLD)
151 scr.addstr(i + 1, 10, "starting VM")
152 scr.addstr(num_servers + 1, 0, "Total:", curses.A_BOLD)
153 scr.addstr(num_servers + 1, 20, "TOTAL={} STARTED=0 PASS=0 FAIL=0 SKIP=0".format(total_tests))
156 completed_first_pass = False
161 first_running = False
164 for i in range(0, num_servers):
165 if completed_first_pass:
167 if vm[i]['first_run_done']:
169 if not vm[i]['proc']:
171 if vm[i]['proc'].poll() is not None:
175 log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
176 with open(log, 'r') as f:
177 if "Kernel panic" in f.read():
178 scr.addstr("kernel panic")
179 logger.info("VM[%d] kernel panic" % i)
181 scr.addstr("unexpected exit")
182 logger.info("VM[%d] unexpected exit" % i)
189 err = vm[i]['proc'].stderr.read()
191 logger.debug("VM[%d] stderr.read[%s]" % (i, err))
195 if vm_read_stdout(vm[i], i):
200 vm[i]['first_run_done'] = True
201 scr.addstr("completed first round")
202 logger.info("VM[%d] completed first round" % i)
206 vm[i]['proc'].stdin.write(name + '\n')
208 logger.debug("VM[%d] start test %s" % (i, name))
210 if not first_running and not completed_first_pass:
211 logger.info("First round of testing completed")
213 logger.info("Unexpected test cases remaining from first round: " + str(tests))
214 raise Exception("Unexpected test cases remaining from first round")
215 completed_first_pass = True
216 for name in get_failed(vm):
218 rerun_tests.append(name)
219 first_run_failures.append(name)
221 for i in range(num_servers):
222 if not completed_first_pass:
224 if not vm[i]['proc']:
226 if vm[i]['proc'].poll() is not None:
230 log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
231 with open(log, 'r') as f:
232 if "Kernel panic" in f.read():
233 scr.addstr("kernel panic")
234 logger.info("VM[%d] kernel panic" % i)
236 scr.addstr("completed run")
237 logger.info("VM[%d] completed run" % i)
243 err = vm[i]['proc'].stderr.read()
245 logger.debug("VM[%d] stderr.read[%s]" % (i, err))
250 if vm[i]['first_run_done']:
251 vm[i]['first_run_done'] = False
254 ready = vm_read_stdout(vm[i], i)
260 vm[i]['proc'].stdin.write('\n')
261 scr.addstr("shutting down")
262 logger.info("VM[%d] shutting down" % i)
264 name = rerun_tests.pop(0)
265 vm[i]['proc'].stdin.write(name + '\n')
266 scr.addstr(name + "(*)")
267 logger.debug("VM[%d] start test %s (*)" % (i, name))
273 scr.move(num_servers + 1, 10)
275 scr.addstr("{} %".format(int(100.0 * (total_passed + total_failed + total_skipped) / total_tests)))
276 scr.addstr(num_servers + 1, 20, "TOTAL={} STARTED={} PASS={} FAIL={} SKIP={}".format(total_tests, total_started, total_passed, total_failed, total_skipped))
277 failed = get_failed(vm)
279 scr.move(num_servers + 2, 0)
281 scr.addstr("Failed test cases: ")
295 scr.addstr("(RETRY FAILED %d)" % len(rerun_tests))
298 elif first_run_failures:
299 scr.addstr("(RETRY FAILED)")
316 global first_run_failures
317 global total_started, total_passed, total_failed, total_skipped
318 global rerun_failures
325 debug_level = logging.INFO
326 rerun_failures = True
327 timestamp = int(time.time())
329 scriptsdir = os.path.dirname(os.path.realpath(sys.argv[0]))
331 p = argparse.ArgumentParser(description='run multiple testing VMs in parallel')
332 p.add_argument('num_servers', metavar='number of VMs', type=int, choices=range(1, 100),
333 help="number of VMs to start")
334 p.add_argument('-f', dest='testmodules', metavar='<test module>',
335 help='execute only tests from these test modules',
337 p.add_argument('-1', dest='no_retry', action='store_const', const=True, default=False,
338 help="don't retry failed tests automatically")
339 p.add_argument('--debug', dest='debug', action='store_const', const=True, default=False,
340 help="enable debug logging")
341 p.add_argument('--codecov', dest='codecov', action='store_const', const=True, default=False,
342 help="enable code coverage collection")
343 p.add_argument('--shuffle-tests', dest='shuffle', action='store_const', const=True, default=False,
344 help="shuffle test cases to randomize order")
345 p.add_argument('--short', dest='short', action='store_const', const=True,
347 help="only run short-duration test cases")
348 p.add_argument('--long', dest='long', action='store_const', const=True,
350 help="include long-duration test cases")
351 p.add_argument('--valgrind', dest='valgrind', action='store_const',
352 const=True, default=False,
353 help="run tests under valgrind")
354 p.add_argument('params', nargs='*')
355 args = p.parse_args()
357 dir = os.environ.get('HWSIM_TEST_LOG_DIR', '/tmp/hwsim-test-logs')
363 num_servers = args.num_servers
364 rerun_failures = not args.no_retry
366 debug_level = logging.DEBUG
369 extra_args += [ '--valgrind' ]
371 extra_args += [ '--long' ]
373 print "Code coverage - build separate binaries"
374 logdir = os.path.join(dir, str(timestamp))
376 subprocess.check_call([os.path.join(scriptsdir, 'build-codecov.sh'),
378 codecov_args = ['--codecov_dir', logdir]
384 first_run_failures = []
389 cmd = [ os.path.join(os.path.dirname(scriptsdir), 'run-tests.py'),
393 cmd += args.testmodules
394 lst = subprocess.Popen(cmd, stdout=subprocess.PIPE)
395 for l in lst.stdout.readlines():
396 name = l.split(' ')[0]
399 sys.exit("No test cases selected")
402 from random import shuffle
404 elif num_servers > 2 and len(tests) > 100:
405 # Move test cases with long duration to the beginning as an
406 # optimization to avoid last part of the test execution running a long
407 # duration test case on a single VM while all other VMs have already
408 # completed their work.
414 tests = [t for t in tests if t not in long_tests]
416 logger.setLevel(debug_level)
417 log_handler = logging.FileHandler('parallel-vm.log')
418 log_handler.setLevel(debug_level)
419 fmt = "%(asctime)s %(levelname)s %(message)s"
420 log_formatter = logging.Formatter(fmt)
421 log_handler.setFormatter(log_formatter)
422 logger.addHandler(log_handler)
425 for i in range(0, num_servers):
426 print("\rStarting virtual machine {}/{}".format(i + 1, num_servers)),
427 logger.info("Starting virtual machine {}/{}".format(i + 1, num_servers))
428 cmd = [os.path.join(scriptsdir, 'vm-run.sh'), '--delay', str(i),
429 '--timestamp', str(timestamp),
430 '--ext', 'srv.%d' % (i + 1),
431 '-i'] + codecov_args + extra_args
433 vm[i]['first_run_done'] = False
434 vm[i]['proc'] = subprocess.Popen(cmd,
435 stdin=subprocess.PIPE,
436 stdout=subprocess.PIPE,
437 stderr=subprocess.PIPE)
439 vm[i]['pending'] = ""
442 vm[i]['fail_seq'] = []
443 for stream in [ vm[i]['proc'].stdout, vm[i]['proc'].stderr ]:
445 fl = fcntl.fcntl(fd, fcntl.F_GETFL)
446 fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
449 curses.wrapper(show_progress)
451 with open('{}/{}-parallel.log'.format(dir, timestamp), 'w') as f:
452 for i in range(0, num_servers):
453 f.write('VM {}\n{}\n{}\n'.format(i, vm[i]['out'], vm[i]['err']))
455 failed = get_failed(vm)
457 if first_run_failures:
458 print "To re-run same failure sequence(s):"
459 for i in range(0, num_servers):
460 if len(vm[i]['failed']) == 0:
465 skip = len(vm[i]['fail_seq'])
466 skip -= min(skip, 30)
467 for t in vm[i]['fail_seq']:
473 print "Failed test cases:"
474 for f in first_run_failures:
476 logger.info("Failed: " + f)
480 double_failed.append(name)
481 for test in first_run_failures:
482 double_failed.remove(test)
483 if not rerun_failures:
485 elif failed and not double_failed:
486 print "All failed cases passed on retry"
487 logger.info("All failed cases passed on retry")
489 print "Failed even on retry:"
490 for f in double_failed:
492 logger.info("Failed on retry: " + f)
494 res = "TOTAL={} PASS={} FAIL={} SKIP={}".format(total_started,
500 print "Logs: " + dir + '/' + str(timestamp)
501 logger.info("Logs: " + dir + '/' + str(timestamp))
503 for i in range(0, num_servers):
504 if len(vm[i]['pending']) > 0:
505 logger.info("Unprocessed stdout from VM[%d]: '%s'" %
506 (i, vm[i]['pending']))
507 log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
508 with open(log, 'r') as f:
509 if "Kernel panic" in f.read():
510 print "Kernel panic in " + log
511 logger.info("Kernel panic in " + log)
514 print "Code coverage - preparing report"
515 for i in range(num_servers):
516 subprocess.check_call([os.path.join(scriptsdir,
517 'process-codecov.sh'),
518 logdir + ".srv.%d" % (i + 1),
520 subprocess.check_call([os.path.join(scriptsdir, 'combine-codecov.sh'),
522 print "file://%s/index.html" % logdir
523 logger.info("Code coverage report: file://%s/index.html" % logdir)
525 if double_failed or (failed and not rerun_failures):
526 logger.info("Test run complete - failures found")
529 logger.info("Test run complete - failures found on first run; passed on retry")
531 logger.info("Test run complete - no failures")
534 if __name__ == "__main__":