blob: 3aea277cdd5cf8367b4091d687e794a224a7c8b6 [file] [log] [blame]
mbligh81103b12008-06-12 19:55:37 +00001#!/usr/bin/python
mbligh7636b3a2008-06-11 15:44:01 +00002
jadmanskia1f3c202008-09-15 19:17:16 +00003import unittest, os, time, tempfile
mbligh7636b3a2008-06-11 15:44:01 +00004import common
jadmanskia1f3c202008-09-15 19:17:16 +00005from autotest_lib.server import server_job, test, subcommand, hosts
jadmanskifbc1f0a2008-07-09 14:12:54 +00006from autotest_lib.client.common_lib import utils, error, host_protections
mblighc5ddfd12008-08-04 17:15:00 +00007from autotest_lib.client.common_lib import packages
mbligh7636b3a2008-06-11 15:44:01 +00008from autotest_lib.tko import db as tko_db, status_lib, utils as tko_utils
9from autotest_lib.client.common_lib.test_utils import mock
10from autotest_lib.tko.parsers import version_1 as parser_mod
11from autotest_lib.tko.parsers import version_0 as parser_mod0
mbligh7636b3a2008-06-11 15:44:01 +000012
13
14class BaseServerJobTest(unittest.TestCase):
15 def setUp(self):
16 self.control = "control"
17 self.args = ""
18 self.resultdir = "results"
19 self.label = "default"
20 self.user = "user"
21 self.machines = ('abcd1', 'abcd2', 'abcd3')
22
23 # make god
24 self.god = mock.mock_god()
25
26 # stub out some common functions
27 self.god.stub_function(os.path, 'exists')
28 self.god.stub_function(os, 'mkdir')
29 self.god.stub_function(os, 'chdir')
30 self.god.stub_function(os, 'unlink')
jadmanski10646442008-08-13 14:05:21 +000031 self.god.stub_function(server_job, 'get_site_job_data')
32 self.god.stub_function(server_job, 'open')
mbligh7636b3a2008-06-11 15:44:01 +000033 self.god.stub_function(utils, 'write_keyval')
34
mbligh7636b3a2008-06-11 15:44:01 +000035
mbligh7636b3a2008-06-11 15:44:01 +000036
37
38 def tearDown(self):
39 self.god.unstub_all()
40
41
42 def construct_server_job(self):
43 # setup recording for constructor
44 file_obj = self.god.create_mock_class(file, "file")
jadmanski10646442008-08-13 14:05:21 +000045 server_job.open.expect_call(self.control,
mbligh7636b3a2008-06-11 15:44:01 +000046 'r').and_return(file_obj)
47 file_obj.read.expect_call().and_return('')
48 os.path.exists.expect_call(
49 mock.is_string_comparator()).and_return(False)
50 os.mkdir.expect_call(mock.is_string_comparator())
51 os.path.exists.expect_call(
52 mock.is_string_comparator()).and_return(False)
53 os.mkdir.expect_call(mock.is_string_comparator())
54 os.path.exists.expect_call(
55 mock.is_string_comparator()).and_return(True)
56 os.unlink.expect_call(mock.is_string_comparator())
jadmanski10646442008-08-13 14:05:21 +000057 cls = server_job.base_server_job
mbligh7636b3a2008-06-11 15:44:01 +000058 compare = mock.is_instance_comparator(cls)
jadmanski10646442008-08-13 14:05:21 +000059 server_job.get_site_job_data.expect_call(
mbligh7636b3a2008-06-11 15:44:01 +000060 compare).and_return({})
61 utils.write_keyval.expect_call(mock.is_string_comparator(),
62 mock.is_instance_comparator(dict))
63
jadmanski10646442008-08-13 14:05:21 +000064 self.job = server_job.base_server_job(self.control,
65 self.args,
66 self.resultdir,
67 self.label,
68 self.user,
69 self.machines)
mbligh7636b3a2008-06-11 15:44:01 +000070
71 self.god.check_playback()
72
73 # use this stub alot
74 self.god.stub_function(self.job, "_execute_code")
75
76
77 def test_constructor(self):
78 self.construct_server_job()
79
80
81 def test_init_parser(self):
82 self.construct_server_job()
83
84 results = "results"
85 log = os.path.join(results, '.parse.log')
86
87 # do some additional setup
88 self.god.stub_function(tko_utils, 'redirect_parser_debugging')
89 self.god.stub_function(tko_db, 'db')
90 self.god.stub_function(status_lib, 'parser')
91
92 # set up recording
93 file_obj = self.god.create_mock_class(file, "file")
jadmanski10646442008-08-13 14:05:21 +000094 server_job.open.expect_call(log, 'w',
mbligh7636b3a2008-06-11 15:44:01 +000095 0).and_return(file_obj)
96 tko_utils.redirect_parser_debugging.expect_call(file_obj)
97 db = self.god.create_mock_class(tko_db.db_sql, "db_sql")
98 tko_db.db.expect_call(autocommit=True).and_return(db)
mbligh81103b12008-06-12 19:55:37 +000099 parser = self.god.create_mock_class(parser_mod.parser, "parser_class")
mbligh7636b3a2008-06-11 15:44:01 +0000100 status_lib.parser.expect_call(1).and_return(parser)
mbligh81103b12008-06-12 19:55:37 +0000101 job_model = self.god.create_mock_class_obj(parser_mod0.job, "pjob")
mbligh7636b3a2008-06-11 15:44:01 +0000102 parser.make_job.expect_call(results).and_return(job_model)
103 parser.start.expect_call(job_model)
104 db.find_job.expect_call(mock.is_string_comparator())
105 db.insert_job.expect_call(mock.is_string_comparator(),
106 job_model)
107
108 # run method
109 self.job.init_parser(results)
110
111 # check
112 self.god.check_playback()
113
114
115 def test_verify(self):
116 self.construct_server_job()
117
118 # record
119 namespace = {'machines' : self.machines, 'job' : self.job, \
120 'ssh_user' : self.job.ssh_user, \
121 'ssh_port' : self.job.ssh_port, \
122 'ssh_pass' : self.job.ssh_pass}
jadmanski10646442008-08-13 14:05:21 +0000123 arg = server_job.preamble + server_job.verify
mbligh7636b3a2008-06-11 15:44:01 +0000124 self.job._execute_code.expect_call(arg, namespace,
125 namespace)
126
127 # run and check
128 self.job.verify()
129 self.god.check_playback()
130
131
132 def test_repair(self):
133 self.construct_server_job()
134
135 # record
jadmanskifbc1f0a2008-07-09 14:12:54 +0000136 verify_namespace = {'machines' : self.machines, 'job' : self.job,
137 'ssh_user' : self.job.ssh_user,
138 'ssh_port' : self.job.ssh_port,
139 'ssh_pass' : self.job.ssh_pass}
140 repair_namespace = verify_namespace.copy()
141 repair_namespace['protection_level'] = host_protections.default
142
jadmanski10646442008-08-13 14:05:21 +0000143 arg = server_job.preamble + server_job.repair
jadmanskifbc1f0a2008-07-09 14:12:54 +0000144 self.job._execute_code.expect_call(arg, repair_namespace,
145 repair_namespace)
jadmanski10646442008-08-13 14:05:21 +0000146 arg = server_job.preamble + server_job.verify
jadmanskifbc1f0a2008-07-09 14:12:54 +0000147 self.job._execute_code.expect_call(arg, verify_namespace,
148 verify_namespace)
mbligh7636b3a2008-06-11 15:44:01 +0000149
150 # run and check
jadmanskifbc1f0a2008-07-09 14:12:54 +0000151 self.job.repair(host_protections.default)
mbligh7636b3a2008-06-11 15:44:01 +0000152 self.god.check_playback()
153
154
jadmanskic0e94602008-07-29 22:34:42 +0000155 def test_parallel_simple_with_one_machine(self):
mbligh7636b3a2008-06-11 15:44:01 +0000156 self.construct_server_job()
jadmanskic0e94602008-07-29 22:34:42 +0000157 self.job.machines = ["hostname"]
mbligh7636b3a2008-06-11 15:44:01 +0000158
159 # setup
160 func = self.god.create_mock_function("wrapper")
mbligh7636b3a2008-06-11 15:44:01 +0000161
162 # record
jadmanskic0e94602008-07-29 22:34:42 +0000163 func.expect_call("hostname")
mbligh7636b3a2008-06-11 15:44:01 +0000164
165 # run and check
jadmanskic0e94602008-07-29 22:34:42 +0000166 self.job.parallel_simple(func, self.job.machines)
mbligh7636b3a2008-06-11 15:44:01 +0000167 self.god.check_playback()
168
169
170 def test_run(self):
171 self.construct_server_job()
172
173 # setup
174 self.god.stub_function(time, 'time')
175 self.god.stub_function(self.job, 'enable_external_logging')
176 self.god.stub_function(self.job, 'disable_external_logging')
177 file_obj = self.god.create_mock_class(file, "file")
178 namespace = {}
179 my_time = 0.0
180 namespace['machines'] = self.machines
181 namespace['args'] = self.args
182 namespace['job'] = self.job
183 namespace['ssh_user'] = self.job.ssh_user
184 namespace['ssh_port'] = self.job.ssh_port
185 namespace['ssh_pass'] = self.job.ssh_pass
186 namespace2 = {}
187 namespace2['machines'] = self.machines
188 namespace2['args'] = self.args
189 namespace2['job'] = self.job
190 namespace2['ssh_user'] = self.job.ssh_user
191 namespace2['ssh_port'] = self.job.ssh_port
192 namespace2['ssh_pass'] = self.job.ssh_pass
193 namespace2['test_start_time'] = int(my_time)
194
195 # record
196 time.time.expect_call().and_return(my_time)
197 os.chdir.expect_call(mock.is_string_comparator())
198 self.job.enable_external_logging.expect_call()
jadmanski10646442008-08-13 14:05:21 +0000199 server_job.open.expect_call(
mbligh7636b3a2008-06-11 15:44:01 +0000200 'control.srv', 'w').and_return(file_obj)
201 file_obj.write.expect_call('')
jadmanski10646442008-08-13 14:05:21 +0000202 arg = server_job.preamble + ''
mbligh7636b3a2008-06-11 15:44:01 +0000203 self.job._execute_code.expect_call(arg, namespace,
204 namespace)
jadmanski10646442008-08-13 14:05:21 +0000205 arg = server_job.preamble + server_job.crashdumps
mbligh7636b3a2008-06-11 15:44:01 +0000206 self.job._execute_code.expect_call(arg, namespace2,
207 namespace2)
208 self.job.disable_external_logging.expect_call()
209
210 # run and check
211 self.job.run()
212 self.god.check_playback()
213
214
215 def test_run_test(self):
216 self.construct_server_job()
217
218 # setup
mblighc5ddfd12008-08-04 17:15:00 +0000219 self.god.stub_function(self.job.pkgmgr, 'get_package_name')
mbligh7636b3a2008-06-11 15:44:01 +0000220 self.god.stub_function(test, 'runtest')
221 self.god.stub_function(self.job, 'record')
222
223 # record
224 url = "my.test.url"
225 group = "group"
226 testname = "testname"
mblighc5ddfd12008-08-04 17:15:00 +0000227 self.job.pkgmgr.get_package_name.expect_call(
228 url, 'test').and_return((group, testname))
mbligh7636b3a2008-06-11 15:44:01 +0000229 outputdir = os.path.join(self.resultdir, testname)
230 os.path.exists.expect_call(outputdir).and_return(False)
231 os.mkdir.expect_call(outputdir)
jadmanskicd95e1f2008-07-01 21:32:22 +0000232 self.job.record.expect_call('START', testname, testname)
mbligh7636b3a2008-06-11 15:44:01 +0000233 test.runtest.expect_call(self.job, url, None, (), {})
234 self.job.record.expect_call('GOOD', testname, testname,
235 'completed successfully')
jadmanskicd95e1f2008-07-01 21:32:22 +0000236 self.job.record.expect_call('END GOOD', testname, testname)
237
238 # run and check
239 self.job.run_test(url)
240 self.god.check_playback()
241
242
243 def test_run_test_with_test_error(self):
244 self.construct_server_job()
245
246 # setup
mblighc5ddfd12008-08-04 17:15:00 +0000247 self.god.stub_function(self.job.pkgmgr, 'get_package_name')
jadmanskicd95e1f2008-07-01 21:32:22 +0000248 self.god.stub_function(test, 'runtest')
249 self.god.stub_function(self.job, 'record')
250
251 # record
252 url = "my.test.url"
253 group = "group"
254 testname = "testname"
255 e = error.TestError("Unexpected error")
mblighc5ddfd12008-08-04 17:15:00 +0000256 self.job.pkgmgr.get_package_name.expect_call(
257 url, 'test').and_return((group, testname))
jadmanskicd95e1f2008-07-01 21:32:22 +0000258 outputdir = os.path.join(self.resultdir, testname)
259 os.path.exists.expect_call(outputdir).and_return(False)
260 os.mkdir.expect_call(outputdir)
261 self.job.record.expect_call('START', testname, testname)
262 test.runtest.expect_call(self.job, url, None, (), {}).and_raises(e)
263 self.job.record.expect_call('ERROR', testname, testname,
264 'Unexpected error')
265 self.job.record.expect_call('END ERROR', testname, testname,
266 'Unexpected error')
267
268 # run and check
269 self.job.run_test(url)
270 self.god.check_playback()
271
272
273 def test_run_test_with_test_fail(self):
274 self.construct_server_job()
275
276 # setup
mblighc5ddfd12008-08-04 17:15:00 +0000277 self.god.stub_function(self.job.pkgmgr, 'get_package_name')
jadmanskicd95e1f2008-07-01 21:32:22 +0000278 self.god.stub_function(test, 'runtest')
279 self.god.stub_function(self.job, 'record')
280
281 # record
282 url = "my.test.url"
283 group = "group"
284 testname = "testname"
285 e = error.TestFail("The test failed!")
mblighc5ddfd12008-08-04 17:15:00 +0000286 self.job.pkgmgr.get_package_name.expect_call(
287 url, 'test').and_return((group, testname))
jadmanskicd95e1f2008-07-01 21:32:22 +0000288 outputdir = os.path.join(self.resultdir, testname)
289 os.path.exists.expect_call(outputdir).and_return(False)
290 os.mkdir.expect_call(outputdir)
291 self.job.record.expect_call('START', testname, testname)
292 test.runtest.expect_call(self.job, url, None, (), {}).and_raises(e)
293 self.job.record.expect_call('FAIL', testname, testname,
294 'The test failed!')
295 self.job.record.expect_call('END FAIL', testname, testname,
296 'The test failed!')
mbligh7636b3a2008-06-11 15:44:01 +0000297
298 # run and check
299 self.job.run_test(url)
300 self.god.check_playback()
301
302
303 def test_run_group(self):
304 self.construct_server_job()
305
306 # setup
307 func = self.god.create_mock_function("function")
308 name = func.__name__
309 self.god.stub_function(self.job, 'record')
310
311 # record
312 self.job.record.expect_call('START', None, name)
313 func.expect_call((), {}).and_return(None)
314 self.job.record.expect_call('END GOOD', None, name)
315
316 # run and check
317 self.job.run_group(func, (), {})
318 self.god.check_playback()
319
320
321 def test_run_reboot(self):
322 self.construct_server_job()
323
324 # setup
325 self.god.stub_function(self.job, 'record')
326 reboot_func = self.god.create_mock_function('reboot')
327 get_kernel_func = self.god.create_mock_function('get_kernel')
328 kernel = '2.6.24'
329
330 # record
331 self.job.record.expect_call('START', None, 'reboot')
332 reboot_func.expect_call()
333 get_kernel_func.expect_call().and_return(kernel)
334 self.job.record.expect_call('END GOOD', None, 'reboot',
335 optional_fields={"kernel": kernel})
336
337 # run and check
338 self.job.run_reboot(reboot_func, get_kernel_func)
339 self.god.check_playback()
340
341
342 def test_record(self):
343 self.construct_server_job()
344
345 # setup
346 self.god.stub_function(self.job, '_read_warnings')
347 self.god.stub_function(self.job, '_record')
348 status_code = 'GOOD'
349 subdir = "subdir"
350 operation = "operation"
351 timestamp = '0'
352 warnings = "danger, danger Will Robinson!"
353
354 # record
355 self.job._read_warnings.expect_call(
356 ).and_return(((timestamp, warnings),))
357 self.job._record.expect_call("WARN", None, None, warnings,
358 timestamp)
359 self.job._record.expect_call(status_code, subdir, operation, '',
360 optional_fields=None)
361
362 # run and check
363 self.job.record(status_code, subdir, operation)
364 self.god.check_playback()
365
366
jadmanskia1f3c202008-09-15 19:17:16 +0000367class BaseServerJobTest(unittest.TestCase):
368 def setUp(self):
369 self.god = mock.mock_god()
370
371 self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
372 self.host.hostname = "testhost"
373
374 self.god.stub_function(os.path, "exists")
375 self.god.stub_function(os, "close")
376 self.god.stub_function(os, "remove")
377 self.god.stub_function(tempfile, "mkstemp")
378 self.god.stub_function(utils, "read_keyval")
379 self.god.stub_function(utils, "write_keyval")
380
381
382 def tearDown(self):
383 self.god.unstub_all()
384
385
386 def test_prepare_for_copying_logs(self):
387 self.host.get_autodir.expect_call().and_return("/autodir")
388 collector = server_job.log_collector(self.host, None, "/resultsdir")
389 self.god.check_playback()
390
391 os.path.exists.expect_call("/resultsdir/keyval").and_return(True)
392 tempfile.mkstemp.expect_call(".keyval_testhost").and_return(
393 (10, "tmp.keyval_testhost"))
394 os.close.expect_call(10)
395 self.host.get_file.expect_call("/autodir/results/default/keyval",
396 "tmp.keyval_testhost")
397 self.host.get_tmp_dir.expect_call().and_return("/autotmp")
398 self.host.run.expect_call(
399 "mv /autodir/results/default/keyval /autotmp/keyval")
400
401 # run and check
402 keyval = collector._prepare_for_copying_logs()
403 self.assertEquals(keyval, "tmp.keyval_testhost")
404 self.god.check_playback()
405
406
407 def test_process_copied_logs(self):
408 self.host.get_autodir.expect_call().and_return("/autodir")
409 collector = server_job.log_collector(self.host, None, "/resultsdir")
410 self.god.check_playback()
411
412 utils.read_keyval.expect_call("tmp.keyval_testhost").and_return(
413 {"field1": "new thing", "field3": "other new thing"})
414 utils.read_keyval.expect_call("/resultsdir").and_return(
415 {"field1": "thing", "field2": "otherthing"})
416 utils.write_keyval.expect_call("/resultsdir",
417 {"field3": "other new thing"})
418 os.remove.expect_call("tmp.keyval_testhost")
419
420 # run and check
421 collector._process_copied_logs("tmp.keyval_testhost")
422 self.god.check_playback()
423
424
mbligh7636b3a2008-06-11 15:44:01 +0000425if __name__ == "__main__":
426 unittest.main()