parallel-vm.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/usr/bin/env python2
  2. #
  3. # Parallel VM test case executor
  4. # Copyright (c) 2014, 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 os
  11. import subprocess
  12. import sys
  13. import time
  14. def get_results():
  15. global vm
  16. started = []
  17. passed = []
  18. failed = []
  19. skipped = []
  20. for i in range(0, num_servers):
  21. lines = vm[i]['out'].splitlines()
  22. started += [ l for l in lines if l.startswith('START ') ]
  23. passed += [ l for l in lines if l.startswith('PASS ') ]
  24. failed += [ l for l in lines if l.startswith('FAIL ') ]
  25. skipped += [ l for l in lines if l.startswith('SKIP ') ]
  26. return (started, passed, failed, skipped)
  27. def show_progress(scr):
  28. global num_servers
  29. global vm
  30. global dir
  31. global timestamp
  32. global tests
  33. total_tests = len(tests)
  34. scr.leaveok(1)
  35. scr.addstr(0, 0, "Parallel test execution status", curses.A_BOLD)
  36. for i in range(0, num_servers):
  37. scr.addstr(i + 1, 0, "VM %d:" % (i + 1), curses.A_BOLD)
  38. scr.addstr(i + 1, 10, "starting VM")
  39. scr.addstr(num_servers + 1, 0, "Total:", curses.A_BOLD)
  40. scr.addstr(num_servers + 1, 20, "TOTAL={} STARTED=0 PASS=0 FAIL=0 SKIP=0".format(total_tests))
  41. scr.refresh()
  42. while True:
  43. running = False
  44. updated = False
  45. for i in range(0, num_servers):
  46. if not vm[i]['proc']:
  47. continue
  48. if vm[i]['proc'].poll() is not None:
  49. vm[i]['proc'] = None
  50. scr.move(i + 1, 10)
  51. scr.clrtoeol()
  52. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  53. with open(log, 'r') as f:
  54. if "Kernel panic" in f.read():
  55. scr.addstr("kernel panic")
  56. else:
  57. scr.addstr("completed run")
  58. updated = True
  59. continue
  60. running = True
  61. try:
  62. err = vm[i]['proc'].stderr.read()
  63. vm[i]['err'] += err
  64. except:
  65. pass
  66. try:
  67. out = vm[i]['proc'].stdout.read()
  68. if "READY" in out or "PASS" in out or "FAIL" in out or "SKIP" in out:
  69. if not tests:
  70. vm[i]['proc'].stdin.write('\n')
  71. else:
  72. name = tests.pop(0)
  73. vm[i]['proc'].stdin.write(name + '\n')
  74. except:
  75. continue
  76. #print("VM {}: '{}'".format(i, out))
  77. vm[i]['out'] += out
  78. lines = vm[i]['out'].splitlines()
  79. last = [ l for l in lines if l.startswith('START ') ]
  80. if len(last) > 0:
  81. try:
  82. info = last[-1].split(' ')
  83. scr.move(i + 1, 10)
  84. scr.clrtoeol()
  85. scr.addstr(info[1])
  86. updated = True
  87. except:
  88. pass
  89. if not running:
  90. break
  91. if updated:
  92. (started, passed, failed, skipped) = get_results()
  93. scr.move(num_servers + 1, 10)
  94. scr.clrtoeol()
  95. scr.addstr("{} %".format(int(100.0 * (len(passed) + len(failed) + len(skipped)) / total_tests)))
  96. scr.addstr(num_servers + 1, 20, "TOTAL={} STARTED={} PASS={} FAIL={} SKIP={}".format(total_tests, len(started), len(passed), len(failed), len(skipped)))
  97. if len(failed) > 0:
  98. scr.move(num_servers + 2, 0)
  99. scr.clrtoeol()
  100. scr.addstr("Failed test cases: ")
  101. for f in failed:
  102. scr.addstr(f.split(' ')[1])
  103. scr.addstr(' ')
  104. scr.refresh()
  105. time.sleep(0.5)
  106. scr.refresh()
  107. time.sleep(0.3)
  108. def main():
  109. global num_servers
  110. global vm
  111. global dir
  112. global timestamp
  113. global tests
  114. if len(sys.argv) < 2:
  115. sys.exit("Usage: %s <number of VMs> [--codecov] [params..]" % sys.argv[0])
  116. num_servers = int(sys.argv[1])
  117. if num_servers < 1:
  118. sys.exit("Too small number of VMs")
  119. timestamp = int(time.time())
  120. if len(sys.argv) > 2 and sys.argv[2] == "--codecov":
  121. idx = 3
  122. print "Code coverage - build separate binaries"
  123. logdir = "/tmp/hwsim-test-logs/" + str(timestamp)
  124. os.makedirs(logdir)
  125. subprocess.check_call(['./build-codecov.sh', logdir])
  126. codecov_args = ['--codecov_dir', logdir]
  127. codecov = True
  128. else:
  129. idx = 2
  130. codecov_args = []
  131. codecov = False
  132. tests = []
  133. cmd = [ '../run-tests.py', '-L' ] + sys.argv[idx:]
  134. lst = subprocess.Popen(cmd, stdout=subprocess.PIPE)
  135. for l in lst.stdout.readlines():
  136. name = l.split(' ')[0]
  137. tests.append(name)
  138. if len(tests) == 0:
  139. sys.exit("No test cases selected")
  140. if '-f' in sys.argv[idx:]:
  141. extra_args = sys.argv[idx:]
  142. else:
  143. extra_args = [x for x in sys.argv[idx:] if x not in tests]
  144. dir = '/tmp/hwsim-test-logs'
  145. try:
  146. os.mkdir(dir)
  147. except:
  148. pass
  149. if num_servers > 2 and len(tests) > 100:
  150. # Move test cases with long duration to the beginning as an
  151. # optimization to avoid last part of the test execution running a long
  152. # duration test case on a single VM while all other VMs have already
  153. # completed their work.
  154. long = [ "ap_roam_open",
  155. "ap_hs20_fetch_osu_stop",
  156. "ap_roam_wpa2_psk",
  157. "ibss_wpa_none_ccmp",
  158. "nfc_wps_er_handover_pk_hash_mismatch_sta",
  159. "go_neg_peers_force_diff_freq",
  160. "p2p_cli_invite",
  161. "sta_ap_scan_2b",
  162. "ap_pmf_sta_unprot_deauth_burst",
  163. "ap_bss_add_remove_during_ht_scan",
  164. "wext_scan_hidden",
  165. "autoscan_exponential",
  166. "nfc_p2p_client",
  167. "wnm_bss_keep_alive",
  168. "ap_inactivity_disconnect",
  169. "scan_bss_expiration_age",
  170. "autoscan_periodic",
  171. "discovery_group_client",
  172. "concurrent_p2pcli",
  173. "ap_bss_add_remove",
  174. "wpas_ap_wps",
  175. "wext_pmksa_cache",
  176. "ibss_wpa_none",
  177. "ap_ht_40mhz_intolerant_ap",
  178. "ibss_rsn",
  179. "discovery_pd_retries",
  180. "ap_wps_setup_locked_timeout",
  181. "ap_vht160",
  182. "dfs_radar",
  183. "dfs",
  184. "grpform_cred_ready_timeout",
  185. "ap_wps_pbc_timeout" ]
  186. for l in long:
  187. if l in tests:
  188. tests.remove(l)
  189. tests.insert(0, l)
  190. vm = {}
  191. for i in range(0, num_servers):
  192. print("\rStarting virtual machine {}/{}".format(i + 1, num_servers)),
  193. cmd = ['./vm-run.sh', '--delay', str(i), '--timestamp', str(timestamp),
  194. '--ext', 'srv.%d' % (i + 1),
  195. '-i'] + codecov_args + extra_args
  196. vm[i] = {}
  197. vm[i]['proc'] = subprocess.Popen(cmd,
  198. stdin=subprocess.PIPE,
  199. stdout=subprocess.PIPE,
  200. stderr=subprocess.PIPE)
  201. vm[i]['out'] = ""
  202. vm[i]['err'] = ""
  203. for stream in [ vm[i]['proc'].stdout, vm[i]['proc'].stderr ]:
  204. fd = stream.fileno()
  205. fl = fcntl.fcntl(fd, fcntl.F_GETFL)
  206. fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
  207. print
  208. curses.wrapper(show_progress)
  209. with open('{}/{}-parallel.log'.format(dir, timestamp), 'w') as f:
  210. for i in range(0, num_servers):
  211. f.write('VM {}\n{}\n{}\n'.format(i, vm[i]['out'], vm[i]['err']))
  212. (started, passed, failed, skipped) = get_results()
  213. if len(failed) > 0:
  214. print "Failed test cases:"
  215. for f in failed:
  216. print f.split(' ')[1],
  217. print
  218. print("TOTAL={} PASS={} FAIL={} SKIP={}".format(len(started), len(passed), len(failed), len(skipped)))
  219. print "Logs: " + dir + '/' + str(timestamp)
  220. for i in range(0, num_servers):
  221. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  222. with open(log, 'r') as f:
  223. if "Kernel panic" in f.read():
  224. print "Kernel panic in " + log
  225. if codecov:
  226. print "Code coverage - preparing report"
  227. for i in range(num_servers):
  228. subprocess.check_call(['./process-codecov.sh',
  229. logdir + ".srv.%d" % (i + 1),
  230. str(i)])
  231. subprocess.check_call(['./combine-codecov.sh', logdir])
  232. print "file://%s/index.html" % logdir
  233. if __name__ == "__main__":
  234. main()