blob: 228027f2070db22a50f15c5cbdbdb12b914841c3 [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
showard34ab0992009-10-05 22:47:57 +0000164 # DroneManager emulation APIs for use by monitor_db
165
166 def get_orphaned_autoserv_processes(self):
167 return set()
168
169
170 def total_running_processes(self):
showard418785b2009-11-23 20:19:59 +0000171 return sum(pidfile_id._num_processes
172 for pidfile_id in self.nonfinished_pidfile_ids())
showard34ab0992009-10-05 22:47:57 +0000173
174
showard9bb960b2009-11-19 01:02:11 +0000175 def max_runnable_processes(self, username):
showard418785b2009-11-23 20:19:59 +0000176 return self.process_capacity - self.total_running_processes()
showard34ab0992009-10-05 22:47:57 +0000177
178
showard4a604792009-10-20 23:49:10 +0000179 def refresh(self):
180 for pidfile_id in self._unregistered_pidfiles:
181 # intentionally handle non-registered pidfiles silently
182 self._pidfiles.pop(pidfile_id, None)
183 self._unregistered_pidfiles = set()
184
185
showard34ab0992009-10-05 22:47:57 +0000186 def execute_actions(self):
187 # executing an "execute_command" causes a pidfile to be created
188 for pidfile_id in self._future_pidfiles:
189 # Process objects are opaque to monitor_db
showardf85a0b72009-10-07 20:48:45 +0000190 process = object()
191 self._pidfiles[pidfile_id].process = process
192 self._process_index[process] = pidfile_id
showard34ab0992009-10-05 22:47:57 +0000193 self._future_pidfiles = []
194
195
196 def attach_file_to_execution(self, result_dir, file_contents,
197 file_path=None):
198 self._attached_files.setdefault(result_dir, set()).add((file_path,
199 file_contents))
200 return 'attach_path'
201
202
showardf85a0b72009-10-07 20:48:45 +0000203 def _initialize_pidfile(self, pidfile_id):
204 if pidfile_id not in self._pidfiles:
showardd1195652009-12-08 22:21:02 +0000205 assert pidfile_id.key() not in self._pidfile_index
showardf85a0b72009-10-07 20:48:45 +0000206 self._pidfiles[pidfile_id] = drone_manager.PidfileContents()
showardd1195652009-12-08 22:21:02 +0000207 self._pidfile_index[pidfile_id.key()] = pidfile_id
showardf85a0b72009-10-07 20:48:45 +0000208
209
210 def _set_last_pidfile(self, pidfile_id, working_directory, pidfile_name):
211 if working_directory.startswith('hosts/'):
212 # such paths look like hosts/host1/1-verify, we'll grab the end
213 type_string = working_directory.rsplit('-', 1)[1]
214 pidfile_type = _PidfileType.get_value(type_string)
215 else:
showardd1195652009-12-08 22:21:02 +0000216 pidfile_type = _PIDFILE_TO_PIDFILE_TYPE[pidfile_name]
showardf85a0b72009-10-07 20:48:45 +0000217 self._last_pidfile_id[pidfile_type] = pidfile_id
218
219
showard34ab0992009-10-05 22:47:57 +0000220 def execute_command(self, command, working_directory, pidfile_name,
showard418785b2009-11-23 20:19:59 +0000221 num_processes, log_file=None, paired_with_pidfile=None,
showard9bb960b2009-11-19 01:02:11 +0000222 username=None):
showarda9545c02009-12-18 22:44:26 +0000223 logging.debug('Executing %s in %s', command, working_directory)
showardd1195652009-12-08 22:21:02 +0000224 pidfile_id = self._DummyPidfileId(working_directory, pidfile_name)
225 if pidfile_id.key() in self._pidfile_index:
226 pidfile_id = self._pidfile_index[pidfile_id.key()]
227 pidfile_id._num_processes = num_processes
228 pidfile_id._paired_with_pidfile = paired_with_pidfile
229
showard34ab0992009-10-05 22:47:57 +0000230 self._future_pidfiles.append(pidfile_id)
showardf85a0b72009-10-07 20:48:45 +0000231 self._initialize_pidfile(pidfile_id)
showard34ab0992009-10-05 22:47:57 +0000232 self._pidfile_index[(working_directory, pidfile_name)] = pidfile_id
showardf85a0b72009-10-07 20:48:45 +0000233 self._set_last_pidfile(pidfile_id, working_directory, pidfile_name)
showard34ab0992009-10-05 22:47:57 +0000234 return pidfile_id
235
236
237 def get_pidfile_contents(self, pidfile_id, use_second_read=False):
showard4a604792009-10-20 23:49:10 +0000238 if pidfile_id not in self._pidfiles:
showarda9545c02009-12-18 22:44:26 +0000239 logging.debug('Request for nonexistent pidfile %s' % pidfile_id)
showard4a604792009-10-20 23:49:10 +0000240 return self._pidfiles.get(pidfile_id, drone_manager.PidfileContents())
showard34ab0992009-10-05 22:47:57 +0000241
242
243 def is_process_running(self, process):
244 return True
245
246
247 def register_pidfile(self, pidfile_id):
showardf85a0b72009-10-07 20:48:45 +0000248 self._initialize_pidfile(pidfile_id)
249
250
251 def unregister_pidfile(self, pidfile_id):
showard4a604792009-10-20 23:49:10 +0000252 self._unregistered_pidfiles.add(pidfile_id)
showard34ab0992009-10-05 22:47:57 +0000253
254
showardd1195652009-12-08 22:21:02 +0000255 def declare_process_count(self, pidfile_id, num_processes):
256 pidfile_id.num_processes = num_processes
257
258
showard34ab0992009-10-05 22:47:57 +0000259 def absolute_path(self, path):
260 return 'absolute/' + path
261
262
263 def write_lines_to_file(self, file_path, lines, paired_with_process=None):
264 # TODO: record this
265 pass
266
267
268 def get_pidfile_id_from(self, execution_tag, pidfile_name):
showardd1195652009-12-08 22:21:02 +0000269 default_pidfile = self._DummyPidfileId(execution_tag, pidfile_name,
270 num_processes=0)
showard4a604792009-10-20 23:49:10 +0000271 return self._pidfile_index.get((execution_tag, pidfile_name),
showardd1195652009-12-08 22:21:02 +0000272 default_pidfile)
showard34ab0992009-10-05 22:47:57 +0000273
274
showardf85a0b72009-10-07 20:48:45 +0000275 def kill_process(self, process):
276 pidfile_id = self._process_index[process]
277 self._killed_pidfiles.add(pidfile_id)
278 self._set_pidfile_exit_status(pidfile_id, 271)
279
280
showard34ab0992009-10-05 22:47:57 +0000281class MockEmailManager(NullMethodObject):
282 _NULL_METHODS = ('send_queued_emails', 'send_email')
283
showardf85a0b72009-10-07 20:48:45 +0000284 def enqueue_notify_email(self, subject, message):
285 logging.warn('enqueue_notify_email: %s', subject)
286 logging.warn(message)
287
showard34ab0992009-10-05 22:47:57 +0000288
289class SchedulerFunctionalTest(unittest.TestCase,
290 frontend_test_utils.FrontendTestMixin):
291 # some number of ticks after which the scheduler is presumed to have
292 # stabilized, given no external changes
293 _A_LOT_OF_TICKS = 10
294
295 def setUp(self):
296 self._frontend_common_setup()
297 self._set_stubs()
298 self._set_global_config_values()
showardd1195652009-12-08 22:21:02 +0000299 self._create_dispatcher()
showard34ab0992009-10-05 22:47:57 +0000300
301 logging.basicConfig(level=logging.DEBUG)
302
303
showardd1195652009-12-08 22:21:02 +0000304 def _create_dispatcher(self):
305 self.dispatcher = monitor_db.Dispatcher()
306
307
showard34ab0992009-10-05 22:47:57 +0000308 def tearDown(self):
309 self._frontend_common_teardown()
310
311
312 def _set_stubs(self):
313 self.mock_config = MockGlobalConfig()
314 self.god.stub_with(global_config, 'global_config', self.mock_config)
315
316 self.mock_drone_manager = MockDroneManager()
317 self.god.stub_with(monitor_db, '_drone_manager',
318 self.mock_drone_manager)
319
320 self.mock_email_manager = MockEmailManager()
321 self.god.stub_with(email_manager, 'manager', self.mock_email_manager)
322
323 self._database = (
324 database_connection.TranslatingDatabase.get_test_database(
325 file_path=self._test_db_file,
326 translators=_DB_TRANSLATORS))
327 self._database.connect(db_type='django')
328 self.god.stub_with(monitor_db, '_db', self._database)
329
330
331 def _set_global_config_values(self):
332 self.mock_config.set_config_value('SCHEDULER', 'pidfile_timeout_mins',
333 1)
334
335
336 def _initialize_test(self):
337 self.dispatcher.initialize()
338
339
340 def _run_dispatcher(self):
341 for _ in xrange(self._A_LOT_OF_TICKS):
342 self.dispatcher.tick()
343
344
345 def test_idle(self):
showardb8900452009-10-12 20:31:01 +0000346 self._initialize_test()
showard34ab0992009-10-05 22:47:57 +0000347 self._run_dispatcher()
348
349
showardb8900452009-10-12 20:31:01 +0000350 def _assert_process_executed(self, working_directory, pidfile_name):
351 process_was_executed = self.mock_drone_manager.was_process_executed(
352 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE)
353 self.assert_(process_was_executed,
354 '%s/%s not executed' % (working_directory, pidfile_name))
355
356
showardd1195652009-12-08 22:21:02 +0000357 def _update_instance(self, model_instance):
358 return type(model_instance).objects.get(pk=model_instance.pk)
359
360
showard418785b2009-11-23 20:19:59 +0000361 def _check_statuses(self, queue_entry, queue_entry_status,
362 host_status=None):
showarda9545c02009-12-18 22:44:26 +0000363 self._check_entry_status(queue_entry, queue_entry_status)
364 if host_status:
365 self._check_host_status(queue_entry.host, host_status)
366
367
368 def _check_entry_status(self, queue_entry, status):
showard4a604792009-10-20 23:49:10 +0000369 # update from DB
showardd1195652009-12-08 22:21:02 +0000370 queue_entry = self._update_instance(queue_entry)
showarda9545c02009-12-18 22:44:26 +0000371 self.assertEquals(queue_entry.status, status)
showard4a604792009-10-20 23:49:10 +0000372
373
showard7b2d7cb2009-10-28 19:53:03 +0000374 def _check_host_status(self, host, status):
375 # update from DB
showarda9545c02009-12-18 22:44:26 +0000376 host = self._update_instance(host)
showard7b2d7cb2009-10-28 19:53:03 +0000377 self.assertEquals(host.status, status)
378
379
showard4a604792009-10-20 23:49:10 +0000380 def _run_pre_job_verify(self, queue_entry):
381 self._run_dispatcher() # launches verify
382 self._check_statuses(queue_entry, HqeStatus.VERIFYING,
383 HostStatus.VERIFYING)
384 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
385
386
showard34ab0992009-10-05 22:47:57 +0000387 def test_simple_job(self):
showardb8900452009-10-12 20:31:01 +0000388 self._initialize_test()
389 job, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000390 self._run_pre_job_verify(queue_entry)
showard34ab0992009-10-05 22:47:57 +0000391 self._run_dispatcher() # launches job
showard4a604792009-10-20 23:49:10 +0000392 self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING)
393 self._finish_job(queue_entry)
394 self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY)
395 self._assert_nothing_is_running()
showardb8900452009-10-12 20:31:01 +0000396
397
showard4a604792009-10-20 23:49:10 +0000398 def _setup_for_pre_job_cleanup(self):
399 self._initialize_test()
400 job, queue_entry = self._make_job_and_queue_entry()
401 job.reboot_before = models.RebootBefore.ALWAYS
402 job.save()
403 return queue_entry
404
405
406 def _run_pre_job_cleanup_job(self, queue_entry):
407 self._run_dispatcher() # cleanup
408 self._check_statuses(queue_entry, HqeStatus.VERIFYING,
409 HostStatus.CLEANING)
410 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
411 self._run_dispatcher() # verify
412 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
413 self._run_dispatcher() # job
414 self._finish_job(queue_entry)
415
416
417 def test_pre_job_cleanup(self):
418 queue_entry = self._setup_for_pre_job_cleanup()
419 self._run_pre_job_cleanup_job(queue_entry)
420
421
422 def _run_pre_job_cleanup_one_failure(self):
423 queue_entry = self._setup_for_pre_job_cleanup()
424 self._run_dispatcher() # cleanup
425 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
426 exit_status=256)
427 self._run_dispatcher() # repair
428 self._check_statuses(queue_entry, HqeStatus.QUEUED,
429 HostStatus.REPAIRING)
430 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
431 return queue_entry
432
433
434 def test_pre_job_cleanup_failure(self):
435 queue_entry = self._run_pre_job_cleanup_one_failure()
436 # from here the job should run as normal
437 self._run_pre_job_cleanup_job(queue_entry)
438
439
440 def test_pre_job_cleanup_double_failure(self):
441 # TODO (showard): this test isn't perfect. in reality, when the second
442 # cleanup fails, it copies its results over to the job directory using
443 # copy_results_on_drone() and then parses them. since we don't handle
444 # that, there appear to be no results at the job directory. the
445 # scheduler handles this gracefully, parsing gets effectively skipped,
446 # and this test passes as is. but we ought to properly test that
447 # behavior.
448 queue_entry = self._run_pre_job_cleanup_one_failure()
449 self._run_dispatcher() # second cleanup
450 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
451 exit_status=256)
452 self._run_dispatcher()
453 self._check_statuses(queue_entry, HqeStatus.FAILED,
454 HostStatus.REPAIR_FAILED)
455 # nothing else should run
456 self._assert_nothing_is_running()
457
458
459 def _assert_nothing_is_running(self):
460 self.assertEquals(self.mock_drone_manager.running_pidfile_ids(), [])
461
462
showard7b2d7cb2009-10-28 19:53:03 +0000463 def _setup_for_post_job_cleanup(self):
showard4a604792009-10-20 23:49:10 +0000464 self._initialize_test()
465 job, queue_entry = self._make_job_and_queue_entry()
466 job.reboot_after = models.RebootAfter.ALWAYS
467 job.save()
showard7b2d7cb2009-10-28 19:53:03 +0000468 return queue_entry
showard4a604792009-10-20 23:49:10 +0000469
showard7b2d7cb2009-10-28 19:53:03 +0000470
471 def _run_post_job_cleanup_failure_up_to_repair(self, queue_entry,
472 include_verify=True):
473 if include_verify:
474 self._run_pre_job_verify(queue_entry)
showard4a604792009-10-20 23:49:10 +0000475 self._run_dispatcher() # job
476 self.mock_drone_manager.finish_process(_PidfileType.JOB)
477 self._run_dispatcher() # parsing + cleanup
478 self.mock_drone_manager.finish_process(_PidfileType.PARSE)
479 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
480 exit_status=256)
481 self._run_dispatcher() # repair, HQE unaffected
showard4a604792009-10-20 23:49:10 +0000482 return queue_entry
483
484
485 def test_post_job_cleanup_failure(self):
showard7b2d7cb2009-10-28 19:53:03 +0000486 queue_entry = self._setup_for_post_job_cleanup()
487 self._run_post_job_cleanup_failure_up_to_repair(queue_entry)
488 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
489 HostStatus.REPAIRING)
showard4a604792009-10-20 23:49:10 +0000490 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
491 self._run_dispatcher()
492 self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY)
493
494
495 def test_post_job_cleanup_failure_repair_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)
showard4a604792009-10-20 23:49:10 +0000498 self.mock_drone_manager.finish_process(_PidfileType.REPAIR,
499 exit_status=256)
500 self._run_dispatcher()
501 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
502 HostStatus.REPAIR_FAILED)
503
504
showardd1195652009-12-08 22:21:02 +0000505 def _ensure_post_job_process_is_paired(self, queue_entry, pidfile_type):
506 pidfile_name = _PIDFILE_TYPE_TO_PIDFILE[pidfile_type]
507 queue_entry = self._update_instance(queue_entry)
508 pidfile_id = self.mock_drone_manager.pidfile_from_path(
509 queue_entry.execution_path(), pidfile_name)
510 self.assert_(pidfile_id._paired_with_pidfile)
511
512
showard4a604792009-10-20 23:49:10 +0000513 def _finish_job(self, queue_entry):
showardf85a0b72009-10-07 20:48:45 +0000514 self.mock_drone_manager.finish_process(_PidfileType.JOB)
showard34ab0992009-10-05 22:47:57 +0000515 self._run_dispatcher() # launches parsing + cleanup
showard4a604792009-10-20 23:49:10 +0000516 self._check_statuses(queue_entry, HqeStatus.PARSING,
517 HostStatus.CLEANING)
showardd1195652009-12-08 22:21:02 +0000518 self._ensure_post_job_process_is_paired(queue_entry, _PidfileType.PARSE)
showardf85a0b72009-10-07 20:48:45 +0000519 self._finish_parsing_and_cleanup()
520
521
522 def _finish_parsing_and_cleanup(self):
523 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
524 self.mock_drone_manager.finish_process(_PidfileType.PARSE)
showard34ab0992009-10-05 22:47:57 +0000525 self._run_dispatcher()
526
527
showard7b2d7cb2009-10-28 19:53:03 +0000528 def _create_reverify_request(self):
529 host = self.hosts[0]
530 models.SpecialTask.objects.create(host=host,
showard9bb960b2009-11-19 01:02:11 +0000531 task=models.SpecialTask.Task.VERIFY,
532 requested_by=self.user)
showard7b2d7cb2009-10-28 19:53:03 +0000533 return host
534
535
536 def test_requested_reverify(self):
537 host = self._create_reverify_request()
538 self._run_dispatcher()
539 self._check_host_status(host, HostStatus.VERIFYING)
540 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
541 self._run_dispatcher()
542 self._check_host_status(host, HostStatus.READY)
543
544
545 def test_requested_reverify_failure(self):
546 host = self._create_reverify_request()
547 self._run_dispatcher()
548 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
549 exit_status=256)
550 self._run_dispatcher() # repair
551 self._check_host_status(host, HostStatus.REPAIRING)
552 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
553 self._run_dispatcher()
554 self._check_host_status(host, HostStatus.READY)
555
556
557 def _setup_for_do_not_verify(self):
558 self._initialize_test()
559 job, queue_entry = self._make_job_and_queue_entry()
560 queue_entry.host.protection = host_protections.Protection.DO_NOT_VERIFY
561 queue_entry.host.save()
562 return queue_entry
563
564
565 def test_do_not_verify_job(self):
566 queue_entry = self._setup_for_do_not_verify()
567 self._run_dispatcher() # runs job directly
568 self._finish_job(queue_entry)
569
570
571 def test_do_not_verify_job_with_cleanup(self):
572 queue_entry = self._setup_for_do_not_verify()
573 queue_entry.job.reboot_before = models.RebootBefore.ALWAYS
574 queue_entry.job.save()
575
576 self._run_dispatcher() # cleanup
577 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
578 self._run_dispatcher() # job
579 self._finish_job(queue_entry)
580
581
582 def test_do_not_verify_pre_job_cleanup_failure(self):
583 queue_entry = self._setup_for_do_not_verify()
584 queue_entry.job.reboot_before = models.RebootBefore.ALWAYS
585 queue_entry.job.save()
586
587 self._run_dispatcher() # cleanup
588 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP,
589 exit_status=256)
590 self._run_dispatcher() # failure ignored; job runs
591 self._finish_job(queue_entry)
592
593
594 def test_do_not_verify_post_job_cleanup_failure(self):
595 queue_entry = self._setup_for_do_not_verify()
596
597 self._run_post_job_cleanup_failure_up_to_repair(queue_entry,
598 include_verify=False)
599 # failure ignored, host still set to Ready
600 self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY)
601 self._run_dispatcher() # nothing else runs
602 self._assert_nothing_is_running()
603
604
605 def test_do_not_verify_requested_reverify_failure(self):
606 host = self._create_reverify_request()
607 host.protection = host_protections.Protection.DO_NOT_VERIFY
608 host.save()
609
610 self._run_dispatcher()
611 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
612 exit_status=256)
613 self._run_dispatcher()
614 self._check_host_status(host, HostStatus.READY) # ignore failure
615 self._assert_nothing_is_running()
616
617
showardf85a0b72009-10-07 20:48:45 +0000618 def test_job_abort_in_verify(self):
showardb8900452009-10-12 20:31:01 +0000619 self._initialize_test()
showardf85a0b72009-10-07 20:48:45 +0000620 job = self._create_job(hosts=[1])
621 self._run_dispatcher() # launches verify
622 job.hostqueueentry_set.update(aborted=True)
623 self._run_dispatcher() # kills verify, launches cleanup
624 self.assert_(self.mock_drone_manager.was_last_process_killed(
625 _PidfileType.VERIFY))
626 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
627 self._run_dispatcher()
628
629
630 def test_job_abort(self):
showardb8900452009-10-12 20:31:01 +0000631 self._initialize_test()
showardf85a0b72009-10-07 20:48:45 +0000632 job = self._create_job(hosts=[1])
633 job.run_verify = False
634 job.save()
635
636 self._run_dispatcher() # launches job
637 job.hostqueueentry_set.update(aborted=True)
638 self._run_dispatcher() # kills job, launches gathering
639 self.assert_(self.mock_drone_manager.was_last_process_killed(
640 _PidfileType.JOB))
641 self.mock_drone_manager.finish_process(_PidfileType.GATHER)
642 self._run_dispatcher() # launches parsing + cleanup
643 self._finish_parsing_and_cleanup()
644
645
646 def test_no_pidfile_leaking(self):
showardb8900452009-10-12 20:31:01 +0000647 self._initialize_test()
showardf85a0b72009-10-07 20:48:45 +0000648 self.test_simple_job()
649 self.assertEquals(self.mock_drone_manager._pidfiles, {})
650
651 self.test_job_abort_in_verify()
652 self.assertEquals(self.mock_drone_manager._pidfiles, {})
653
654 self.test_job_abort()
655 self.assertEquals(self.mock_drone_manager._pidfiles, {})
656
657
showardb8900452009-10-12 20:31:01 +0000658 def _make_job_and_queue_entry(self):
659 job = self._create_job(hosts=[1])
660 queue_entry = job.hostqueueentry_set.all()[0]
661 return job, queue_entry
662
663
664 def test_recover_running_no_process(self):
665 # recovery should re-execute a Running HQE if no process is found
666 _, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000667 queue_entry.status = HqeStatus.RUNNING
showardb8900452009-10-12 20:31:01 +0000668 queue_entry.execution_subdir = '1-myuser/host1'
669 queue_entry.save()
showard4a604792009-10-20 23:49:10 +0000670 queue_entry.host.status = HostStatus.RUNNING
showardb8900452009-10-12 20:31:01 +0000671 queue_entry.host.save()
672
673 self._initialize_test()
674 self._run_dispatcher()
showard4a604792009-10-20 23:49:10 +0000675 self._finish_job(queue_entry)
showardb8900452009-10-12 20:31:01 +0000676
677
678 def test_recover_verifying_hqe_no_special_task(self):
679 # recovery should fail on a Verifing HQE with no corresponding
680 # Verify or Cleanup SpecialTask
681 _, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000682 queue_entry.status = HqeStatus.VERIFYING
showardb8900452009-10-12 20:31:01 +0000683 queue_entry.save()
684
685 # make some dummy SpecialTasks that shouldn't count
686 models.SpecialTask.objects.create(host=queue_entry.host,
687 task=models.SpecialTask.Task.VERIFY)
688 models.SpecialTask.objects.create(host=queue_entry.host,
689 task=models.SpecialTask.Task.CLEANUP,
690 queue_entry=queue_entry,
691 is_complete=True)
692
693 self.assertRaises(monitor_db.SchedulerError, self._initialize_test)
694
695
696 def _test_recover_verifying_hqe_helper(self, task, pidfile_type):
697 _, queue_entry = self._make_job_and_queue_entry()
showard4a604792009-10-20 23:49:10 +0000698 queue_entry.status = HqeStatus.VERIFYING
showardb8900452009-10-12 20:31:01 +0000699 queue_entry.save()
700
701 special_task = models.SpecialTask.objects.create(
702 host=queue_entry.host, task=task, queue_entry=queue_entry)
703
704 self._initialize_test()
705 self._run_dispatcher()
706 self.mock_drone_manager.finish_process(pidfile_type)
707 self._run_dispatcher()
708 # don't bother checking the rest of the job execution, as long as the
709 # SpecialTask ran
710
711
712 def test_recover_verifying_hqe_with_cleanup(self):
713 # recover an HQE that was in pre-job cleanup
714 self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.CLEANUP,
715 _PidfileType.CLEANUP)
716
717
718 def test_recover_verifying_hqe_with_verify(self):
719 # recover an HQE that was in pre-job verify
720 self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.VERIFY,
721 _PidfileType.VERIFY)
722
723
showarda21b9492009-11-04 20:43:18 +0000724 def test_recover_pending_hqes_with_group(self):
725 # recover a group of HQEs that are in Pending, in the same group (e.g.,
726 # in a job with atomic hosts)
727 job = self._create_job(hosts=[1,2], atomic_group=1)
728 job.save()
729
730 job.hostqueueentry_set.all().update(status=HqeStatus.PENDING)
731
732 self._initialize_test()
733 for queue_entry in job.hostqueueentry_set.all():
734 self.assertEquals(queue_entry.status, HqeStatus.STARTING)
735
736
showardd1195652009-12-08 22:21:02 +0000737 def test_recover_parsing(self):
738 self._initialize_test()
739 job, queue_entry = self._make_job_and_queue_entry()
740 job.run_verify = False
741 job.reboot_after = models.RebootAfter.NEVER
742 job.save()
743
744 self._run_dispatcher() # launches job
745 self.mock_drone_manager.finish_process(_PidfileType.JOB)
746 self._run_dispatcher() # launches parsing
747
748 # now "restart" the scheduler
749 self._create_dispatcher()
750 self._initialize_test()
751 self._run_dispatcher()
752 self.mock_drone_manager.finish_process(_PidfileType.PARSE)
753 self._run_dispatcher()
754
755
756 def test_recover_parsing__no_process_already_aborted(self):
757 _, queue_entry = self._make_job_and_queue_entry()
758 queue_entry.execution_subdir = 'host1'
759 queue_entry.status = HqeStatus.PARSING
760 queue_entry.aborted = True
761 queue_entry.save()
762
763 self._initialize_test()
764 self._run_dispatcher()
765
766
showard65db3932009-10-28 19:54:35 +0000767 def test_job_scheduled_just_after_abort(self):
768 # test a pretty obscure corner case where a job is aborted while queued,
769 # another job is ready to run, and throttling is active. the post-abort
770 # cleanup must not be pre-empted by the second job.
771 job1, queue_entry1 = self._make_job_and_queue_entry()
772 job2, queue_entry2 = self._make_job_and_queue_entry()
773
showard418785b2009-11-23 20:19:59 +0000774 self.mock_drone_manager.process_capacity = 0
showard65db3932009-10-28 19:54:35 +0000775 self._run_dispatcher() # schedule job1, but won't start verify
776 job1.hostqueueentry_set.update(aborted=True)
showard418785b2009-11-23 20:19:59 +0000777 self.mock_drone_manager.process_capacity = 100
showard65db3932009-10-28 19:54:35 +0000778 self._run_dispatcher() # cleanup must run here, not verify for job2
779 self._check_statuses(queue_entry1, HqeStatus.ABORTED,
780 HostStatus.CLEANING)
781 self.mock_drone_manager.finish_process(_PidfileType.CLEANUP)
782 self._run_dispatcher() # now verify starts for job2
783 self._check_statuses(queue_entry2, HqeStatus.VERIFYING,
784 HostStatus.VERIFYING)
785
786
showard65db3932009-10-28 19:54:35 +0000787 def test_reverify_interrupting_pre_job(self):
788 # ensure things behave sanely if a reverify is scheduled in the middle
789 # of pre-job actions
790 _, queue_entry = self._make_job_and_queue_entry()
791
792 self._run_dispatcher() # pre-job verify
793 self._create_reverify_request()
794 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
795 exit_status=256)
796 self._run_dispatcher() # repair
797 self.mock_drone_manager.finish_process(_PidfileType.REPAIR)
798 self._run_dispatcher() # reverify runs now
799 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
800 self._run_dispatcher() # pre-job verify
801 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
802 self._run_dispatcher() # and job runs...
803 self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING)
804 self._finish_job(queue_entry) # reverify has been deleted
805 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
806 HostStatus.READY)
807 self._assert_nothing_is_running()
808
809
810 def test_reverify_while_job_running(self):
811 # once a job is running, a reverify must not be allowed to preempt
812 # Gathering
813 _, queue_entry = self._make_job_and_queue_entry()
814 self._run_pre_job_verify(queue_entry)
815 self._run_dispatcher() # job runs
816 self._create_reverify_request()
817 # make job end with a signal, so gathering will run
818 self.mock_drone_manager.finish_process(_PidfileType.JOB,
819 exit_status=271)
820 self._run_dispatcher() # gathering must start
821 self.mock_drone_manager.finish_process(_PidfileType.GATHER)
822 self._run_dispatcher() # parsing and cleanup
823 self._finish_parsing_and_cleanup()
824 self._run_dispatcher() # now reverify runs
825 self._check_statuses(queue_entry, HqeStatus.FAILED,
826 HostStatus.VERIFYING)
827 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
828 self._run_dispatcher()
829 self._check_host_status(queue_entry.host, HostStatus.READY)
830
831
832 def test_reverify_while_host_pending(self):
833 # ensure that if a reverify is scheduled while a host is in Pending, it
834 # won't run until the host is actually free
835 job = self._create_job(hosts=[1,2])
836 queue_entry = job.hostqueueentry_set.get(host__hostname='host1')
837 job.synch_count = 2
838 job.save()
839
840 host2 = self.hosts[1]
841 host2.locked = True
842 host2.save()
843
844 self._run_dispatcher() # verify host1
845 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
846 self._run_dispatcher() # host1 Pending
847 self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING)
848 self._create_reverify_request()
849 self._run_dispatcher() # nothing should happen here
850 self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING)
851
852 # now let the job run
853 host2.locked = False
854 host2.save()
855 self._run_dispatcher() # verify host2
856 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
857 self._run_dispatcher() # run job
858 self._finish_job(queue_entry)
859 # need to explicitly finish host1's post-job cleanup
860 self.mock_drone_manager.finish_specific_process(
861 'hosts/host1/4-cleanup', monitor_db._AUTOSERV_PID_FILE)
862 self._run_dispatcher()
863 # the reverify should now be running
864 self._check_statuses(queue_entry, HqeStatus.COMPLETED,
865 HostStatus.VERIFYING)
866 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
867 self._run_dispatcher()
868 self._check_host_status(queue_entry.host, HostStatus.READY)
869
870
showard418785b2009-11-23 20:19:59 +0000871 def test_throttling(self):
872 job = self._create_job(hosts=[1,2,3])
873 job.synch_count = 3
874 job.save()
875
876 queue_entries = list(job.hostqueueentry_set.all())
877 def _check_hqe_statuses(*statuses):
878 for queue_entry, status in zip(queue_entries, statuses):
879 self._check_statuses(queue_entry, status)
880
881 self.mock_drone_manager.process_capacity = 2
882 self._run_dispatcher() # verify runs on 1 and 2
883 _check_hqe_statuses(HqeStatus.VERIFYING, HqeStatus.VERIFYING,
884 HqeStatus.VERIFYING)
885 self.assertEquals(len(self.mock_drone_manager.running_pidfile_ids()), 2)
886
887 self.mock_drone_manager.finish_specific_process(
888 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE)
889 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
890 self._run_dispatcher() # verify runs on 3
891 _check_hqe_statuses(HqeStatus.PENDING, HqeStatus.PENDING,
892 HqeStatus.VERIFYING)
893
894 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
895 self._run_dispatcher() # job won't run due to throttling
896 _check_hqe_statuses(HqeStatus.STARTING, HqeStatus.STARTING,
897 HqeStatus.STARTING)
898 self._assert_nothing_is_running()
899
900 self.mock_drone_manager.process_capacity = 3
901 self._run_dispatcher() # now job runs
902 _check_hqe_statuses(HqeStatus.RUNNING, HqeStatus.RUNNING,
903 HqeStatus.RUNNING)
904
905 self.mock_drone_manager.process_capacity = 2
906 self.mock_drone_manager.finish_process(_PidfileType.JOB,
907 exit_status=271)
908 self._run_dispatcher() # gathering won't run due to throttling
909 _check_hqe_statuses(HqeStatus.GATHERING, HqeStatus.GATHERING,
910 HqeStatus.GATHERING)
911 self._assert_nothing_is_running()
912
913 self.mock_drone_manager.process_capacity = 3
914 self._run_dispatcher() # now gathering runs
915
916 self.mock_drone_manager.process_capacity = 0
917 self.mock_drone_manager.finish_process(_PidfileType.GATHER)
918 self._run_dispatcher() # parsing runs despite throttling
919 _check_hqe_statuses(HqeStatus.PARSING, HqeStatus.PARSING,
920 HqeStatus.PARSING)
921
922
showardd1195652009-12-08 22:21:02 +0000923 def test_simple_atomic_group_job(self):
924 job = self._create_job(atomic_group=1)
925 self._run_dispatcher() # expand + verify
926 queue_entries = job.hostqueueentry_set.all()
927 self.assertEquals(len(queue_entries), 2)
928 self.assertEquals(queue_entries[0].host.hostname, 'host5')
929 self.assertEquals(queue_entries[1].host.hostname, 'host6')
930
931 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
932 self._run_dispatcher() # delay task started waiting
933
934 self.mock_drone_manager.finish_specific_process(
935 'hosts/host5/1-verify', monitor_db._AUTOSERV_PID_FILE)
936 self._run_dispatcher() # job starts now
937 for entry in queue_entries:
938 self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING)
939
940 # rest of job proceeds normally
941
942
showard2ca64c92009-12-10 21:41:02 +0000943 def test_simple_metahost_assignment(self):
944 job = self._create_job(metahosts=[1])
945 self._run_dispatcher()
946 entry = job.hostqueueentry_set.all()[0]
947 self.assertEquals(entry.host.hostname, 'host1')
948 self._check_statuses(entry, HqeStatus.VERIFYING, HostStatus.VERIFYING)
949 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
950 self._run_dispatcher()
951 self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING)
952 # rest of job proceeds normally
953
954
955 def test_metahost_fail_verify(self):
956 self.hosts[1].labels.add(self.labels[0]) # put label1 also on host2
957 job = self._create_job(metahosts=[1])
958 self._run_dispatcher() # assigned to host1
959 self.mock_drone_manager.finish_process(_PidfileType.VERIFY,
960 exit_status=256)
961 self._run_dispatcher() # host1 failed, gets reassigned to host2
962 entry = job.hostqueueentry_set.all()[0]
963 self.assertEquals(entry.host.hostname, 'host2')
964 self._check_statuses(entry, HqeStatus.VERIFYING, HostStatus.VERIFYING)
965 self._check_host_status(self.hosts[0], HostStatus.REPAIRING)
966
967 self.mock_drone_manager.finish_process(_PidfileType.VERIFY)
968 self._run_dispatcher()
969 self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING)
970
971
showarda9545c02009-12-18 22:44:26 +0000972 def test_hostless_job(self):
973 job = self._create_job(hostless=True)
974 entry = job.hostqueueentry_set.all()[0]
975
976 self._run_dispatcher()
977 self._check_entry_status(entry, HqeStatus.RUNNING)
978
979 self.mock_drone_manager.finish_process(_PidfileType.JOB)
980 self._run_dispatcher()
981 self._check_entry_status(entry, HqeStatus.COMPLETED)
982
983
showard34ab0992009-10-05 22:47:57 +0000984if __name__ == '__main__':
985 unittest.main()