aboutsummaryrefslogtreecommitdiff
path: root/client/common_lib/hosts/base_classes_unittest.py
blob: 9b773e6f200f9f3cc7b1fe597833e56aab29bb50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python2

import common
import os
import unittest

from autotest_lib.client.common_lib import error, utils
from autotest_lib.client.common_lib.test_utils import mock
from autotest_lib.client.common_lib.hosts import base_classes


class test_host_class(unittest.TestCase):
    def setUp(self):
        self.god = mock.mock_god()


    def tearDown(self):
        self.god.unstub_all()


    def test_run_output_notimplemented(self):
        host = base_classes.Host()
        self.assertRaises(NotImplementedError, host.run_output, "fake command")


    def _setup_test_check_diskspace(self, command, command_result,
                                    command_exit_status, directory,
                                    directory_exists):
        self.god.stub_function(os.path, 'isdir')
        self.god.stub_function(base_classes.Host, 'run')
        host = base_classes.Host()
        host.hostname = 'unittest-host'
        host.run.expect_call(
                'test -e "%s"' % directory,
                ignore_status=True).and_return(
                        utils.CmdResult(
                                exit_status = 0 if directory_exists else 1))
        if directory_exists:
            fake_cmd_status = utils.CmdResult(
                exit_status=command_exit_status, stdout=command_result)
            host.run.expect_call(command).and_return(fake_cmd_status)
        return host


    def test_check_diskspace(self):
        df_cmd = 'df -PB 1000000 /foo | tail -1'
        test_df_tail = ('/dev/sda1                    1061       939'
                        '       123      89% /')
        host = self._setup_test_check_diskspace(
            df_cmd, test_df_tail, 0, '/foo', True)
        host.check_diskspace('/foo', 0.1)
        self.god.check_playback()


    def test_check_diskspace_disk_full(self):
        df_cmd = 'df -PB 1000000 /foo | tail -1'
        test_df_tail = ('/dev/sda1                    1061       939'
                        '       123      89% /')
        host = self._setup_test_check_diskspace(
            df_cmd, test_df_tail, 0, '/foo', True)
        self.assertRaises(error.AutoservDiskFullHostError,
                          host.check_diskspace, '/foo', 0.2)
        self.god.check_playback()


    def test_check_diskspace_directory_not_found(self):
        df_cmd = 'df -PB 1000000 /foo | tail -1'
        test_df_tail = ('/dev/sda1                    1061       939'
                        '       123      89% /')
        host = self._setup_test_check_diskspace(
            df_cmd, test_df_tail, 0, '/foo', False)
        self.assertRaises(error.AutoservDirectoryNotFoundError,
                          host.check_diskspace, '/foo', 0.2)
        self.god.check_playback()


    def test_check_diskspace_directory_du_failed(self):
        df_cmd = 'df -PB 1000000 /foo | tail -1'
        test_df_tail = ('df: /foo: No such file or directory')
        host = self._setup_test_check_diskspace(
            df_cmd, test_df_tail, 1, '/foo', True)
        self.assertRaises(error.AutoservDiskSizeUnknownError,
                          host.check_diskspace, '/foo', 0.1)
        self.god.check_playback()


if __name__ == "__main__":
    unittest.main()