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)
82            msg = 'File could not be copied. Output: %s' % output
83            self.assertEqual(status, 0, msg=msg)
84
85            parser = LtpParser()
86            results, sections  = parser.parse(dst)
87
88            runtime = int(endtime-starttime)
89            sections['duration'] = runtime
90            self.sections[ltp_group] =  sections
91
92            failed_tests = {}
93            for test in results:
94                result = results[test]
95                testname = ("ltpresult." + ltp_group + "." + test)
96                self.extras[testname] = {'status': result}
97                if result == 'FAILED':
98                    failed_tests[ltp_group] = test
99
100            if failed_tests:
101                self.failmsg = self.failmsg + "Failed ptests:\n%s" % pprint.pformat(failed_tests)
102
103    # LTP runtime tests
104    @OETestDepends(['ssh.SSHTest.test_ssh'])
105    @OEHasPackage(["ltp"])
106    def test_ltp_help(self):
107        (status, output) = self.target.run('/opt/ltp/runltp --help')
108        msg = 'Failed to get ltp help. Output: %s' % output
109        self.assertEqual(status, 0, msg=msg)
110
111    @OETestDepends(['ltp.LtpTest.test_ltp_help'])
112    def test_ltp_groups(self):
113        for ltp_group in self.ltp_groups:
114            self.runltp(ltp_group)
115
116    @OETestDepends(['ltp.LtpTest.test_ltp_groups'])
117    def test_ltp_runltp_cve(self):
118        self.runltp("cve")
119