fetch.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import subprocess
  2. import csv
  3. import os
  4. import time
  5. import signal
  6. import tempfile
  7. import multiprocessing
  8. import sys
  9. def run(cmd, timeout, test):
  10. try:
  11. timeout = timeout * 100
  12. result = {}
  13. result['test'] = test
  14. outfile = tempfile.NamedTemporaryFile(mode='w+b')
  15. p = subprocess.Popen(cmd, shell=True, stdout=outfile, stderr=subprocess.STDOUT, preexec_fn=os.setsid, close_fds=True)
  16. result['curtime'] = time.time()
  17. result['endtime'] = result['curtime'] + timeout
  18. sleep_time = 0
  19. finish = False
  20. while sleep_time < timeout:
  21. if p.poll() is not None:
  22. finish = True
  23. break
  24. sleep_time += 1
  25. time.sleep(.01)
  26. result['finish'] = finish
  27. outfile.seek(0)
  28. result['output'] = outfile.readlines()
  29. return result
  30. except Exception as e:
  31. print str(e)
  32. return None
  33. finally:
  34. if p is not None and p.poll() is None:
  35. print 'killing %s' % test
  36. os.killpg(os.getpgid(p.pid), signal.SIGKILL)
  37. def finish(result):
  38. try:
  39. test = result['test']
  40. if not result['finish']:
  41. print CRED + "[Hanged ] " + test + CEND
  42. current_hanged[test] = 1
  43. else:
  44. reported = False
  45. count = 1
  46. for output in result['output']:
  47. tokens = output.split()
  48. if len(tokens) < 2:
  49. continue
  50. # Drop this line so that we get consistent offsets
  51. if output == "WARNING: no physical memory support, process creation may be slow.\n":
  52. continue
  53. if tokens[1].isdigit():
  54. test_subtest = test + "," + tokens[1]
  55. count = int(tokens[1]) + 1
  56. else:
  57. test_subtest = test + "," + str(count)
  58. count = count + 1
  59. if "TINFO" in output or test_subtest in current_passed or test_subtest in current_failed or test in current_hanged or test_subtest in current_broken:
  60. continue
  61. if output:
  62. output = output.strip()
  63. print >>f1, output
  64. if "TFAIL" in output:
  65. print >>failed_tests_fh, test_subtest
  66. print CRED + "[Fail ] " + test_subtest + CEND
  67. current_failed[test_subtest] = 1
  68. reported = True
  69. elif "TPASS" in output or "PASS:" in output:
  70. print >>passed_tests_fh, test_subtest
  71. print CGREEN + "[Pass ] " + test_subtest + CEND
  72. current_passed[test_subtest] = 1
  73. reported = True
  74. elif "TCONF" in output or "TBROK" in output or "BROK" in output or "error" in output:
  75. print >>broken_tests_fh, test_subtest
  76. # Syscall not implemented or test preparation failed
  77. print "[Broken(a) ] " + test_subtest + CEND
  78. current_broken[test_subtest] = 1
  79. reported = True
  80. if (not reported):
  81. print >>broken_tests_fh, test
  82. print CRED + "[Broken(b) ] " + test + CEND
  83. for output in result['output']:
  84. print output
  85. current_broken[test] = 1
  86. except Exception as e:
  87. print str(e)
  88. CRED = '\033[91m'
  89. CGREEN = '\033[92m'
  90. CEND = '\033[0m'
  91. DEFAULT_TIMEOUT = 30
  92. resultfile = "run_output"
  93. stablePass = "PASSED"
  94. timeouts = "TIMEOUTS"
  95. failed_tests_file = "Failed.csv"
  96. passed_tests_file = "Passed.csv"
  97. broken_tests_file = "Broken.csv"
  98. f1 = open(resultfile, 'w')
  99. failed_tests_fh = open(failed_tests_file, 'w', 0)
  100. passed_tests_fh = open(passed_tests_file, 'w', 0)
  101. broken_tests_fh = open(broken_tests_file, 'w', 0)
  102. failed_tests_fh.write("Test,Subtest number,Status\n")
  103. passed_tests_fh.write("Test,Subtest number\n")
  104. broken_tests_fh.write("Test,Subtest number,Status\n")
  105. current_passed = dict()
  106. current_failed = dict()
  107. current_broken = dict()
  108. current_hanged = dict()
  109. timeouts_dict = dict()
  110. with open(timeouts, 'rb') as csvfile:
  111. test_timeout = csv.reader(csvfile)
  112. test_timeout.next()
  113. for row in test_timeout:
  114. test = row[0]
  115. timeout = row[1]
  116. timeouts_dict[test] = int(timeout)
  117. os.chdir("opt/ltp/testcases/bin")
  118. pool = multiprocessing.Pool()
  119. with open('../../../../syscalls.graphene') as testcases:
  120. for line in testcases:
  121. line = line.strip('\r\n\t')
  122. tokens = line.split( )
  123. if (tokens[1] == "SGX") :
  124. test = tokens[2]
  125. else :
  126. test = tokens[1]
  127. if test=="seq":
  128. test = tokens[6] #splice02
  129. try:
  130. timeout = timeouts_dict[test]
  131. except KeyError:
  132. timeout = DEFAULT_TIMEOUT
  133. pool.apply_async(run, args=([line], timeout, test), callback=finish)
  134. os.chdir("../../../..")
  135. pool.close()
  136. pool.join()
  137. stable_passed = dict()
  138. with open(stablePass, 'rb') as csvfile:
  139. test_subtest = csv.reader(csvfile)
  140. test_subtest.next()
  141. for row in test_subtest:
  142. tst = row[0] + "," + row[1]
  143. stable_passed[tst] = 1
  144. print "\n\nRESULT [Difference] :\n---------------------\n"
  145. rv = 0
  146. for test in sorted(stable_passed):
  147. if not test in current_passed:
  148. print CRED + "Test '" + test + "' did not pass in the current run!!" + CEND
  149. rv = -1
  150. for test in sorted(current_passed):
  151. if not test in stable_passed:
  152. print CGREEN + "Test '" + test + "' passed in the current run!!" + CEND
  153. print "\n"
  154. sys.exit(rv)