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