showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 3 | import logging, os, unittest |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 4 | import common |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import enum, global_config, host_protections |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 6 | from autotest_lib.database import database_connection |
| 7 | from autotest_lib.frontend import setup_django_environment |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 8 | from autotest_lib.frontend.afe import frontend_test_utils, models |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 9 | from 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")'), |
| 15 | # older SQLite doesn't support group_concat, so just don't bother until |
| 16 | # it arises in an important query |
| 17 | _re_translator(r'GROUP_CONCAT\((.*?)\)', r'\1'), |
| 18 | ) |
| 19 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 20 | HqeStatus = models.HostQueueEntry.Status |
| 21 | HostStatus = models.Host.Status |
| 22 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 23 | class NullMethodObject(object): |
| 24 | _NULL_METHODS = () |
| 25 | |
| 26 | def __init__(self): |
| 27 | def null_method(*args, **kwargs): |
| 28 | pass |
| 29 | |
| 30 | for method_name in self._NULL_METHODS: |
| 31 | setattr(self, method_name, null_method) |
| 32 | |
| 33 | class MockGlobalConfig(object): |
| 34 | def __init__(self): |
| 35 | self._config_info = {} |
| 36 | |
| 37 | |
| 38 | def set_config_value(self, section, key, value): |
| 39 | self._config_info[(section, key)] = value |
| 40 | |
| 41 | |
| 42 | def get_config_value(self, section, key, type=str, |
| 43 | default=None, allow_blank=False): |
| 44 | identifier = (section, key) |
| 45 | if identifier not in self._config_info: |
| 46 | raise RuntimeError('Unset global config value: %s' % (identifier,)) |
| 47 | return self._config_info[identifier] |
| 48 | |
| 49 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 50 | # the SpecialTask names here must match the suffixes used on the SpecialTask |
| 51 | # results directories |
| 52 | _PidfileType = enum.Enum('verify', 'cleanup', 'repair', 'job', 'gather', |
| 53 | 'parse') |
| 54 | |
| 55 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 56 | class MockDroneManager(NullMethodObject): |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame^] | 57 | """ |
| 58 | Public attributes: |
| 59 | max_runnable_processes_value: value returned by max_runnable_processes(). |
| 60 | tests can change this to activate throttling. |
| 61 | """ |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 62 | _NULL_METHODS = ('reinitialize_drones', 'copy_to_results_repository', |
| 63 | 'copy_results_on_drone') |
| 64 | |
| 65 | class _DummyPidfileId(object): |
| 66 | """ |
| 67 | Object to represent pidfile IDs that is opaque to the scheduler code but |
| 68 | still debugging-friendly for us. |
| 69 | """ |
| 70 | def __init__(self, debug_string): |
| 71 | self._debug_string = debug_string |
| 72 | |
| 73 | |
| 74 | def __str__(self): |
| 75 | return self._debug_string |
| 76 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 77 | |
| 78 | def __init__(self): |
| 79 | super(MockDroneManager, self).__init__() |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame^] | 80 | self.max_runnable_processes_value = 100 |
| 81 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 82 | # maps result_dir to set of tuples (file_path, file_contents) |
| 83 | self._attached_files = {} |
| 84 | # maps pidfile IDs to PidfileContents |
| 85 | self._pidfiles = {} |
| 86 | # pidfile IDs that haven't been created yet |
| 87 | self._future_pidfiles = [] |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 88 | # maps _PidfileType to the most recently created pidfile ID of that type |
| 89 | self._last_pidfile_id = {} |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 90 | # maps (working_directory, pidfile_name) to pidfile IDs |
| 91 | self._pidfile_index = {} |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 92 | # maps process to pidfile IDs |
| 93 | self._process_index = {} |
| 94 | # tracks pidfiles of processes that have been killed |
| 95 | self._killed_pidfiles = set() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 96 | # pidfile IDs that have just been unregistered (so will disappear on the |
| 97 | # next cycle) |
| 98 | self._unregistered_pidfiles = set() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | # utility APIs for use by the test |
| 102 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 103 | def finish_process(self, pidfile_type, exit_status=0): |
| 104 | pidfile_id = self._last_pidfile_id[pidfile_type] |
| 105 | self._set_pidfile_exit_status(pidfile_id, exit_status) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 106 | |
| 107 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame^] | 108 | def finish_specific_process(self, working_directory, pidfile_name): |
| 109 | pidfile_id = self._pidfile_index[(working_directory, pidfile_name)] |
| 110 | self._set_pidfile_exit_status(pidfile_id, 0) |
| 111 | |
| 112 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 113 | def _set_pidfile_exit_status(self, pidfile_id, exit_status): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 114 | assert pidfile_id is not None |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 115 | contents = self._pidfiles[pidfile_id] |
| 116 | contents.exit_status = exit_status |
| 117 | contents.num_tests_failed = 0 |
| 118 | |
| 119 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 120 | def was_last_process_killed(self, pidfile_type): |
| 121 | pidfile_id = self._last_pidfile_id[pidfile_type] |
| 122 | return pidfile_id in self._killed_pidfiles |
| 123 | |
| 124 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 125 | def running_pidfile_ids(self): |
| 126 | return [str(pidfile_id) for pidfile_id, pidfile_contents |
| 127 | in self._pidfiles.iteritems() |
| 128 | if pidfile_contents.process is not None |
| 129 | and pidfile_contents.exit_status is None] |
| 130 | |
| 131 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 132 | # DroneManager emulation APIs for use by monitor_db |
| 133 | |
| 134 | def get_orphaned_autoserv_processes(self): |
| 135 | return set() |
| 136 | |
| 137 | |
| 138 | def total_running_processes(self): |
| 139 | return 0 |
| 140 | |
| 141 | |
| 142 | def max_runnable_processes(self): |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame^] | 143 | return self.max_runnable_processes_value |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 144 | |
| 145 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 146 | def refresh(self): |
| 147 | for pidfile_id in self._unregistered_pidfiles: |
| 148 | # intentionally handle non-registered pidfiles silently |
| 149 | self._pidfiles.pop(pidfile_id, None) |
| 150 | self._unregistered_pidfiles = set() |
| 151 | |
| 152 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 153 | def execute_actions(self): |
| 154 | # executing an "execute_command" causes a pidfile to be created |
| 155 | for pidfile_id in self._future_pidfiles: |
| 156 | # Process objects are opaque to monitor_db |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 157 | process = object() |
| 158 | self._pidfiles[pidfile_id].process = process |
| 159 | self._process_index[process] = pidfile_id |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 160 | self._future_pidfiles = [] |
| 161 | |
| 162 | |
| 163 | def attach_file_to_execution(self, result_dir, file_contents, |
| 164 | file_path=None): |
| 165 | self._attached_files.setdefault(result_dir, set()).add((file_path, |
| 166 | file_contents)) |
| 167 | return 'attach_path' |
| 168 | |
| 169 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 170 | def _initialize_pidfile(self, pidfile_id): |
| 171 | if pidfile_id not in self._pidfiles: |
| 172 | self._pidfiles[pidfile_id] = drone_manager.PidfileContents() |
| 173 | |
| 174 | |
| 175 | _pidfile_type_map = { |
| 176 | monitor_db._AUTOSERV_PID_FILE: _PidfileType.JOB, |
| 177 | monitor_db._CRASHINFO_PID_FILE: _PidfileType.GATHER, |
| 178 | monitor_db._PARSER_PID_FILE: _PidfileType.PARSE, |
| 179 | } |
| 180 | |
| 181 | |
| 182 | def _set_last_pidfile(self, pidfile_id, working_directory, pidfile_name): |
| 183 | if working_directory.startswith('hosts/'): |
| 184 | # such paths look like hosts/host1/1-verify, we'll grab the end |
| 185 | type_string = working_directory.rsplit('-', 1)[1] |
| 186 | pidfile_type = _PidfileType.get_value(type_string) |
| 187 | else: |
| 188 | pidfile_type = self._pidfile_type_map[pidfile_name] |
| 189 | self._last_pidfile_id[pidfile_type] = pidfile_id |
| 190 | |
| 191 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 192 | def execute_command(self, command, working_directory, pidfile_name, |
| 193 | log_file=None, paired_with_pidfile=None): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 194 | pidfile_id = self._DummyPidfileId( |
| 195 | self._get_pidfile_debug_string(working_directory, pidfile_name)) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 196 | self._future_pidfiles.append(pidfile_id) |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 197 | self._initialize_pidfile(pidfile_id) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 198 | self._pidfile_index[(working_directory, pidfile_name)] = pidfile_id |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 199 | self._set_last_pidfile(pidfile_id, working_directory, pidfile_name) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 200 | return pidfile_id |
| 201 | |
| 202 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 203 | def _get_pidfile_debug_string(self, working_directory, pidfile_name): |
| 204 | return os.path.join(working_directory, pidfile_name) |
| 205 | |
| 206 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 207 | def get_pidfile_contents(self, pidfile_id, use_second_read=False): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 208 | if pidfile_id not in self._pidfiles: |
| 209 | print 'Request for nonexistent pidfile %s' % pidfile_id |
| 210 | return self._pidfiles.get(pidfile_id, drone_manager.PidfileContents()) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def is_process_running(self, process): |
| 214 | return True |
| 215 | |
| 216 | |
| 217 | def register_pidfile(self, pidfile_id): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 218 | self._initialize_pidfile(pidfile_id) |
| 219 | |
| 220 | |
| 221 | def unregister_pidfile(self, pidfile_id): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 222 | self._unregistered_pidfiles.add(pidfile_id) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def absolute_path(self, path): |
| 226 | return 'absolute/' + path |
| 227 | |
| 228 | |
| 229 | def write_lines_to_file(self, file_path, lines, paired_with_process=None): |
| 230 | # TODO: record this |
| 231 | pass |
| 232 | |
| 233 | |
| 234 | def get_pidfile_id_from(self, execution_tag, pidfile_name): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 235 | debug_string = ('Nonexistent pidfile: ' |
| 236 | + self._get_pidfile_debug_string(execution_tag, |
| 237 | pidfile_name)) |
| 238 | return self._pidfile_index.get((execution_tag, pidfile_name), |
| 239 | self._DummyPidfileId(debug_string)) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 240 | |
| 241 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 242 | def kill_process(self, process): |
| 243 | pidfile_id = self._process_index[process] |
| 244 | self._killed_pidfiles.add(pidfile_id) |
| 245 | self._set_pidfile_exit_status(pidfile_id, 271) |
| 246 | |
| 247 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 248 | class MockEmailManager(NullMethodObject): |
| 249 | _NULL_METHODS = ('send_queued_emails', 'send_email') |
| 250 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 251 | def enqueue_notify_email(self, subject, message): |
| 252 | logging.warn('enqueue_notify_email: %s', subject) |
| 253 | logging.warn(message) |
| 254 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 255 | |
| 256 | class SchedulerFunctionalTest(unittest.TestCase, |
| 257 | frontend_test_utils.FrontendTestMixin): |
| 258 | # some number of ticks after which the scheduler is presumed to have |
| 259 | # stabilized, given no external changes |
| 260 | _A_LOT_OF_TICKS = 10 |
| 261 | |
| 262 | def setUp(self): |
| 263 | self._frontend_common_setup() |
| 264 | self._set_stubs() |
| 265 | self._set_global_config_values() |
| 266 | self.dispatcher = monitor_db.Dispatcher() |
| 267 | |
| 268 | logging.basicConfig(level=logging.DEBUG) |
| 269 | |
| 270 | |
| 271 | def tearDown(self): |
| 272 | self._frontend_common_teardown() |
| 273 | |
| 274 | |
| 275 | def _set_stubs(self): |
| 276 | self.mock_config = MockGlobalConfig() |
| 277 | self.god.stub_with(global_config, 'global_config', self.mock_config) |
| 278 | |
| 279 | self.mock_drone_manager = MockDroneManager() |
| 280 | self.god.stub_with(monitor_db, '_drone_manager', |
| 281 | self.mock_drone_manager) |
| 282 | |
| 283 | self.mock_email_manager = MockEmailManager() |
| 284 | self.god.stub_with(email_manager, 'manager', self.mock_email_manager) |
| 285 | |
| 286 | self._database = ( |
| 287 | database_connection.TranslatingDatabase.get_test_database( |
| 288 | file_path=self._test_db_file, |
| 289 | translators=_DB_TRANSLATORS)) |
| 290 | self._database.connect(db_type='django') |
| 291 | self.god.stub_with(monitor_db, '_db', self._database) |
| 292 | |
| 293 | |
| 294 | def _set_global_config_values(self): |
| 295 | self.mock_config.set_config_value('SCHEDULER', 'pidfile_timeout_mins', |
| 296 | 1) |
| 297 | |
| 298 | |
| 299 | def _initialize_test(self): |
| 300 | self.dispatcher.initialize() |
| 301 | |
| 302 | |
| 303 | def _run_dispatcher(self): |
| 304 | for _ in xrange(self._A_LOT_OF_TICKS): |
| 305 | self.dispatcher.tick() |
| 306 | |
| 307 | |
| 308 | def test_idle(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 309 | self._initialize_test() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 310 | self._run_dispatcher() |
| 311 | |
| 312 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 313 | def _assert_process_executed(self, working_directory, pidfile_name): |
| 314 | process_was_executed = self.mock_drone_manager.was_process_executed( |
| 315 | 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE) |
| 316 | self.assert_(process_was_executed, |
| 317 | '%s/%s not executed' % (working_directory, pidfile_name)) |
| 318 | |
| 319 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 320 | def _check_statuses(self, queue_entry, queue_entry_status, host_status): |
| 321 | # update from DB |
| 322 | queue_entry = models.HostQueueEntry.objects.get(id=queue_entry.id) |
| 323 | self.assertEquals(queue_entry.status, queue_entry_status) |
| 324 | self.assertEquals(queue_entry.host.status, host_status) |
| 325 | |
| 326 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 327 | def _check_host_status(self, host, status): |
| 328 | # update from DB |
| 329 | host = models.Host.objects.get(id=host.id) |
| 330 | self.assertEquals(host.status, status) |
| 331 | |
| 332 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 333 | def _run_pre_job_verify(self, queue_entry): |
| 334 | self._run_dispatcher() # launches verify |
| 335 | self._check_statuses(queue_entry, HqeStatus.VERIFYING, |
| 336 | HostStatus.VERIFYING) |
| 337 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 338 | |
| 339 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 340 | def test_simple_job(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 341 | self._initialize_test() |
| 342 | job, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 343 | self._run_pre_job_verify(queue_entry) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 344 | self._run_dispatcher() # launches job |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 345 | self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 346 | self._finish_job(queue_entry) |
| 347 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY) |
| 348 | self._assert_nothing_is_running() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 349 | |
| 350 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 351 | def _setup_for_pre_job_cleanup(self): |
| 352 | self._initialize_test() |
| 353 | job, queue_entry = self._make_job_and_queue_entry() |
| 354 | job.reboot_before = models.RebootBefore.ALWAYS |
| 355 | job.save() |
| 356 | return queue_entry |
| 357 | |
| 358 | |
| 359 | def _run_pre_job_cleanup_job(self, queue_entry): |
| 360 | self._run_dispatcher() # cleanup |
| 361 | self._check_statuses(queue_entry, HqeStatus.VERIFYING, |
| 362 | HostStatus.CLEANING) |
| 363 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 364 | self._run_dispatcher() # verify |
| 365 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 366 | self._run_dispatcher() # job |
| 367 | self._finish_job(queue_entry) |
| 368 | |
| 369 | |
| 370 | def test_pre_job_cleanup(self): |
| 371 | queue_entry = self._setup_for_pre_job_cleanup() |
| 372 | self._run_pre_job_cleanup_job(queue_entry) |
| 373 | |
| 374 | |
| 375 | def _run_pre_job_cleanup_one_failure(self): |
| 376 | queue_entry = self._setup_for_pre_job_cleanup() |
| 377 | self._run_dispatcher() # cleanup |
| 378 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 379 | exit_status=256) |
| 380 | self._run_dispatcher() # repair |
| 381 | self._check_statuses(queue_entry, HqeStatus.QUEUED, |
| 382 | HostStatus.REPAIRING) |
| 383 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 384 | return queue_entry |
| 385 | |
| 386 | |
| 387 | def test_pre_job_cleanup_failure(self): |
| 388 | queue_entry = self._run_pre_job_cleanup_one_failure() |
| 389 | # from here the job should run as normal |
| 390 | self._run_pre_job_cleanup_job(queue_entry) |
| 391 | |
| 392 | |
| 393 | def test_pre_job_cleanup_double_failure(self): |
| 394 | # TODO (showard): this test isn't perfect. in reality, when the second |
| 395 | # cleanup fails, it copies its results over to the job directory using |
| 396 | # copy_results_on_drone() and then parses them. since we don't handle |
| 397 | # that, there appear to be no results at the job directory. the |
| 398 | # scheduler handles this gracefully, parsing gets effectively skipped, |
| 399 | # and this test passes as is. but we ought to properly test that |
| 400 | # behavior. |
| 401 | queue_entry = self._run_pre_job_cleanup_one_failure() |
| 402 | self._run_dispatcher() # second cleanup |
| 403 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 404 | exit_status=256) |
| 405 | self._run_dispatcher() |
| 406 | self._check_statuses(queue_entry, HqeStatus.FAILED, |
| 407 | HostStatus.REPAIR_FAILED) |
| 408 | # nothing else should run |
| 409 | self._assert_nothing_is_running() |
| 410 | |
| 411 | |
| 412 | def _assert_nothing_is_running(self): |
| 413 | self.assertEquals(self.mock_drone_manager.running_pidfile_ids(), []) |
| 414 | |
| 415 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 416 | def _setup_for_post_job_cleanup(self): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 417 | self._initialize_test() |
| 418 | job, queue_entry = self._make_job_and_queue_entry() |
| 419 | job.reboot_after = models.RebootAfter.ALWAYS |
| 420 | job.save() |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 421 | return queue_entry |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 422 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 423 | |
| 424 | def _run_post_job_cleanup_failure_up_to_repair(self, queue_entry, |
| 425 | include_verify=True): |
| 426 | if include_verify: |
| 427 | self._run_pre_job_verify(queue_entry) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 428 | self._run_dispatcher() # job |
| 429 | self.mock_drone_manager.finish_process(_PidfileType.JOB) |
| 430 | self._run_dispatcher() # parsing + cleanup |
| 431 | self.mock_drone_manager.finish_process(_PidfileType.PARSE) |
| 432 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 433 | exit_status=256) |
| 434 | self._run_dispatcher() # repair, HQE unaffected |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 435 | return queue_entry |
| 436 | |
| 437 | |
| 438 | def test_post_job_cleanup_failure(self): |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 439 | queue_entry = self._setup_for_post_job_cleanup() |
| 440 | self._run_post_job_cleanup_failure_up_to_repair(queue_entry) |
| 441 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 442 | HostStatus.REPAIRING) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 443 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 444 | self._run_dispatcher() |
| 445 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY) |
| 446 | |
| 447 | |
| 448 | def test_post_job_cleanup_failure_repair_failure(self): |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 449 | queue_entry = self._setup_for_post_job_cleanup() |
| 450 | self._run_post_job_cleanup_failure_up_to_repair(queue_entry) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 451 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR, |
| 452 | exit_status=256) |
| 453 | self._run_dispatcher() |
| 454 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 455 | HostStatus.REPAIR_FAILED) |
| 456 | |
| 457 | |
| 458 | def _finish_job(self, queue_entry): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 459 | self.mock_drone_manager.finish_process(_PidfileType.JOB) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 460 | self._run_dispatcher() # launches parsing + cleanup |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 461 | self._check_statuses(queue_entry, HqeStatus.PARSING, |
| 462 | HostStatus.CLEANING) |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 463 | self._finish_parsing_and_cleanup() |
| 464 | |
| 465 | |
| 466 | def _finish_parsing_and_cleanup(self): |
| 467 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 468 | self.mock_drone_manager.finish_process(_PidfileType.PARSE) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 469 | self._run_dispatcher() |
| 470 | |
| 471 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 472 | def _create_reverify_request(self): |
| 473 | host = self.hosts[0] |
| 474 | models.SpecialTask.objects.create(host=host, |
| 475 | task=models.SpecialTask.Task.VERIFY) |
| 476 | return host |
| 477 | |
| 478 | |
| 479 | def test_requested_reverify(self): |
| 480 | host = self._create_reverify_request() |
| 481 | self._run_dispatcher() |
| 482 | self._check_host_status(host, HostStatus.VERIFYING) |
| 483 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 484 | self._run_dispatcher() |
| 485 | self._check_host_status(host, HostStatus.READY) |
| 486 | |
| 487 | |
| 488 | def test_requested_reverify_failure(self): |
| 489 | host = self._create_reverify_request() |
| 490 | self._run_dispatcher() |
| 491 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 492 | exit_status=256) |
| 493 | self._run_dispatcher() # repair |
| 494 | self._check_host_status(host, HostStatus.REPAIRING) |
| 495 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 496 | self._run_dispatcher() |
| 497 | self._check_host_status(host, HostStatus.READY) |
| 498 | |
| 499 | |
| 500 | def _setup_for_do_not_verify(self): |
| 501 | self._initialize_test() |
| 502 | job, queue_entry = self._make_job_and_queue_entry() |
| 503 | queue_entry.host.protection = host_protections.Protection.DO_NOT_VERIFY |
| 504 | queue_entry.host.save() |
| 505 | return queue_entry |
| 506 | |
| 507 | |
| 508 | def test_do_not_verify_job(self): |
| 509 | queue_entry = self._setup_for_do_not_verify() |
| 510 | self._run_dispatcher() # runs job directly |
| 511 | self._finish_job(queue_entry) |
| 512 | |
| 513 | |
| 514 | def test_do_not_verify_job_with_cleanup(self): |
| 515 | queue_entry = self._setup_for_do_not_verify() |
| 516 | queue_entry.job.reboot_before = models.RebootBefore.ALWAYS |
| 517 | queue_entry.job.save() |
| 518 | |
| 519 | self._run_dispatcher() # cleanup |
| 520 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 521 | self._run_dispatcher() # job |
| 522 | self._finish_job(queue_entry) |
| 523 | |
| 524 | |
| 525 | def test_do_not_verify_pre_job_cleanup_failure(self): |
| 526 | queue_entry = self._setup_for_do_not_verify() |
| 527 | queue_entry.job.reboot_before = models.RebootBefore.ALWAYS |
| 528 | queue_entry.job.save() |
| 529 | |
| 530 | self._run_dispatcher() # cleanup |
| 531 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 532 | exit_status=256) |
| 533 | self._run_dispatcher() # failure ignored; job runs |
| 534 | self._finish_job(queue_entry) |
| 535 | |
| 536 | |
| 537 | def test_do_not_verify_post_job_cleanup_failure(self): |
| 538 | queue_entry = self._setup_for_do_not_verify() |
| 539 | |
| 540 | self._run_post_job_cleanup_failure_up_to_repair(queue_entry, |
| 541 | include_verify=False) |
| 542 | # failure ignored, host still set to Ready |
| 543 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY) |
| 544 | self._run_dispatcher() # nothing else runs |
| 545 | self._assert_nothing_is_running() |
| 546 | |
| 547 | |
| 548 | def test_do_not_verify_requested_reverify_failure(self): |
| 549 | host = self._create_reverify_request() |
| 550 | host.protection = host_protections.Protection.DO_NOT_VERIFY |
| 551 | host.save() |
| 552 | |
| 553 | self._run_dispatcher() |
| 554 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 555 | exit_status=256) |
| 556 | self._run_dispatcher() |
| 557 | self._check_host_status(host, HostStatus.READY) # ignore failure |
| 558 | self._assert_nothing_is_running() |
| 559 | |
| 560 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 561 | def test_job_abort_in_verify(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 562 | self._initialize_test() |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 563 | job = self._create_job(hosts=[1]) |
| 564 | self._run_dispatcher() # launches verify |
| 565 | job.hostqueueentry_set.update(aborted=True) |
| 566 | self._run_dispatcher() # kills verify, launches cleanup |
| 567 | self.assert_(self.mock_drone_manager.was_last_process_killed( |
| 568 | _PidfileType.VERIFY)) |
| 569 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 570 | self._run_dispatcher() |
| 571 | |
| 572 | |
| 573 | def test_job_abort(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 574 | self._initialize_test() |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 575 | job = self._create_job(hosts=[1]) |
| 576 | job.run_verify = False |
| 577 | job.save() |
| 578 | |
| 579 | self._run_dispatcher() # launches job |
| 580 | job.hostqueueentry_set.update(aborted=True) |
| 581 | self._run_dispatcher() # kills job, launches gathering |
| 582 | self.assert_(self.mock_drone_manager.was_last_process_killed( |
| 583 | _PidfileType.JOB)) |
| 584 | self.mock_drone_manager.finish_process(_PidfileType.GATHER) |
| 585 | self._run_dispatcher() # launches parsing + cleanup |
| 586 | self._finish_parsing_and_cleanup() |
| 587 | |
| 588 | |
| 589 | def test_no_pidfile_leaking(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 590 | self._initialize_test() |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 591 | self.test_simple_job() |
| 592 | self.assertEquals(self.mock_drone_manager._pidfiles, {}) |
| 593 | |
| 594 | self.test_job_abort_in_verify() |
| 595 | self.assertEquals(self.mock_drone_manager._pidfiles, {}) |
| 596 | |
| 597 | self.test_job_abort() |
| 598 | self.assertEquals(self.mock_drone_manager._pidfiles, {}) |
| 599 | |
| 600 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 601 | def _make_job_and_queue_entry(self): |
| 602 | job = self._create_job(hosts=[1]) |
| 603 | queue_entry = job.hostqueueentry_set.all()[0] |
| 604 | return job, queue_entry |
| 605 | |
| 606 | |
| 607 | def test_recover_running_no_process(self): |
| 608 | # recovery should re-execute a Running HQE if no process is found |
| 609 | _, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 610 | queue_entry.status = HqeStatus.RUNNING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 611 | queue_entry.execution_subdir = '1-myuser/host1' |
| 612 | queue_entry.save() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 613 | queue_entry.host.status = HostStatus.RUNNING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 614 | queue_entry.host.save() |
| 615 | |
| 616 | self._initialize_test() |
| 617 | self._run_dispatcher() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 618 | self._finish_job(queue_entry) |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 619 | |
| 620 | |
| 621 | def test_recover_verifying_hqe_no_special_task(self): |
| 622 | # recovery should fail on a Verifing HQE with no corresponding |
| 623 | # Verify or Cleanup SpecialTask |
| 624 | _, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 625 | queue_entry.status = HqeStatus.VERIFYING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 626 | queue_entry.save() |
| 627 | |
| 628 | # make some dummy SpecialTasks that shouldn't count |
| 629 | models.SpecialTask.objects.create(host=queue_entry.host, |
| 630 | task=models.SpecialTask.Task.VERIFY) |
| 631 | models.SpecialTask.objects.create(host=queue_entry.host, |
| 632 | task=models.SpecialTask.Task.CLEANUP, |
| 633 | queue_entry=queue_entry, |
| 634 | is_complete=True) |
| 635 | |
| 636 | self.assertRaises(monitor_db.SchedulerError, self._initialize_test) |
| 637 | |
| 638 | |
| 639 | def _test_recover_verifying_hqe_helper(self, task, pidfile_type): |
| 640 | _, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 641 | queue_entry.status = HqeStatus.VERIFYING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 642 | queue_entry.save() |
| 643 | |
| 644 | special_task = models.SpecialTask.objects.create( |
| 645 | host=queue_entry.host, task=task, queue_entry=queue_entry) |
| 646 | |
| 647 | self._initialize_test() |
| 648 | self._run_dispatcher() |
| 649 | self.mock_drone_manager.finish_process(pidfile_type) |
| 650 | self._run_dispatcher() |
| 651 | # don't bother checking the rest of the job execution, as long as the |
| 652 | # SpecialTask ran |
| 653 | |
| 654 | |
| 655 | def test_recover_verifying_hqe_with_cleanup(self): |
| 656 | # recover an HQE that was in pre-job cleanup |
| 657 | self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.CLEANUP, |
| 658 | _PidfileType.CLEANUP) |
| 659 | |
| 660 | |
| 661 | def test_recover_verifying_hqe_with_verify(self): |
| 662 | # recover an HQE that was in pre-job verify |
| 663 | self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.VERIFY, |
| 664 | _PidfileType.VERIFY) |
| 665 | |
| 666 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame^] | 667 | def test_job_scheduled_just_after_abort(self): |
| 668 | # test a pretty obscure corner case where a job is aborted while queued, |
| 669 | # another job is ready to run, and throttling is active. the post-abort |
| 670 | # cleanup must not be pre-empted by the second job. |
| 671 | job1, queue_entry1 = self._make_job_and_queue_entry() |
| 672 | job2, queue_entry2 = self._make_job_and_queue_entry() |
| 673 | |
| 674 | self.mock_drone_manager.max_runnable_processes_value = 0 |
| 675 | self._run_dispatcher() # schedule job1, but won't start verify |
| 676 | job1.hostqueueentry_set.update(aborted=True) |
| 677 | self.mock_drone_manager.max_runnable_processes_value = 100 |
| 678 | self._run_dispatcher() # cleanup must run here, not verify for job2 |
| 679 | self._check_statuses(queue_entry1, HqeStatus.ABORTED, |
| 680 | HostStatus.CLEANING) |
| 681 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 682 | self._run_dispatcher() # now verify starts for job2 |
| 683 | self._check_statuses(queue_entry2, HqeStatus.VERIFYING, |
| 684 | HostStatus.VERIFYING) |
| 685 | |
| 686 | |
| 687 | def _test_job_scheduled_just_after_abort_2(self): |
| 688 | # test a pretty obscure corner case where a job is aborted while queued, |
| 689 | # another job is ready to run, and throttling is active. the post-abort |
| 690 | # cleanup must not be pre-empted by the second job. |
| 691 | job1, _ = self._make_job_and_queue_entry() |
| 692 | job2 = self._create_job(hosts=[1,2]) |
| 693 | job2.synch_count = 2 |
| 694 | job2.save() |
| 695 | |
| 696 | self.mock_drone_manager.max_runnable_processes_value = 0 |
| 697 | self._run_dispatcher() # schedule job1, but won't start verify |
| 698 | job1.hostqueueentry_set.update(aborted=True) |
| 699 | self.mock_drone_manager.max_runnable_processes_value = 100 |
| 700 | self._run_dispatcher() |
| 701 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 702 | self._run_dispatcher() |
| 703 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 704 | self._run_dispatcher() |
| 705 | self.mock_drone_manager.finish_specific_process( |
| 706 | 'hosts/host2/2-verify', monitor_db._AUTOSERV_PID_FILE) |
| 707 | self._run_dispatcher() |
| 708 | |
| 709 | |
| 710 | def test_reverify_interrupting_pre_job(self): |
| 711 | # ensure things behave sanely if a reverify is scheduled in the middle |
| 712 | # of pre-job actions |
| 713 | _, queue_entry = self._make_job_and_queue_entry() |
| 714 | |
| 715 | self._run_dispatcher() # pre-job verify |
| 716 | self._create_reverify_request() |
| 717 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 718 | exit_status=256) |
| 719 | self._run_dispatcher() # repair |
| 720 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 721 | self._run_dispatcher() # reverify runs now |
| 722 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 723 | self._run_dispatcher() # pre-job verify |
| 724 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 725 | self._run_dispatcher() # and job runs... |
| 726 | self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 727 | self._finish_job(queue_entry) # reverify has been deleted |
| 728 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 729 | HostStatus.READY) |
| 730 | self._assert_nothing_is_running() |
| 731 | |
| 732 | |
| 733 | def test_reverify_while_job_running(self): |
| 734 | # once a job is running, a reverify must not be allowed to preempt |
| 735 | # Gathering |
| 736 | _, queue_entry = self._make_job_and_queue_entry() |
| 737 | self._run_pre_job_verify(queue_entry) |
| 738 | self._run_dispatcher() # job runs |
| 739 | self._create_reverify_request() |
| 740 | # make job end with a signal, so gathering will run |
| 741 | self.mock_drone_manager.finish_process(_PidfileType.JOB, |
| 742 | exit_status=271) |
| 743 | self._run_dispatcher() # gathering must start |
| 744 | self.mock_drone_manager.finish_process(_PidfileType.GATHER) |
| 745 | self._run_dispatcher() # parsing and cleanup |
| 746 | self._finish_parsing_and_cleanup() |
| 747 | self._run_dispatcher() # now reverify runs |
| 748 | self._check_statuses(queue_entry, HqeStatus.FAILED, |
| 749 | HostStatus.VERIFYING) |
| 750 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 751 | self._run_dispatcher() |
| 752 | self._check_host_status(queue_entry.host, HostStatus.READY) |
| 753 | |
| 754 | |
| 755 | def test_reverify_while_host_pending(self): |
| 756 | # ensure that if a reverify is scheduled while a host is in Pending, it |
| 757 | # won't run until the host is actually free |
| 758 | job = self._create_job(hosts=[1,2]) |
| 759 | queue_entry = job.hostqueueentry_set.get(host__hostname='host1') |
| 760 | job.synch_count = 2 |
| 761 | job.save() |
| 762 | |
| 763 | host2 = self.hosts[1] |
| 764 | host2.locked = True |
| 765 | host2.save() |
| 766 | |
| 767 | self._run_dispatcher() # verify host1 |
| 768 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 769 | self._run_dispatcher() # host1 Pending |
| 770 | self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING) |
| 771 | self._create_reverify_request() |
| 772 | self._run_dispatcher() # nothing should happen here |
| 773 | self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING) |
| 774 | |
| 775 | # now let the job run |
| 776 | host2.locked = False |
| 777 | host2.save() |
| 778 | self._run_dispatcher() # verify host2 |
| 779 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 780 | self._run_dispatcher() # run job |
| 781 | self._finish_job(queue_entry) |
| 782 | # need to explicitly finish host1's post-job cleanup |
| 783 | self.mock_drone_manager.finish_specific_process( |
| 784 | 'hosts/host1/4-cleanup', monitor_db._AUTOSERV_PID_FILE) |
| 785 | self._run_dispatcher() |
| 786 | # the reverify should now be running |
| 787 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 788 | HostStatus.VERIFYING) |
| 789 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 790 | self._run_dispatcher() |
| 791 | self._check_host_status(queue_entry.host, HostStatus.READY) |
| 792 | |
| 793 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 794 | if __name__ == '__main__': |
| 795 | unittest.main() |