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            cmd = '/opt/ltp/runltp -f %s -p -q -r /opt/ltp -l /opt/ltp/results/%s -I 1 -d /opt/ltp' % (ltp_group, ltp_group)
69            starttime = time.time()
70            (status, output) = self.target.run(cmd)
71            endtime = time.time()
72
73            with open(os.path.join(self.ltptest_log_dir, "%s-raw.log" % ltp_group), 'w') as f:
74                f.write(output)
75
76            self.extras['ltpresult.rawlogs']['log'] = self.extras['ltpresult.rawlogs']['log'] + output
77
78            # copy nice log from DUT
79            dst = os.path.join(self.ltptest_log_dir, "%s" %  ltp_group )
80            remote_src = "/opt/ltp/results/%s" % ltp_group
81            (status, output) = self.target.copyFrom(remote_src, dst, True)
82            msg = 'File could not be copied. Output: %s' % output
83            if status:
84                self.target.logger.warning(msg)
85
86            parser = LtpParser()
87            results, sections  = parser.parse(dst)
88
89            runtime = int(endtime-starttime)
90            sections['duration'] = runtime
91            self.sections[ltp_group] =  sections
92
93            failed_tests = {}
94            for test in results:
95                result = results[test]
96                testname = ("ltpresult." + ltp_group + "." + test)
97                self.extras[testname] = {'status': result}
98                if result == 'FAILED':
99                    failed_tests[ltp_group] = test
100
101            if failed_tests:
102                self.failmsg = self.failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests)
103
104    # LTP runtime tests
105    @OETestDepends(['ssh.SSHTest.test_ssh'])
106    @OEHasPackage(["ltp"])
107    def test_ltp_help(self):
108        (status, output) = self.target.run('/opt/ltp/runltp --help')
109        msg = 'Failed to get ltp help. Output: %s' % output
110        self.assertEqual(status, 0, msg=msg)
111
112    @OETestDepends(['ltp.LtpTest.test_ltp_help'])
113    def test_ltp_groups(self):
114        for ltp_group in self.ltp_groups:
115            self.runltp(ltp_group)
116
117    @OETestDepends(['ltp.LtpTest.test_ltp_groups'])
118    def test_ltp_runltp_cve(self):
119        self.runltp("cve")
120