Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 1 | import unittest |
| 2 | import pprint |
| 3 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 4 | from oeqa.runtime.case import OERuntimeTestCase |
| 5 | from oeqa.core.decorator.depends import OETestDepends |
| 6 | from oeqa.core.decorator.oeid import OETestID |
| 7 | from oeqa.core.decorator.data import skipIfNotFeature |
| 8 | from oeqa.utils.logparser import Lparser, Result |
| 9 | |
| 10 | class PtestRunnerTest(OERuntimeTestCase): |
| 11 | |
| 12 | # a ptest log parser |
| 13 | def parse_ptest(self, logfile): |
| 14 | parser = Lparser(test_0_pass_regex="^PASS:(.+)", |
| 15 | test_0_fail_regex="^FAIL:(.+)", |
| 16 | test_0_skip_regex="^SKIP:(.+)", |
| 17 | section_0_begin_regex="^BEGIN: .*/(.+)/ptest", |
| 18 | section_0_end_regex="^END: .*/(.+)/ptest") |
| 19 | parser.init() |
| 20 | result = Result() |
| 21 | |
| 22 | with open(logfile, errors='replace') as f: |
| 23 | for line in f: |
| 24 | result_tuple = parser.parse_line(line) |
| 25 | if not result_tuple: |
| 26 | continue |
| 27 | result_tuple = line_type, category, status, name = parser.parse_line(line) |
| 28 | |
| 29 | if line_type == 'section' and status == 'begin': |
| 30 | current_section = name |
| 31 | continue |
| 32 | |
| 33 | if line_type == 'section' and status == 'end': |
| 34 | current_section = None |
| 35 | continue |
| 36 | |
| 37 | if line_type == 'test' and status == 'pass': |
| 38 | result.store(current_section, name, status) |
| 39 | continue |
| 40 | |
| 41 | if line_type == 'test' and status == 'fail': |
| 42 | result.store(current_section, name, status) |
| 43 | continue |
| 44 | |
| 45 | if line_type == 'test' and status == 'skip': |
| 46 | result.store(current_section, name, status) |
| 47 | continue |
| 48 | |
| 49 | result.sort_tests() |
| 50 | return result |
| 51 | |
| 52 | @OETestID(1600) |
| 53 | @skipIfNotFeature('ptest', 'Test requires ptest to be in DISTRO_FEATURES') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 55 | @unittest.expectedFailure |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 56 | def test_ptestrunner(self): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 57 | status, output = self.target.run('which ptest-runner', 0) |
| 58 | if status != 0: |
| 59 | self.skipTest("No -ptest packages are installed in the image") |
| 60 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 61 | import datetime |
| 62 | |
| 63 | test_log_dir = self.td.get('TEST_LOG_DIR', '') |
| 64 | # The TEST_LOG_DIR maybe NULL when testimage is added after |
| 65 | # testdata.json is generated. |
| 66 | if not test_log_dir: |
| 67 | test_log_dir = os.path.join(self.td.get('WORKDIR', ''), 'testimage') |
| 68 | # Don't use self.td.get('DATETIME'), it's from testdata.json, not |
| 69 | # up-to-date, and may cause "File exists" when re-reun. |
| 70 | datetime = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
| 71 | ptest_log_dir_link = os.path.join(test_log_dir, 'ptest_log') |
| 72 | ptest_log_dir = '%s.%s' % (ptest_log_dir_link, datetime) |
| 73 | ptest_runner_log = os.path.join(ptest_log_dir, 'ptest-runner.log') |
| 74 | |
| 75 | status, output = self.target.run('ptest-runner', 0) |
| 76 | os.makedirs(ptest_log_dir) |
| 77 | with open(ptest_runner_log, 'w') as f: |
| 78 | f.write(output) |
| 79 | |
| 80 | # status != 0 is OK since some ptest tests may fail |
| 81 | self.assertTrue(status != 127, msg="Cannot execute ptest-runner!") |
| 82 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 83 | if not hasattr(self.tc, "extraresults"): |
| 84 | self.tc.extraresults = {} |
| 85 | extras = self.tc.extraresults |
| 86 | extras['ptestresult.rawlogs'] = {'log': output} |
| 87 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 88 | # Parse and save results |
| 89 | parse_result = self.parse_ptest(ptest_runner_log) |
| 90 | parse_result.log_as_files(ptest_log_dir, test_status = ['pass','fail', 'skip']) |
| 91 | if os.path.exists(ptest_log_dir_link): |
| 92 | # Remove the old link to create a new one |
| 93 | os.remove(ptest_log_dir_link) |
| 94 | os.symlink(os.path.basename(ptest_log_dir), ptest_log_dir_link) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 95 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 96 | trans = str.maketrans("()", "__") |
| 97 | resmap = {'pass': 'PASSED', 'skip': 'SKIPPED', 'fail': 'FAILED'} |
| 98 | for section in parse_result.result_dict: |
| 99 | for test, result in parse_result.result_dict[section]: |
| 100 | testname = "ptestresult." + section + "." + "_".join(test.translate(trans).split()) |
| 101 | extras[testname] = {'status': resmap[result]} |
| 102 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 103 | failed_tests = {} |
| 104 | for section in parse_result.result_dict: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 105 | failed_testcases = [ "_".join(test.translate(trans).split()) for test, result in parse_result.result_dict[section] if result == 'fail' ] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 106 | if failed_testcases: |
| 107 | failed_tests[section] = failed_testcases |
| 108 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 109 | if failed_tests: |
| 110 | self.fail("Failed ptests:\n%s" % pprint.pformat(failed_tests)) |