parallel-vm.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #!/usr/bin/env python2
  2. #
  3. # Parallel VM test case executor
  4. # Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import curses
  9. import fcntl
  10. import logging
  11. import os
  12. import subprocess
  13. import sys
  14. import time
  15. logger = logging.getLogger()
  16. def get_failed(vm):
  17. failed = []
  18. for i in range(num_servers):
  19. failed += vm[i]['failed']
  20. return failed
  21. def vm_read_stdout(vm, i):
  22. global total_started, total_passed, total_failed, total_skipped
  23. global rerun_failures
  24. ready = False
  25. try:
  26. out = vm['proc'].stdout.read()
  27. except:
  28. return False
  29. logger.debug("VM[%d] stdout.read[%s]" % (i, out))
  30. pending = vm['pending'] + out
  31. lines = []
  32. while True:
  33. pos = pending.find('\n')
  34. if pos < 0:
  35. break
  36. line = pending[0:pos].rstrip()
  37. pending = pending[(pos + 1):]
  38. logger.debug("VM[%d] stdout full line[%s]" % (i, line))
  39. if line.startswith("READY"):
  40. ready = True
  41. elif line.startswith("PASS"):
  42. ready = True
  43. total_passed += 1
  44. elif line.startswith("FAIL"):
  45. ready = True
  46. total_failed += 1
  47. name = line.split(' ')[1]
  48. logger.debug("VM[%d] test case failed: %s" % (i, name))
  49. vm['failed'].append(name)
  50. elif line.startswith("NOT-FOUND"):
  51. ready = True
  52. total_failed += 1
  53. logger.info("VM[%d] test case not found" % i)
  54. elif line.startswith("SKIP"):
  55. ready = True
  56. total_skipped += 1
  57. elif line.startswith("START"):
  58. total_started += 1
  59. vm['out'] += line + '\n'
  60. lines.append(line)
  61. vm['pending'] = pending
  62. return ready
  63. def show_progress(scr):
  64. global num_servers
  65. global vm
  66. global dir
  67. global timestamp
  68. global tests
  69. global first_run_failures
  70. global total_started, total_passed, total_failed, total_skipped
  71. total_tests = len(tests)
  72. logger.info("Total tests: %d" % total_tests)
  73. scr.leaveok(1)
  74. scr.addstr(0, 0, "Parallel test execution status", curses.A_BOLD)
  75. for i in range(0, num_servers):
  76. scr.addstr(i + 1, 0, "VM %d:" % (i + 1), curses.A_BOLD)
  77. scr.addstr(i + 1, 10, "starting VM")
  78. scr.addstr(num_servers + 1, 0, "Total:", curses.A_BOLD)
  79. scr.addstr(num_servers + 1, 20, "TOTAL={} STARTED=0 PASS=0 FAIL=0 SKIP=0".format(total_tests))
  80. scr.refresh()
  81. completed_first_pass = False
  82. rerun_tests = []
  83. while True:
  84. running = False
  85. first_running = False
  86. updated = False
  87. for i in range(0, num_servers):
  88. if completed_first_pass:
  89. continue
  90. if vm[i]['first_run_done']:
  91. continue
  92. if not vm[i]['proc']:
  93. continue
  94. if vm[i]['proc'].poll() is not None:
  95. vm[i]['proc'] = None
  96. scr.move(i + 1, 10)
  97. scr.clrtoeol()
  98. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  99. with open(log, 'r') as f:
  100. if "Kernel panic" in f.read():
  101. scr.addstr("kernel panic")
  102. logger.info("VM[%d] kernel panic" % i)
  103. else:
  104. scr.addstr("unexpected exit")
  105. logger.info("VM[%d] unexpected exit" % i)
  106. updated = True
  107. continue
  108. running = True
  109. first_running = True
  110. try:
  111. err = vm[i]['proc'].stderr.read()
  112. vm[i]['err'] += err
  113. logger.debug("VM[%d] stderr.read[%s]" % (i, err))
  114. except:
  115. pass
  116. if vm_read_stdout(vm[i], i):
  117. scr.move(i + 1, 10)
  118. scr.clrtoeol()
  119. updated = True
  120. if not tests:
  121. vm[i]['first_run_done'] = True
  122. scr.addstr("completed first round")
  123. logger.info("VM[%d] completed first round" % i)
  124. continue
  125. else:
  126. name = tests.pop(0)
  127. vm[i]['proc'].stdin.write(name + '\n')
  128. scr.addstr(name)
  129. logger.debug("VM[%d] start test %s" % (i, name))
  130. if not first_running and not completed_first_pass:
  131. logger.info("First round of testing completed")
  132. if tests:
  133. logger.info("Unexpected test cases remaining from first round: " + str(tests))
  134. raise Exception("Unexpected test cases remaining from first round")
  135. completed_first_pass = True
  136. for name in get_failed(vm):
  137. if rerun_failures:
  138. rerun_tests.append(name)
  139. first_run_failures.append(name)
  140. for i in range(num_servers):
  141. if not completed_first_pass:
  142. continue
  143. if not vm[i]['proc']:
  144. continue
  145. if vm[i]['proc'].poll() is not None:
  146. vm[i]['proc'] = None
  147. scr.move(i + 1, 10)
  148. scr.clrtoeol()
  149. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  150. with open(log, 'r') as f:
  151. if "Kernel panic" in f.read():
  152. scr.addstr("kernel panic")
  153. logger.info("VM[%d] kernel panic" % i)
  154. else:
  155. scr.addstr("completed run")
  156. logger.info("VM[%d] completed run" % i)
  157. updated = True
  158. continue
  159. running = True
  160. try:
  161. err = vm[i]['proc'].stderr.read()
  162. vm[i]['err'] += err
  163. logger.debug("VM[%d] stderr.read[%s]" % (i, err))
  164. except:
  165. pass
  166. ready = False
  167. if vm[i]['first_run_done']:
  168. vm[i]['first_run_done'] = False
  169. ready = True
  170. else:
  171. ready = vm_read_stdout(vm[i], i)
  172. if ready:
  173. scr.move(i + 1, 10)
  174. scr.clrtoeol()
  175. updated = True
  176. if not rerun_tests:
  177. vm[i]['proc'].stdin.write('\n')
  178. scr.addstr("shutting down")
  179. logger.info("VM[%d] shutting down" % i)
  180. else:
  181. name = rerun_tests.pop(0)
  182. vm[i]['proc'].stdin.write(name + '\n')
  183. scr.addstr(name + "(*)")
  184. logger.debug("VM[%d] start test %s (*)" % (i, name))
  185. if not running:
  186. break
  187. if updated:
  188. scr.move(num_servers + 1, 10)
  189. scr.clrtoeol()
  190. scr.addstr("{} %".format(int(100.0 * (total_passed + total_failed + total_skipped) / total_tests)))
  191. scr.addstr(num_servers + 1, 20, "TOTAL={} STARTED={} PASS={} FAIL={} SKIP={}".format(total_tests, total_started, total_passed, total_failed, total_skipped))
  192. failed = get_failed(vm)
  193. if len(failed) > 0:
  194. scr.move(num_servers + 2, 0)
  195. scr.clrtoeol()
  196. scr.addstr("Failed test cases: ")
  197. count = 0
  198. for f in failed:
  199. count += 1
  200. if count > 30:
  201. scr.addstr('...')
  202. scr.clrtoeol()
  203. break
  204. scr.addstr(f)
  205. scr.addstr(' ')
  206. scr.move(0, 35)
  207. scr.clrtoeol()
  208. if rerun_tests:
  209. scr.addstr("(RETRY FAILED %d)" % len(rerun_tests))
  210. elif rerun_failures:
  211. pass
  212. elif first_run_failures:
  213. scr.addstr("(RETRY FAILED)")
  214. scr.refresh()
  215. time.sleep(0.25)
  216. scr.refresh()
  217. time.sleep(0.3)
  218. def main():
  219. global num_servers
  220. global vm
  221. global dir
  222. global timestamp
  223. global tests
  224. global first_run_failures
  225. global total_started, total_passed, total_failed, total_skipped
  226. global rerun_failures
  227. total_started = 0
  228. total_passed = 0
  229. total_failed = 0
  230. total_skipped = 0
  231. debug_level = logging.INFO
  232. rerun_failures = True
  233. if len(sys.argv) < 2:
  234. sys.exit("Usage: %s <number of VMs> [-1] [--debug] [--codecov] [params..]" % sys.argv[0])
  235. num_servers = int(sys.argv[1])
  236. if num_servers < 1:
  237. sys.exit("Too small number of VMs")
  238. timestamp = int(time.time())
  239. idx = 2
  240. if len(sys.argv) > idx and sys.argv[idx] == "-1":
  241. idx += 1
  242. rerun_failures = False
  243. if len(sys.argv) > idx and sys.argv[idx] == "--debug":
  244. idx += 1
  245. debug_level = logging.DEBUG
  246. if len(sys.argv) > idx and sys.argv[idx] == "--codecov":
  247. idx += 1
  248. print "Code coverage - build separate binaries"
  249. logdir = "/tmp/hwsim-test-logs/" + str(timestamp)
  250. os.makedirs(logdir)
  251. subprocess.check_call(['./build-codecov.sh', logdir])
  252. codecov_args = ['--codecov_dir', logdir]
  253. codecov = True
  254. else:
  255. codecov_args = []
  256. codecov = False
  257. first_run_failures = []
  258. tests = []
  259. cmd = [ '../run-tests.py', '-L' ] + sys.argv[idx:]
  260. lst = subprocess.Popen(cmd, stdout=subprocess.PIPE)
  261. for l in lst.stdout.readlines():
  262. name = l.split(' ')[0]
  263. tests.append(name)
  264. if len(tests) == 0:
  265. sys.exit("No test cases selected")
  266. if '-f' in sys.argv[idx:]:
  267. extra_args = sys.argv[idx:]
  268. else:
  269. extra_args = [x for x in sys.argv[idx:] if x not in tests]
  270. dir = '/tmp/hwsim-test-logs'
  271. try:
  272. os.mkdir(dir)
  273. except:
  274. pass
  275. if num_servers > 2 and len(tests) > 100:
  276. # Move test cases with long duration to the beginning as an
  277. # optimization to avoid last part of the test execution running a long
  278. # duration test case on a single VM while all other VMs have already
  279. # completed their work.
  280. long = [ "ap_roam_open",
  281. "hostapd_oom_wpa2_psk_connect",
  282. "ap_hs20_fetch_osu_stop",
  283. "ap_roam_wpa2_psk",
  284. "ibss_wpa_none_ccmp",
  285. "nfc_wps_er_handover_pk_hash_mismatch_sta",
  286. "go_neg_peers_force_diff_freq",
  287. "p2p_cli_invite",
  288. "sta_ap_scan_2b",
  289. "ap_pmf_sta_unprot_deauth_burst",
  290. "ap_bss_add_remove_during_ht_scan",
  291. "wext_scan_hidden",
  292. "autoscan_exponential",
  293. "nfc_p2p_client",
  294. "wnm_bss_keep_alive",
  295. "ap_inactivity_disconnect",
  296. "scan_bss_expiration_age",
  297. "autoscan_periodic",
  298. "discovery_group_client",
  299. "concurrent_p2pcli",
  300. "ap_bss_add_remove",
  301. "wpas_ap_wps",
  302. "wext_pmksa_cache",
  303. "ibss_wpa_none",
  304. "ap_ht_40mhz_intolerant_ap",
  305. "ibss_rsn",
  306. "discovery_pd_retries",
  307. "ap_wps_setup_locked_timeout",
  308. "ap_vht160",
  309. "dfs_radar",
  310. "dfs",
  311. "grpform_cred_ready_timeout",
  312. "hostapd_oom_wpa2_eap_connect",
  313. "ap_wps_pbc_timeout" ]
  314. for l in long:
  315. if l in tests:
  316. tests.remove(l)
  317. tests.insert(0, l)
  318. logger.setLevel(debug_level)
  319. log_handler = logging.FileHandler('parallel-vm.log')
  320. log_handler.setLevel(debug_level)
  321. fmt = "%(asctime)s %(levelname)s %(message)s"
  322. log_formatter = logging.Formatter(fmt)
  323. log_handler.setFormatter(log_formatter)
  324. logger.addHandler(log_handler)
  325. vm = {}
  326. for i in range(0, num_servers):
  327. print("\rStarting virtual machine {}/{}".format(i + 1, num_servers)),
  328. logger.info("Starting virtual machine {}/{}".format(i + 1, num_servers))
  329. cmd = ['./vm-run.sh', '--delay', str(i), '--timestamp', str(timestamp),
  330. '--ext', 'srv.%d' % (i + 1),
  331. '-i'] + codecov_args + extra_args
  332. vm[i] = {}
  333. vm[i]['first_run_done'] = False
  334. vm[i]['proc'] = subprocess.Popen(cmd,
  335. stdin=subprocess.PIPE,
  336. stdout=subprocess.PIPE,
  337. stderr=subprocess.PIPE)
  338. vm[i]['out'] = ""
  339. vm[i]['pending'] = ""
  340. vm[i]['err'] = ""
  341. vm[i]['failed'] = []
  342. for stream in [ vm[i]['proc'].stdout, vm[i]['proc'].stderr ]:
  343. fd = stream.fileno()
  344. fl = fcntl.fcntl(fd, fcntl.F_GETFL)
  345. fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
  346. print
  347. curses.wrapper(show_progress)
  348. with open('{}/{}-parallel.log'.format(dir, timestamp), 'w') as f:
  349. for i in range(0, num_servers):
  350. f.write('VM {}\n{}\n{}\n'.format(i, vm[i]['out'], vm[i]['err']))
  351. failed = get_failed(vm)
  352. if first_run_failures:
  353. print "Failed test cases:"
  354. for f in first_run_failures:
  355. print f,
  356. logger.info("Failed: " + f)
  357. print
  358. double_failed = []
  359. for name in failed:
  360. double_failed.append(name)
  361. for test in first_run_failures:
  362. double_failed.remove(test)
  363. if not rerun_failures:
  364. pass
  365. elif failed and not double_failed:
  366. print "All failed cases passed on retry"
  367. logger.info("All failed cases passed on retry")
  368. elif double_failed:
  369. print "Failed even on retry:"
  370. for f in double_failed:
  371. print f,
  372. logger.info("Failed on retry: " + f)
  373. print
  374. res = "TOTAL={} PASS={} FAIL={} SKIP={}".format(total_started,
  375. total_passed,
  376. total_failed,
  377. total_skipped)
  378. print(res)
  379. logger.info(res)
  380. print "Logs: " + dir + '/' + str(timestamp)
  381. logger.info("Logs: " + dir + '/' + str(timestamp))
  382. for i in range(0, num_servers):
  383. if len(vm[i]['pending']) > 0:
  384. logger.info("Unprocessed stdout from VM[%d]: '%s'" %
  385. (i, vm[i]['pending']))
  386. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  387. with open(log, 'r') as f:
  388. if "Kernel panic" in f.read():
  389. print "Kernel panic in " + log
  390. logger.info("Kernel panic in " + log)
  391. if codecov:
  392. print "Code coverage - preparing report"
  393. for i in range(num_servers):
  394. subprocess.check_call(['./process-codecov.sh',
  395. logdir + ".srv.%d" % (i + 1),
  396. str(i)])
  397. subprocess.check_call(['./combine-codecov.sh', logdir])
  398. print "file://%s/index.html" % logdir
  399. logger.info("Code coverage report: file://%s/index.html" % logdir)
  400. if double_failed or (failed and not rerun_failures):
  401. logger.info("Test run complete - failures found")
  402. sys.exit(2)
  403. if failed:
  404. logger.info("Test run complete - failures found on first run; passed on retry")
  405. sys.exit(1)
  406. logger.info("Test run complete - no failures")
  407. sys.exit(0)
  408. if __name__ == "__main__":
  409. main()