blob: 1b1474adcf10192aebf18ef9f7b5b827bed0eabf [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
Andrew Geissler4b740dc2020-05-05 08:54:39 -050052 libdir = self.td.get('libdir', '')
53 ptest_dirs = [ '/usr/lib' ]
54 if not libdir in ptest_dirs:
55 ptest_dirs.append(libdir)
56 status, output = self.target.run('ptest-runner -d \"{}\"'.format(' '.join(ptest_dirs)), 0)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057 os.makedirs(ptest_log_dir)
58 with open(ptest_runner_log, 'w') as f:
59 f.write(output)
60
61 # status != 0 is OK since some ptest tests may fail
62 self.assertTrue(status != 127, msg="Cannot execute ptest-runner!")
63
Brad Bishopf86d0552018-12-04 14:18:15 -080064 if not hasattr(self.tc, "extraresults"):
65 self.tc.extraresults = {}
66 extras = self.tc.extraresults
67 extras['ptestresult.rawlogs'] = {'log': output}
68
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069 # Parse and save results
Andrew Geissler99467da2019-02-25 18:54:23 -060070 parser = PtestParser()
71 results, sections = parser.parse(ptest_runner_log)
72 parser.results_as_files(ptest_log_dir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073 if os.path.exists(ptest_log_dir_link):
74 # Remove the old link to create a new one
75 os.remove(ptest_log_dir_link)
76 os.symlink(os.path.basename(ptest_log_dir), ptest_log_dir_link)
Brad Bishop316dfdd2018-06-25 12:45:53 -040077
Andrew Geissler99467da2019-02-25 18:54:23 -060078 extras['ptestresult.sections'] = sections
79
Brad Bishopf86d0552018-12-04 14:18:15 -080080 trans = str.maketrans("()", "__")
Andrew Geissler99467da2019-02-25 18:54:23 -060081 for section in results:
82 for test in results[section]:
83 result = results[section][test]
84 testname = "ptestresult." + (section or "No-section") + "." + "_".join(test.translate(trans).split())
85 extras[testname] = {'status': result}
Brad Bishopf86d0552018-12-04 14:18:15 -080086
Brad Bishop316dfdd2018-06-25 12:45:53 -040087 failed_tests = {}
Andrew Geissler82c905d2020-04-13 13:39:40 -050088
89 for section in sections:
90 if 'exitcode' in sections[section].keys():
91 failed_tests[section] = sections[section]["log"]
92
Andrew Geissler99467da2019-02-25 18:54:23 -060093 for section in results:
Andrew Geissler82c905d2020-04-13 13:39:40 -050094 failed_testcases = [ "_".join(test.translate(trans).split()) for test in results[section] if results[section][test] == 'FAILED' ]
Brad Bishop316dfdd2018-06-25 12:45:53 -040095 if failed_testcases:
96 failed_tests[section] = failed_testcases
97
Andrew Geissler99467da2019-02-25 18:54:23 -060098 failmsg = ""
99 status, output = self.target.run('dmesg | grep "Killed process"', 0)
100 if output:
101 failmsg = "ERROR: Processes were killed by the OOM Killer:\n%s\n" % output
102
Brad Bishopf86d0552018-12-04 14:18:15 -0800103 if failed_tests:
Andrew Geissler99467da2019-02-25 18:54:23 -0600104 failmsg = failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests)
105
106 if failmsg:
107 self.fail(failmsg)