1#!/usr/bin/python
2
3import common
4import os
5import unittest
6
7from autotest_lib.client.common_lib import error, utils
8from autotest_lib.client.common_lib.test_utils import mock
9from autotest_lib.client.common_lib.hosts import base_classes
10
11
12class test_host_class(unittest.TestCase):
13    def setUp(self):
14        self.god = mock.mock_god()
15
16
17    def tearDown(self):
18        self.god.unstub_all()
19
20
21    def test_run_output_notimplemented(self):
22        host = base_classes.Host()
23        self.assertRaises(NotImplementedError, host.run_output, "fake command")
24
25
26    def _setup_test_check_diskspace(self, command, command_result,
27                                    command_exit_status, directory,
28                                    directory_exists):
29        self.god.stub_function(os.path, 'isdir')
30        self.god.stub_function(base_classes.Host, 'run')
31        host = base_classes.Host()
32        host.hostname = 'unittest-host'
33        host.run.expect_call(
34                'test -e "%s"' % directory,
35                ignore_status=True).and_return(
36                        utils.CmdResult(
37                                exit_status = 0 if directory_exists else 1))
38        if directory_exists:
39            fake_cmd_status = utils.CmdResult(
40                exit_status=command_exit_status, stdout=command_result)
41            host.run.expect_call(command).and_return(fake_cmd_status)
42        return host
43
44
45    def test_check_diskspace(self):
46        df_cmd = 'df -PB 1000000 /foo | tail -1'
47        test_df_tail = ('/dev/sda1                    1061       939'
48                        '       123      89% /')
49        host = self._setup_test_check_diskspace(
50            df_cmd, test_df_tail, 0, '/foo', True)
51        host.check_diskspace('/foo', 0.1)
52        self.god.check_playback()
53
54
55    def test_check_diskspace_disk_full(self):
56        df_cmd = 'df -PB 1000000 /foo | tail -1'
57        test_df_tail = ('/dev/sda1                    1061       939'
58                        '       123      89% /')
59        host = self._setup_test_check_diskspace(
60            df_cmd, test_df_tail, 0, '/foo', True)
61        self.assertRaises(error.AutoservDiskFullHostError,
62                          host.check_diskspace, '/foo', 0.2)
63        self.god.check_playback()
64
65
66    def test_check_diskspace_directory_not_found(self):
67        df_cmd = 'df -PB 1000000 /foo | tail -1'
68        test_df_tail = ('/dev/sda1                    1061       939'
69                        '       123      89% /')
70        host = self._setup_test_check_diskspace(
71            df_cmd, test_df_tail, 0, '/foo', False)
72        self.assertRaises(error.AutoservDirectoryNotFoundError,
73                          host.check_diskspace, '/foo', 0.2)
74        self.god.check_playback()
75
76
77    def test_check_diskspace_directory_du_failed(self):
78        df_cmd = 'df -PB 1000000 /foo | tail -1'
79        test_df_tail = ('df: /foo: No such file or directory')
80        host = self._setup_test_check_diskspace(
81            df_cmd, test_df_tail, 1, '/foo', True)
82        self.assertRaises(error.AutoservDiskSizeUnknownError,
83                          host.check_diskspace, '/foo', 0.1)
84        self.god.check_playback()
85
86
87if __name__ == "__main__":
88    unittest.main()
89