blob: f14ea21f662727ef93ad8542aeceb5a09589066f [file] [log] [blame]
mbligh5fe5c952008-05-27 20:14:43 +00001#!/usr/bin/python2.4
2
3__author__ = "raphtee@google.com (Travis Miller)"
4
5import os, unittest
6import common
7from autotest_lib.server import autotest, utils
8from autotest_lib.client.common_lib import utils as client_utils
9from autotest_lib.server.hosts import ssh_host
jadmanski3d161b02008-06-06 15:43:36 +000010from autotest_lib.client.common_lib.test_utils import mock
mbligh5fe5c952008-05-27 20:14:43 +000011
12
13class TestBaseAutotest(unittest.TestCase):
14 def setUp(self):
15 # create god
16 self.god = mock.mock_god()
17
18 # stub out utils
19 self.utils_obj = self.god.create_mock_class(utils, "utils")
20 self.old_utils = autotest.utils
21 autotest.utils = self.utils_obj
22
23 # stub out os
24 self.old_os = autotest.os
25 self.os_obj = self.god.create_mock_class(os, "os")
26 autotest.os = self.os_obj
27
jadmanski3d161b02008-06-06 15:43:36 +000028 # stub out os.path
mbligh5fe5c952008-05-27 20:14:43 +000029 self.path_obj = self.god.create_mock_class(os.path, "os.path")
30 autotest.os.path = self.path_obj
31
32 # need to set return of one function in utils called in constr.
33 self.server_dir = "autotest_lib.server"
34 func_call = self.utils_obj.get_server_dir.expect_call()
35 func_call.and_return(self.server_dir)
36
37 # create our host mock (and give it a hostname)
jadmanski3d161b02008-06-06 15:43:36 +000038 self.host = self.god.create_mock_class(ssh_host.SSHHost,
mbligh5fe5c952008-05-27 20:14:43 +000039 "SSHHost")
40 self.host.hostname = "foo"
41
42 # create the autotest object
43 self.base_autotest = autotest.BaseAutotest(self.host)
44
45
46 def tearDown(self):
47 # put things back
48 autotest.utils = self.old_utils
49 autotest.os = self.old_os
50
51
52 def test_constructor(self):
53 # we should check the calls
jadmanski3d161b02008-06-06 15:43:36 +000054 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000055
56 def common_install_test_setup(self, autodir, is_site_install_autotest):
57 # mock other methods
58 old_get_autodir = autotest._get_autodir
59 get_autodir_obj = self.god.create_mock_function("_get_autodir")
60 autotest._get_autodir = get_autodir_obj
61
62 self.base_autotest.got = True
63 self.source_material = None
jadmanski3d161b02008-06-06 15:43:36 +000064
mbligh5fe5c952008-05-27 20:14:43 +000065 # record calls
66 self.host.wait_up.expect_call(timeout=30)
67 self.host.setup.expect_call()
68 get_autodir_obj.expect_call(self.host).and_return(autodir)
69 rt = self.utils_obj.sh_escape.expect_call(autodir)
70 rt.and_return(autodir)
71 self.host.run.expect_call('mkdir -p "%s"' % (autodir))
72 rt = self.host.site_install_autotest.expect_call()
73 rt.and_return(is_site_install_autotest)
74
75 return old_get_autodir
76
77
78 def common_install_test_teardown(self, old_get_autodir):
79 # put things back
80 autotest._get_autodir = old_get_autodir
81
82
83 def test_install1(self):
84 # setup
85 autodir = "autodir"
86 old_get_autodir = self.common_install_test_setup(autodir, True)
87
88 # run test
89 self.base_autotest.install()
90
91 # check
92 self.assertTrue(self.base_autotest.installed)
jadmanski3d161b02008-06-06 15:43:36 +000093 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000094
95 # put back
96 self.common_install_test_teardown(old_get_autodir)
97
98
99 def test_install2(self):
100 # setup
101 autodir = "autodir"
102 old_get_autodir = self.common_install_test_setup(autodir, False)
103 cmd = 'which svn'
104 cmdresult = client_utils.CmdResult(cmd)
105 self.utils_obj.run.expect_call(cmd).and_return(cmdresult)
106 cmd = 'svn checkout %s %s' % (autotest.AUTOTEST_SVN, autodir)
107 self.host.run.expect_call(cmd)
108
109 # run test
110 self.base_autotest.install()
111
112 # check
113 self.assertTrue(self.base_autotest.installed)
jadmanski3d161b02008-06-06 15:43:36 +0000114 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000115
116 # put back
117 self.common_install_test_teardown(old_get_autodir)
118
119
120 def test_get(self):
121 # setup
122 location = "autotest_lib.client"
123 cwd = "current_dir"
124 self.os_obj.getcwd.expect_call().and_return(cwd)
125 self.os_obj.chdir.expect_call(location)
126 self.os_obj.system.expect_call('tools/make_clean')
127 self.os_obj.chdir.expect_call(cwd)
128
129 # call method under test
130 self.base_autotest.get(location)
131
132 # do tests
133 self.assertTrue(self.base_autotest.got)
jadmanski3d161b02008-06-06 15:43:36 +0000134 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000135
136
137 def test_get_default(self):
138 # setup the test
139 location = "autotest_lib.client"
jadmanski3d161b02008-06-06 15:43:36 +0000140 self.path_obj.join.expect_call(self.base_autotest.serverdir,
mbligh5fe5c952008-05-27 20:14:43 +0000141 '../client').and_return(location)
142 self.path_obj.abspath.expect_call(location).and_return(location)
143 cwd = "current_dir"
144 self.os_obj.getcwd.expect_call().and_return(cwd)
145 self.os_obj.chdir.expect_call(location)
146 self.os_obj.system.expect_call('tools/make_clean')
147 self.os_obj.chdir.expect_call(cwd)
148
149 # call method under test
150 self.base_autotest.get()
151
152 # do tests
153 self.assertTrue(self.base_autotest.got)
jadmanski3d161b02008-06-06 15:43:36 +0000154 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000155
156
157 def test_run_default(self):
158 # need to stub out _get_host_and_setup
159 old_func = self.base_autotest._get_host_and_setup
160 name = "_get_host_and_setup"
161 new_func = self.god.create_mock_function(name)
162 self.base_autotest._get_host_and_setup = new_func
163
164 # need to stub out _do_run
165 old_do_run = self.base_autotest._do_run
166 do_run = self.god.create_mock_function("_do_run")
167 self.base_autotest._do_run = do_run
168
169 # need a mock of _Run object
170 run = self.god.create_mock_class(autotest._Run, "run")
171
172 # need a mock for _Run constuctor
173 oldRun = autotest._Run
174 newRun = self.god.create_mock_function("_Run")
175 autotest._Run = newRun
176
177 new_func.expect_call(None).and_return(self.host)
178 results_dir = "results_dir"
179 self.path_obj.abspath.expect_call(".").and_return(results_dir)
jadmanski3d161b02008-06-06 15:43:36 +0000180 newRun.expect_call(self.host,
mbligh5fe5c952008-05-27 20:14:43 +0000181 results_dir, None, False).and_return(run)
182 do_run.expect_call("control", results_dir, self.host, run, None)
183
184 # call method
185 self.base_autotest.run("control")
186
187 # do test
jadmanski3d161b02008-06-06 15:43:36 +0000188 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000189
190 # put things back
191 self.base_autotest._get_host_and_setup = old_func
192 self.base_autotest._do_run = old_do_run
193 autotest._Run = oldRun
194
195
196 def test_prepare_for_copying_logs1(self):
197 src = "src"
198 dest = "dest"
199 keyval_path = ''
200 dkeyval = "dest/keyval"
201
202 # setup
jadmanski3d161b02008-06-06 15:43:36 +0000203 self.path_obj.join.expect_call(dest,
mbligh5fe5c952008-05-27 20:14:43 +0000204 'keyval').and_return(dkeyval)
205 self.path_obj.exists.expect_call(dkeyval).and_return(False)
206
207 # run test
jadmanski3d161b02008-06-06 15:43:36 +0000208 self.base_autotest.prepare_for_copying_logs(src, dest,
mbligh5fe5c952008-05-27 20:14:43 +0000209 self.host)
210
211 # check
jadmanski3d161b02008-06-06 15:43:36 +0000212 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000213
214
215 def test_prepare_for_copying_logs2(self):
216 src = "src"
217 dest = "dest"
218 keyval_path = ''
219 dkeyval = "dest/keyval"
220 skeyval = "src/keyval"
221 file_path = (0, ".keyavl_host")
222
223 # make stub for tempfile.mkstemp
224 old_mkstemp = autotest.tempfile.mkstemp
jadmanski3d161b02008-06-06 15:43:36 +0000225 mkstemp_obj = self.god.create_mock_function("tempfile.mkstemp")
mbligh5fe5c952008-05-27 20:14:43 +0000226 autotest.tempfile.mkstemp = mkstemp_obj
227
228 # setup
jadmanski3d161b02008-06-06 15:43:36 +0000229 self.path_obj.join.expect_call(dest,
mbligh5fe5c952008-05-27 20:14:43 +0000230 'keyval').and_return(dkeyval)
231 self.path_obj.exists.expect_call(dkeyval).and_return(True)
jadmanski3d161b02008-06-06 15:43:36 +0000232 mkstemp_obj.expect_call('.keyval_%s'
mbligh5fe5c952008-05-27 20:14:43 +0000233 % self.host.hostname).and_return(file_path)
jadmanski3d161b02008-06-06 15:43:36 +0000234 self.path_obj.join.expect_call(src,
mbligh5fe5c952008-05-27 20:14:43 +0000235 'keyval').and_return(skeyval)
236 self.host.get_file.expect_call(skeyval, file_path[1])
jadmanski3d161b02008-06-06 15:43:36 +0000237 self.path_obj.join.expect_call(src,
mbligh5fe5c952008-05-27 20:14:43 +0000238 'keyval').and_return(skeyval)
239 self.host.run.expect_call('rm -rf %s' % (skeyval))
240
241 # run test
jadmanski3d161b02008-06-06 15:43:36 +0000242 self.base_autotest.prepare_for_copying_logs(src, dest,
mbligh5fe5c952008-05-27 20:14:43 +0000243 self.host)
244
245 # check results
jadmanski3d161b02008-06-06 15:43:36 +0000246 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000247
248 # set things back
249 autotest.tempfile.mkstemp = old_mkstemp
250
251
252 def test_process_copied_logs_no_dest_keyval(self):
253 # setup test
254 dest = "dest"
255 path = "keyval_path"
256 self.path_obj.join.expect_call(dest, 'keyval').and_return(path)
257 self.path_obj.exists.expect_call(path).and_return(False)
258
259 # run test
260 self.base_autotest.process_copied_logs(dest, self.host, path)
261
262 # run check
jadmanski3d161b02008-06-06 15:43:36 +0000263 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000264
265
266 def test_process_copied_logs_with_dest_keyval(self):
267 # setup test
268 dest = "dest"
269 kpath = "keyval_path"
270 path = "path"
271 self.path_obj.join.expect_call(dest, 'keyval').and_return(path)
272 self.path_obj.exists.expect_call(path).and_return(True)
273
274 vals = {'version': 1, 'author': "wonder woman"}
275 kvals = {'version': 1}
276 mvals = {'author': "wonder woman"}
277
278 self.utils_obj.read_keyval.expect_call(path).and_return(vals)
279 self.path_obj.join.expect_call(dest, 'keyval').and_return(kpath)
280 self.utils_obj.read_keyval.expect_call(kpath).and_return(kvals)
281 self.path_obj.join.expect_call(dest, 'keyval').and_return(dest)
282 self.utils_obj.write_keyval.expect_call(dest, mvals)
283 self.os_obj.remove.expect_call(path)
284
285 # call test
286 self.base_autotest.process_copied_logs(dest, self.host, path)
287
288 # run check
jadmanski3d161b02008-06-06 15:43:36 +0000289 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000290
291
292 def test_run_timed_test(self):
293 pass
294
295
296if __name__ == "__main__":
297 unittest.main()