virt.virt_test_utils: run_autotest - 'tar' needs relative paths to strip the leading '/'
[autotest-zwu.git] / client / bin / local_host_unittest.py
blob6026d469548f6f22895b220d221e6a55343c4692
1 #!/usr/bin/python
3 import os
4 import common
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):
12 def setUp(self):
13 self.god = mock.mock_god()
14 self.god.stub_function(local_host.utils, 'run')
16 self.tmpdir = autotemp.tempdir(unique_id='localhost_unittest')
19 def tearDown(self):
20 self.god.unstub_all()
21 self.tmpdir.clean()
24 def test_init(self):
25 self.god.stub_function(local_host.platform, 'node')
26 local_host.platform.node.expect_call().and_return('foo')
28 # run the actual test
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()
41 host.wait_up(1)
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=())
51 .and_return(result))
53 return host
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,
75 'yes', timeout=123)
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),
86 result)
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(
101 files,
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
115 # nothing is added
116 self.assertSameElements(
117 [fname, sname],
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(
132 [fname, sname],
133 host.symlink_closure([sname]))
136 if __name__ == "__main__":
137 unittest.main()