Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 5 | import os |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 6 | import unittest |
| 7 | import pprint |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 8 | import datetime |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 9 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 10 | from oeqa.runtime.case import OERuntimeTestCase |
| 11 | from oeqa.core.decorator.depends import OETestDepends |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 12 | from oeqa.core.decorator.data import skipIfNotFeature |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 13 | from oeqa.runtime.decorator.package import OEHasPackage |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 14 | from oeqa.utils.logparser import PtestParser |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 15 | |
| 16 | class PtestRunnerTest(OERuntimeTestCase): |
| 17 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 19 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 20 | @OEHasPackage(['ptest-runner']) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 21 | @unittest.expectedFailure |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 22 | 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 36 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 40 | 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 Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 47 | timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 48 | ptest_log_dir_link = os.path.join(test_log_dir, 'ptest_log') |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 49 | ptest_log_dir = '%s.%s' % (ptest_log_dir_link, timestamp) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 50 | ptest_runner_log = os.path.join(ptest_log_dir, 'ptest-runner.log') |
| 51 | |
Andrew Geissler | 4b740dc | 2020-05-05 08:54:39 -0500 | [diff] [blame] | 52 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 57 | 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 Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 64 | if not hasattr(self.tc, "extraresults"): |
| 65 | self.tc.extraresults = {} |
| 66 | extras = self.tc.extraresults |
| 67 | extras['ptestresult.rawlogs'] = {'log': output} |
| 68 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 69 | # Parse and save results |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 70 | parser = PtestParser() |
| 71 | results, sections = parser.parse(ptest_runner_log) |
| 72 | parser.results_as_files(ptest_log_dir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 73 | 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 77 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 78 | extras['ptestresult.sections'] = sections |
| 79 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 80 | trans = str.maketrans("()", "__") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 81 | 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 Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 86 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 87 | failed_tests = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 88 | |
| 89 | for section in sections: |
| 90 | if 'exitcode' in sections[section].keys(): |
| 91 | failed_tests[section] = sections[section]["log"] |
| 92 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 93 | for section in results: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 94 | failed_testcases = [ "_".join(test.translate(trans).split()) for test in results[section] if results[section][test] == 'FAILED' ] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 95 | if failed_testcases: |
| 96 | failed_tests[section] = failed_testcases |
| 97 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 98 | 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 Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 103 | if failed_tests: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 104 | failmsg = failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests) |
| 105 | |
| 106 | if failmsg: |
| 107 | self.fail(failmsg) |