blob: 3ada21edbd0236f267eded30fe6924665c34dfeb [file] [log] [blame]
showard34ab0992009-10-05 22:47:57 +00001#!/usr/bin/python
2
showard4a604792009-10-20 23:49:10 +00003import logging, os, unittest
showard34ab0992009-10-05 22:47:57 +00004import common
showard7b2d7cb2009-10-28 19:53:03 +00005from autotest_lib.client.common_lib import enum, global_config, host_protections
showard34ab0992009-10-05 22:47:57 +00006from autotest_lib.database import database_connection
7from autotest_lib.frontend import setup_django_environment
showardb8900452009-10-12 20:31:01 +00008from autotest_lib.frontend.afe import frontend_test_utils, models
showard34ab0992009-10-05 22:47:57 +00009from autotest_lib.scheduler import drone_manager, email_manager, monitor_db
10
11# translations necessary for scheduler queries to work with SQLite
12_re_translator = database_connection.TranslatingDatabase.make_regexp_translator
13_DB_TRANSLATORS = (
14 _re_translator(r'NOW\(\)', 'time("now")'),
showardd1195652009-12-08 22:21:02 +000015 _re_translator(r'LAST_INSERT_ID\(\)', 'LAST_INSERT_ROWID()'),
showard34ab0992009-10-05 22:47:57 +000016 # older SQLite doesn't support group_concat, so just don't bother until
17 # it arises in an important query
18 _re_translator(r'GROUP_CONCAT\((.*?)\)', r'\1'),
19)
20
showard4a604792009-10-20 23:49:10 +000021HqeStatus = models.HostQueueEntry.Status
22HostStatus = models.Host.Status
23
showard34ab0992009-10-05 22:47:57 +000024class NullMethodObject(object):
25 _NULL_METHODS = ()
26
27 def __init__(self):
28 def null_method(*args, **kwargs):
29 pass
30
31 for method_name in self._NULL_METHODS:
32 setattr(self, method_name, null_method)
33
34class MockGlobalConfig(object):
35 def __init__(self):
36 self._config_info = {}
37
38
39 def set_config_value(self, section, key, value):
40 self._config_info[(section, key)] = value
41
42
43 def get_config_value(self, section, key, type=str,
44 default=None, allow_blank=False):
45 identifier = (section, key)
46 if identifier not in self._config_info:
47 raise RuntimeError('Unset global config value: %s' % (identifier,))
48 return self._config_info[identifier]
49
50
showardf85a0b72009-10-07 20:48:45 +000051# the SpecialTask names here must match the suffixes used on the SpecialTask
52# results directories
53_PidfileType = enum.Enum('verify', 'cleanup', 'repair', 'job', 'gather',
54 'parse')
55
56
showardd1195652009-12-08 22:21:02 +000057_PIDFILE_TO_PIDFILE_TYPE = {
58 monitor_db._AUTOSERV_PID_FILE: _PidfileType.JOB,
59 monitor_db._CRASHINFO_PID_FILE: _PidfileType.GATHER,
60 monitor_db._PARSER_PID_FILE: _PidfileType.PARSE,
61 }
62
63
64_PIDFILE_TYPE_TO_PIDFILE = dict((value, key) for key, value
65 in _PIDFILE_TO_PIDFILE_TYPE.iteritems())
66
67
showard34ab0992009-10-05 22:47:57 +000068class MockDroneManager(NullMethodObject):
showard65db3932009-10-28 19:54:35 +000069 """
70 Public attributes:
71 max_runnable_processes_value: value returned by max_runnable_processes().
72 tests can change this to activate throttling.
73 """
showard4a604792009-10-20 23:49:10 +000074 _NULL_METHODS = ('reinitialize_drones', 'copy_to_results_repository',
75 'copy_results_on_drone')
76
77 class _DummyPidfileId(object):
78 """
79 Object to represent pidfile IDs that is opaque to the scheduler code but
80 still debugging-friendly for us.
81 """
showardd1195652009-12-08 22:21:02 +000082 def __init__(self, working_directory, pidfile_name, num_processes=None):
83 self._working_directory = working_directory
84 self._pidfile_name = pidfile_name
showard418785b2009-11-23 20:19:59 +000085 self._num_processes = num_processes
showardd1195652009-12-08 22:21:02 +000086 self._paired_with_pidfile = None
87
88
89 def key(self):
90 """Key for MockDroneManager._pidfile_index"""
91 return (self._working_directory, self._pidfile_name)
showard4a604792009-10-20 23:49:10 +000092
93
94 def __str__(self):
showardd1195652009-12-08 22:21:02 +000095 return os.path.join(self._working_directory, self._pidfile_name)
showard4a604792009-10-20 23:49:10 +000096
showard34ab0992009-10-05 22:47:57 +000097
showard418785b2009-11-23 20:19:59 +000098 def __repr__(self):
99 return '<_DummyPidfileId: %s>' % str(self)
100
101
showard34ab0992009-10-05 22:47:57 +0000102 def __init__(self):
103 super(MockDroneManager, self).__init__()
showard418785b2009-11-23 20:19:59 +0000104 self.process_capacity = 100
showard65db3932009-10-28 19:54:35 +0000105
showard34ab0992009-10-05 22:47:57 +0000106 # maps result_dir to set of tuples (file_path, file_contents)
107 self._attached_files = {}
108 # maps pidfile IDs to PidfileContents
109 self._pidfiles = {}
110 # pidfile IDs that haven't been created yet
111 self._future_pidfiles = []
showardf85a0b72009-10-07 20:48:45 +0000112 # maps _PidfileType to the most recently created pidfile ID of that type
113 self._last_pidfile_id = {}
showard34ab0992009-10-05 22:47:57 +0000114 # maps (working_directory, pidfile_name) to pidfile IDs
115 self._pidfile_index = {}
showardf85a0b72009-10-07 20:48:45 +0000116 # maps process to pidfile IDs
117 self._process_index = {}
118 # tracks pidfiles of processes that have been killed
119 self._killed_pidfiles = set()
showard4a604792009-10-20 23:49:10 +0000120 # pidfile IDs that have just been unregistered (so will disappear on the
121 # next cycle)
122 self._unregistered_pidfiles = set()
showard34ab0992009-10-05 22:47:57 +0000123
124
125 # utility APIs for use by the test
126
showardf85a0b72009-10-07 20:48:45 +0000127 def finish_process(self, pidfile_type, exit_status=0):
128 pidfile_id = self._last_pidfile_id[pidfile_type]
129 self._set_pidfile_exit_status(pidfile_id, exit_status)
showard34ab0992009-10-05 22:47:57 +0000130
131
showard65db3932009-10-28 19:54:35 +0000132 def finish_specific_process(self, working_directory, pidfile_name):
showardd1195652009-12-08 22:21:02 +0000133 pidfile_id = self.pidfile_from_path(working_directory, pidfile_name)
showard65db3932009-10-28 19:54:35 +0000134 self._set_pidfile_exit_status(pidfile_id, 0)
135
136
showard34ab0992009-10-05 22:47:57 +0000137 def _set_pidfile_exit_status(self, pidfile_id, exit_status):
showardf85a0b72009-10-07 20:48:45 +0000138 assert pidfile_id is not None
showard34ab0992009-10-05 22:47:57 +0000139 contents = self._pidfiles[pidfile_id]
140 contents.exit_status = exit_status
141 contents.num_tests_failed = 0
142
143
showardf85a0b72009-10-07 20:48:45 +0000144 def was_last_process_killed(self, pidfile_type):
145 pidfile_id = self._last_pidfile_id[pidfile_type]
146 return pidfile_id in self._killed_pidfiles
147
148
showard418785b2009-11-23 20:19:59 +0000149 def nonfinished_pidfile_ids(self):
150 return [pidfile_id for pidfile_id, pidfile_contents
showard4a604792009-10-20 23:49:10 +0000151 in self._pidfiles.iteritems()
showard418785b2009-11-23 20:19:59 +0000152 if pidfile_contents.exit_status is None]
153
154
155 def running_pidfile_ids(self):
156 return [pidfile_id for pidfile_id in self.nonfinished_pidfile_ids()
157 if self._pidfiles[pidfile_id].process is not None]
showard4a604792009-10-20 23:49:10 +0000158
159
showardd1195652009-12-08 22:21:02 +0000160 def pidfile_from_path(self, working_directory, pidfile_name):
161 return self._pidfile_index[(working_directory, pidfile_name)]
162
163
showard493beaa2009-12-18 22:44:45 +0000164 def attached_files(self, working_directory):
165 """
166 Return dict mapping path to contents for attached files with specified
167 paths.
168 """
169 return dict((path, contents) for path, contents
170 in self._attached_files.get(working_directory, [])
171 if path is not None)
172
173
showard34ab0992009-10-05 22:47:57 +0000174 # DroneManager emulation APIs for use by monitor_db
175
176 def get_orphaned_autoserv_processes(self):
177 return set()
178
179
180 def total_running_processes(self):
showard418785b2009-11-23 20:19:59 +0000181 return sum(pidfile_id._num_processes
182 for pidfile_id in self.nonfinished_pidfile_ids())
showard34ab0992009-10-05 22:47:57 +0000183
184
showard9bb960b2009-11-19 01:02:11 +0000185 def max_runnable_processes(self, username):
showard418785b2009-11-23 20:19:59 +0000186 return self.process_capacity - self.total_running_processes()
showard34ab0992009-10-05 22:47:57 +0000187
188
showard4a604792009-10-20 23:49:10 +0000189 def refresh(self):
190 for pidfile_id in self._unregistered_pidfiles:
191 # intentionally handle non-registered pidfiles silently
192 self._pidfiles.pop(pidfile_id, None)
193 self._unregistered_pidfiles = set()
194
195
showard34ab0992009-10-05 22:47:57 +0000196 def execute_actions(self):
197 # executing an "execute_command" causes a pidfile to be created
198 for pidfile_id in self._future_pidfiles:
199 # Process objects are opaque to monitor_db
showardf85a0b72009-10-07 20:48:45 +0000200 process = object()
201 self._pidfiles[pidfile_id].process = process
202 self._process_index[process] = pidfile_id
showard34ab0992009-10-05 22:47:57 +0000203 self._future_pidfiles = []
204
205
206 def attach_file_to_execution(self, result_dir, file_contents,
207 file_path=None):
208 self._attached_files.setdefault(result_dir, set()).add((file_path,
209 file_contents))
210 return 'attach_path'
211
212
showardf85a0b72009-10-07 20:48:45 +0000213 def _initialize_pidfile(self, pidfile_id):
214 if pidfile_id not in self._pidfiles:
showardd1195652009-12-08 22:21:02 +0000215 assert pidfile_id.key() not in self._pidfile_index
showardf85a0b72009-10-07 20:48:45 +0000216 self._pidfiles[pidfile_id] = drone_manager.PidfileContents()
showardd1195652009-12-08 22:21:02 +0000217 self._pidfile_index[pidfile_id.key()] = pidfile_id
showardf85a0b72009-10-07 20:48:45 +0000218
219
220 def _set_last_pidfile(self, pidfile_id, working_directory, pidfile_name):
221 if working_directory.startswith('hosts/'):
222 # such paths look like hosts/host1/1-verify, we'll grab the end
223 type_string = working_directory.rsplit('-', 1)[1]
224 pidfile_type = _PidfileType.get_value(type_string)
225 else:
showardd1195652009-12-08 22:21:02 +0000226 pidfile_type = _PIDFILE_TO_PIDFILE_TYPE[pidfile_name]
showardf85a0b72009-10-07 20:48:45 +0000227 self._last_pidfile_id[pidfile_type] = pidfile_id
228
229
showard34ab0992009-10-05 22:47:57 +0000230 def execute_command(self, command, working_directory, pidfile_name,
showard418785b2009-11-23 20:19:59 +0000231 num_processes, log_file=None, paired_with_pidfile=None,
showard9bb960b2009-11-19 01:02:11 +0000232 username=None):
showarda9545c02009-12-18 22:44:26 +0000233 logging.debug('Executing %s in %s', command, working_directory)
showardd1195652009-12-08 22:21:02 +0000234 pidfile_id = self._DummyPidfileId(working_directory, pidfile_name)
235 if pidfile_id.key() in self._pidfile_index:
236 pidfile_id = self._pidfile_index[pidfile_id.key()]
237 pidfile_id._num_processes = num_processes
238 pidfile_id._paired_with_pidfile = paired_with_pidfile
239
showard34ab0992009-10-05 22:47:57 +0000240 self._future_pidfiles.append(pidfile_id)
showardf85a0b72009-10-07 20:48:45 +0000241 self._initialize_pidfile(pidfile_id)
showard34ab0992009-10-05 22:47:57 +0000242 self._pidfile_index[(working_directory, pidfile_name)] = pidfile_id
showardf85a0b72009-10-07 20:48:45 +0000243 self._set_last_pidfile(pidfile_id, working_directory, pidfile_name)
showard34ab0992009-10-05 22:47:57 +0000244 return pidfile_id
245
246
247 def get_pidfile_contents(self, pidfile_id, use_second_read=False):
showard4a604792009-10-20 23:49:10 +0000248 if pidfile_id not in self._pidfiles:
showarda9545c02009-12-18 22:44:26 +0000249 logging.debug('Request for nonexistent pidfile %s' % pidfile_id)
showard4a604792009-10-20 23:49:10 +0000250 return self._pidfiles.get(pidfile_id, drone_manager.PidfileContents())
showard34ab0992009-10-05 22:47:57 +0000251
252
253 def is_process_running(self, process):
254 return True
255
256
257 def register_pidfile(self, pidfile_id):
showardf85a0b72009-10-07 20:48:45 +0000258 self._initialize_pidfile(pidfile_id)
259
260
261 def unregister_pidfile(self, pidfile_id):
showard4a604792009-10-20 23:49:10 +0000262 self._unregistered_pidfiles.add(pidfile_id)
showard34ab0992009-10-05 22:47:57 +0000263
264
showardd1195652009-12-08 22:21:02 +0000265 def declare_process_count(self, pidfile_id, num_processes):
266 pidfile_id.num_processes = num_processes
267
268
showard34ab0992009-10-05 22:47:57 +0000269 def absolute_path(self, path):
270 return 'absolute/' + path
271
272
273 def write_lines_to_file(self, file_path, lines, paired_with_process=None):
274 # TODO: record this
275 pass
276
277
278 def get_pidfile_id_from(self, execution_tag, pidfile_name):
showardd1195652009-12-08 22:21:02 +0000279 default_pidfile = self._DummyPidfileId(execution_tag, pidfile_name,
280 num_processes=0)
showard4a604792009-10-20 23:49:10 +0000281 return self._pidfile_index.get((execution_tag, pidfile_name),
showardd1195652009-12-08 22:21:02 +0000282 default_pidfile)
showard34ab0992009-10-05 22:47:57 +0000283
284
showardf85a0b72009-10-07 20:48:45 +0000285 def kill_process(self, process):
286 pidfile_id = self._process_index[process]
287 self._killed_pidfiles.add(pidfile_id)
288 self._set_pidfile_exit_status(pidfile_id, 271)
289
290
showard34ab0992009-10-05 22:47:57 +0000291class MockEmailManager(NullMethodObject):
292 _NULL_METHODS = ('send_queued_emails', 'send_email')
293
showardf85a0b72009-10-07 20:48:45 +0000294 def enqueue_notify_email(self, subject, message):
295 logging.warn('enqueue_notify_email: %s', subject)
296 logging.warn(message)
297
showard34ab0992009-10-05 22:47:57 +0000298
299class SchedulerFunctionalTest(unittest.TestCase,
300 frontend_test_utils.FrontendTestMixin):
301 # some number of ticks after which the scheduler is presumed to have
302 # stabilized, given no external changes
303 _A_LOT_OF_TICKS = 10
304
305 def setUp(self):
306 self._frontend_common_setup()
307 self._set_stubs()
308 self._set_global_config_values()
showardd1195652009-12-08 22:21:02 +0000309 self._create_dispatcher()
showard34ab0992009-10-05 22:47:57 +0000310
311 logging.basicConfig(level=logging.DEBUG)
312
313
showardd1195652009-12-08 22:21:02 +0000314 def _create_dispatcher(self):
315 self.dispatcher = monitor_db.Dispatcher()
316
317
showard34ab0992009-10-05 22:47:57 +0000318 def tearDown(self):
319 self._frontend_common_teardown()
320
321
322 def _set_stubs(self):
323 self.mock_config = MockGlobalConfig()
324 self.god.stub_with(global_config, 'global_config', self.mock_config)
325
326 self.mock_drone_manager = MockDroneManager()
327 self.god.stub_with(monitor_db, '_drone_manager',
328 self.mock_drone_manager)
329
330 self.mock_email_manager = MockEmailManager()
331 self.god.stub_with(email_manager, 'manager', self.mock_email_manager)
332
333 self._database = (
334 database_connection.TranslatingDatabase.get_test_database(
335 file_path=self._test_db_file,
336 translators=_DB_TRANSLATORS))
337 self._database.connect(db_type='django')
338 self.god.stub_with(monitor_db, '_db', self._database)
339
340
341 def _set_global_config_values(self):
342 self.mock_config.set_config_value('SCHEDULER', 'pidfile_timeout_mins',
343 1)
344
345
346 def _initialize_test(self):
347 self.dispatcher.initialize()
348
349
350 def _run_dispatcher(self):
351 for _ in xrange(self._A_LOT_OF_TICKS):
352 self.dispatcher.tick()
353
354
355 def test_idle(self):
showardb8900452009-10-12 20:31:01 +0000356 self._initialize_test()
showard34ab0992009-10-05 22:47:57 +0000357 self._run_dispatcher()
358
359
showardb8900452009-10-12 20:31:01 +0000360 def _assert_process_executed(self, working_directory, pidfile_name):
361 process_was_executed = self.mock_drone_manager.was_process_executed(
362 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE)
363 self.assert_(process_was_executed,
364 '%s/%s not executed' % (working_directory, pidfile_name))
365
366
showardd1195652009-12-08 22:21:02 +0000367 def _update_instance(self, model_instance):
368 return type(model_instance).objects.get(pk=model_instance.pk)
369
370
showard418785b2009-11-23 20:19:59 +0000371 def _check_statuses(self, queue_entry, queue_entry_status,
372 host_status=None):
showarda9545c02009-12-18 22:44:26 +0000373 self._check_entry_status(queue_entry, queue_entry_status)
374 if host_status:
375 self._check_host_status(queue_entry.host, host_status)
376
377
378 def _check_entry_status(self, queue_entry, status):
showard4a604792009-10-20 23:49:10 +0000379 # update from DB
showardd1195652009-12-08 22:21:02 +0000380 queue_entry = self._update_instance(queue_entry)
showarda9545c02009-12-18 22:44:26 +0000381 self.assertEquals(queue_entry.status, status)
showard4a604792009-10-20 23:49:10 +0000382
383
showard7b2d7cb2009-10-28 19:53:03 +0000384 def _check_host_status(self, host, status):
385 # update from DB
showarda9545c02009-12-18 22:44:26 +0000386 host = self._update_instance(host)
showard7b2d7cb2009-10-28 19:53:03 +0000387 self.assertEquals(host.status, status)
388
389
showard4a604792009-10-20 23:49:10 +0000390 def _run_pre_job_verify(self, queue_entry):
391 self._run_dispatcher() # launches verify
392 self._check_statuses(queue_entry, HqeStatus.VERIFYING,
393 HostStatus.VERIFYING)
394 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
395
396
showard34ab0992009-10-05 22:47:57 +0000397 def test_simple_job(self):
showardb8900452009-10-12 20:31:01 +0000398 self._initialize_test()
399 job, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000400 self._run_pre_job_verify(queue_entry)
showard34ab0992009-10-05 22:47:57 +0000401 self._run_dispatcher() # launches job
showard4a604792009-10-20 23:49:10 +0000402 self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING)
403 self._finish_job(queue_entry)
404 self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY)
405 self._assert_nothing_is_running()
showardb8900452009-10-12 20:31:01 +0000406
407
showard4a604792009-10-20 23:49:10 +0000408 def _setup_for_pre_job_cleanup(self):
409 self._initialize_test()
410 job, queue_entry = self._make_job_and_queue_entry()
411 job.reboot_before = models.RebootBefore.ALWAYS
412 job.save()
413 return queue_entry
414
415
416 def _run_pre_job_cleanup_job(self, queue_entry):
417 self._run_dispatcher() # cleanup
418 self._check_statuses(queue_entry, HqeStatus.VERIFYING,
419 HostStatus.CLEANING)
420 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
421 self._run_dispatcher() # verify
422 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
423 self._run_dispatcher() # job
424 self._finish_job(queue_entry)
425
426
427 def test_pre_job_cleanup(self):
428 queue_entry = self._setup_for_pre_job_cleanup()
429 self._run_pre_job_cleanup_job(queue_entry)
430
431
432 def _run_pre_job_cleanup_one_failure(self):
433 queue_entry = self._setup_for_pre_job_cleanup()
434 self._run_dispatcher() # cleanup
435 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
436 exit_status=256)
437 self._run_dispatcher() # repair
438 self._check_statuses(queue_entry, HqeStatus.QUEUED,
439 HostStatus.REPAIRING)
440 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
441 return queue_entry
442
443
444 def test_pre_job_cleanup_failure(self):
445 queue_entry = self._run_pre_job_cleanup_one_failure()
446 # from here the job should run as normal
447 self._run_pre_job_cleanup_job(queue_entry)
448
449
450 def test_pre_job_cleanup_double_failure(self):
451 # TODO (showard): this test isn't perfect. in reality, when the second
452 # cleanup fails, it copies its results over to the job directory using
453 # copy_results_on_drone() and then parses them. since we don't handle
454 # that, there appear to be no results at the job directory. the
455 # scheduler handles this gracefully, parsing gets effectively skipped,
456 # and this test passes as is. but we ought to properly test that
457 # behavior.
458 queue_entry = self._run_pre_job_cleanup_one_failure()
459 self._run_dispatcher() # second cleanup
460 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
461 exit_status=256)
462 self._run_dispatcher()
463 self._check_statuses(queue_entry, HqeStatus.FAILED,
464 HostStatus.REPAIR_FAILED)
465 # nothing else should run
466 self._assert_nothing_is_running()
467
468
469 def _assert_nothing_is_running(self):
470 self.assertEquals(self.mock_drone_manager.running_pidfile_ids(), [])
471
472
showard7b2d7cb2009-10-28 19:53:03 +0000473 def _setup_for_post_job_cleanup(self):
showard4a604792009-10-20 23:49:10 +0000474 self._initialize_test()
475 job, queue_entry = self._make_job_and_queue_entry()
476 job.reboot_after = models.RebootAfter.ALWAYS
477 job.save()
showard7b2d7cb2009-10-28 19:53:03 +0000478 return queue_entry
showard4a604792009-10-20 23:49:10 +0000479
showard7b2d7cb2009-10-28 19:53:03 +0000480
481 def _run_post_job_cleanup_failure_up_to_repair(self, queue_entry,
482 include_verify=True):
483 if include_verify:
484 self._run_pre_job_verify(queue_entry)
showard4a604792009-10-20 23:49:10 +0000485 self._run_dispatcher() # job
486 self.mock_drone_manager.finish_process(_PidfileType.JOB)
487 self._run_dispatcher() # parsing + cleanup
488 self.mock_drone_manager.finish_process(_PidfileType.PARSE)
489 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
490 exit_status=256)
491 self._run_dispatcher() # repair, HQE unaffected
showard4a604792009-10-20 23:49:10 +0000492 return queue_entry
493
494
495 def test_post_job_cleanup_failure(self):
showard7b2d7cb2009-10-28 19:53:03 +0000496 queue_entry = self._setup_for_post_job_cleanup()
497 self._run_post_job_cleanup_failure_up_to_repair(queue_entry)
498 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
499 HostStatus.REPAIRING)
showard4a604792009-10-20 23:49:10 +0000500 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
501 self._run_dispatcher()
502 self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY)
503
504
505 def test_post_job_cleanup_failure_repair_failure(self):
showard7b2d7cb2009-10-28 19:53:03 +0000506 queue_entry = self._setup_for_post_job_cleanup()
507 self._run_post_job_cleanup_failure_up_to_repair(queue_entry)
showard4a604792009-10-20 23:49:10 +0000508 self.mock_drone_manager.finish_process(_PidfileType.REPAIR,
509 exit_status=256)
510 self._run_dispatcher()
511 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
512 HostStatus.REPAIR_FAILED)
513
514
showardd1195652009-12-08 22:21:02 +0000515 def _ensure_post_job_process_is_paired(self, queue_entry, pidfile_type):
516 pidfile_name = _PIDFILE_TYPE_TO_PIDFILE[pidfile_type]
517 queue_entry = self._update_instance(queue_entry)
518 pidfile_id = self.mock_drone_manager.pidfile_from_path(
519 queue_entry.execution_path(), pidfile_name)
520 self.assert_(pidfile_id._paired_with_pidfile)
521
522
showard4a604792009-10-20 23:49:10 +0000523 def _finish_job(self, queue_entry):
showardf85a0b72009-10-07 20:48:45 +0000524 self.mock_drone_manager.finish_process(_PidfileType.JOB)
showard34ab0992009-10-05 22:47:57 +0000525 self._run_dispatcher() # launches parsing + cleanup
showard4a604792009-10-20 23:49:10 +0000526 self._check_statuses(queue_entry, HqeStatus.PARSING,
527 HostStatus.CLEANING)
showardd1195652009-12-08 22:21:02 +0000528 self._ensure_post_job_process_is_paired(queue_entry, _PidfileType.PARSE)
showardf85a0b72009-10-07 20:48:45 +0000529 self._finish_parsing_and_cleanup()
530
531
532 def _finish_parsing_and_cleanup(self):
533 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
534 self.mock_drone_manager.finish_process(_PidfileType.PARSE)
showard34ab0992009-10-05 22:47:57 +0000535 self._run_dispatcher()
536
537
showard7b2d7cb2009-10-28 19:53:03 +0000538 def _create_reverify_request(self):
539 host = self.hosts[0]
540 models.SpecialTask.objects.create(host=host,
showard9bb960b2009-11-19 01:02:11 +0000541 task=models.SpecialTask.Task.VERIFY,
542 requested_by=self.user)
showard7b2d7cb2009-10-28 19:53:03 +0000543 return host
544
545
546 def test_requested_reverify(self):
547 host = self._create_reverify_request()
548 self._run_dispatcher()
549 self._check_host_status(host, HostStatus.VERIFYING)
550 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
551 self._run_dispatcher()
552 self._check_host_status(host, HostStatus.READY)
553
554
555 def test_requested_reverify_failure(self):
556 host = self._create_reverify_request()
557 self._run_dispatcher()
558 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
559 exit_status=256)
560 self._run_dispatcher() # repair
561 self._check_host_status(host, HostStatus.REPAIRING)
562 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
563 self._run_dispatcher()
564 self._check_host_status(host, HostStatus.READY)
565
566
567 def _setup_for_do_not_verify(self):
568 self._initialize_test()
569 job, queue_entry = self._make_job_and_queue_entry()
570 queue_entry.host.protection = host_protections.Protection.DO_NOT_VERIFY
571 queue_entry.host.save()
572 return queue_entry
573
574
575 def test_do_not_verify_job(self):
576 queue_entry = self._setup_for_do_not_verify()
577 self._run_dispatcher() # runs job directly
578 self._finish_job(queue_entry)
579
580
581 def test_do_not_verify_job_with_cleanup(self):
582 queue_entry = self._setup_for_do_not_verify()
583 queue_entry.job.reboot_before = models.RebootBefore.ALWAYS
584 queue_entry.job.save()
585
586 self._run_dispatcher() # cleanup
587 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
588 self._run_dispatcher() # job
589 self._finish_job(queue_entry)
590
591
592 def test_do_not_verify_pre_job_cleanup_failure(self):
593 queue_entry = self._setup_for_do_not_verify()
594 queue_entry.job.reboot_before = models.RebootBefore.ALWAYS
595 queue_entry.job.save()
596
597 self._run_dispatcher() # cleanup
598 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
599 exit_status=256)
600 self._run_dispatcher() # failure ignored; job runs
601 self._finish_job(queue_entry)
602
603
604 def test_do_not_verify_post_job_cleanup_failure(self):
605 queue_entry = self._setup_for_do_not_verify()
606
607 self._run_post_job_cleanup_failure_up_to_repair(queue_entry,
608 include_verify=False)
609 # failure ignored, host still set to Ready
610 self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY)
611 self._run_dispatcher() # nothing else runs
612 self._assert_nothing_is_running()
613
614
615 def test_do_not_verify_requested_reverify_failure(self):
616 host = self._create_reverify_request()
617 host.protection = host_protections.Protection.DO_NOT_VERIFY
618 host.save()
619
620 self._run_dispatcher()
621 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
622 exit_status=256)
623 self._run_dispatcher()
624 self._check_host_status(host, HostStatus.READY) # ignore failure
625 self._assert_nothing_is_running()
626
627
showardf85a0b72009-10-07 20:48:45 +0000628 def test_job_abort_in_verify(self):
showardb8900452009-10-12 20:31:01 +0000629 self._initialize_test()
showardf85a0b72009-10-07 20:48:45 +0000630 job = self._create_job(hosts=[1])
631 self._run_dispatcher() # launches verify
632 job.hostqueueentry_set.update(aborted=True)
633 self._run_dispatcher() # kills verify, launches cleanup
634 self.assert_(self.mock_drone_manager.was_last_process_killed(
635 _PidfileType.VERIFY))
636 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
637 self._run_dispatcher()
638
639
640 def test_job_abort(self):
showardb8900452009-10-12 20:31:01 +0000641 self._initialize_test()
showardf85a0b72009-10-07 20:48:45 +0000642 job = self._create_job(hosts=[1])
643 job.run_verify = False
644 job.save()
645
646 self._run_dispatcher() # launches job
647 job.hostqueueentry_set.update(aborted=True)
648 self._run_dispatcher() # kills job, launches gathering
649 self.assert_(self.mock_drone_manager.was_last_process_killed(
650 _PidfileType.JOB))
651 self.mock_drone_manager.finish_process(_PidfileType.GATHER)
652 self._run_dispatcher() # launches parsing + cleanup
653 self._finish_parsing_and_cleanup()
654
655
656 def test_no_pidfile_leaking(self):
showardb8900452009-10-12 20:31:01 +0000657 self._initialize_test()
showardf85a0b72009-10-07 20:48:45 +0000658 self.test_simple_job()
659 self.assertEquals(self.mock_drone_manager._pidfiles, {})
660
661 self.test_job_abort_in_verify()
662 self.assertEquals(self.mock_drone_manager._pidfiles, {})
663
664 self.test_job_abort()
665 self.assertEquals(self.mock_drone_manager._pidfiles, {})
666
667
showardb8900452009-10-12 20:31:01 +0000668 def _make_job_and_queue_entry(self):
669 job = self._create_job(hosts=[1])
670 queue_entry = job.hostqueueentry_set.all()[0]
671 return job, queue_entry
672
673
674 def test_recover_running_no_process(self):
675 # recovery should re-execute a Running HQE if no process is found
676 _, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000677 queue_entry.status = HqeStatus.RUNNING
showardb8900452009-10-12 20:31:01 +0000678 queue_entry.execution_subdir = '1-myuser/host1'
679 queue_entry.save()
showard4a604792009-10-20 23:49:10 +0000680 queue_entry.host.status = HostStatus.RUNNING
showardb8900452009-10-12 20:31:01 +0000681 queue_entry.host.save()
682
683 self._initialize_test()
684 self._run_dispatcher()
showard4a604792009-10-20 23:49:10 +0000685 self._finish_job(queue_entry)
showardb8900452009-10-12 20:31:01 +0000686
687
688 def test_recover_verifying_hqe_no_special_task(self):
689 # recovery should fail on a Verifing HQE with no corresponding
690 # Verify or Cleanup SpecialTask
691 _, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000692 queue_entry.status = HqeStatus.VERIFYING
showardb8900452009-10-12 20:31:01 +0000693 queue_entry.save()
694
695 # make some dummy SpecialTasks that shouldn't count
696 models.SpecialTask.objects.create(host=queue_entry.host,
697 task=models.SpecialTask.Task.VERIFY)
698 models.SpecialTask.objects.create(host=queue_entry.host,
699 task=models.SpecialTask.Task.CLEANUP,
700 queue_entry=queue_entry,
701 is_complete=True)
702
703 self.assertRaises(monitor_db.SchedulerError, self._initialize_test)
704
705
706 def _test_recover_verifying_hqe_helper(self, task, pidfile_type):
707 _, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000708 queue_entry.status = HqeStatus.VERIFYING
showardb8900452009-10-12 20:31:01 +0000709 queue_entry.save()
710
711 special_task = models.SpecialTask.objects.create(
712 host=queue_entry.host, task=task, queue_entry=queue_entry)
713
714 self._initialize_test()
715 self._run_dispatcher()
716 self.mock_drone_manager.finish_process(pidfile_type)
717 self._run_dispatcher()
718 # don't bother checking the rest of the job execution, as long as the
719 # SpecialTask ran
720
721
722 def test_recover_verifying_hqe_with_cleanup(self):
723 # recover an HQE that was in pre-job cleanup
724 self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.CLEANUP,
725 _PidfileType.CLEANUP)
726
727
728 def test_recover_verifying_hqe_with_verify(self):
729 # recover an HQE that was in pre-job verify
730 self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.VERIFY,
731 _PidfileType.VERIFY)
732
733
showarda21b9492009-11-04 20:43:18 +0000734 def test_recover_pending_hqes_with_group(self):
735 # recover a group of HQEs that are in Pending, in the same group (e.g.,
736 # in a job with atomic hosts)
737 job = self._create_job(hosts=[1,2], atomic_group=1)
738 job.save()
739
740 job.hostqueueentry_set.all().update(status=HqeStatus.PENDING)
741
742 self._initialize_test()
743 for queue_entry in job.hostqueueentry_set.all():
744 self.assertEquals(queue_entry.status, HqeStatus.STARTING)
745
746
showardd1195652009-12-08 22:21:02 +0000747 def test_recover_parsing(self):
748 self._initialize_test()
749 job, queue_entry = self._make_job_and_queue_entry()
750 job.run_verify = False
751 job.reboot_after = models.RebootAfter.NEVER
752 job.save()
753
754 self._run_dispatcher() # launches job
755 self.mock_drone_manager.finish_process(_PidfileType.JOB)
756 self._run_dispatcher() # launches parsing
757
758 # now "restart" the scheduler
759 self._create_dispatcher()
760 self._initialize_test()
761 self._run_dispatcher()
762 self.mock_drone_manager.finish_process(_PidfileType.PARSE)
763 self._run_dispatcher()
764
765
766 def test_recover_parsing__no_process_already_aborted(self):
767 _, queue_entry = self._make_job_and_queue_entry()
768 queue_entry.execution_subdir = 'host1'
769 queue_entry.status = HqeStatus.PARSING
770 queue_entry.aborted = True
771 queue_entry.save()
772
773 self._initialize_test()
774 self._run_dispatcher()
775
776
showard65db3932009-10-28 19:54:35 +0000777 def test_job_scheduled_just_after_abort(self):
778 # test a pretty obscure corner case where a job is aborted while queued,
779 # another job is ready to run, and throttling is active. the post-abort
780 # cleanup must not be pre-empted by the second job.
781 job1, queue_entry1 = self._make_job_and_queue_entry()
782 job2, queue_entry2 = self._make_job_and_queue_entry()
783
showard418785b2009-11-23 20:19:59 +0000784 self.mock_drone_manager.process_capacity = 0
showard65db3932009-10-28 19:54:35 +0000785 self._run_dispatcher() # schedule job1, but won't start verify
786 job1.hostqueueentry_set.update(aborted=True)
showard418785b2009-11-23 20:19:59 +0000787 self.mock_drone_manager.process_capacity = 100
showard65db3932009-10-28 19:54:35 +0000788 self._run_dispatcher() # cleanup must run here, not verify for job2
789 self._check_statuses(queue_entry1, HqeStatus.ABORTED,
790 HostStatus.CLEANING)
791 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
792 self._run_dispatcher() # now verify starts for job2
793 self._check_statuses(queue_entry2, HqeStatus.VERIFYING,
794 HostStatus.VERIFYING)
795
796
showard65db3932009-10-28 19:54:35 +0000797 def test_reverify_interrupting_pre_job(self):
798 # ensure things behave sanely if a reverify is scheduled in the middle
799 # of pre-job actions
800 _, queue_entry = self._make_job_and_queue_entry()
801
802 self._run_dispatcher() # pre-job verify
803 self._create_reverify_request()
804 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
805 exit_status=256)
806 self._run_dispatcher() # repair
807 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
808 self._run_dispatcher() # reverify runs now
809 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
810 self._run_dispatcher() # pre-job verify
811 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
812 self._run_dispatcher() # and job runs...
813 self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING)
814 self._finish_job(queue_entry) # reverify has been deleted
815 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
816 HostStatus.READY)
817 self._assert_nothing_is_running()
818
819
820 def test_reverify_while_job_running(self):
821 # once a job is running, a reverify must not be allowed to preempt
822 # Gathering
823 _, queue_entry = self._make_job_and_queue_entry()
824 self._run_pre_job_verify(queue_entry)
825 self._run_dispatcher() # job runs
826 self._create_reverify_request()
827 # make job end with a signal, so gathering will run
828 self.mock_drone_manager.finish_process(_PidfileType.JOB,
829 exit_status=271)
830 self._run_dispatcher() # gathering must start
831 self.mock_drone_manager.finish_process(_PidfileType.GATHER)
832 self._run_dispatcher() # parsing and cleanup
833 self._finish_parsing_and_cleanup()
834 self._run_dispatcher() # now reverify runs
835 self._check_statuses(queue_entry, HqeStatus.FAILED,
836 HostStatus.VERIFYING)
837 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
838 self._run_dispatcher()
839 self._check_host_status(queue_entry.host, HostStatus.READY)
840
841
842 def test_reverify_while_host_pending(self):
843 # ensure that if a reverify is scheduled while a host is in Pending, it
844 # won't run until the host is actually free
845 job = self._create_job(hosts=[1,2])
846 queue_entry = job.hostqueueentry_set.get(host__hostname='host1')
847 job.synch_count = 2
848 job.save()
849
850 host2 = self.hosts[1]
851 host2.locked = True
852 host2.save()
853
854 self._run_dispatcher() # verify host1
855 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
856 self._run_dispatcher() # host1 Pending
857 self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING)
858 self._create_reverify_request()
859 self._run_dispatcher() # nothing should happen here
860 self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING)
861
862 # now let the job run
863 host2.locked = False
864 host2.save()
865 self._run_dispatcher() # verify host2
866 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
867 self._run_dispatcher() # run job
868 self._finish_job(queue_entry)
869 # need to explicitly finish host1's post-job cleanup
870 self.mock_drone_manager.finish_specific_process(
871 'hosts/host1/4-cleanup', monitor_db._AUTOSERV_PID_FILE)
872 self._run_dispatcher()
873 # the reverify should now be running
874 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
875 HostStatus.VERIFYING)
876 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
877 self._run_dispatcher()
878 self._check_host_status(queue_entry.host, HostStatus.READY)
879
880
showard418785b2009-11-23 20:19:59 +0000881 def test_throttling(self):
882 job = self._create_job(hosts=[1,2,3])
883 job.synch_count = 3
884 job.save()
885
886 queue_entries = list(job.hostqueueentry_set.all())
887 def _check_hqe_statuses(*statuses):
888 for queue_entry, status in zip(queue_entries, statuses):
889 self._check_statuses(queue_entry, status)
890
891 self.mock_drone_manager.process_capacity = 2
892 self._run_dispatcher() # verify runs on 1 and 2
893 _check_hqe_statuses(HqeStatus.VERIFYING, HqeStatus.VERIFYING,
894 HqeStatus.VERIFYING)
895 self.assertEquals(len(self.mock_drone_manager.running_pidfile_ids()), 2)
896
897 self.mock_drone_manager.finish_specific_process(
898 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE)
899 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
900 self._run_dispatcher() # verify runs on 3
901 _check_hqe_statuses(HqeStatus.PENDING, HqeStatus.PENDING,
902 HqeStatus.VERIFYING)
903
904 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
905 self._run_dispatcher() # job won't run due to throttling
906 _check_hqe_statuses(HqeStatus.STARTING, HqeStatus.STARTING,
907 HqeStatus.STARTING)
908 self._assert_nothing_is_running()
909
910 self.mock_drone_manager.process_capacity = 3
911 self._run_dispatcher() # now job runs
912 _check_hqe_statuses(HqeStatus.RUNNING, HqeStatus.RUNNING,
913 HqeStatus.RUNNING)
914
915 self.mock_drone_manager.process_capacity = 2
916 self.mock_drone_manager.finish_process(_PidfileType.JOB,
917 exit_status=271)
918 self._run_dispatcher() # gathering won't run due to throttling
919 _check_hqe_statuses(HqeStatus.GATHERING, HqeStatus.GATHERING,
920 HqeStatus.GATHERING)
921 self._assert_nothing_is_running()
922
923 self.mock_drone_manager.process_capacity = 3
924 self._run_dispatcher() # now gathering runs
925
926 self.mock_drone_manager.process_capacity = 0
927 self.mock_drone_manager.finish_process(_PidfileType.GATHER)
928 self._run_dispatcher() # parsing runs despite throttling
929 _check_hqe_statuses(HqeStatus.PARSING, HqeStatus.PARSING,
930 HqeStatus.PARSING)
931
932
showardd1195652009-12-08 22:21:02 +0000933 def test_simple_atomic_group_job(self):
934 job = self._create_job(atomic_group=1)
935 self._run_dispatcher() # expand + verify
936 queue_entries = job.hostqueueentry_set.all()
937 self.assertEquals(len(queue_entries), 2)
938 self.assertEquals(queue_entries[0].host.hostname, 'host5')
939 self.assertEquals(queue_entries[1].host.hostname, 'host6')
940
941 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
942 self._run_dispatcher() # delay task started waiting
943
944 self.mock_drone_manager.finish_specific_process(
945 'hosts/host5/1-verify', monitor_db._AUTOSERV_PID_FILE)
946 self._run_dispatcher() # job starts now
947 for entry in queue_entries:
948 self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING)
949
950 # rest of job proceeds normally
951
952
showard2ca64c92009-12-10 21:41:02 +0000953 def test_simple_metahost_assignment(self):
954 job = self._create_job(metahosts=[1])
955 self._run_dispatcher()
956 entry = job.hostqueueentry_set.all()[0]
957 self.assertEquals(entry.host.hostname, 'host1')
958 self._check_statuses(entry, HqeStatus.VERIFYING, HostStatus.VERIFYING)
959 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
960 self._run_dispatcher()
961 self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING)
962 # rest of job proceeds normally
963
964
965 def test_metahost_fail_verify(self):
966 self.hosts[1].labels.add(self.labels[0]) # put label1 also on host2
967 job = self._create_job(metahosts=[1])
968 self._run_dispatcher() # assigned to host1
969 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
970 exit_status=256)
971 self._run_dispatcher() # host1 failed, gets reassigned to host2
972 entry = job.hostqueueentry_set.all()[0]
973 self.assertEquals(entry.host.hostname, 'host2')
974 self._check_statuses(entry, HqeStatus.VERIFYING, HostStatus.VERIFYING)
975 self._check_host_status(self.hosts[0], HostStatus.REPAIRING)
976
977 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
978 self._run_dispatcher()
979 self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING)
980
981
showarda9545c02009-12-18 22:44:26 +0000982 def test_hostless_job(self):
983 job = self._create_job(hostless=True)
984 entry = job.hostqueueentry_set.all()[0]
985
986 self._run_dispatcher()
987 self._check_entry_status(entry, HqeStatus.RUNNING)
988
989 self.mock_drone_manager.finish_process(_PidfileType.JOB)
990 self._run_dispatcher()
991 self._check_entry_status(entry, HqeStatus.COMPLETED)
992
993
showard493beaa2009-12-18 22:44:45 +0000994 def test_pre_job_keyvals(self):
995 self.test_simple_job()
996 attached_files = self.mock_drone_manager.attached_files(
997 '1-my_user/host1')
998 job_keyval_path = '1-my_user/host1/keyval'
999 self.assert_(job_keyval_path in attached_files, attached_files)
1000 keyval_contents = attached_files[job_keyval_path]
1001 keyval_dict = dict(line.strip().split('=', 1)
1002 for line in keyval_contents.splitlines())
1003 self.assert_('job_queued' in keyval_dict, keyval_dict)
1004
1005
showard34ab0992009-10-05 22:47:57 +00001006if __name__ == '__main__':
1007 unittest.main()