ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python
 
import mock
import os
import unittest
 
import common
from autotest_lib.client.common_lib import autotemp
from autotest_lib.client.common_lib import error
from autotest_lib.client.bin import local_host
 
 
class test_local_host_class(unittest.TestCase):
    def setUp(self):
        self.tmpdir = autotemp.tempdir(unique_id='localhost_unittest')
        self.addCleanup(self.tmpdir.clean)
 
 
    @mock.patch('autotest_lib.client.bin.local_host.platform.node')
    def test_init_default_hostname(self, mock_node):
        mock_node.return_value = 'foo'
        host = local_host.LocalHost()
        self.assertEqual(host.hostname, 'foo')
 
 
    @mock.patch('autotest_lib.client.bin.local_host.platform.node')
    def test_init_with_hostname(self, mock_node):
        mock_node.return_value = 'foo'
        host = local_host.LocalHost(hostname='bar')
        self.assertEqual(host.hostname, 'bar')
 
 
    def test_wait_up(self):
        # just test that wait_up always works
        host = local_host.LocalHost()
        host.wait_up(1)
 
 
    @mock.patch('autotest_lib.client.bin.local_host.utils.run')
    def test_run_success(self, mock_run):
        result = local_host.utils.CmdResult(
                command='yes',
                stdout='y',
                stderr='',
                exit_status=0,
                duration=1,
        )
        mock_run.return_value = result
 
        host = local_host.LocalHost()
        got = host.run(
                'yes',
                timeout=123,
                ignore_status=True,
                stdout_tee=local_host.utils.TEE_TO_LOGS,
                stderr_tee=local_host.utils.TEE_TO_LOGS,
                stdin=None,
        )
 
        self.assertEqual(got, result)
        mock_run.assert_called_once_with(
                result.command,
                timeout=123,
                ignore_status=True,
                stdout_tee=local_host.utils.TEE_TO_LOGS,
                stderr_tee=local_host.utils.TEE_TO_LOGS,
                stdin=None,
                ignore_timeout=False,
                args=(),
        )
 
 
    @mock.patch('autotest_lib.client.bin.local_host.utils.run')
    def test_run_cmd_failure_raised(self, mock_run):
        mock_result = mock.MagicMock()
        mock_run.side_effect = error.CmdError('yes', mock_result)
 
        host = local_host.LocalHost()
        with self.assertRaises(error.AutotestHostRunCmdError) as exc_cm:
            host.run('yes', timeout=123)
 
        self.assertEqual(exc_cm.exception.result_obj, mock_result)
        mock_run.assert_called_once_with(
                'yes',
                timeout=123,
                ignore_status=False,
                stdout_tee=local_host.utils.TEE_TO_LOGS,
                stderr_tee=local_host.utils.TEE_TO_LOGS,
                stdin=None,
                ignore_timeout=False,
                args=(),
        )
 
 
    @mock.patch('autotest_lib.client.bin.local_host.utils.run')
    def test_run_cmd_timeout_raised(self, mock_run):
        mock_result = mock.MagicMock()
        mock_run.side_effect = error.CmdTimeoutError('yes', mock_result)
 
        host = local_host.LocalHost()
        with self.assertRaises(error.AutotestHostRunTimeoutError) as exc_cm:
            host.run('yes', timeout=123)
 
        self.assertEqual(exc_cm.exception.result_obj, mock_result)
        mock_run.assert_called_once_with(
                'yes',
                timeout=123,
                ignore_status=False,
                stdout_tee=local_host.utils.TEE_TO_LOGS,
                stderr_tee=local_host.utils.TEE_TO_LOGS,
                stdin=None,
                ignore_timeout=False,
                args=(),
        )
 
 
    @mock.patch('autotest_lib.client.bin.local_host.utils.run')
    def test_run_failure_ignored(self, mock_run):
        result = local_host.utils.CmdResult(
                command='yes',
                stdout='',
                stderr='err',
                exit_status=1,
                duration=1,
        )
        mock_run.return_value = result
 
        host = local_host.LocalHost()
        got = host.run('yes', timeout=123, ignore_status=True)
 
        self.assertEqual(got, result)
        mock_run.assert_called_once_with(
                result.command,
                timeout=123,
                ignore_status=True,
                stdout_tee=local_host.utils.TEE_TO_LOGS,
                stderr_tee=local_host.utils.TEE_TO_LOGS,
                stdin=None,
                ignore_timeout=False,
                args=(),
        )
 
 
    def test_list_files_glob(self):
        host = local_host.LocalHost()
 
        files = (os.path.join(self.tmpdir.name, 'file1'),
                 os.path.join(self.tmpdir.name, 'file2'))
 
        # create some files in tmpdir
        open(files[0], 'w').close()
        open(files[1], 'w').close()
 
        self.assertItemsEqual(
                files,
                host.list_files_glob(os.path.join(self.tmpdir.name, '*')))
 
 
    def test_symlink_closure_does_not_add_existent_file(self):
        host = local_host.LocalHost()
 
        # create a file and a symlink to it
        fname = os.path.join(self.tmpdir.name, 'file')
        sname = os.path.join(self.tmpdir.name, 'sym')
        open(fname, 'w').close()
        os.symlink(fname, sname)
 
        # test that when the symlinks point to already know files
        # nothing is added
        self.assertItemsEqual(
                [fname, sname],
                host.symlink_closure([fname, sname]))
 
 
    def test_symlink_closure_adds_missing_files(self):
        host = local_host.LocalHost()
 
        # create a file and a symlink to it
        fname = os.path.join(self.tmpdir.name, 'file')
        sname = os.path.join(self.tmpdir.name, 'sym')
        open(fname, 'w').close()
        os.symlink(fname, sname)
 
        # test that when the symlinks point to unknown files they are added
        self.assertItemsEqual(
                [fname, sname],
                host.symlink_closure([sname]))
 
 
    def test_get_file(self):
        """Tests get_file() copying a regular file."""
        host = local_host.LocalHost()
 
        source_file = os.path.join(self.tmpdir.name, 'file')
        open(os.path.join(source_file), 'w').close()
 
        dest_file = os.path.join(self.tmpdir.name, 'dest')
 
        host.get_file(source_file, dest_file)
        self.assertTrue(os.path.isfile(dest_file))
 
 
    def test_get_directory_into_new_directory(self):
        """Tests get_file() copying a directory into a new dir."""
        host = local_host.LocalHost()
 
        source_dir = os.path.join(self.tmpdir.name, 'dir')
        os.mkdir(source_dir)
        open(os.path.join(source_dir, 'file'), 'w').close()
 
        dest_dir = os.path.join(self.tmpdir.name, 'dest')
 
        host.get_file(source_dir, dest_dir)
 
        self.assertTrue(os.path.isfile(os.path.join(dest_dir, 'dir', 'file')))
 
 
    def test_get_directory_into_existing_directory(self):
        """Tests get_file() copying a directory into an existing dir."""
        host = local_host.LocalHost()
 
        source_dir = os.path.join(self.tmpdir.name, 'dir')
        os.mkdir(source_dir)
        open(os.path.join(source_dir, 'file'), 'w').close()
 
        dest_dir = os.path.join(self.tmpdir.name, 'dest')
        os.mkdir(dest_dir)
 
        host.get_file(source_dir, dest_dir)
 
        self.assertTrue(os.path.isfile(os.path.join(dest_dir, 'dir', 'file')))
 
 
    def test_get_directory_delete_dest(self):
        """Tests get_file() replacing a dir."""
        host = local_host.LocalHost()
 
        source_dir = os.path.join(self.tmpdir.name, 'dir')
        os.mkdir(source_dir)
        open(os.path.join(source_dir, 'file'), 'w').close()
 
        dest_dir = os.path.join(self.tmpdir.name, 'dest')
        os.mkdir(dest_dir)
        os.mkdir(os.path.join(dest_dir, 'dir'))
        open(os.path.join(dest_dir, 'dir', 'file2'), 'w').close()
 
        host.get_file(source_dir, dest_dir, delete_dest=True)
 
        self.assertTrue(os.path.isfile(os.path.join(dest_dir, 'dir', 'file')))
        self.assertFalse(os.path.isfile(os.path.join(dest_dir, 'dir', 'file2')))
 
 
    def test_get_directory_contents_into_new_directory(self):
        """Tests get_file() copying dir contents to a new dir."""
        host = local_host.LocalHost()
 
        source_dir = os.path.join(self.tmpdir.name, 'dir')
        os.mkdir(source_dir)
        open(os.path.join(source_dir, 'file'), 'w').close()
 
        dest_dir = os.path.join(self.tmpdir.name, 'dest')
 
        # End the source with '/' to copy the contents only.
        host.get_file(os.path.join(source_dir, ''), dest_dir)
 
        self.assertTrue(os.path.isfile(os.path.join(dest_dir, 'file')))
 
 
    def test_get_directory_contents_into_existing_directory(self):
        """Tests get_file() copying dir contents into an existing dir."""
        host = local_host.LocalHost()
 
        source_dir = os.path.join(self.tmpdir.name, 'dir')
        os.mkdir(source_dir)
        open(os.path.join(source_dir, 'file'), 'w').close()
 
        dest_dir = os.path.join(self.tmpdir.name, 'dest')
        os.mkdir(dest_dir)
 
        # End the source with '/' to copy the contents only.
        host.get_file(os.path.join(source_dir, ''), dest_dir)
 
        self.assertTrue(os.path.isfile(os.path.join(dest_dir, 'file')))
 
 
    def test_get_directory_contents_delete_dest(self):
        """Tests get_file() replacing contents of a dir."""
        host = local_host.LocalHost()
 
        source_dir = os.path.join(self.tmpdir.name, 'dir')
        os.mkdir(source_dir)
        open(os.path.join(source_dir, 'file'), 'w').close()
 
        dest_dir = os.path.join(self.tmpdir.name, 'dest')
        os.mkdir(dest_dir)
        open(os.path.join(dest_dir, 'file2'), 'w').close()
 
        # End the source with '/' to copy the contents only.
        host.get_file(os.path.join(source_dir, ''), dest_dir, delete_dest=True)
 
        self.assertTrue(os.path.isfile(os.path.join(dest_dir, 'file')))
        self.assertFalse(os.path.isfile(os.path.join(dest_dir, 'file2')))
 
 
if __name__ == "__main__":
    unittest.main()