forked from autotest/autotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_host_unittest.py
executable file
·140 lines (96 loc) · 4.12 KB
/
local_host_unittest.py
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
#!/usr/bin/python
import os
try:
import autotest.common as common
except ImportError:
import common
from autotest.client.shared.test_utils import mock, unittest
from autotest.client.shared import autotemp
from autotest.client import local_host
class test_local_host_class(unittest.TestCase):
def setUp(self):
self.god = mock.mock_god()
self.god.stub_function(local_host.utils, 'run')
self.tmpdir = autotemp.tempdir(unique_id='localhost_unittest')
def tearDown(self):
self.god.unstub_all()
self.tmpdir.clean()
def test_init(self):
self.god.stub_function(local_host.platform, 'node')
local_host.platform.node.expect_call().and_return('foo')
# run the actual test
host = local_host.LocalHost()
self.assertEqual(host.hostname, 'foo')
self.god.check_playback()
host = local_host.LocalHost(hostname='bar')
self.assertEqual(host.hostname, 'bar')
self.god.check_playback()
def test_wait_up(self):
# just test that wait_up always works
host = local_host.LocalHost()
host.wait_up(1)
self.god.check_playback()
def _setup_run(self, result):
host = local_host.LocalHost()
(local_host.utils.run.expect_call(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, args=())
.and_return(result))
return host
def test_run_success(self):
result = local_host.utils.CmdResult(command='yes', stdout='y',
stderr='', exit_status=0, duration=1)
host = self._setup_run(result)
self.assertEqual(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), result)
self.god.check_playback()
def test_run_failure_raised(self):
result = local_host.utils.CmdResult(command='yes', stdout='',
stderr='err', exit_status=1, duration=1)
host = self._setup_run(result)
self.assertRaises(local_host.error.AutotestHostRunError, host.run,
'yes', timeout=123)
self.god.check_playback()
def test_run_failure_ignored(self):
result = local_host.utils.CmdResult(command='yes', stdout='',
stderr='err', exit_status=1, duration=1)
host = self._setup_run(result)
self.assertEqual(host.run('yes', timeout=123, ignore_status=True),
result)
self.god.check_playback()
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.assertSameElements(
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.assertSameElements(
[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.assertSameElements(
[fname, sname],
host.symlink_closure([sname]))
if __name__ == "__main__":
unittest.main()