blob: 77ae7b6b8635e96187ff8351f8f7abce3abc05c4 [file] [log] [blame]
Brad Bishopf86d0552018-12-04 14:18:15 -08001import unittest
2import pprint
3
Brad Bishopd7bf8c12018-02-25 22:55:05 -05004from oeqa.runtime.case import OERuntimeTestCase
5from oeqa.core.decorator.depends import OETestDepends
6from oeqa.core.decorator.oeid import OETestID
7from oeqa.core.decorator.data import skipIfNotFeature
8from oeqa.utils.logparser import Lparser, Result
9
10class 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 Bishopd7bf8c12018-02-25 22:55:05 -050054 @OETestDepends(['ssh.SSHTest.test_ssh'])
Brad Bishopf86d0552018-12-04 14:18:15 -080055 @unittest.expectedFailure
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056 def test_ptestrunner(self):
Brad Bishop316dfdd2018-06-25 12:45:53 -040057 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 Bishopd7bf8c12018-02-25 22:55:05 -050061 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 Bishopf86d0552018-12-04 14:18:15 -080083 if not hasattr(self.tc, "extraresults"):
84 self.tc.extraresults = {}
85 extras = self.tc.extraresults
86 extras['ptestresult.rawlogs'] = {'log': output}
87
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 # 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 Bishop316dfdd2018-06-25 12:45:53 -040095
Brad Bishopf86d0552018-12-04 14:18:15 -080096 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 Bishop316dfdd2018-06-25 12:45:53 -0400103 failed_tests = {}
104 for section in parse_result.result_dict:
Brad Bishopf86d0552018-12-04 14:18:15 -0800105 failed_testcases = [ "_".join(test.translate(trans).split()) for test, result in parse_result.result_dict[section] if result == 'fail' ]
Brad Bishop316dfdd2018-06-25 12:45:53 -0400106 if failed_testcases:
107 failed_tests[section] = failed_testcases
108
Brad Bishopf86d0552018-12-04 14:18:15 -0800109 if failed_tests:
110 self.fail("Failed ptests:\n%s" % pprint.pformat(failed_tests))