Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame^] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 7 | import os |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 8 | import unittest |
| 9 | import pprint |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 10 | import datetime |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 11 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 12 | from oeqa.runtime.case import OERuntimeTestCase |
| 13 | from oeqa.core.decorator.depends import OETestDepends |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 14 | from oeqa.core.decorator.data import skipIfNotFeature |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 15 | from oeqa.runtime.decorator.package import OEHasPackage |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 16 | from oeqa.utils.logparser import PtestParser |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 17 | |
| 18 | class PtestRunnerTest(OERuntimeTestCase): |
| 19 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 20 | @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 21 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 22 | @OEHasPackage(['ptest-runner']) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 23 | @unittest.expectedFailure |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 24 | def test_ptestrunner_expectfail(self): |
| 25 | if not self.td.get('PTEST_EXPECT_FAILURE'): |
| 26 | self.skipTest('Cannot run ptests with @expectedFailure as ptests are required to pass') |
| 27 | self.do_ptestrunner() |
| 28 | |
| 29 | @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES') |
| 30 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
| 31 | @OEHasPackage(['ptest-runner']) |
| 32 | def test_ptestrunner_expectsuccess(self): |
| 33 | if self.td.get('PTEST_EXPECT_FAILURE'): |
| 34 | self.skipTest('Cannot run ptests without @expectedFailure as ptests are expected to fail') |
| 35 | self.do_ptestrunner() |
| 36 | |
| 37 | def do_ptestrunner(self): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 38 | status, output = self.target.run('which ptest-runner', 0) |
| 39 | if status != 0: |
| 40 | self.skipTest("No -ptest packages are installed in the image") |
| 41 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 42 | test_log_dir = self.td.get('TEST_LOG_DIR', '') |
| 43 | # The TEST_LOG_DIR maybe NULL when testimage is added after |
| 44 | # testdata.json is generated. |
| 45 | if not test_log_dir: |
| 46 | test_log_dir = os.path.join(self.td.get('WORKDIR', ''), 'testimage') |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 47 | # Make the test output path absolute, otherwise the output content will be |
| 48 | # created relative to current directory |
| 49 | if not os.path.isabs(test_log_dir): |
| 50 | 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] | 51 | # Don't use self.td.get('DATETIME'), it's from testdata.json, not |
| 52 | # up-to-date, and may cause "File exists" when re-reun. |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 53 | timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | ptest_log_dir_link = os.path.join(test_log_dir, 'ptest_log') |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 55 | ptest_log_dir = '%s.%s' % (ptest_log_dir_link, timestamp) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 56 | ptest_runner_log = os.path.join(ptest_log_dir, 'ptest-runner.log') |
| 57 | |
Andrew Geissler | 4b740dc | 2020-05-05 08:54:39 -0500 | [diff] [blame] | 58 | libdir = self.td.get('libdir', '') |
| 59 | ptest_dirs = [ '/usr/lib' ] |
| 60 | if not libdir in ptest_dirs: |
| 61 | ptest_dirs.append(libdir) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 62 | status, output = self.target.run('ptest-runner -t 450 -d \"{}\"'.format(' '.join(ptest_dirs)), 0) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 63 | os.makedirs(ptest_log_dir) |
| 64 | with open(ptest_runner_log, 'w') as f: |
| 65 | f.write(output) |
| 66 | |
| 67 | # status != 0 is OK since some ptest tests may fail |
| 68 | self.assertTrue(status != 127, msg="Cannot execute ptest-runner!") |
| 69 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 70 | if not hasattr(self.tc, "extraresults"): |
| 71 | self.tc.extraresults = {} |
| 72 | extras = self.tc.extraresults |
| 73 | extras['ptestresult.rawlogs'] = {'log': output} |
| 74 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | # Parse and save results |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 76 | parser = PtestParser() |
| 77 | results, sections = parser.parse(ptest_runner_log) |
| 78 | parser.results_as_files(ptest_log_dir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 79 | if os.path.exists(ptest_log_dir_link): |
| 80 | # Remove the old link to create a new one |
| 81 | os.remove(ptest_log_dir_link) |
| 82 | os.symlink(os.path.basename(ptest_log_dir), ptest_log_dir_link) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 83 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 84 | extras['ptestresult.sections'] = sections |
| 85 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 86 | trans = str.maketrans("()", "__") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 87 | for section in results: |
| 88 | for test in results[section]: |
| 89 | result = results[section][test] |
| 90 | testname = "ptestresult." + (section or "No-section") + "." + "_".join(test.translate(trans).split()) |
| 91 | extras[testname] = {'status': result} |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 92 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 93 | failed_tests = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 94 | |
| 95 | for section in sections: |
| 96 | if 'exitcode' in sections[section].keys(): |
| 97 | failed_tests[section] = sections[section]["log"] |
| 98 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 99 | for section in results: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 100 | 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] | 101 | if failed_testcases: |
| 102 | failed_tests[section] = failed_testcases |
| 103 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 104 | failmsg = "" |
| 105 | status, output = self.target.run('dmesg | grep "Killed process"', 0) |
| 106 | if output: |
| 107 | failmsg = "ERROR: Processes were killed by the OOM Killer:\n%s\n" % output |
| 108 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 109 | if failed_tests: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 110 | failmsg = failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests) |
| 111 | |
| 112 | if failmsg: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 113 | self.logger.warning("There were failing ptests.") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 114 | self.fail(failmsg) |