6 from autotest_lib
.client
.common_lib
.test_utils
import mock
, unittest
7 from autotest_lib
.client
.common_lib
import autotemp
8 from autotest_lib
.client
.bin
import local_host
11 class test_local_host_class(unittest
.TestCase
):
13 self
.god
= mock
.mock_god()
14 self
.god
.stub_function(local_host
.utils
, 'run')
16 self
.tmpdir
= autotemp
.tempdir(unique_id
='localhost_unittest')
25 self
.god
.stub_function(local_host
.platform
, 'node')
26 local_host
.platform
.node
.expect_call().and_return('foo')
29 host
= local_host
.LocalHost()
30 self
.assertEqual(host
.hostname
, 'foo')
31 self
.god
.check_playback()
33 host
= local_host
.LocalHost(hostname
='bar')
34 self
.assertEqual(host
.hostname
, 'bar')
35 self
.god
.check_playback()
38 def test_wait_up(self
):
39 # just test that wait_up always works
40 host
= local_host
.LocalHost()
42 self
.god
.check_playback()
45 def _setup_run(self
, result
):
46 host
= local_host
.LocalHost()
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
=())
56 def test_run_success(self
):
57 result
= local_host
.utils
.CmdResult(command
='yes', stdout
='y',
58 stderr
='', exit_status
=0, duration
=1)
60 host
= self
._setup
_run
(result
)
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()
68 def test_run_failure_raised(self
):
69 result
= local_host
.utils
.CmdResult(command
='yes', stdout
='',
70 stderr
='err', exit_status
=1, duration
=1)
72 host
= self
._setup
_run
(result
)
74 self
.assertRaises(local_host
.error
.AutotestHostRunError
, host
.run
,
76 self
.god
.check_playback()
79 def test_run_failure_ignored(self
):
80 result
= local_host
.utils
.CmdResult(command
='yes', stdout
='',
81 stderr
='err', exit_status
=1, duration
=1)
83 host
= self
._setup
_run
(result
)
85 self
.assertEqual(host
.run('yes', timeout
=123, ignore_status
=True),
87 self
.god
.check_playback()
90 def test_list_files_glob(self
):
91 host
= local_host
.LocalHost()
93 files
= (os
.path
.join(self
.tmpdir
.name
, 'file1'),
94 os
.path
.join(self
.tmpdir
.name
, 'file2'))
96 # create some files in tmpdir
97 open(files
[0], 'w').close()
98 open(files
[1], 'w').close()
100 self
.assertSameElements(
102 host
.list_files_glob(os
.path
.join(self
.tmpdir
.name
, '*')))
105 def test_symlink_closure_does_not_add_existent_file(self
):
106 host
= local_host
.LocalHost()
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
)
114 # test that when the symlinks point to already know files
116 self
.assertSameElements(
118 host
.symlink_closure([fname
, sname
]))
121 def test_symlink_closure_adds_missing_files(self
):
122 host
= local_host
.LocalHost()
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
)
130 # test that when the symlinks point to unknown files they are added
131 self
.assertSameElements(
133 host
.symlink_closure([sname
]))
136 if __name__
== "__main__":