imagetest-qemu.bbclass 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Test related variables
  2. # By default, TEST_DIR is created under WORKDIR
  3. TEST_DIR ?= "${WORKDIR}/qemuimagetest"
  4. TEST_LOG ?= "${LOG_DIR}/qemuimagetests"
  5. TEST_RESULT ?= "${TEST_DIR}/result"
  6. TEST_TMP ?= "${TEST_DIR}/tmp"
  7. TEST_SCEN ?= "sanity"
  8. TEST_STATUS ?= "${TEST_TMP}/status"
  9. TARGET_IPSAVE ?= "${TEST_TMP}/target_ip"
  10. TEST_SERIALIZE ?= "1"
  11. python do_qemuimagetest() {
  12. qemuimagetest_main(d)
  13. }
  14. addtask qemuimagetest before do_build after do_rootfs
  15. do_qemuimagetest[nostamp] = "1"
  16. do_qemuimagetest[depends] += "qemu-native:do_populate_sysroot"
  17. python do_qemuimagetest_standalone() {
  18. qemuimagetest_main(d)
  19. }
  20. addtask qemuimagetest_standalone
  21. do_qemuimagetest_standalone[nostamp] = "1"
  22. do_qemuimagetest_standalone[depends] += "qemu-native:do_populate_sysroot"
  23. def qemuimagetest_main(d):
  24. import sys
  25. import re
  26. import os
  27. """
  28. Test Controller for automated testing.
  29. """
  30. casestr = re.compile(r'(?P<scen>\w+\b):(?P<case>\w+$)')
  31. resultstr = re.compile(r'\s*(?P<case>\w+)\s*(?P<pass>\d+)\s*(?P<fail>\d+)\s*(?P<noresult>\d+)')
  32. machine = bb.data.getVar('MACHINE', d, 1)
  33. pname = bb.data.getVar('PN', d, 1)
  34. """function to save test cases running status"""
  35. def teststatus(test, status, index, length):
  36. test_status = bb.data.getVar('TEST_STATUS', d, 1)
  37. if not os.path.exists(test_status):
  38. raise bb.build.FuncFailed("No test status file existing under TEST_TMP")
  39. f = open(test_status, "w")
  40. f.write("\t%-15s%-15s%-15s%-15s\n" % ("Case", "Status", "Number", "Total"))
  41. f.write("\t%-15s%-15s%-15s%-15s\n" % (case, status, index, length))
  42. f.close()
  43. """funtion to run each case under scenario"""
  44. def runtest(scen, case, fulltestpath):
  45. resultpath = bb.data.getVar('TEST_RESULT', d, 1)
  46. tmppath = bb.data.getVar('TEST_TMP', d, 1)
  47. """initialize log file for testcase"""
  48. logpath = bb.data.getVar('TEST_LOG', d, 1)
  49. bb.utils.mkdirhier("%s/%s" % (logpath, scen))
  50. caselog = os.path.join(logpath, "%s/log_%s.%s" % (scen, case, bb.data.getVar('DATETIME', d, 1)))
  51. os.system("touch %s" % caselog)
  52. """export TEST_TMP, TEST_RESULT, DEPLOY_DIR and QEMUARCH"""
  53. os.environ["PATH"] = bb.data.getVar("PATH", d, True)
  54. os.environ["TEST_TMP"] = tmppath
  55. os.environ["TEST_RESULT"] = resultpath
  56. os.environ["DEPLOY_DIR"] = bb.data.getVar("DEPLOY_DIR", d, True)
  57. os.environ["QEMUARCH"] = machine
  58. os.environ["QEMUTARGET"] = pname
  59. os.environ["DISPLAY"] = bb.data.getVar("DISPLAY", d, True)
  60. os.environ["COREBASE"] = bb.data.getVar("COREBASE", d, True)
  61. os.environ["TOPDIR"] = bb.data.getVar("TOPDIR", d, True)
  62. os.environ["TEST_STATUS"] = bb.data.getVar("TEST_STATUS", d, True)
  63. os.environ["TARGET_IPSAVE"] = bb.data.getVar("TARGET_IPSAVE", d, True)
  64. os.environ["TEST_SERIALIZE"] = bb.data.getVar("TEST_SERIALIZE", d, True)
  65. """run Test Case"""
  66. bb.note("Run %s test in scenario %s" % (case, scen))
  67. os.system("%s" % fulltestpath)
  68. """Generate testcase list in runtime"""
  69. def generate_list(testlist):
  70. list = []
  71. if len(testlist) == 0:
  72. raise bb.build.FuncFailed("No testcase defined in TEST_SCEN")
  73. """check testcase folder and add case list according to TEST_SCEN"""
  74. for item in testlist.split(" "):
  75. n = casestr.match(item)
  76. if n:
  77. item = n.group('scen')
  78. casefile = n.group('case')
  79. for dir in bb.data.getVar("QEMUIMAGETESTS", d, True).split():
  80. fulltestcase = os.path.join(dir, item, casefile)
  81. if not os.path.isfile(fulltestcase):
  82. raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase)
  83. list.append((item, casefile, fulltestcase))
  84. else:
  85. for dir in bb.data.getVar("QEMUIMAGETESTS", d, True).split():
  86. scenlist = os.path.join(dir, "scenario", machine, pname)
  87. if not os.path.isfile(scenlist):
  88. raise bb.build.FuncFailed("No scenario list file named %s found" % scenlist)
  89. f = open(scenlist, "r")
  90. for line in f:
  91. if item != line.split()[0]:
  92. continue
  93. else:
  94. casefile = line.split()[1]
  95. fulltestcase = os.path.join(dir, item, casefile)
  96. if not os.path.isfile(fulltestcase):
  97. raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase)
  98. list.append((item, casefile, fulltestcase))
  99. return list
  100. """Clean tmp folder for testing"""
  101. def clean_tmp():
  102. tmppath = bb.data.getVar('TEST_TMP', d, 1)
  103. if os.path.isdir(tmppath):
  104. for f in os.listdir(tmppath):
  105. tmpfile = os.path.join(tmppath, f)
  106. os.remove(tmpfile)
  107. """Before running testing, clean temp folder first"""
  108. clean_tmp()
  109. """check testcase folder and create test log folder"""
  110. testpath = bb.data.getVar('TEST_DIR', d, 1)
  111. bb.utils.mkdirhier(testpath)
  112. logpath = bb.data.getVar('TEST_LOG', d, 1)
  113. bb.utils.mkdirhier(logpath)
  114. tmppath = bb.data.getVar('TEST_TMP', d, 1)
  115. bb.utils.mkdirhier(tmppath)
  116. """initialize test status file"""
  117. test_status = bb.data.getVar('TEST_STATUS', d, 1)
  118. if os.path.exists(test_status):
  119. os.remove(test_status)
  120. os.system("touch %s" % test_status)
  121. """initialize result file"""
  122. resultpath = bb.data.getVar('TEST_RESULT', d, 1)
  123. bb.utils.mkdirhier(resultpath)
  124. resultfile = os.path.join(resultpath, "testresult.%s" % bb.data.getVar('DATETIME', d, 1))
  125. sresultfile = os.path.join(resultpath, "testresult.log")
  126. machine = bb.data.getVar('MACHINE', d, 1)
  127. if os.path.exists(sresultfile):
  128. os.remove(sresultfile)
  129. os.system("touch %s" % resultfile)
  130. os.symlink(resultfile, sresultfile)
  131. f = open(sresultfile, "a")
  132. f.write("\tTest Result for %s\n" % machine)
  133. f.write("\t%-15s%-15s%-15s%-15s\n" % ("Testcase", "PASS", "FAIL", "NORESULT"))
  134. f.close()
  135. """generate pre-defined testcase list"""
  136. testlist = bb.data.getVar('TEST_SCEN', d, 1)
  137. fulllist = generate_list(testlist)
  138. """Begin testing"""
  139. for index,test in enumerate(fulllist):
  140. (scen, case, fullpath) = test
  141. teststatus(case, "running", index, (len(fulllist) - 1))
  142. runtest(scen, case, fullpath)
  143. teststatus(case, "finished", index, (len(fulllist) - 1))
  144. """Print Test Result"""
  145. ret = 0
  146. f = open(sresultfile, "r")
  147. for line in f:
  148. m = resultstr.match(line)
  149. if m:
  150. if m.group('fail') == "1":
  151. ret = 1
  152. elif m.group('noresult') == "1":
  153. ret = 2
  154. line = line.strip('\n')
  155. bb.note(line)
  156. else:
  157. line = line.strip('\n')
  158. bb.note(line)
  159. f.close()
  160. """Clean temp files for testing"""
  161. clean_tmp()
  162. if ret != 0:
  163. raise bb.build.FuncFailed("Some testcases fail, pls. check test result and test log!!!")