ptest.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # SPDX-License-Identifier: MIT
  3. #
  4. import unittest
  5. import pprint
  6. import datetime
  7. from oeqa.runtime.case import OERuntimeTestCase
  8. from oeqa.core.decorator.depends import OETestDepends
  9. from oeqa.core.decorator.data import skipIfNotFeature
  10. from oeqa.runtime.decorator.package import OEHasPackage
  11. from oeqa.utils.logparser import PtestParser
  12. class PtestRunnerTest(OERuntimeTestCase):
  13. @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES')
  14. @OETestDepends(['ssh.SSHTest.test_ssh'])
  15. @OEHasPackage(['ptest-runner'])
  16. @unittest.expectedFailure
  17. def test_ptestrunner(self):
  18. status, output = self.target.run('which ptest-runner', 0)
  19. if status != 0:
  20. self.skipTest("No -ptest packages are installed in the image")
  21. test_log_dir = self.td.get('TEST_LOG_DIR', '')
  22. # The TEST_LOG_DIR maybe NULL when testimage is added after
  23. # testdata.json is generated.
  24. if not test_log_dir:
  25. test_log_dir = os.path.join(self.td.get('WORKDIR', ''), 'testimage')
  26. # Don't use self.td.get('DATETIME'), it's from testdata.json, not
  27. # up-to-date, and may cause "File exists" when re-reun.
  28. timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
  29. ptest_log_dir_link = os.path.join(test_log_dir, 'ptest_log')
  30. ptest_log_dir = '%s.%s' % (ptest_log_dir_link, timestamp)
  31. ptest_runner_log = os.path.join(ptest_log_dir, 'ptest-runner.log')
  32. status, output = self.target.run('ptest-runner', 0)
  33. os.makedirs(ptest_log_dir)
  34. with open(ptest_runner_log, 'w') as f:
  35. f.write(output)
  36. # status != 0 is OK since some ptest tests may fail
  37. self.assertTrue(status != 127, msg="Cannot execute ptest-runner!")
  38. if not hasattr(self.tc, "extraresults"):
  39. self.tc.extraresults = {}
  40. extras = self.tc.extraresults
  41. extras['ptestresult.rawlogs'] = {'log': output}
  42. # Parse and save results
  43. parser = PtestParser()
  44. results, sections = parser.parse(ptest_runner_log)
  45. parser.results_as_files(ptest_log_dir)
  46. if os.path.exists(ptest_log_dir_link):
  47. # Remove the old link to create a new one
  48. os.remove(ptest_log_dir_link)
  49. os.symlink(os.path.basename(ptest_log_dir), ptest_log_dir_link)
  50. extras['ptestresult.sections'] = sections
  51. trans = str.maketrans("()", "__")
  52. for section in results:
  53. for test in results[section]:
  54. result = results[section][test]
  55. testname = "ptestresult." + (section or "No-section") + "." + "_".join(test.translate(trans).split())
  56. extras[testname] = {'status': result}
  57. failed_tests = {}
  58. for section in results:
  59. failed_testcases = [ "_".join(test.translate(trans).split()) for test in results[section] if results[section][test] == 'fail' ]
  60. if failed_testcases:
  61. failed_tests[section] = failed_testcases
  62. failmsg = ""
  63. status, output = self.target.run('dmesg | grep "Killed process"', 0)
  64. if output:
  65. failmsg = "ERROR: Processes were killed by the OOM Killer:\n%s\n" % output
  66. if failed_tests:
  67. failmsg = failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests)
  68. if failmsg:
  69. self.fail(failmsg)