blob: 99a44f07670bb48dfbe229dca5068d15e2b837c7 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Andrew Geissler82c905d2020-04-13 13:39:40 -05005import os
Brad Bishopf86d0552018-12-04 14:18:15 -08006import unittest
7import pprint
Andrew Geissler99467da2019-02-25 18:54:23 -06008import datetime
Brad Bishopf86d0552018-12-04 14:18:15 -08009
Brad Bishopd7bf8c12018-02-25 22:55:05 -050010from oeqa.runtime.case import OERuntimeTestCase
11from oeqa.core.decorator.depends import OETestDepends
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012from oeqa.core.decorator.data import skipIfNotFeature
Brad Bishop977dc1a2019-02-06 16:01:43 -050013from oeqa.runtime.decorator.package import OEHasPackage
Andrew Geissler99467da2019-02-25 18:54:23 -060014from oeqa.utils.logparser import PtestParser
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015
16class PtestRunnerTest(OERuntimeTestCase):
17
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018 @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050019 @OETestDepends(['ssh.SSHTest.test_ssh'])
Brad Bishop977dc1a2019-02-06 16:01:43 -050020 @OEHasPackage(['ptest-runner'])
Brad Bishopf86d0552018-12-04 14:18:15 -080021 @unittest.expectedFailure
Andrew Geissler82c905d2020-04-13 13:39:40 -050022 def test_ptestrunner_expectfail(self):
23 if not self.td.get('PTEST_EXPECT_FAILURE'):
24 self.skipTest('Cannot run ptests with @expectedFailure as ptests are required to pass')
25 self.do_ptestrunner()
26
27 @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES')
28 @OETestDepends(['ssh.SSHTest.test_ssh'])
29 @OEHasPackage(['ptest-runner'])
30 def test_ptestrunner_expectsuccess(self):
31 if self.td.get('PTEST_EXPECT_FAILURE'):
32 self.skipTest('Cannot run ptests without @expectedFailure as ptests are expected to fail')
33 self.do_ptestrunner()
34
35 def do_ptestrunner(self):
Brad Bishop316dfdd2018-06-25 12:45:53 -040036 status, output = self.target.run('which ptest-runner', 0)
37 if status != 0:
38 self.skipTest("No -ptest packages are installed in the image")
39
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 test_log_dir = self.td.get('TEST_LOG_DIR', '')
41 # The TEST_LOG_DIR maybe NULL when testimage is added after
42 # testdata.json is generated.
43 if not test_log_dir:
44 test_log_dir = os.path.join(self.td.get('WORKDIR', ''), 'testimage')
45 # Don't use self.td.get('DATETIME'), it's from testdata.json, not
46 # up-to-date, and may cause "File exists" when re-reun.
Andrew Geissler99467da2019-02-25 18:54:23 -060047 timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048 ptest_log_dir_link = os.path.join(test_log_dir, 'ptest_log')
Andrew Geissler99467da2019-02-25 18:54:23 -060049 ptest_log_dir = '%s.%s' % (ptest_log_dir_link, timestamp)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050 ptest_runner_log = os.path.join(ptest_log_dir, 'ptest-runner.log')
51
52 status, output = self.target.run('ptest-runner', 0)
53 os.makedirs(ptest_log_dir)
54 with open(ptest_runner_log, 'w') as f:
55 f.write(output)
56
57 # status != 0 is OK since some ptest tests may fail
58 self.assertTrue(status != 127, msg="Cannot execute ptest-runner!")
59
Brad Bishopf86d0552018-12-04 14:18:15 -080060 if not hasattr(self.tc, "extraresults"):
61 self.tc.extraresults = {}
62 extras = self.tc.extraresults
63 extras['ptestresult.rawlogs'] = {'log': output}
64
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065 # Parse and save results
Andrew Geissler99467da2019-02-25 18:54:23 -060066 parser = PtestParser()
67 results, sections = parser.parse(ptest_runner_log)
68 parser.results_as_files(ptest_log_dir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069 if os.path.exists(ptest_log_dir_link):
70 # Remove the old link to create a new one
71 os.remove(ptest_log_dir_link)
72 os.symlink(os.path.basename(ptest_log_dir), ptest_log_dir_link)
Brad Bishop316dfdd2018-06-25 12:45:53 -040073
Andrew Geissler99467da2019-02-25 18:54:23 -060074 extras['ptestresult.sections'] = sections
75
Brad Bishopf86d0552018-12-04 14:18:15 -080076 trans = str.maketrans("()", "__")
Andrew Geissler99467da2019-02-25 18:54:23 -060077 for section in results:
78 for test in results[section]:
79 result = results[section][test]
80 testname = "ptestresult." + (section or "No-section") + "." + "_".join(test.translate(trans).split())
81 extras[testname] = {'status': result}
Brad Bishopf86d0552018-12-04 14:18:15 -080082
Brad Bishop316dfdd2018-06-25 12:45:53 -040083 failed_tests = {}
Andrew Geissler82c905d2020-04-13 13:39:40 -050084
85 for section in sections:
86 if 'exitcode' in sections[section].keys():
87 failed_tests[section] = sections[section]["log"]
88
Andrew Geissler99467da2019-02-25 18:54:23 -060089 for section in results:
Andrew Geissler82c905d2020-04-13 13:39:40 -050090 failed_testcases = [ "_".join(test.translate(trans).split()) for test in results[section] if results[section][test] == 'FAILED' ]
Brad Bishop316dfdd2018-06-25 12:45:53 -040091 if failed_testcases:
92 failed_tests[section] = failed_testcases
93
Andrew Geissler99467da2019-02-25 18:54:23 -060094 failmsg = ""
95 status, output = self.target.run('dmesg | grep "Killed process"', 0)
96 if output:
97 failmsg = "ERROR: Processes were killed by the OOM Killer:\n%s\n" % output
98
Brad Bishopf86d0552018-12-04 14:18:15 -080099 if failed_tests:
Andrew Geissler99467da2019-02-25 18:54:23 -0600100 failmsg = failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests)
101
102 if failmsg:
103 self.fail(failmsg)