parallel-vm.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. scr.leaveok(1)
  33. scr.addstr(0, 0, "Parallel test execution status", curses.A_BOLD)
  34. for i in range(0, num_servers):
  35. scr.addstr(i + 1, 0, "VM %d:" % (i + 1), curses.A_BOLD)
  36. scr.addstr(i + 1, 20, "starting VM")
  37. scr.addstr(num_servers + 1, 0, "Total:", curses.A_BOLD)
  38. scr.refresh()
  39. while True:
  40. running = False
  41. updated = False
  42. for i in range(0, num_servers):
  43. if not vm[i]['proc']:
  44. continue
  45. if vm[i]['proc'].poll() is not None:
  46. vm[i]['proc'] = None
  47. vm[i]['done'] = vm[i]['total']
  48. scr.move(i + 1, 10)
  49. scr.clrtoeol()
  50. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  51. with open(log, 'r') as f:
  52. if "Kernel panic" in f.read():
  53. scr.addstr("kernel panic")
  54. else:
  55. scr.addstr("completed run")
  56. updated = True
  57. continue
  58. running = True
  59. try:
  60. err = vm[i]['proc'].stderr.read()
  61. vm[i]['err'] += err
  62. except:
  63. pass
  64. try:
  65. out = vm[i]['proc'].stdout.read()
  66. except:
  67. continue
  68. #print("VM {}: '{}'".format(i, out))
  69. vm[i]['out'] += out
  70. lines = vm[i]['out'].splitlines()
  71. last = [ l for l in lines if l.startswith('START ') ]
  72. if len(last) > 0:
  73. try:
  74. info = last[-1].split(' ')
  75. vm[i]['pos'] = info[2]
  76. pos = info[2].split('/')
  77. if int(pos[0]) > 0:
  78. vm[i]['done'] = int(pos[0]) - 1
  79. vm[i]['total'] = int(pos[1])
  80. p = float(pos[0]) / float(pos[1]) * 100.0
  81. scr.move(i + 1, 10)
  82. scr.clrtoeol()
  83. scr.addstr("{} %".format(int(p)))
  84. scr.addstr(i + 1, 20, info[1])
  85. updated = True
  86. except:
  87. pass
  88. else:
  89. vm[i]['pos'] = ''
  90. if not running:
  91. break
  92. if updated:
  93. done = 0
  94. total = 0
  95. for i in range(0, num_servers):
  96. done += vm[i]['done']
  97. total += vm[i]['total']
  98. scr.move(num_servers + 1, 10)
  99. scr.clrtoeol()
  100. if total > 0:
  101. scr.addstr("{} %".format(int(100.0 * done / total)))
  102. (started, passed, failed, skipped) = get_results()
  103. scr.addstr(num_servers + 1, 20, "TOTAL={} PASS={} FAIL={} SKIP={}".format(len(started), len(passed), len(failed), len(skipped)))
  104. if len(failed) > 0:
  105. scr.move(num_servers + 2, 0)
  106. scr.clrtoeol()
  107. scr.addstr("Failed test cases: ")
  108. for f in failed:
  109. scr.addstr(f.split(' ')[1])
  110. scr.addstr(' ')
  111. scr.refresh()
  112. time.sleep(1)
  113. def main():
  114. global num_servers
  115. global vm
  116. global dir
  117. global timestamp
  118. if len(sys.argv) < 2:
  119. sys.exit("Usage: %s <number of VMs> [params..]" % sys.argv[0])
  120. num_servers = int(sys.argv[1])
  121. if num_servers < 1:
  122. sys.exit("Too small number of VMs")
  123. dir = '/tmp/hwsim-test-logs'
  124. try:
  125. os.mkdir(dir)
  126. except:
  127. pass
  128. timestamp = int(time.time())
  129. vm = {}
  130. for i in range(0, num_servers):
  131. print("\rStarting virtual machine {}/{}".format(i + 1, num_servers)),
  132. cmd = ['./vm-run.sh', '--timestamp', str(timestamp),
  133. '--ext', 'srv.%d' % (i + 1),
  134. '--split', '%d/%d' % (i + 1, num_servers)] + sys.argv[2:]
  135. vm[i] = {}
  136. vm[i]['proc'] = subprocess.Popen(cmd,
  137. stdin=subprocess.PIPE,
  138. stdout=subprocess.PIPE,
  139. stderr=subprocess.PIPE)
  140. vm[i]['out'] = ""
  141. vm[i]['err'] = ""
  142. vm[i]['pos'] = ""
  143. vm[i]['done'] = 0
  144. vm[i]['total'] = 0
  145. for stream in [ vm[i]['proc'].stdout, vm[i]['proc'].stderr ]:
  146. fd = stream.fileno()
  147. fl = fcntl.fcntl(fd, fcntl.F_GETFL)
  148. fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
  149. print
  150. curses.wrapper(show_progress)
  151. with open('{}/{}-parallel.log'.format(dir, timestamp), 'w') as f:
  152. for i in range(0, num_servers):
  153. f.write('VM {}\n{}\n{}\n'.format(i, vm[i]['out'], vm[i]['err']))
  154. (started, passed, failed, skipped) = get_results()
  155. if len(failed) > 0:
  156. print "Failed test cases:"
  157. for f in failed:
  158. print f.split(' ')[1],
  159. print
  160. print("TOTAL={} PASS={} FAIL={} SKIP={}".format(len(started), len(passed), len(failed), len(skipped)))
  161. for i in range(0, num_servers):
  162. log = '{}/{}.srv.{}/console'.format(dir, timestamp, i + 1)
  163. with open(log, 'r') as f:
  164. if "Kernel panic" in f.read():
  165. print "Kernel panic in " + log
  166. if __name__ == "__main__":
  167. main()