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') |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 45 | # Make the test output path absolute, otherwise the output content will be |
| 46 | # created relative to current directory |
| 47 | if not os.path.isabs(test_log_dir): |
| 48 | test_log_dir = os.path.join(self.td.get('TOPDIR', ''), test_log_dir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 49 | # Don't use self.td.get('DATETIME'), it's from testdata.json, not |
| 50 | # up-to-date, and may cause "File exists" when re-reun. |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 51 | timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 52 | ptest_log_dir_link = os.path.join(test_log_dir, 'ptest_log') |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 53 | ptest_log_dir = '%s.%s' % (ptest_log_dir_link, timestamp) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | ptest_runner_log = os.path.join(ptest_log_dir, 'ptest-runner.log') |
| 55 | |
Andrew Geissler | 4b740dc | 2020-05-05 08:54:39 -0500 | [diff] [blame] | 56 | libdir = self.td.get('libdir', '') |
| 57 | ptest_dirs = [ '/usr/lib' ] |
| 58 | if not libdir in ptest_dirs: |
| 59 | ptest_dirs.append(libdir) |
| 60 | 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] | 61 | os.makedirs(ptest_log_dir) |
| 62 | with open(ptest_runner_log, 'w') as f: |
| 63 | f.write(output) |
| 64 | |
| 65 | # status != 0 is OK since some ptest tests may fail |
| 66 | self.assertTrue(status != 127, msg="Cannot execute ptest-runner!") |
| 67 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 68 | if not hasattr(self.tc, "extraresults"): |
| 69 | self.tc.extraresults = {} |
| 70 | extras = self.tc.extraresults |
| 71 | extras['ptestresult.rawlogs'] = {'log': output} |
| 72 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 73 | # Parse and save results |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 74 | parser = PtestParser() |
| 75 | results, sections = parser.parse(ptest_runner_log) |
| 76 | parser.results_as_files(ptest_log_dir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | if os.path.exists(ptest_log_dir_link): |
| 78 | # Remove the old link to create a new one |
| 79 | os.remove(ptest_log_dir_link) |
| 80 | os.symlink(os.path.basename(ptest_log_dir), ptest_log_dir_link) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 81 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 82 | extras['ptestresult.sections'] = sections |
| 83 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 84 | trans = str.maketrans("()", "__") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 85 | for section in results: |
| 86 | for test in results[section]: |
| 87 | result = results[section][test] |
| 88 | testname = "ptestresult." + (section or "No-section") + "." + "_".join(test.translate(trans).split()) |
| 89 | extras[testname] = {'status': result} |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 90 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 91 | failed_tests = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 92 | |
| 93 | for section in sections: |
| 94 | if 'exitcode' in sections[section].keys(): |
| 95 | failed_tests[section] = sections[section]["log"] |
| 96 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 97 | for section in results: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 98 | 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] | 99 | if failed_testcases: |
| 100 | failed_tests[section] = failed_testcases |
| 101 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 102 | failmsg = "" |
| 103 | status, output = self.target.run('dmesg | grep "Killed process"', 0) |
| 104 | if output: |
| 105 | failmsg = "ERROR: Processes were killed by the OOM Killer:\n%s\n" % output |
| 106 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 107 | if failed_tests: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 108 | failmsg = failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests) |
| 109 | |
| 110 | if failmsg: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 111 | self.logger.warning("There were failing ptests.") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 112 | self.fail(failmsg) |