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")'), |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 15 | _re_translator(r'LAST_INSERT_ID\(\)', 'LAST_INSERT_ROWID()'), |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 16 | # 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 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 21 | HqeStatus = models.HostQueueEntry.Status |
| 22 | HostStatus = models.Host.Status |
| 23 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 24 | class 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 | |
| 34 | class 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 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 51 | # the SpecialTask names here must match the suffixes used on the SpecialTask |
| 52 | # results directories |
| 53 | _PidfileType = enum.Enum('verify', 'cleanup', 'repair', 'job', 'gather', |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 54 | 'parse', 'archive') |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 55 | |
| 56 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 57 | _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, |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 61 | monitor_db._ARCHIVER_PID_FILE: _PidfileType.ARCHIVE, |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | _PIDFILE_TYPE_TO_PIDFILE = dict((value, key) for key, value |
| 66 | in _PIDFILE_TO_PIDFILE_TYPE.iteritems()) |
| 67 | |
| 68 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 69 | class MockDroneManager(NullMethodObject): |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 70 | """ |
| 71 | Public attributes: |
| 72 | max_runnable_processes_value: value returned by max_runnable_processes(). |
| 73 | tests can change this to activate throttling. |
| 74 | """ |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 75 | _NULL_METHODS = ('reinitialize_drones', 'copy_to_results_repository', |
| 76 | 'copy_results_on_drone') |
| 77 | |
| 78 | class _DummyPidfileId(object): |
| 79 | """ |
| 80 | Object to represent pidfile IDs that is opaque to the scheduler code but |
| 81 | still debugging-friendly for us. |
| 82 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 83 | def __init__(self, working_directory, pidfile_name, num_processes=None): |
| 84 | self._working_directory = working_directory |
| 85 | self._pidfile_name = pidfile_name |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 86 | self._num_processes = num_processes |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 87 | self._paired_with_pidfile = None |
| 88 | |
| 89 | |
| 90 | def key(self): |
| 91 | """Key for MockDroneManager._pidfile_index""" |
| 92 | return (self._working_directory, self._pidfile_name) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def __str__(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 96 | return os.path.join(self._working_directory, self._pidfile_name) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 97 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 98 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 99 | def __repr__(self): |
| 100 | return '<_DummyPidfileId: %s>' % str(self) |
| 101 | |
| 102 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 103 | def __init__(self): |
| 104 | super(MockDroneManager, self).__init__() |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 105 | self.process_capacity = 100 |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 106 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 107 | # maps result_dir to set of tuples (file_path, file_contents) |
| 108 | self._attached_files = {} |
| 109 | # maps pidfile IDs to PidfileContents |
| 110 | self._pidfiles = {} |
| 111 | # pidfile IDs that haven't been created yet |
| 112 | self._future_pidfiles = [] |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 113 | # maps _PidfileType to the most recently created pidfile ID of that type |
| 114 | self._last_pidfile_id = {} |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 115 | # maps (working_directory, pidfile_name) to pidfile IDs |
| 116 | self._pidfile_index = {} |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 117 | # maps process to pidfile IDs |
| 118 | self._process_index = {} |
| 119 | # tracks pidfiles of processes that have been killed |
| 120 | self._killed_pidfiles = set() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 121 | # pidfile IDs that have just been unregistered (so will disappear on the |
| 122 | # next cycle) |
| 123 | self._unregistered_pidfiles = set() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | # utility APIs for use by the test |
| 127 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 128 | def finish_process(self, pidfile_type, exit_status=0): |
| 129 | pidfile_id = self._last_pidfile_id[pidfile_type] |
| 130 | self._set_pidfile_exit_status(pidfile_id, exit_status) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 131 | |
| 132 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 133 | def finish_specific_process(self, working_directory, pidfile_name): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 134 | pidfile_id = self.pidfile_from_path(working_directory, pidfile_name) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 135 | self._set_pidfile_exit_status(pidfile_id, 0) |
| 136 | |
| 137 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 138 | def _set_pidfile_exit_status(self, pidfile_id, exit_status): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 139 | assert pidfile_id is not None |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 140 | contents = self._pidfiles[pidfile_id] |
| 141 | contents.exit_status = exit_status |
| 142 | contents.num_tests_failed = 0 |
| 143 | |
| 144 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 145 | def was_last_process_killed(self, pidfile_type): |
| 146 | pidfile_id = self._last_pidfile_id[pidfile_type] |
| 147 | return pidfile_id in self._killed_pidfiles |
| 148 | |
| 149 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 150 | def nonfinished_pidfile_ids(self): |
| 151 | return [pidfile_id for pidfile_id, pidfile_contents |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 152 | in self._pidfiles.iteritems() |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 153 | if pidfile_contents.exit_status is None] |
| 154 | |
| 155 | |
| 156 | def running_pidfile_ids(self): |
| 157 | return [pidfile_id for pidfile_id in self.nonfinished_pidfile_ids() |
| 158 | if self._pidfiles[pidfile_id].process is not None] |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 159 | |
| 160 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 161 | def pidfile_from_path(self, working_directory, pidfile_name): |
| 162 | return self._pidfile_index[(working_directory, pidfile_name)] |
| 163 | |
| 164 | |
showard | 493beaa | 2009-12-18 22:44:45 +0000 | [diff] [blame] | 165 | def attached_files(self, working_directory): |
| 166 | """ |
| 167 | Return dict mapping path to contents for attached files with specified |
| 168 | paths. |
| 169 | """ |
| 170 | return dict((path, contents) for path, contents |
| 171 | in self._attached_files.get(working_directory, []) |
| 172 | if path is not None) |
| 173 | |
| 174 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 175 | # DroneManager emulation APIs for use by monitor_db |
| 176 | |
| 177 | def get_orphaned_autoserv_processes(self): |
| 178 | return set() |
| 179 | |
| 180 | |
| 181 | def total_running_processes(self): |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 182 | return sum(pidfile_id._num_processes |
| 183 | for pidfile_id in self.nonfinished_pidfile_ids()) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 184 | |
| 185 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 186 | def max_runnable_processes(self, username): |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 187 | return self.process_capacity - self.total_running_processes() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 188 | |
| 189 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 190 | def refresh(self): |
| 191 | for pidfile_id in self._unregistered_pidfiles: |
| 192 | # intentionally handle non-registered pidfiles silently |
| 193 | self._pidfiles.pop(pidfile_id, None) |
| 194 | self._unregistered_pidfiles = set() |
| 195 | |
| 196 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 197 | def execute_actions(self): |
| 198 | # executing an "execute_command" causes a pidfile to be created |
| 199 | for pidfile_id in self._future_pidfiles: |
| 200 | # Process objects are opaque to monitor_db |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 201 | process = object() |
| 202 | self._pidfiles[pidfile_id].process = process |
| 203 | self._process_index[process] = pidfile_id |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 204 | self._future_pidfiles = [] |
| 205 | |
| 206 | |
| 207 | def attach_file_to_execution(self, result_dir, file_contents, |
| 208 | file_path=None): |
| 209 | self._attached_files.setdefault(result_dir, set()).add((file_path, |
| 210 | file_contents)) |
| 211 | return 'attach_path' |
| 212 | |
| 213 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 214 | def _initialize_pidfile(self, pidfile_id): |
| 215 | if pidfile_id not in self._pidfiles: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 216 | assert pidfile_id.key() not in self._pidfile_index |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 217 | self._pidfiles[pidfile_id] = drone_manager.PidfileContents() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 218 | self._pidfile_index[pidfile_id.key()] = pidfile_id |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | def _set_last_pidfile(self, pidfile_id, working_directory, pidfile_name): |
| 222 | if working_directory.startswith('hosts/'): |
| 223 | # such paths look like hosts/host1/1-verify, we'll grab the end |
| 224 | type_string = working_directory.rsplit('-', 1)[1] |
| 225 | pidfile_type = _PidfileType.get_value(type_string) |
| 226 | else: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 227 | pidfile_type = _PIDFILE_TO_PIDFILE_TYPE[pidfile_name] |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 228 | self._last_pidfile_id[pidfile_type] = pidfile_id |
| 229 | |
| 230 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 231 | def execute_command(self, command, working_directory, pidfile_name, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 232 | num_processes, log_file=None, paired_with_pidfile=None, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 233 | username=None): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 234 | logging.debug('Executing %s in %s', command, working_directory) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 235 | pidfile_id = self._DummyPidfileId(working_directory, pidfile_name) |
| 236 | if pidfile_id.key() in self._pidfile_index: |
| 237 | pidfile_id = self._pidfile_index[pidfile_id.key()] |
| 238 | pidfile_id._num_processes = num_processes |
| 239 | pidfile_id._paired_with_pidfile = paired_with_pidfile |
| 240 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 241 | self._future_pidfiles.append(pidfile_id) |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 242 | self._initialize_pidfile(pidfile_id) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 243 | self._pidfile_index[(working_directory, pidfile_name)] = pidfile_id |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 244 | self._set_last_pidfile(pidfile_id, working_directory, pidfile_name) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 245 | return pidfile_id |
| 246 | |
| 247 | |
| 248 | def get_pidfile_contents(self, pidfile_id, use_second_read=False): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 249 | if pidfile_id not in self._pidfiles: |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 250 | logging.debug('Request for nonexistent pidfile %s' % pidfile_id) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 251 | return self._pidfiles.get(pidfile_id, drone_manager.PidfileContents()) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | def is_process_running(self, process): |
| 255 | return True |
| 256 | |
| 257 | |
| 258 | def register_pidfile(self, pidfile_id): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 259 | self._initialize_pidfile(pidfile_id) |
| 260 | |
| 261 | |
| 262 | def unregister_pidfile(self, pidfile_id): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 263 | self._unregistered_pidfiles.add(pidfile_id) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 264 | |
| 265 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 266 | def declare_process_count(self, pidfile_id, num_processes): |
| 267 | pidfile_id.num_processes = num_processes |
| 268 | |
| 269 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 270 | def absolute_path(self, path): |
| 271 | return 'absolute/' + path |
| 272 | |
| 273 | |
| 274 | def write_lines_to_file(self, file_path, lines, paired_with_process=None): |
| 275 | # TODO: record this |
| 276 | pass |
| 277 | |
| 278 | |
| 279 | def get_pidfile_id_from(self, execution_tag, pidfile_name): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 280 | default_pidfile = self._DummyPidfileId(execution_tag, pidfile_name, |
| 281 | num_processes=0) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 282 | return self._pidfile_index.get((execution_tag, pidfile_name), |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 283 | default_pidfile) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 284 | |
| 285 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 286 | def kill_process(self, process): |
| 287 | pidfile_id = self._process_index[process] |
| 288 | self._killed_pidfiles.add(pidfile_id) |
| 289 | self._set_pidfile_exit_status(pidfile_id, 271) |
| 290 | |
| 291 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 292 | class MockEmailManager(NullMethodObject): |
| 293 | _NULL_METHODS = ('send_queued_emails', 'send_email') |
| 294 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 295 | def enqueue_notify_email(self, subject, message): |
| 296 | logging.warn('enqueue_notify_email: %s', subject) |
| 297 | logging.warn(message) |
| 298 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 299 | |
| 300 | class SchedulerFunctionalTest(unittest.TestCase, |
| 301 | frontend_test_utils.FrontendTestMixin): |
| 302 | # some number of ticks after which the scheduler is presumed to have |
| 303 | # stabilized, given no external changes |
| 304 | _A_LOT_OF_TICKS = 10 |
| 305 | |
| 306 | def setUp(self): |
| 307 | self._frontend_common_setup() |
| 308 | self._set_stubs() |
| 309 | self._set_global_config_values() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 310 | self._create_dispatcher() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 311 | |
| 312 | logging.basicConfig(level=logging.DEBUG) |
| 313 | |
| 314 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 315 | def _create_dispatcher(self): |
| 316 | self.dispatcher = monitor_db.Dispatcher() |
| 317 | |
| 318 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 319 | def tearDown(self): |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame] | 320 | self._database.disconnect() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 321 | self._frontend_common_teardown() |
| 322 | |
| 323 | |
| 324 | def _set_stubs(self): |
| 325 | self.mock_config = MockGlobalConfig() |
| 326 | self.god.stub_with(global_config, 'global_config', self.mock_config) |
| 327 | |
| 328 | self.mock_drone_manager = MockDroneManager() |
| 329 | self.god.stub_with(monitor_db, '_drone_manager', |
| 330 | self.mock_drone_manager) |
| 331 | |
| 332 | self.mock_email_manager = MockEmailManager() |
| 333 | self.god.stub_with(email_manager, 'manager', self.mock_email_manager) |
| 334 | |
| 335 | self._database = ( |
| 336 | database_connection.TranslatingDatabase.get_test_database( |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 337 | translators=_DB_TRANSLATORS)) |
| 338 | self._database.connect(db_type='django') |
| 339 | self.god.stub_with(monitor_db, '_db', self._database) |
| 340 | |
| 341 | |
| 342 | def _set_global_config_values(self): |
| 343 | self.mock_config.set_config_value('SCHEDULER', 'pidfile_timeout_mins', |
| 344 | 1) |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 345 | self.mock_config.set_config_value('SCHEDULER', 'gc_stats_interval_mins', |
| 346 | 999999) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 347 | |
| 348 | |
| 349 | def _initialize_test(self): |
| 350 | self.dispatcher.initialize() |
| 351 | |
| 352 | |
| 353 | def _run_dispatcher(self): |
| 354 | for _ in xrange(self._A_LOT_OF_TICKS): |
| 355 | self.dispatcher.tick() |
| 356 | |
| 357 | |
| 358 | def test_idle(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 359 | self._initialize_test() |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 360 | self._run_dispatcher() |
| 361 | |
| 362 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 363 | def _assert_process_executed(self, working_directory, pidfile_name): |
| 364 | process_was_executed = self.mock_drone_manager.was_process_executed( |
| 365 | 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE) |
| 366 | self.assert_(process_was_executed, |
| 367 | '%s/%s not executed' % (working_directory, pidfile_name)) |
| 368 | |
| 369 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 370 | def _update_instance(self, model_instance): |
| 371 | return type(model_instance).objects.get(pk=model_instance.pk) |
| 372 | |
| 373 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 374 | def _check_statuses(self, queue_entry, queue_entry_status, |
| 375 | host_status=None): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 376 | self._check_entry_status(queue_entry, queue_entry_status) |
| 377 | if host_status: |
| 378 | self._check_host_status(queue_entry.host, host_status) |
| 379 | |
| 380 | |
| 381 | def _check_entry_status(self, queue_entry, status): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 382 | # update from DB |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 383 | queue_entry = self._update_instance(queue_entry) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 384 | self.assertEquals(queue_entry.status, status) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 385 | |
| 386 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 387 | def _check_host_status(self, host, status): |
| 388 | # update from DB |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 389 | host = self._update_instance(host) |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 390 | self.assertEquals(host.status, status) |
| 391 | |
| 392 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 393 | def _run_pre_job_verify(self, queue_entry): |
| 394 | self._run_dispatcher() # launches verify |
| 395 | self._check_statuses(queue_entry, HqeStatus.VERIFYING, |
| 396 | HostStatus.VERIFYING) |
| 397 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 398 | |
| 399 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 400 | def test_simple_job(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 401 | self._initialize_test() |
| 402 | job, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 403 | self._run_pre_job_verify(queue_entry) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 404 | self._run_dispatcher() # launches job |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 405 | self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 406 | self._finish_job(queue_entry) |
| 407 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY) |
| 408 | self._assert_nothing_is_running() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 409 | |
| 410 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 411 | def _setup_for_pre_job_cleanup(self): |
| 412 | self._initialize_test() |
| 413 | job, queue_entry = self._make_job_and_queue_entry() |
| 414 | job.reboot_before = models.RebootBefore.ALWAYS |
| 415 | job.save() |
| 416 | return queue_entry |
| 417 | |
| 418 | |
| 419 | def _run_pre_job_cleanup_job(self, queue_entry): |
| 420 | self._run_dispatcher() # cleanup |
| 421 | self._check_statuses(queue_entry, HqeStatus.VERIFYING, |
| 422 | HostStatus.CLEANING) |
| 423 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 424 | self._run_dispatcher() # verify |
| 425 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 426 | self._run_dispatcher() # job |
| 427 | self._finish_job(queue_entry) |
| 428 | |
| 429 | |
| 430 | def test_pre_job_cleanup(self): |
| 431 | queue_entry = self._setup_for_pre_job_cleanup() |
| 432 | self._run_pre_job_cleanup_job(queue_entry) |
| 433 | |
| 434 | |
| 435 | def _run_pre_job_cleanup_one_failure(self): |
| 436 | queue_entry = self._setup_for_pre_job_cleanup() |
| 437 | self._run_dispatcher() # cleanup |
| 438 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 439 | exit_status=256) |
| 440 | self._run_dispatcher() # repair |
| 441 | self._check_statuses(queue_entry, HqeStatus.QUEUED, |
| 442 | HostStatus.REPAIRING) |
| 443 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 444 | return queue_entry |
| 445 | |
| 446 | |
| 447 | def test_pre_job_cleanup_failure(self): |
| 448 | queue_entry = self._run_pre_job_cleanup_one_failure() |
| 449 | # from here the job should run as normal |
| 450 | self._run_pre_job_cleanup_job(queue_entry) |
| 451 | |
| 452 | |
| 453 | def test_pre_job_cleanup_double_failure(self): |
| 454 | # TODO (showard): this test isn't perfect. in reality, when the second |
| 455 | # cleanup fails, it copies its results over to the job directory using |
| 456 | # copy_results_on_drone() and then parses them. since we don't handle |
| 457 | # that, there appear to be no results at the job directory. the |
| 458 | # scheduler handles this gracefully, parsing gets effectively skipped, |
| 459 | # and this test passes as is. but we ought to properly test that |
| 460 | # behavior. |
| 461 | queue_entry = self._run_pre_job_cleanup_one_failure() |
| 462 | self._run_dispatcher() # second cleanup |
| 463 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 464 | exit_status=256) |
| 465 | self._run_dispatcher() |
| 466 | self._check_statuses(queue_entry, HqeStatus.FAILED, |
| 467 | HostStatus.REPAIR_FAILED) |
| 468 | # nothing else should run |
| 469 | self._assert_nothing_is_running() |
| 470 | |
| 471 | |
| 472 | def _assert_nothing_is_running(self): |
| 473 | self.assertEquals(self.mock_drone_manager.running_pidfile_ids(), []) |
| 474 | |
| 475 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 476 | def _setup_for_post_job_cleanup(self): |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 477 | self._initialize_test() |
| 478 | job, queue_entry = self._make_job_and_queue_entry() |
| 479 | job.reboot_after = models.RebootAfter.ALWAYS |
| 480 | job.save() |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 481 | return queue_entry |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 482 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 483 | |
| 484 | def _run_post_job_cleanup_failure_up_to_repair(self, queue_entry, |
| 485 | include_verify=True): |
| 486 | if include_verify: |
| 487 | self._run_pre_job_verify(queue_entry) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 488 | self._run_dispatcher() # job |
| 489 | self.mock_drone_manager.finish_process(_PidfileType.JOB) |
| 490 | self._run_dispatcher() # parsing + cleanup |
| 491 | self.mock_drone_manager.finish_process(_PidfileType.PARSE) |
| 492 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 493 | exit_status=256) |
| 494 | self._run_dispatcher() # repair, HQE unaffected |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 495 | self.mock_drone_manager.finish_process(_PidfileType.ARCHIVE) |
| 496 | self._run_dispatcher() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 497 | return queue_entry |
| 498 | |
| 499 | |
| 500 | def test_post_job_cleanup_failure(self): |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 501 | queue_entry = self._setup_for_post_job_cleanup() |
| 502 | self._run_post_job_cleanup_failure_up_to_repair(queue_entry) |
| 503 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 504 | HostStatus.REPAIRING) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 505 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 506 | self._run_dispatcher() |
| 507 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY) |
| 508 | |
| 509 | |
| 510 | def test_post_job_cleanup_failure_repair_failure(self): |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 511 | queue_entry = self._setup_for_post_job_cleanup() |
| 512 | self._run_post_job_cleanup_failure_up_to_repair(queue_entry) |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 513 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR, |
| 514 | exit_status=256) |
| 515 | self._run_dispatcher() |
| 516 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 517 | HostStatus.REPAIR_FAILED) |
| 518 | |
| 519 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 520 | def _ensure_post_job_process_is_paired(self, queue_entry, pidfile_type): |
| 521 | pidfile_name = _PIDFILE_TYPE_TO_PIDFILE[pidfile_type] |
| 522 | queue_entry = self._update_instance(queue_entry) |
| 523 | pidfile_id = self.mock_drone_manager.pidfile_from_path( |
| 524 | queue_entry.execution_path(), pidfile_name) |
| 525 | self.assert_(pidfile_id._paired_with_pidfile) |
| 526 | |
| 527 | |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 528 | def _finish_job(self, queue_entry): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 529 | self.mock_drone_manager.finish_process(_PidfileType.JOB) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 530 | self._run_dispatcher() # launches parsing + cleanup |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 531 | self._check_statuses(queue_entry, HqeStatus.PARSING, |
| 532 | HostStatus.CLEANING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 533 | self._ensure_post_job_process_is_paired(queue_entry, _PidfileType.PARSE) |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 534 | self._finish_parsing_and_cleanup(queue_entry) |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 535 | |
| 536 | |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 537 | def _finish_parsing_and_cleanup(self, queue_entry): |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 538 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 539 | self.mock_drone_manager.finish_process(_PidfileType.PARSE) |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 540 | self._run_dispatcher() |
| 541 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 542 | self._check_entry_status(queue_entry, HqeStatus.ARCHIVING) |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 543 | self.mock_drone_manager.finish_process(_PidfileType.ARCHIVE) |
| 544 | self._run_dispatcher() |
| 545 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 546 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 547 | def _create_reverify_request(self): |
| 548 | host = self.hosts[0] |
| 549 | models.SpecialTask.objects.create(host=host, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 550 | task=models.SpecialTask.Task.VERIFY, |
| 551 | requested_by=self.user) |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 552 | return host |
| 553 | |
| 554 | |
| 555 | def test_requested_reverify(self): |
| 556 | host = self._create_reverify_request() |
| 557 | self._run_dispatcher() |
| 558 | self._check_host_status(host, HostStatus.VERIFYING) |
| 559 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 560 | self._run_dispatcher() |
| 561 | self._check_host_status(host, HostStatus.READY) |
| 562 | |
| 563 | |
| 564 | def test_requested_reverify_failure(self): |
| 565 | host = self._create_reverify_request() |
| 566 | self._run_dispatcher() |
| 567 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 568 | exit_status=256) |
| 569 | self._run_dispatcher() # repair |
| 570 | self._check_host_status(host, HostStatus.REPAIRING) |
| 571 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 572 | self._run_dispatcher() |
| 573 | self._check_host_status(host, HostStatus.READY) |
| 574 | |
| 575 | |
| 576 | def _setup_for_do_not_verify(self): |
| 577 | self._initialize_test() |
| 578 | job, queue_entry = self._make_job_and_queue_entry() |
| 579 | queue_entry.host.protection = host_protections.Protection.DO_NOT_VERIFY |
| 580 | queue_entry.host.save() |
| 581 | return queue_entry |
| 582 | |
| 583 | |
| 584 | def test_do_not_verify_job(self): |
| 585 | queue_entry = self._setup_for_do_not_verify() |
| 586 | self._run_dispatcher() # runs job directly |
| 587 | self._finish_job(queue_entry) |
| 588 | |
| 589 | |
| 590 | def test_do_not_verify_job_with_cleanup(self): |
| 591 | queue_entry = self._setup_for_do_not_verify() |
| 592 | queue_entry.job.reboot_before = models.RebootBefore.ALWAYS |
| 593 | queue_entry.job.save() |
| 594 | |
| 595 | self._run_dispatcher() # cleanup |
| 596 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 597 | self._run_dispatcher() # job |
| 598 | self._finish_job(queue_entry) |
| 599 | |
| 600 | |
| 601 | def test_do_not_verify_pre_job_cleanup_failure(self): |
| 602 | queue_entry = self._setup_for_do_not_verify() |
| 603 | queue_entry.job.reboot_before = models.RebootBefore.ALWAYS |
| 604 | queue_entry.job.save() |
| 605 | |
| 606 | self._run_dispatcher() # cleanup |
| 607 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP, |
| 608 | exit_status=256) |
| 609 | self._run_dispatcher() # failure ignored; job runs |
| 610 | self._finish_job(queue_entry) |
| 611 | |
| 612 | |
| 613 | def test_do_not_verify_post_job_cleanup_failure(self): |
| 614 | queue_entry = self._setup_for_do_not_verify() |
| 615 | |
| 616 | self._run_post_job_cleanup_failure_up_to_repair(queue_entry, |
| 617 | include_verify=False) |
| 618 | # failure ignored, host still set to Ready |
| 619 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, HostStatus.READY) |
| 620 | self._run_dispatcher() # nothing else runs |
| 621 | self._assert_nothing_is_running() |
| 622 | |
| 623 | |
| 624 | def test_do_not_verify_requested_reverify_failure(self): |
| 625 | host = self._create_reverify_request() |
| 626 | host.protection = host_protections.Protection.DO_NOT_VERIFY |
| 627 | host.save() |
| 628 | |
| 629 | self._run_dispatcher() |
| 630 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 631 | exit_status=256) |
| 632 | self._run_dispatcher() |
| 633 | self._check_host_status(host, HostStatus.READY) # ignore failure |
| 634 | self._assert_nothing_is_running() |
| 635 | |
| 636 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 637 | def test_job_abort_in_verify(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 638 | self._initialize_test() |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 639 | job = self._create_job(hosts=[1]) |
| 640 | self._run_dispatcher() # launches verify |
| 641 | job.hostqueueentry_set.update(aborted=True) |
| 642 | self._run_dispatcher() # kills verify, launches cleanup |
| 643 | self.assert_(self.mock_drone_manager.was_last_process_killed( |
| 644 | _PidfileType.VERIFY)) |
| 645 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 646 | self._run_dispatcher() |
| 647 | |
| 648 | |
| 649 | def test_job_abort(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 650 | self._initialize_test() |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 651 | job = self._create_job(hosts=[1]) |
| 652 | job.run_verify = False |
| 653 | job.save() |
| 654 | |
| 655 | self._run_dispatcher() # launches job |
| 656 | job.hostqueueentry_set.update(aborted=True) |
| 657 | self._run_dispatcher() # kills job, launches gathering |
| 658 | self.assert_(self.mock_drone_manager.was_last_process_killed( |
| 659 | _PidfileType.JOB)) |
| 660 | self.mock_drone_manager.finish_process(_PidfileType.GATHER) |
| 661 | self._run_dispatcher() # launches parsing + cleanup |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 662 | queue_entry = job.hostqueueentry_set.all()[0] |
| 663 | self._finish_parsing_and_cleanup(queue_entry) |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 664 | |
| 665 | |
| 666 | def test_no_pidfile_leaking(self): |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 667 | self._initialize_test() |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 668 | self.test_simple_job() |
| 669 | self.assertEquals(self.mock_drone_manager._pidfiles, {}) |
| 670 | |
| 671 | self.test_job_abort_in_verify() |
| 672 | self.assertEquals(self.mock_drone_manager._pidfiles, {}) |
| 673 | |
| 674 | self.test_job_abort() |
| 675 | self.assertEquals(self.mock_drone_manager._pidfiles, {}) |
| 676 | |
| 677 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 678 | def _make_job_and_queue_entry(self): |
| 679 | job = self._create_job(hosts=[1]) |
| 680 | queue_entry = job.hostqueueentry_set.all()[0] |
| 681 | return job, queue_entry |
| 682 | |
| 683 | |
| 684 | def test_recover_running_no_process(self): |
| 685 | # recovery should re-execute a Running HQE if no process is found |
| 686 | _, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 687 | queue_entry.status = HqeStatus.RUNNING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 688 | queue_entry.execution_subdir = '1-myuser/host1' |
| 689 | queue_entry.save() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 690 | queue_entry.host.status = HostStatus.RUNNING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 691 | queue_entry.host.save() |
| 692 | |
| 693 | self._initialize_test() |
| 694 | self._run_dispatcher() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 695 | self._finish_job(queue_entry) |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 696 | |
| 697 | |
| 698 | def test_recover_verifying_hqe_no_special_task(self): |
| 699 | # recovery should fail on a Verifing HQE with no corresponding |
| 700 | # Verify or Cleanup SpecialTask |
| 701 | _, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 702 | queue_entry.status = HqeStatus.VERIFYING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 703 | queue_entry.save() |
| 704 | |
| 705 | # make some dummy SpecialTasks that shouldn't count |
| 706 | models.SpecialTask.objects.create(host=queue_entry.host, |
| 707 | task=models.SpecialTask.Task.VERIFY) |
| 708 | models.SpecialTask.objects.create(host=queue_entry.host, |
| 709 | task=models.SpecialTask.Task.CLEANUP, |
| 710 | queue_entry=queue_entry, |
| 711 | is_complete=True) |
| 712 | |
| 713 | self.assertRaises(monitor_db.SchedulerError, self._initialize_test) |
| 714 | |
| 715 | |
| 716 | def _test_recover_verifying_hqe_helper(self, task, pidfile_type): |
| 717 | _, queue_entry = self._make_job_and_queue_entry() |
showard | 4a60479 | 2009-10-20 23:49:10 +0000 | [diff] [blame] | 718 | queue_entry.status = HqeStatus.VERIFYING |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 719 | queue_entry.save() |
| 720 | |
| 721 | special_task = models.SpecialTask.objects.create( |
| 722 | host=queue_entry.host, task=task, queue_entry=queue_entry) |
| 723 | |
| 724 | self._initialize_test() |
| 725 | self._run_dispatcher() |
| 726 | self.mock_drone_manager.finish_process(pidfile_type) |
| 727 | self._run_dispatcher() |
| 728 | # don't bother checking the rest of the job execution, as long as the |
| 729 | # SpecialTask ran |
| 730 | |
| 731 | |
| 732 | def test_recover_verifying_hqe_with_cleanup(self): |
| 733 | # recover an HQE that was in pre-job cleanup |
| 734 | self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.CLEANUP, |
| 735 | _PidfileType.CLEANUP) |
| 736 | |
| 737 | |
| 738 | def test_recover_verifying_hqe_with_verify(self): |
| 739 | # recover an HQE that was in pre-job verify |
| 740 | self._test_recover_verifying_hqe_helper(models.SpecialTask.Task.VERIFY, |
| 741 | _PidfileType.VERIFY) |
| 742 | |
| 743 | |
showard | a21b949 | 2009-11-04 20:43:18 +0000 | [diff] [blame] | 744 | def test_recover_pending_hqes_with_group(self): |
| 745 | # recover a group of HQEs that are in Pending, in the same group (e.g., |
| 746 | # in a job with atomic hosts) |
| 747 | job = self._create_job(hosts=[1,2], atomic_group=1) |
| 748 | job.save() |
| 749 | |
| 750 | job.hostqueueentry_set.all().update(status=HqeStatus.PENDING) |
| 751 | |
| 752 | self._initialize_test() |
| 753 | for queue_entry in job.hostqueueentry_set.all(): |
| 754 | self.assertEquals(queue_entry.status, HqeStatus.STARTING) |
| 755 | |
| 756 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 757 | def test_recover_parsing(self): |
| 758 | self._initialize_test() |
| 759 | job, queue_entry = self._make_job_and_queue_entry() |
| 760 | job.run_verify = False |
| 761 | job.reboot_after = models.RebootAfter.NEVER |
| 762 | job.save() |
| 763 | |
| 764 | self._run_dispatcher() # launches job |
| 765 | self.mock_drone_manager.finish_process(_PidfileType.JOB) |
| 766 | self._run_dispatcher() # launches parsing |
| 767 | |
| 768 | # now "restart" the scheduler |
| 769 | self._create_dispatcher() |
| 770 | self._initialize_test() |
| 771 | self._run_dispatcher() |
| 772 | self.mock_drone_manager.finish_process(_PidfileType.PARSE) |
| 773 | self._run_dispatcher() |
| 774 | |
| 775 | |
| 776 | def test_recover_parsing__no_process_already_aborted(self): |
| 777 | _, queue_entry = self._make_job_and_queue_entry() |
| 778 | queue_entry.execution_subdir = 'host1' |
| 779 | queue_entry.status = HqeStatus.PARSING |
| 780 | queue_entry.aborted = True |
| 781 | queue_entry.save() |
| 782 | |
| 783 | self._initialize_test() |
| 784 | self._run_dispatcher() |
| 785 | |
| 786 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 787 | def test_job_scheduled_just_after_abort(self): |
| 788 | # test a pretty obscure corner case where a job is aborted while queued, |
| 789 | # another job is ready to run, and throttling is active. the post-abort |
| 790 | # cleanup must not be pre-empted by the second job. |
| 791 | job1, queue_entry1 = self._make_job_and_queue_entry() |
| 792 | job2, queue_entry2 = self._make_job_and_queue_entry() |
| 793 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 794 | self.mock_drone_manager.process_capacity = 0 |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 795 | self._run_dispatcher() # schedule job1, but won't start verify |
| 796 | job1.hostqueueentry_set.update(aborted=True) |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 797 | self.mock_drone_manager.process_capacity = 100 |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 798 | self._run_dispatcher() # cleanup must run here, not verify for job2 |
| 799 | self._check_statuses(queue_entry1, HqeStatus.ABORTED, |
| 800 | HostStatus.CLEANING) |
| 801 | self.mock_drone_manager.finish_process(_PidfileType.CLEANUP) |
| 802 | self._run_dispatcher() # now verify starts for job2 |
| 803 | self._check_statuses(queue_entry2, HqeStatus.VERIFYING, |
| 804 | HostStatus.VERIFYING) |
| 805 | |
| 806 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 807 | def test_reverify_interrupting_pre_job(self): |
| 808 | # ensure things behave sanely if a reverify is scheduled in the middle |
| 809 | # of pre-job actions |
| 810 | _, queue_entry = self._make_job_and_queue_entry() |
| 811 | |
| 812 | self._run_dispatcher() # pre-job verify |
| 813 | self._create_reverify_request() |
| 814 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 815 | exit_status=256) |
| 816 | self._run_dispatcher() # repair |
| 817 | self.mock_drone_manager.finish_process(_PidfileType.REPAIR) |
| 818 | self._run_dispatcher() # reverify runs now |
| 819 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 820 | self._run_dispatcher() # pre-job verify |
| 821 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 822 | self._run_dispatcher() # and job runs... |
| 823 | self._check_statuses(queue_entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 824 | self._finish_job(queue_entry) # reverify has been deleted |
| 825 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 826 | HostStatus.READY) |
| 827 | self._assert_nothing_is_running() |
| 828 | |
| 829 | |
| 830 | def test_reverify_while_job_running(self): |
| 831 | # once a job is running, a reverify must not be allowed to preempt |
| 832 | # Gathering |
| 833 | _, queue_entry = self._make_job_and_queue_entry() |
| 834 | self._run_pre_job_verify(queue_entry) |
| 835 | self._run_dispatcher() # job runs |
| 836 | self._create_reverify_request() |
| 837 | # make job end with a signal, so gathering will run |
| 838 | self.mock_drone_manager.finish_process(_PidfileType.JOB, |
| 839 | exit_status=271) |
| 840 | self._run_dispatcher() # gathering must start |
| 841 | self.mock_drone_manager.finish_process(_PidfileType.GATHER) |
| 842 | self._run_dispatcher() # parsing and cleanup |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 843 | self._finish_parsing_and_cleanup(queue_entry) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 844 | self._run_dispatcher() # now reverify runs |
| 845 | self._check_statuses(queue_entry, HqeStatus.FAILED, |
| 846 | HostStatus.VERIFYING) |
| 847 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 848 | self._run_dispatcher() |
| 849 | self._check_host_status(queue_entry.host, HostStatus.READY) |
| 850 | |
| 851 | |
| 852 | def test_reverify_while_host_pending(self): |
| 853 | # ensure that if a reverify is scheduled while a host is in Pending, it |
| 854 | # won't run until the host is actually free |
| 855 | job = self._create_job(hosts=[1,2]) |
| 856 | queue_entry = job.hostqueueentry_set.get(host__hostname='host1') |
| 857 | job.synch_count = 2 |
| 858 | job.save() |
| 859 | |
| 860 | host2 = self.hosts[1] |
| 861 | host2.locked = True |
| 862 | host2.save() |
| 863 | |
| 864 | self._run_dispatcher() # verify host1 |
| 865 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 866 | self._run_dispatcher() # host1 Pending |
| 867 | self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING) |
| 868 | self._create_reverify_request() |
| 869 | self._run_dispatcher() # nothing should happen here |
| 870 | self._check_statuses(queue_entry, HqeStatus.PENDING, HostStatus.PENDING) |
| 871 | |
| 872 | # now let the job run |
| 873 | host2.locked = False |
| 874 | host2.save() |
| 875 | self._run_dispatcher() # verify host2 |
| 876 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 877 | self._run_dispatcher() # run job |
| 878 | self._finish_job(queue_entry) |
| 879 | # need to explicitly finish host1's post-job cleanup |
| 880 | self.mock_drone_manager.finish_specific_process( |
| 881 | 'hosts/host1/4-cleanup', monitor_db._AUTOSERV_PID_FILE) |
| 882 | self._run_dispatcher() |
| 883 | # the reverify should now be running |
| 884 | self._check_statuses(queue_entry, HqeStatus.COMPLETED, |
| 885 | HostStatus.VERIFYING) |
| 886 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 887 | self._run_dispatcher() |
| 888 | self._check_host_status(queue_entry.host, HostStatus.READY) |
| 889 | |
| 890 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 891 | def test_throttling(self): |
| 892 | job = self._create_job(hosts=[1,2,3]) |
| 893 | job.synch_count = 3 |
| 894 | job.save() |
| 895 | |
| 896 | queue_entries = list(job.hostqueueentry_set.all()) |
| 897 | def _check_hqe_statuses(*statuses): |
| 898 | for queue_entry, status in zip(queue_entries, statuses): |
| 899 | self._check_statuses(queue_entry, status) |
| 900 | |
| 901 | self.mock_drone_manager.process_capacity = 2 |
| 902 | self._run_dispatcher() # verify runs on 1 and 2 |
| 903 | _check_hqe_statuses(HqeStatus.VERIFYING, HqeStatus.VERIFYING, |
| 904 | HqeStatus.VERIFYING) |
| 905 | self.assertEquals(len(self.mock_drone_manager.running_pidfile_ids()), 2) |
| 906 | |
| 907 | self.mock_drone_manager.finish_specific_process( |
| 908 | 'hosts/host1/1-verify', monitor_db._AUTOSERV_PID_FILE) |
| 909 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 910 | self._run_dispatcher() # verify runs on 3 |
| 911 | _check_hqe_statuses(HqeStatus.PENDING, HqeStatus.PENDING, |
| 912 | HqeStatus.VERIFYING) |
| 913 | |
| 914 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 915 | self._run_dispatcher() # job won't run due to throttling |
| 916 | _check_hqe_statuses(HqeStatus.STARTING, HqeStatus.STARTING, |
| 917 | HqeStatus.STARTING) |
| 918 | self._assert_nothing_is_running() |
| 919 | |
| 920 | self.mock_drone_manager.process_capacity = 3 |
| 921 | self._run_dispatcher() # now job runs |
| 922 | _check_hqe_statuses(HqeStatus.RUNNING, HqeStatus.RUNNING, |
| 923 | HqeStatus.RUNNING) |
| 924 | |
| 925 | self.mock_drone_manager.process_capacity = 2 |
| 926 | self.mock_drone_manager.finish_process(_PidfileType.JOB, |
| 927 | exit_status=271) |
| 928 | self._run_dispatcher() # gathering won't run due to throttling |
| 929 | _check_hqe_statuses(HqeStatus.GATHERING, HqeStatus.GATHERING, |
| 930 | HqeStatus.GATHERING) |
| 931 | self._assert_nothing_is_running() |
| 932 | |
| 933 | self.mock_drone_manager.process_capacity = 3 |
| 934 | self._run_dispatcher() # now gathering runs |
| 935 | |
| 936 | self.mock_drone_manager.process_capacity = 0 |
| 937 | self.mock_drone_manager.finish_process(_PidfileType.GATHER) |
| 938 | self._run_dispatcher() # parsing runs despite throttling |
| 939 | _check_hqe_statuses(HqeStatus.PARSING, HqeStatus.PARSING, |
| 940 | HqeStatus.PARSING) |
| 941 | |
| 942 | |
showard | 2b38f67 | 2009-12-23 00:08:44 +0000 | [diff] [blame] | 943 | def test_abort_starting_while_throttling(self): |
| 944 | self._initialize_test() |
| 945 | job = self._create_job(hosts=[1,2], synchronous=True) |
| 946 | queue_entry = job.hostqueueentry_set.all()[0] |
| 947 | job.run_verify = False |
| 948 | job.reboot_after = models.RebootAfter.NEVER |
| 949 | job.save() |
| 950 | |
| 951 | self.mock_drone_manager.process_capacity = 0 |
| 952 | self._run_dispatcher() # go to starting, but don't start job |
| 953 | self._check_statuses(queue_entry, HqeStatus.STARTING, |
| 954 | HostStatus.PENDING) |
| 955 | |
| 956 | job.hostqueueentry_set.update(aborted=True) |
| 957 | self._run_dispatcher() |
| 958 | |
| 959 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 960 | def test_simple_atomic_group_job(self): |
| 961 | job = self._create_job(atomic_group=1) |
| 962 | self._run_dispatcher() # expand + verify |
| 963 | queue_entries = job.hostqueueentry_set.all() |
| 964 | self.assertEquals(len(queue_entries), 2) |
| 965 | self.assertEquals(queue_entries[0].host.hostname, 'host5') |
| 966 | self.assertEquals(queue_entries[1].host.hostname, 'host6') |
| 967 | |
| 968 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 969 | self._run_dispatcher() # delay task started waiting |
| 970 | |
| 971 | self.mock_drone_manager.finish_specific_process( |
| 972 | 'hosts/host5/1-verify', monitor_db._AUTOSERV_PID_FILE) |
| 973 | self._run_dispatcher() # job starts now |
| 974 | for entry in queue_entries: |
| 975 | self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 976 | |
| 977 | # rest of job proceeds normally |
| 978 | |
| 979 | |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 980 | def test_simple_metahost_assignment(self): |
| 981 | job = self._create_job(metahosts=[1]) |
| 982 | self._run_dispatcher() |
| 983 | entry = job.hostqueueentry_set.all()[0] |
| 984 | self.assertEquals(entry.host.hostname, 'host1') |
| 985 | self._check_statuses(entry, HqeStatus.VERIFYING, HostStatus.VERIFYING) |
| 986 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 987 | self._run_dispatcher() |
| 988 | self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 989 | # rest of job proceeds normally |
| 990 | |
| 991 | |
| 992 | def test_metahost_fail_verify(self): |
| 993 | self.hosts[1].labels.add(self.labels[0]) # put label1 also on host2 |
| 994 | job = self._create_job(metahosts=[1]) |
| 995 | self._run_dispatcher() # assigned to host1 |
| 996 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY, |
| 997 | exit_status=256) |
| 998 | self._run_dispatcher() # host1 failed, gets reassigned to host2 |
| 999 | entry = job.hostqueueentry_set.all()[0] |
| 1000 | self.assertEquals(entry.host.hostname, 'host2') |
| 1001 | self._check_statuses(entry, HqeStatus.VERIFYING, HostStatus.VERIFYING) |
| 1002 | self._check_host_status(self.hosts[0], HostStatus.REPAIRING) |
| 1003 | |
| 1004 | self.mock_drone_manager.finish_process(_PidfileType.VERIFY) |
| 1005 | self._run_dispatcher() |
| 1006 | self._check_statuses(entry, HqeStatus.RUNNING, HostStatus.RUNNING) |
| 1007 | |
| 1008 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1009 | def test_hostless_job(self): |
| 1010 | job = self._create_job(hostless=True) |
| 1011 | entry = job.hostqueueentry_set.all()[0] |
| 1012 | |
| 1013 | self._run_dispatcher() |
| 1014 | self._check_entry_status(entry, HqeStatus.RUNNING) |
| 1015 | |
| 1016 | self.mock_drone_manager.finish_process(_PidfileType.JOB) |
| 1017 | self._run_dispatcher() |
| 1018 | self._check_entry_status(entry, HqeStatus.COMPLETED) |
| 1019 | |
| 1020 | |
showard | 493beaa | 2009-12-18 22:44:45 +0000 | [diff] [blame] | 1021 | def test_pre_job_keyvals(self): |
| 1022 | self.test_simple_job() |
| 1023 | attached_files = self.mock_drone_manager.attached_files( |
| 1024 | '1-my_user/host1') |
| 1025 | job_keyval_path = '1-my_user/host1/keyval' |
| 1026 | self.assert_(job_keyval_path in attached_files, attached_files) |
| 1027 | keyval_contents = attached_files[job_keyval_path] |
| 1028 | keyval_dict = dict(line.strip().split('=', 1) |
| 1029 | for line in keyval_contents.splitlines()) |
| 1030 | self.assert_('job_queued' in keyval_dict, keyval_dict) |
| 1031 | |
| 1032 | |
showard | 34ab099 | 2009-10-05 22:47:57 +0000 | [diff] [blame] | 1033 | if __name__ == '__main__': |
| 1034 | unittest.main() |