• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3import os
4import common
5
6from autotest_lib.client.common_lib.test_utils import mock, unittest
7from autotest_lib.client.common_lib import autotemp
8from autotest_lib.client.bin import local_host
9
10
11class test_local_host_class(unittest.TestCase):
12    def setUp(self):
13        self.god = mock.mock_god()
14        self.god.stub_function(local_host.utils, 'run')
15
16        self.tmpdir = autotemp.tempdir(unique_id='localhost_unittest')
17
18
19    def tearDown(self):
20        self.god.unstub_all()
21        self.tmpdir.clean()
22
23
24    def test_init(self):
25        self.god.stub_function(local_host.platform, 'node')
26        local_host.platform.node.expect_call().and_return('foo')
27
28        # run the actual test
29        host = local_host.LocalHost()
30        self.assertEqual(host.hostname, 'foo')
31        self.god.check_playback()
32
33        host = local_host.LocalHost(hostname='bar')
34        self.assertEqual(host.hostname, 'bar')
35        self.god.check_playback()
36
37
38    def test_wait_up(self):
39        # just test that wait_up always works
40        host = local_host.LocalHost()
41        host.wait_up(1)
42        self.god.check_playback()
43
44
45    def _setup_run(self, result):
46        host = local_host.LocalHost()
47
48        (local_host.utils.run.expect_call(result.command, timeout=123,
49                ignore_status=True, stdout_tee=local_host.utils.TEE_TO_LOGS,
50                stderr_tee=local_host.utils.TEE_TO_LOGS, stdin=None, args=())
51                .and_return(result))
52
53        return host
54
55
56    def test_run_success(self):
57        result = local_host.utils.CmdResult(command='yes', stdout='y',
58                stderr='', exit_status=0, duration=1)
59
60        host = self._setup_run(result)
61
62        self.assertEqual(host.run('yes', timeout=123, ignore_status=True,
63                stdout_tee=local_host.utils.TEE_TO_LOGS,
64                stderr_tee=local_host.utils.TEE_TO_LOGS, stdin=None), result)
65        self.god.check_playback()
66
67
68    def test_run_failure_raised(self):
69        result = local_host.utils.CmdResult(command='yes', stdout='',
70                stderr='err', exit_status=1, duration=1)
71
72        host = self._setup_run(result)
73
74        self.assertRaises(local_host.error.AutotestHostRunError, host.run,
75                          'yes', timeout=123)
76        self.god.check_playback()
77
78
79    def test_run_failure_ignored(self):
80        result = local_host.utils.CmdResult(command='yes', stdout='',
81                stderr='err', exit_status=1, duration=1)
82
83        host = self._setup_run(result)
84
85        self.assertEqual(host.run('yes', timeout=123, ignore_status=True),
86                         result)
87        self.god.check_playback()
88
89
90    def test_list_files_glob(self):
91        host = local_host.LocalHost()
92
93        files = (os.path.join(self.tmpdir.name, 'file1'),
94                 os.path.join(self.tmpdir.name, 'file2'))
95
96        # create some files in tmpdir
97        open(files[0], 'w').close()
98        open(files[1], 'w').close()
99
100        self.assertSameElements(
101                files,
102                host.list_files_glob(os.path.join(self.tmpdir.name, '*')))
103
104
105    def test_symlink_closure_does_not_add_existent_file(self):
106        host = local_host.LocalHost()
107
108        # create a file and a symlink to it
109        fname = os.path.join(self.tmpdir.name, 'file')
110        sname = os.path.join(self.tmpdir.name, 'sym')
111        open(fname, 'w').close()
112        os.symlink(fname, sname)
113
114        # test that when the symlinks point to already know files
115        # nothing is added
116        self.assertSameElements(
117                [fname, sname],
118                host.symlink_closure([fname, sname]))
119
120
121    def test_symlink_closure_adds_missing_files(self):
122        host = local_host.LocalHost()
123
124        # create a file and a symlink to it
125        fname = os.path.join(self.tmpdir.name, 'file')
126        sname = os.path.join(self.tmpdir.name, 'sym')
127        open(fname, 'w').close()
128        os.symlink(fname, sname)
129
130        # test that when the symlinks point to unknown files they are added
131        self.assertSameElements(
132                [fname, sname],
133                host.symlink_closure([sname]))
134
135
136if __name__ == "__main__":
137    unittest.main()
138