1# LTP runtime
2#
3# Copyright (c) 2019 MontaVista Software, LLC
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7
8import time
9import datetime
10import pprint
11
12from oeqa.runtime.case import OERuntimeTestCase
13from oeqa.core.decorator.depends import OETestDepends
14from oeqa.runtime.decorator.package import OEHasPackage
15from oeqa.utils.logparser import LtpParser
16
17class LtpTestBase(OERuntimeTestCase):
18
19    @classmethod
20    def setUpClass(cls):
21        cls.ltp_startup()
22
23    @classmethod
24    def tearDownClass(cls):
25        cls.ltp_finishup()
26
27    @classmethod
28    def ltp_startup(cls):
29        cls.sections = {}
30        cls.failmsg = ""
31        test_log_dir = os.path.join(cls.td.get('WORKDIR', ''), 'testimage')
32        timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
33
34        cls.ltptest_log_dir_link = os.path.join(test_log_dir, 'ltp_log')
35        cls.ltptest_log_dir = '%s.%s' % (cls.ltptest_log_dir_link, timestamp)
36        os.makedirs(cls.ltptest_log_dir)
37
38        cls.tc.target.run("mkdir -p /opt/ltp/results")
39
40        if not hasattr(cls.tc, "extraresults"):
41            cls.tc.extraresults = {}
42        cls.extras = cls.tc.extraresults
43        cls.extras['ltpresult.rawlogs'] = {'log': ""}
44
45
46    @classmethod
47    def ltp_finishup(cls):
48        cls.extras['ltpresult.sections'] =  cls.sections
49
50        # update symlink to ltp_log
51        if os.path.exists(cls.ltptest_log_dir_link):
52            os.remove(cls.ltptest_log_dir_link)
53        os.symlink(os.path.basename(cls.ltptest_log_dir), cls.ltptest_log_dir_link)
54
55        if cls.failmsg:
56            cls.fail(cls.failmsg)
57
58class LtpTest(LtpTestBase):
59
60    ltp_groups = ["math", "syscalls", "dio", "io", "mm", "ipc", "sched", "nptl", "pty", "containers", "controllers", "filecaps", "cap_bounds", "fcntl-locktests", "connectors", "commands", "net.ipv6_lib", "input","fs_perms_simple"]
61
62    ltp_fs = ["fs", "fsx", "fs_bind"]
63    # skip kernel cpuhotplug
64    ltp_kernel = ["power_management_tests", "hyperthreading ", "kernel_misc", "hugetlb"]
65    ltp_groups += ltp_fs
66
67    def runltp(self, ltp_group):
68            # LTP appends to log files, so ensure we start with a clean log
69            self.target.deleteFiles("/opt/ltp/results/", ltp_group)
70
71            cmd = '/opt/ltp/runltp -f %s -q -r /opt/ltp -l /opt/ltp/results/%s -I 1 -d /opt/ltp' % (ltp_group, ltp_group)
72
73            starttime = time.time()
74            (status, output) = self.target.run(cmd, timeout=1200)
75            endtime = time.time()
76
77            # status of 1 is 'just' tests failing. 255 likely was a command output timeout
78            if status and status != 1:
79                msg = 'Command %s returned exit code %s' % (cmd, status)
80                self.target.logger.warning(msg)
81
82            # Write the console log to disk for convenience
83            with open(os.path.join(self.ltptest_log_dir, "%s-raw.log" % ltp_group), 'w') as f:
84                f.write(output)
85
86            # Also put the console log into the test result JSON
87            self.extras['ltpresult.rawlogs']['log'] = self.extras['ltpresult.rawlogs']['log'] + output
88
89            # Copy the machine-readable test results locally so we can parse it
90            dst = os.path.join(self.ltptest_log_dir, ltp_group)
91            remote_src = "/opt/ltp/results/%s" % ltp_group
92            (status, output) = self.target.copyFrom(remote_src, dst, True)
93            if status:
94                msg = 'File could not be copied. Output: %s' % output
95                self.target.logger.warning(msg)
96
97            parser = LtpParser()
98            results, sections  = parser.parse(dst)
99
100            sections['duration'] = int(endtime-starttime)
101            self.sections[ltp_group] =  sections
102
103            failed_tests = {}
104            for test in results:
105                result = results[test]
106                testname = ("ltpresult." + ltp_group + "." + test)
107                self.extras[testname] = {'status': result}
108                if result == 'FAILED':
109                    failed_tests[ltp_group] = test
110
111            if failed_tests:
112                self.failmsg = self.failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests)
113
114    # LTP runtime tests
115    @OETestDepends(['ssh.SSHTest.test_ssh'])
116    @OEHasPackage(["ltp"])
117    def test_ltp_help(self):
118        (status, output) = self.target.run('/opt/ltp/runltp --help')
119        msg = 'Failed to get ltp help. Output: %s' % output
120        self.assertEqual(status, 0, msg=msg)
121
122    @OETestDepends(['ltp.LtpTest.test_ltp_help'])
123    def test_ltp_groups(self):
124        for ltp_group in self.ltp_groups:
125            self.runltp(ltp_group)
126
127    @OETestDepends(['ltp.LtpTest.test_ltp_groups'])
128    def test_ltp_runltp_cve(self):
129        self.runltp("cve")
130