test_sysinfo.py 3.4 KB
Newer Older
1
import os
2
import tempfile
3
import shutil
4
import unittest
5

6
from avocado.core import sysinfo
7

8

9 10
class SysinfoTest(unittest.TestCase):

11 12 13
    def setUp(self):
        self.tmpdir = tempfile.mkdtemp(prefix="sysinfo_unittest")

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    def testLoggablesEqual(self):
        cmd1 = sysinfo.Command("ls -l")
        cmd2 = sysinfo.Command("ls -l")
        self.assertEqual(cmd1, cmd2)
        file1 = sysinfo.Logfile("/proc/cpuinfo")
        file2 = sysinfo.Logfile("/proc/cpuinfo")
        self.assertEqual(file1, file2)

    def testLoggablesNotEqual(self):
        cmd1 = sysinfo.Command("ls -l")
        cmd2 = sysinfo.Command("ls -la")
        self.assertNotEqual(cmd1, cmd2)
        file1 = sysinfo.Logfile("/proc/cpuinfo")
        file2 = sysinfo.Logfile("/etc/fstab")
        self.assertNotEqual(file1, file2)

    def testLoggablesSet(self):
        container = set()
        cmd1 = sysinfo.Command("ls -l")
        cmd2 = sysinfo.Command("ls -l")
        cmd3 = sysinfo.Command("ps -ef")
        cmd4 = sysinfo.Command("uname -a")
        file1 = sysinfo.Command("/proc/cpuinfo")
        file2 = sysinfo.Command("/etc/fstab")
        file3 = sysinfo.Command("/etc/fstab")
        file4 = sysinfo.Command("/etc/fstab")
        # From the above 8 objects, only 5 are unique loggables.
        container.add(cmd1)
        container.add(cmd2)
        container.add(cmd3)
        container.add(cmd4)
        container.add(file1)
        container.add(file2)
        container.add(file3)
        container.add(file4)

        self.assertEqual(len(container), 5)

52 53 54 55 56
    def test_logger_job_hooks(self):
        jobdir = os.path.join(self.tmpdir, 'job')
        sysinfo_logger = sysinfo.SysInfo(basedir=jobdir)
        sysinfo_logger.start_job_hook()
        self.assertTrue(os.path.isdir(jobdir))
57 58
        self.assertGreaterEqual(len(os.listdir(jobdir)), 1,
                                "Job does not have 'pre' dir")
59 60 61 62 63 64 65 66 67 68 69
        job_predir = os.path.join(jobdir, 'pre')
        self.assertTrue(os.path.isdir(job_predir))
        sysinfo_logger.end_job_hook()
        job_postdir = os.path.join(jobdir, 'post')
        self.assertTrue(os.path.isdir(job_postdir))

    def test_logger_test_hooks(self):
        testdir = os.path.join(self.tmpdir, 'job', 'test1')
        sysinfo_logger = sysinfo.SysInfo(basedir=testdir)
        sysinfo_logger.start_test_hook()
        self.assertTrue(os.path.isdir(testdir))
70 71
        self.assertGreaterEqual(len(os.listdir(testdir)), 1,
                                "Test does not have 'pre' dir")
72 73 74 75 76 77
        test_predir = os.path.join(testdir, 'pre')
        self.assertTrue(os.path.isdir(test_predir))
        # By default, there are no pre test files
        self.assertEqual(len(os.listdir(test_predir)), 0,
                         "Test pre dir is not empty")
        sysinfo_logger.end_test_hook()
78 79
        self.assertGreaterEqual(len(os.listdir(testdir)), 2,
                                "Test does not have 'pre' dir")
80 81 82
        job_postdir = os.path.join(testdir, 'post')
        self.assertTrue(os.path.isdir(job_postdir))
        # By default, there are no post test files
83 84
        self.assertLess(len(os.listdir(job_postdir)), 3,
                        "Post dir can contain 0-2 files depending on whether "
85 86
                        "sys messages are obtainable or not:\n%s"
                        % os.listdir(job_postdir))
87 88

    def tearDown(self):
89
        shutil.rmtree(self.tmpdir)
90

L
Lukáš Doktor 已提交
91

92
if __name__ == '__main__':
93
    unittest.main()