showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 3 | import gc, logging, time |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 4 | import common |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 5 | from autotest_lib.frontend import setup_django_environment |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend.afe import frontend_test_utils |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 7 | from autotest_lib.client.common_lib.test_utils import mock |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib.test_utils import unittest |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 9 | from autotest_lib.database import database_connection |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 10 | from autotest_lib.frontend.afe import models |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 11 | from autotest_lib.scheduler import monitor_db, drone_manager, email_manager |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 12 | from autotest_lib.scheduler import scheduler_config, gc_stats, host_scheduler |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame] | 13 | from autotest_lib.scheduler import monitor_db_functional_test |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 14 | from autotest_lib.scheduler import scheduler_models |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 15 | |
| 16 | _DEBUG = False |
| 17 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 18 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 19 | class DummyAgentTask(object): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 20 | num_processes = 1 |
| 21 | owner_username = 'my_user' |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 22 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 23 | def get_drone_hostnames_allowed(self): |
| 24 | return None |
| 25 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 26 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 27 | class DummyAgent(object): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 28 | started = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 29 | _is_done = False |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 30 | host_ids = () |
| 31 | queue_entry_ids = () |
| 32 | |
| 33 | def __init__(self): |
| 34 | self.task = DummyAgentTask() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 35 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 36 | |
| 37 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 38 | self.started = True |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | def is_done(self): |
| 42 | return self._is_done |
| 43 | |
| 44 | |
| 45 | def set_done(self, done): |
| 46 | self._is_done = done |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 47 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 48 | |
| 49 | class IsRow(mock.argument_comparator): |
| 50 | def __init__(self, row_id): |
| 51 | self.row_id = row_id |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 52 | |
| 53 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 54 | def is_satisfied_by(self, parameter): |
| 55 | return list(parameter)[0] == self.row_id |
| 56 | |
| 57 | |
| 58 | def __str__(self): |
| 59 | return 'row with id %s' % self.row_id |
| 60 | |
| 61 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 62 | class IsAgentWithTask(mock.argument_comparator): |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 63 | def __init__(self, task): |
| 64 | self._task = task |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 65 | |
| 66 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 67 | def is_satisfied_by(self, parameter): |
| 68 | if not isinstance(parameter, monitor_db.Agent): |
| 69 | return False |
| 70 | tasks = list(parameter.queue.queue) |
| 71 | if len(tasks) != 1: |
| 72 | return False |
| 73 | return tasks[0] == self._task |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 74 | |
| 75 | |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 76 | def _set_host_and_qe_ids(agent_or_task, id_list=None): |
| 77 | if id_list is None: |
| 78 | id_list = [] |
| 79 | agent_or_task.host_ids = agent_or_task.queue_entry_ids = id_list |
| 80 | |
| 81 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 82 | class BaseSchedulerTest(unittest.TestCase, |
| 83 | frontend_test_utils.FrontendTestMixin): |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 84 | _config_section = 'AUTOTEST_WEB' |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 85 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 86 | def _do_query(self, sql): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 87 | self._database.execute(sql) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 88 | |
| 89 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 90 | def _set_monitor_stubs(self): |
| 91 | # Clear the instance cache as this is a brand new database. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 92 | scheduler_models.DBObject._clear_instance_cache() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 93 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 94 | self._database = ( |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame] | 95 | database_connection.TranslatingDatabase.get_test_database( |
| 96 | translators=monitor_db_functional_test._DB_TRANSLATORS)) |
| 97 | self._database.connect(db_type='django') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 98 | self._database.debug = _DEBUG |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 99 | |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame] | 100 | self.god.stub_with(monitor_db, '_db', self._database) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 101 | self.god.stub_with(scheduler_models, '_db', self._database) |
| 102 | self.god.stub_with(drone_manager.instance(), '_results_dir', |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame] | 103 | '/test/path') |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 104 | self.god.stub_with(drone_manager.instance(), '_temporary_directory', |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame] | 105 | '/test/path/tmp') |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 106 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 107 | monitor_db.initialize_globals() |
| 108 | scheduler_models.initialize_globals() |
| 109 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 110 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 111 | def setUp(self): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 112 | self._frontend_common_setup() |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 113 | self._set_monitor_stubs() |
| 114 | self._dispatcher = monitor_db.Dispatcher() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 115 | |
| 116 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 117 | def tearDown(self): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 118 | self._database.disconnect() |
| 119 | self._frontend_common_teardown() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 120 | |
| 121 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 122 | def _update_hqe(self, set, where=''): |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 123 | query = 'UPDATE afe_host_queue_entries SET ' + set |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 124 | if where: |
| 125 | query += ' WHERE ' + where |
| 126 | self._do_query(query) |
| 127 | |
| 128 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 129 | class DispatcherSchedulingTest(BaseSchedulerTest): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 130 | _jobs_scheduled = [] |
| 131 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 132 | |
| 133 | def tearDown(self): |
| 134 | super(DispatcherSchedulingTest, self).tearDown() |
| 135 | |
| 136 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 137 | def _set_monitor_stubs(self): |
| 138 | super(DispatcherSchedulingTest, self)._set_monitor_stubs() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 139 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 140 | def hqe__do_schedule_pre_job_tasks_stub(queue_entry): |
| 141 | """Called by HostQueueEntry.run().""" |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 142 | self._record_job_scheduled(queue_entry.job.id, queue_entry.host.id) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 143 | queue_entry.set_status('Starting') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 144 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 145 | self.god.stub_with(scheduler_models.HostQueueEntry, |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 146 | '_do_schedule_pre_job_tasks', |
| 147 | hqe__do_schedule_pre_job_tasks_stub) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 148 | |
| 149 | def hqe_queue_log_record_stub(self, log_line): |
| 150 | """No-Op to avoid calls down to the _drone_manager during tests.""" |
| 151 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 152 | self.god.stub_with(scheduler_models.HostQueueEntry, 'queue_log_record', |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 153 | hqe_queue_log_record_stub) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def _record_job_scheduled(self, job_id, host_id): |
| 157 | record = (job_id, host_id) |
| 158 | self.assert_(record not in self._jobs_scheduled, |
| 159 | 'Job %d scheduled on host %d twice' % |
| 160 | (job_id, host_id)) |
| 161 | self._jobs_scheduled.append(record) |
| 162 | |
| 163 | |
| 164 | def _assert_job_scheduled_on(self, job_id, host_id): |
| 165 | record = (job_id, host_id) |
| 166 | self.assert_(record in self._jobs_scheduled, |
| 167 | 'Job %d not scheduled on host %d as expected\n' |
| 168 | 'Jobs scheduled: %s' % |
| 169 | (job_id, host_id, self._jobs_scheduled)) |
| 170 | self._jobs_scheduled.remove(record) |
| 171 | |
| 172 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 173 | def _assert_job_scheduled_on_number_of(self, job_id, host_ids, number): |
| 174 | """Assert job was scheduled on exactly number hosts out of a set.""" |
| 175 | found = [] |
| 176 | for host_id in host_ids: |
| 177 | record = (job_id, host_id) |
| 178 | if record in self._jobs_scheduled: |
| 179 | found.append(record) |
| 180 | self._jobs_scheduled.remove(record) |
| 181 | if len(found) < number: |
| 182 | self.fail('Job %d scheduled on fewer than %d hosts in %s.\n' |
| 183 | 'Jobs scheduled: %s' % (job_id, number, host_ids, found)) |
| 184 | elif len(found) > number: |
| 185 | self.fail('Job %d scheduled on more than %d hosts in %s.\n' |
| 186 | 'Jobs scheduled: %s' % (job_id, number, host_ids, found)) |
| 187 | |
| 188 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 189 | def _check_for_extra_schedulings(self): |
| 190 | if len(self._jobs_scheduled) != 0: |
| 191 | self.fail('Extra jobs scheduled: ' + |
| 192 | str(self._jobs_scheduled)) |
| 193 | |
| 194 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 195 | def _convert_jobs_to_metahosts(self, *job_ids): |
| 196 | sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')' |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 197 | self._do_query('UPDATE afe_host_queue_entries SET ' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 198 | 'meta_host=host_id, host_id=NULL ' |
| 199 | 'WHERE job_id IN ' + sql_tuple) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 200 | |
| 201 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | def _lock_host(self, host_id): |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 203 | self._do_query('UPDATE afe_hosts SET locked=1 WHERE id=' + |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 204 | str(host_id)) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 205 | |
| 206 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 207 | def setUp(self): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 208 | super(DispatcherSchedulingTest, self).setUp() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 209 | self._jobs_scheduled = [] |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 210 | |
| 211 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 212 | def _run_scheduler(self): |
| 213 | for _ in xrange(2): # metahost scheduling can take two cycles |
| 214 | self._dispatcher._schedule_new_jobs() |
| 215 | |
| 216 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 217 | def _test_basic_scheduling_helper(self, use_metahosts): |
| 218 | 'Basic nonmetahost scheduling' |
| 219 | self._create_job_simple([1], use_metahosts) |
| 220 | self._create_job_simple([2], use_metahosts) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 221 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 222 | self._assert_job_scheduled_on(1, 1) |
| 223 | self._assert_job_scheduled_on(2, 2) |
| 224 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 225 | |
| 226 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 227 | def _test_priorities_helper(self, use_metahosts): |
| 228 | 'Test prioritization ordering' |
| 229 | self._create_job_simple([1], use_metahosts) |
| 230 | self._create_job_simple([2], use_metahosts) |
| 231 | self._create_job_simple([1,2], use_metahosts) |
| 232 | self._create_job_simple([1], use_metahosts, priority=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 233 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 234 | self._assert_job_scheduled_on(4, 1) # higher priority |
| 235 | self._assert_job_scheduled_on(2, 2) # earlier job over later |
| 236 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 237 | |
| 238 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 239 | def _test_hosts_ready_helper(self, use_metahosts): |
| 240 | """ |
| 241 | Only hosts that are status=Ready, unlocked and not invalid get |
| 242 | scheduled. |
| 243 | """ |
| 244 | self._create_job_simple([1], use_metahosts) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 245 | self._do_query('UPDATE afe_hosts SET status="Running" WHERE id=1') |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 246 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 247 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 248 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 249 | self._do_query('UPDATE afe_hosts SET status="Ready", locked=1 ' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 250 | 'WHERE id=1') |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 251 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 252 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 253 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 254 | self._do_query('UPDATE afe_hosts SET locked=0, invalid=1 ' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 255 | 'WHERE id=1') |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 256 | self._run_scheduler() |
showard | 5df2b19 | 2008-07-03 19:51:57 +0000 | [diff] [blame] | 257 | if not use_metahosts: |
| 258 | self._assert_job_scheduled_on(1, 1) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 259 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 260 | |
| 261 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 262 | def _test_hosts_idle_helper(self, use_metahosts): |
| 263 | 'Only idle hosts get scheduled' |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 264 | self._create_job(hosts=[1], active=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 265 | self._create_job_simple([1], use_metahosts) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 266 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 267 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 268 | |
| 269 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 270 | def _test_obey_ACLs_helper(self, use_metahosts): |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 271 | self._do_query('DELETE FROM afe_acl_groups_hosts WHERE host_id=1') |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 272 | self._create_job_simple([1], use_metahosts) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 273 | self._run_scheduler() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 274 | self._check_for_extra_schedulings() |
| 275 | |
| 276 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 277 | def test_basic_scheduling(self): |
| 278 | self._test_basic_scheduling_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 279 | |
| 280 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 281 | def test_priorities(self): |
| 282 | self._test_priorities_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 283 | |
| 284 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 285 | def test_hosts_ready(self): |
| 286 | self._test_hosts_ready_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 287 | |
| 288 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 289 | def test_hosts_idle(self): |
| 290 | self._test_hosts_idle_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 291 | |
| 292 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 293 | def test_obey_ACLs(self): |
| 294 | self._test_obey_ACLs_helper(False) |
| 295 | |
| 296 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 297 | def test_one_time_hosts_ignore_ACLs(self): |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 298 | self._do_query('DELETE FROM afe_acl_groups_hosts WHERE host_id=1') |
| 299 | self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=1') |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 300 | self._create_job_simple([1]) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 301 | self._run_scheduler() |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 302 | self._assert_job_scheduled_on(1, 1) |
| 303 | self._check_for_extra_schedulings() |
| 304 | |
| 305 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 306 | def test_non_metahost_on_invalid_host(self): |
| 307 | """ |
| 308 | Non-metahost entries can get scheduled on invalid hosts (this is how |
| 309 | one-time hosts work). |
| 310 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 311 | self._do_query('UPDATE afe_hosts SET invalid=1') |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 312 | self._test_basic_scheduling_helper(False) |
| 313 | |
| 314 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 315 | def test_metahost_scheduling(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 316 | """ |
| 317 | Basic metahost scheduling |
| 318 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 319 | self._test_basic_scheduling_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 320 | |
| 321 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 322 | def test_metahost_priorities(self): |
| 323 | self._test_priorities_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 324 | |
| 325 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 326 | def test_metahost_hosts_ready(self): |
| 327 | self._test_hosts_ready_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 328 | |
| 329 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 330 | def test_metahost_hosts_idle(self): |
| 331 | self._test_hosts_idle_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 332 | |
| 333 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 334 | def test_metahost_obey_ACLs(self): |
| 335 | self._test_obey_ACLs_helper(True) |
| 336 | |
| 337 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 338 | def _setup_test_only_if_needed_labels(self): |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 339 | # apply only_if_needed label3 to host1 |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 340 | models.Host.smart_get('host1').labels.add(self.label3) |
| 341 | return self._create_job_simple([1], use_metahost=True) |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 342 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 343 | |
| 344 | def test_only_if_needed_labels_avoids_host(self): |
| 345 | job = self._setup_test_only_if_needed_labels() |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 346 | # if the job doesn't depend on label3, there should be no scheduling |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 347 | self._run_scheduler() |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 348 | self._check_for_extra_schedulings() |
| 349 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 350 | |
| 351 | def test_only_if_needed_labels_schedules(self): |
| 352 | job = self._setup_test_only_if_needed_labels() |
| 353 | job.dependency_labels.add(self.label3) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 354 | self._run_scheduler() |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 355 | self._assert_job_scheduled_on(1, 1) |
| 356 | self._check_for_extra_schedulings() |
| 357 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 358 | |
| 359 | def test_only_if_needed_labels_via_metahost(self): |
| 360 | job = self._setup_test_only_if_needed_labels() |
| 361 | job.dependency_labels.add(self.label3) |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 362 | # should also work if the metahost is the only_if_needed label |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 363 | self._do_query('DELETE FROM afe_jobs_dependency_labels') |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 364 | self._create_job(metahosts=[3]) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 365 | self._run_scheduler() |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 366 | self._assert_job_scheduled_on(2, 1) |
| 367 | self._check_for_extra_schedulings() |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 368 | |
| 369 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 370 | def test_nonmetahost_over_metahost(self): |
| 371 | """ |
| 372 | Non-metahost entries should take priority over metahost entries |
| 373 | for the same host |
| 374 | """ |
| 375 | self._create_job(metahosts=[1]) |
| 376 | self._create_job(hosts=[1]) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 377 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 378 | self._assert_job_scheduled_on(2, 1) |
| 379 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 380 | |
| 381 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 382 | def test_metahosts_obey_blocks(self): |
| 383 | """ |
| 384 | Metahosts can't get scheduled on hosts already scheduled for |
| 385 | that job. |
| 386 | """ |
| 387 | self._create_job(metahosts=[1], hosts=[1]) |
| 388 | # make the nonmetahost entry complete, so the metahost can try |
| 389 | # to get scheduled |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 390 | self._update_hqe(set='complete = 1', where='host_id=1') |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 391 | self._run_scheduler() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 392 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 393 | |
| 394 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 395 | # TODO(gps): These should probably live in their own TestCase class |
| 396 | # specific to testing HostScheduler methods directly. It was convenient |
| 397 | # to put it here for now to share existing test environment setup code. |
| 398 | def test_HostScheduler_check_atomic_group_labels(self): |
| 399 | normal_job = self._create_job(metahosts=[0]) |
| 400 | atomic_job = self._create_job(atomic_group=1) |
| 401 | # Indirectly initialize the internal state of the host scheduler. |
| 402 | self._dispatcher._refresh_pending_queue_entries() |
| 403 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 404 | atomic_hqe = scheduler_models.HostQueueEntry.fetch(where='job_id=%d' % |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 405 | atomic_job.id)[0] |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 406 | normal_hqe = scheduler_models.HostQueueEntry.fetch(where='job_id=%d' % |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 407 | normal_job.id)[0] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 408 | |
| 409 | host_scheduler = self._dispatcher._host_scheduler |
| 410 | self.assertTrue(host_scheduler._check_atomic_group_labels( |
| 411 | [self.label4.id], atomic_hqe)) |
| 412 | self.assertFalse(host_scheduler._check_atomic_group_labels( |
| 413 | [self.label4.id], normal_hqe)) |
| 414 | self.assertFalse(host_scheduler._check_atomic_group_labels( |
| 415 | [self.label5.id, self.label6.id, self.label7.id], normal_hqe)) |
| 416 | self.assertTrue(host_scheduler._check_atomic_group_labels( |
| 417 | [self.label4.id, self.label6.id], atomic_hqe)) |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 418 | self.assertTrue(host_scheduler._check_atomic_group_labels( |
| 419 | [self.label4.id, self.label5.id], |
| 420 | atomic_hqe)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | def test_HostScheduler_get_host_atomic_group_id(self): |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 424 | job = self._create_job(metahosts=[self.label6.id]) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 425 | queue_entry = scheduler_models.HostQueueEntry.fetch( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 426 | where='job_id=%d' % job.id)[0] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 427 | # Indirectly initialize the internal state of the host scheduler. |
| 428 | self._dispatcher._refresh_pending_queue_entries() |
| 429 | |
| 430 | # Test the host scheduler |
| 431 | host_scheduler = self._dispatcher._host_scheduler |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 432 | |
| 433 | # Two labels each in a different atomic group. This should log an |
| 434 | # error and continue. |
| 435 | orig_logging_error = logging.error |
| 436 | def mock_logging_error(message, *args): |
| 437 | mock_logging_error._num_calls += 1 |
| 438 | # Test the logging call itself, we just wrapped it to count it. |
| 439 | orig_logging_error(message, *args) |
| 440 | mock_logging_error._num_calls = 0 |
| 441 | self.god.stub_with(logging, 'error', mock_logging_error) |
| 442 | self.assertNotEquals(None, host_scheduler._get_host_atomic_group_id( |
| 443 | [self.label4.id, self.label8.id], queue_entry)) |
| 444 | self.assertTrue(mock_logging_error._num_calls > 0) |
| 445 | self.god.unstub(logging, 'error') |
| 446 | |
| 447 | # Two labels both in the same atomic group, this should not raise an |
| 448 | # error, it will merely cause the job to schedule on the intersection. |
| 449 | self.assertEquals(1, host_scheduler._get_host_atomic_group_id( |
| 450 | [self.label4.id, self.label5.id])) |
| 451 | |
| 452 | self.assertEquals(None, host_scheduler._get_host_atomic_group_id([])) |
| 453 | self.assertEquals(None, host_scheduler._get_host_atomic_group_id( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 454 | [self.label3.id, self.label7.id, self.label6.id])) |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 455 | self.assertEquals(1, host_scheduler._get_host_atomic_group_id( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 456 | [self.label4.id, self.label7.id, self.label6.id])) |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 457 | self.assertEquals(1, host_scheduler._get_host_atomic_group_id( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 458 | [self.label7.id, self.label5.id])) |
| 459 | |
| 460 | |
| 461 | def test_atomic_group_hosts_blocked_from_non_atomic_jobs(self): |
| 462 | # Create a job scheduled to run on label6. |
| 463 | self._create_job(metahosts=[self.label6.id]) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 464 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 465 | # label6 only has hosts that are in atomic groups associated with it, |
| 466 | # there should be no scheduling. |
| 467 | self._check_for_extra_schedulings() |
| 468 | |
| 469 | |
| 470 | def test_atomic_group_hosts_blocked_from_non_atomic_jobs_explicit(self): |
| 471 | # Create a job scheduled to run on label5. This is an atomic group |
| 472 | # label but this job does not request atomic group scheduling. |
| 473 | self._create_job(metahosts=[self.label5.id]) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 474 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 475 | # label6 only has hosts that are in atomic groups associated with it, |
| 476 | # there should be no scheduling. |
| 477 | self._check_for_extra_schedulings() |
| 478 | |
| 479 | |
| 480 | def test_atomic_group_scheduling_basics(self): |
| 481 | # Create jobs scheduled to run on an atomic group. |
| 482 | job_a = self._create_job(synchronous=True, metahosts=[self.label4.id], |
| 483 | atomic_group=1) |
| 484 | job_b = self._create_job(synchronous=True, metahosts=[self.label5.id], |
| 485 | atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 486 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 487 | # atomic_group.max_number_of_machines was 2 so we should run on 2. |
| 488 | self._assert_job_scheduled_on_number_of(job_a.id, (5, 6, 7), 2) |
| 489 | self._assert_job_scheduled_on(job_b.id, 8) # label5 |
| 490 | self._assert_job_scheduled_on(job_b.id, 9) # label5 |
| 491 | self._check_for_extra_schedulings() |
| 492 | |
| 493 | # The three host label4 atomic group still has one host available. |
| 494 | # That means a job with a synch_count of 1 asking to be scheduled on |
| 495 | # the atomic group can still use the final machine. |
| 496 | # |
| 497 | # This may seem like a somewhat odd use case. It allows the use of an |
| 498 | # atomic group as a set of machines to run smaller jobs within (a set |
| 499 | # of hosts configured for use in network tests with eachother perhaps?) |
| 500 | onehost_job = self._create_job(atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 501 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 502 | self._assert_job_scheduled_on_number_of(onehost_job.id, (5, 6, 7), 1) |
| 503 | self._check_for_extra_schedulings() |
| 504 | |
| 505 | # No more atomic groups have hosts available, no more jobs should |
| 506 | # be scheduled. |
| 507 | self._create_job(atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 508 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 509 | self._check_for_extra_schedulings() |
| 510 | |
| 511 | |
| 512 | def test_atomic_group_scheduling_obeys_acls(self): |
| 513 | # Request scheduling on a specific atomic label but be denied by ACLs. |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 514 | self._do_query('DELETE FROM afe_acl_groups_hosts ' |
| 515 | 'WHERE host_id in (8,9)') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 516 | job = self._create_job(metahosts=[self.label5.id], atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 517 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 518 | self._check_for_extra_schedulings() |
| 519 | |
| 520 | |
| 521 | def test_atomic_group_scheduling_dependency_label_exclude(self): |
| 522 | # A dependency label that matches no hosts in the atomic group. |
| 523 | job_a = self._create_job(atomic_group=1) |
| 524 | job_a.dependency_labels.add(self.label3) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 525 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 526 | self._check_for_extra_schedulings() |
| 527 | |
| 528 | |
| 529 | def test_atomic_group_scheduling_metahost_dependency_label_exclude(self): |
| 530 | # A metahost and dependency label that excludes too many hosts. |
| 531 | job_b = self._create_job(synchronous=True, metahosts=[self.label4.id], |
| 532 | atomic_group=1) |
| 533 | job_b.dependency_labels.add(self.label7) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 534 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 535 | self._check_for_extra_schedulings() |
| 536 | |
| 537 | |
| 538 | def test_atomic_group_scheduling_dependency_label_match(self): |
| 539 | # A dependency label that exists on enough atomic group hosts in only |
| 540 | # one of the two atomic group labels. |
| 541 | job_c = self._create_job(synchronous=True, atomic_group=1) |
| 542 | job_c.dependency_labels.add(self.label7) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 543 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 544 | self._assert_job_scheduled_on_number_of(job_c.id, (8, 9), 2) |
| 545 | self._check_for_extra_schedulings() |
| 546 | |
| 547 | |
| 548 | def test_atomic_group_scheduling_no_metahost(self): |
| 549 | # Force it to schedule on the other group for a reliable test. |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 550 | self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=9') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 551 | # An atomic job without a metahost. |
| 552 | job = self._create_job(synchronous=True, atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 553 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 554 | self._assert_job_scheduled_on_number_of(job.id, (5, 6, 7), 2) |
| 555 | self._check_for_extra_schedulings() |
| 556 | |
| 557 | |
| 558 | def test_atomic_group_scheduling_partial_group(self): |
| 559 | # Make one host in labels[3] unavailable so that there are only two |
| 560 | # hosts left in the group. |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 561 | self._do_query('UPDATE afe_hosts SET status="Repair Failed" WHERE id=5') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 562 | job = self._create_job(synchronous=True, metahosts=[self.label4.id], |
| 563 | atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 564 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 565 | # Verify that it was scheduled on the 2 ready hosts in that group. |
| 566 | self._assert_job_scheduled_on(job.id, 6) |
| 567 | self._assert_job_scheduled_on(job.id, 7) |
| 568 | self._check_for_extra_schedulings() |
| 569 | |
| 570 | |
| 571 | def test_atomic_group_scheduling_not_enough_available(self): |
| 572 | # Mark some hosts in each atomic group label as not usable. |
| 573 | # One host running, another invalid in the first group label. |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 574 | self._do_query('UPDATE afe_hosts SET status="Running" WHERE id=5') |
| 575 | self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=6') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 576 | # One host invalid in the second group label. |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 577 | self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=9') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 578 | # Nothing to schedule when no group label has enough (2) good hosts.. |
| 579 | self._create_job(atomic_group=1, synchronous=True) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 580 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 581 | # There are not enough hosts in either atomic group, |
| 582 | # No more scheduling should occur. |
| 583 | self._check_for_extra_schedulings() |
| 584 | |
| 585 | # Now create an atomic job that has a synch count of 1. It should |
| 586 | # schedule on exactly one of the hosts. |
| 587 | onehost_job = self._create_job(atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 588 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 589 | self._assert_job_scheduled_on_number_of(onehost_job.id, (7, 8), 1) |
| 590 | |
| 591 | |
| 592 | def test_atomic_group_scheduling_no_valid_hosts(self): |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 593 | self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id in (8,9)') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 594 | self._create_job(synchronous=True, metahosts=[self.label5.id], |
| 595 | atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 596 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 597 | # no hosts in the selected group and label are valid. no schedulings. |
| 598 | self._check_for_extra_schedulings() |
| 599 | |
| 600 | |
| 601 | def test_atomic_group_scheduling_metahost_works(self): |
| 602 | # Test that atomic group scheduling also obeys metahosts. |
| 603 | self._create_job(metahosts=[0], atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 604 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 605 | # There are no atomic group hosts that also have that metahost. |
| 606 | self._check_for_extra_schedulings() |
| 607 | |
| 608 | job_b = self._create_job(metahosts=[self.label5.id], atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 609 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 610 | self._assert_job_scheduled_on(job_b.id, 8) |
| 611 | self._assert_job_scheduled_on(job_b.id, 9) |
| 612 | self._check_for_extra_schedulings() |
| 613 | |
| 614 | |
| 615 | def test_atomic_group_skips_ineligible_hosts(self): |
| 616 | # Test hosts marked ineligible for this job are not eligible. |
| 617 | # How would this ever happen anyways? |
| 618 | job = self._create_job(metahosts=[self.label4.id], atomic_group=1) |
| 619 | models.IneligibleHostQueue.objects.create(job=job, host_id=5) |
| 620 | models.IneligibleHostQueue.objects.create(job=job, host_id=6) |
| 621 | models.IneligibleHostQueue.objects.create(job=job, host_id=7) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 622 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 623 | # No scheduling should occur as all desired hosts were ineligible. |
| 624 | self._check_for_extra_schedulings() |
| 625 | |
| 626 | |
| 627 | def test_atomic_group_scheduling_fail(self): |
| 628 | # If synch_count is > the atomic group number of machines, the job |
| 629 | # should be aborted immediately. |
| 630 | model_job = self._create_job(synchronous=True, atomic_group=1) |
| 631 | model_job.synch_count = 4 |
| 632 | model_job.save() |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 633 | job = scheduler_models.Job(id=model_job.id) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 634 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 635 | self._check_for_extra_schedulings() |
| 636 | queue_entries = job.get_host_queue_entries() |
| 637 | self.assertEqual(1, len(queue_entries)) |
| 638 | self.assertEqual(queue_entries[0].status, |
| 639 | models.HostQueueEntry.Status.ABORTED) |
| 640 | |
| 641 | |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 642 | def test_atomic_group_no_labels_no_scheduling(self): |
| 643 | # Never schedule on atomic groups marked invalid. |
| 644 | job = self._create_job(metahosts=[self.label5.id], synchronous=True, |
| 645 | atomic_group=1) |
| 646 | # Deleting an atomic group via the frontend marks it invalid and |
| 647 | # removes all label references to the group. The job now references |
| 648 | # an invalid atomic group with no labels associated with it. |
| 649 | self.label5.atomic_group.invalid = True |
| 650 | self.label5.atomic_group.save() |
| 651 | self.label5.atomic_group = None |
| 652 | self.label5.save() |
| 653 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 654 | self._run_scheduler() |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 655 | self._check_for_extra_schedulings() |
| 656 | |
| 657 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 658 | def test_schedule_directly_on_atomic_group_host_fail(self): |
| 659 | # Scheduling a job directly on hosts in an atomic group must |
| 660 | # fail to avoid users inadvertently holding up the use of an |
| 661 | # entire atomic group by using the machines individually. |
| 662 | job = self._create_job(hosts=[5]) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 663 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 664 | self._check_for_extra_schedulings() |
| 665 | |
| 666 | |
| 667 | def test_schedule_directly_on_atomic_group_host(self): |
| 668 | # Scheduling a job directly on one host in an atomic group will |
| 669 | # work when the atomic group is listed on the HQE in addition |
| 670 | # to the host (assuming the sync count is 1). |
| 671 | job = self._create_job(hosts=[5], atomic_group=1) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 672 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 673 | self._assert_job_scheduled_on(job.id, 5) |
| 674 | self._check_for_extra_schedulings() |
| 675 | |
| 676 | |
| 677 | def test_schedule_directly_on_atomic_group_hosts_sync2(self): |
| 678 | job = self._create_job(hosts=[5,8], atomic_group=1, synchronous=True) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 679 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 680 | self._assert_job_scheduled_on(job.id, 5) |
| 681 | self._assert_job_scheduled_on(job.id, 8) |
| 682 | self._check_for_extra_schedulings() |
| 683 | |
| 684 | |
| 685 | def test_schedule_directly_on_atomic_group_hosts_wrong_group(self): |
| 686 | job = self._create_job(hosts=[5,8], atomic_group=2, synchronous=True) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 687 | self._run_scheduler() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 688 | self._check_for_extra_schedulings() |
| 689 | |
| 690 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 691 | def test_only_schedule_queued_entries(self): |
| 692 | self._create_job(metahosts=[1]) |
| 693 | self._update_hqe(set='active=1, host_id=2') |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 694 | self._run_scheduler() |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 695 | self._check_for_extra_schedulings() |
| 696 | |
| 697 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 698 | def test_no_ready_hosts(self): |
| 699 | self._create_job(hosts=[1]) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 700 | self._do_query('UPDATE afe_hosts SET status="Repair Failed"') |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 701 | self._run_scheduler() |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 702 | self._check_for_extra_schedulings() |
| 703 | |
| 704 | |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 705 | def test_garbage_collection(self): |
| 706 | self.god.stub_with(self._dispatcher, '_seconds_between_garbage_stats', |
| 707 | 999999) |
| 708 | self.god.stub_function(gc, 'collect') |
| 709 | self.god.stub_function(gc_stats, '_log_garbage_collector_stats') |
| 710 | gc.collect.expect_call().and_return(0) |
| 711 | gc_stats._log_garbage_collector_stats.expect_call() |
| 712 | # Force a garbage collection run |
| 713 | self._dispatcher._last_garbage_stats_time = 0 |
| 714 | self._dispatcher._garbage_collection() |
| 715 | # The previous call should have reset the time, it won't do anything |
| 716 | # the second time. If it does, we'll get an unexpected call. |
| 717 | self._dispatcher._garbage_collection() |
| 718 | |
| 719 | |
| 720 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 721 | class DispatcherThrottlingTest(BaseSchedulerTest): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 722 | """ |
| 723 | Test that the dispatcher throttles: |
| 724 | * total number of running processes |
| 725 | * number of processes started per cycle |
| 726 | """ |
| 727 | _MAX_RUNNING = 3 |
| 728 | _MAX_STARTED = 2 |
| 729 | |
| 730 | def setUp(self): |
| 731 | super(DispatcherThrottlingTest, self).setUp() |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 732 | scheduler_config.config.max_processes_per_drone = self._MAX_RUNNING |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 733 | scheduler_config.config.max_processes_started_per_cycle = ( |
| 734 | self._MAX_STARTED) |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 735 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 736 | def fake_max_runnable_processes(fake_self, username, |
| 737 | drone_hostnames_allowed): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 738 | running = sum(agent.task.num_processes |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 739 | for agent in self._agents |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 740 | if agent.started and not agent.is_done()) |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 741 | return self._MAX_RUNNING - running |
| 742 | self.god.stub_with(drone_manager.DroneManager, 'max_runnable_processes', |
| 743 | fake_max_runnable_processes) |
showard | 2fa5169 | 2009-01-13 23:48:08 +0000 | [diff] [blame] | 744 | |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 745 | |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 746 | def _setup_some_agents(self, num_agents): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 747 | self._agents = [DummyAgent() for i in xrange(num_agents)] |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 748 | self._dispatcher._agents = list(self._agents) |
| 749 | |
| 750 | |
| 751 | def _run_a_few_cycles(self): |
| 752 | for i in xrange(4): |
| 753 | self._dispatcher._handle_agents() |
| 754 | |
| 755 | |
| 756 | def _assert_agents_started(self, indexes, is_started=True): |
| 757 | for i in indexes: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 758 | self.assert_(self._agents[i].started == is_started, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 759 | 'Agent %d %sstarted' % |
| 760 | (i, is_started and 'not ' or '')) |
| 761 | |
| 762 | |
| 763 | def _assert_agents_not_started(self, indexes): |
| 764 | self._assert_agents_started(indexes, False) |
| 765 | |
| 766 | |
| 767 | def test_throttle_total(self): |
| 768 | self._setup_some_agents(4) |
| 769 | self._run_a_few_cycles() |
| 770 | self._assert_agents_started([0, 1, 2]) |
| 771 | self._assert_agents_not_started([3]) |
| 772 | |
| 773 | |
| 774 | def test_throttle_per_cycle(self): |
| 775 | self._setup_some_agents(3) |
| 776 | self._dispatcher._handle_agents() |
| 777 | self._assert_agents_started([0, 1]) |
| 778 | self._assert_agents_not_started([2]) |
| 779 | |
| 780 | |
| 781 | def test_throttle_with_synchronous(self): |
| 782 | self._setup_some_agents(2) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 783 | self._agents[0].task.num_processes = 3 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 784 | self._run_a_few_cycles() |
| 785 | self._assert_agents_started([0]) |
| 786 | self._assert_agents_not_started([1]) |
| 787 | |
| 788 | |
| 789 | def test_large_agent_starvation(self): |
| 790 | """ |
| 791 | Ensure large agents don't get starved by lower-priority agents. |
| 792 | """ |
| 793 | self._setup_some_agents(3) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 794 | self._agents[1].task.num_processes = 3 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 795 | self._run_a_few_cycles() |
| 796 | self._assert_agents_started([0]) |
| 797 | self._assert_agents_not_started([1, 2]) |
| 798 | |
| 799 | self._agents[0].set_done(True) |
| 800 | self._run_a_few_cycles() |
| 801 | self._assert_agents_started([1]) |
| 802 | self._assert_agents_not_started([2]) |
| 803 | |
| 804 | |
| 805 | def test_zero_process_agent(self): |
| 806 | self._setup_some_agents(5) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 807 | self._agents[4].task.num_processes = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 808 | self._run_a_few_cycles() |
| 809 | self._assert_agents_started([0, 1, 2, 4]) |
| 810 | self._assert_agents_not_started([3]) |
| 811 | |
| 812 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 813 | class PidfileRunMonitorTest(unittest.TestCase): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 814 | execution_tag = 'test_tag' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 815 | pid = 12345 |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 816 | process = drone_manager.Process('myhost', pid) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 817 | num_tests_failed = 1 |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 818 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 819 | def setUp(self): |
| 820 | self.god = mock.mock_god() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 821 | self.mock_drone_manager = self.god.create_mock_class( |
| 822 | drone_manager.DroneManager, 'drone_manager') |
| 823 | self.god.stub_with(monitor_db, '_drone_manager', |
| 824 | self.mock_drone_manager) |
| 825 | self.god.stub_function(email_manager.manager, 'enqueue_notify_email') |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 826 | self.god.stub_with(monitor_db, '_get_pidfile_timeout_secs', |
| 827 | self._mock_get_pidfile_timeout_secs) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 828 | |
| 829 | self.pidfile_id = object() |
| 830 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 831 | (self.mock_drone_manager.get_pidfile_id_from |
| 832 | .expect_call(self.execution_tag, |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 833 | pidfile_name=drone_manager.AUTOSERV_PID_FILE) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 834 | .and_return(self.pidfile_id)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 835 | |
| 836 | self.monitor = monitor_db.PidfileRunMonitor() |
| 837 | self.monitor.attach_to_existing_process(self.execution_tag) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 838 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 839 | def tearDown(self): |
| 840 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 841 | |
| 842 | |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 843 | def _mock_get_pidfile_timeout_secs(self): |
| 844 | return 300 |
| 845 | |
| 846 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 847 | def setup_pidfile(self, pid=None, exit_code=None, tests_failed=None, |
| 848 | use_second_read=False): |
| 849 | contents = drone_manager.PidfileContents() |
| 850 | if pid is not None: |
| 851 | contents.process = drone_manager.Process('myhost', pid) |
| 852 | contents.exit_status = exit_code |
| 853 | contents.num_tests_failed = tests_failed |
| 854 | self.mock_drone_manager.get_pidfile_contents.expect_call( |
| 855 | self.pidfile_id, use_second_read=use_second_read).and_return( |
| 856 | contents) |
| 857 | |
| 858 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 859 | def set_not_yet_run(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 860 | self.setup_pidfile() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 861 | |
| 862 | |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 863 | def set_empty_pidfile(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 864 | self.setup_pidfile() |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 865 | |
| 866 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 867 | def set_running(self, use_second_read=False): |
| 868 | self.setup_pidfile(self.pid, use_second_read=use_second_read) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 869 | |
| 870 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 871 | def set_complete(self, error_code, use_second_read=False): |
| 872 | self.setup_pidfile(self.pid, error_code, self.num_tests_failed, |
| 873 | use_second_read=use_second_read) |
| 874 | |
| 875 | |
| 876 | def _check_monitor(self, expected_pid, expected_exit_status, |
| 877 | expected_num_tests_failed): |
| 878 | if expected_pid is None: |
| 879 | self.assertEquals(self.monitor._state.process, None) |
| 880 | else: |
| 881 | self.assertEquals(self.monitor._state.process.pid, expected_pid) |
| 882 | self.assertEquals(self.monitor._state.exit_status, expected_exit_status) |
| 883 | self.assertEquals(self.monitor._state.num_tests_failed, |
| 884 | expected_num_tests_failed) |
| 885 | |
| 886 | |
| 887 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 888 | |
| 889 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 890 | def _test_read_pidfile_helper(self, expected_pid, expected_exit_status, |
| 891 | expected_num_tests_failed): |
| 892 | self.monitor._read_pidfile() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 893 | self._check_monitor(expected_pid, expected_exit_status, |
| 894 | expected_num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 895 | |
| 896 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 897 | def _get_expected_tests_failed(self, expected_exit_status): |
| 898 | if expected_exit_status is None: |
| 899 | expected_tests_failed = None |
| 900 | else: |
| 901 | expected_tests_failed = self.num_tests_failed |
| 902 | return expected_tests_failed |
| 903 | |
| 904 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 905 | def test_read_pidfile(self): |
| 906 | self.set_not_yet_run() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 907 | self._test_read_pidfile_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 908 | |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 909 | self.set_empty_pidfile() |
| 910 | self._test_read_pidfile_helper(None, None, None) |
| 911 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 912 | self.set_running() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 913 | self._test_read_pidfile_helper(self.pid, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 914 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 915 | self.set_complete(123) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 916 | self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 917 | |
| 918 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 919 | def test_read_pidfile_error(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 920 | self.mock_drone_manager.get_pidfile_contents.expect_call( |
| 921 | self.pidfile_id, use_second_read=False).and_return( |
| 922 | drone_manager.InvalidPidfile('error')) |
| 923 | self.assertRaises(monitor_db.PidfileRunMonitor._PidfileException, |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 924 | self.monitor._read_pidfile) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 925 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 926 | |
| 927 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 928 | def setup_is_running(self, is_running): |
| 929 | self.mock_drone_manager.is_process_running.expect_call( |
| 930 | self.process).and_return(is_running) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 931 | |
| 932 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 933 | def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status, |
| 934 | expected_num_tests_failed): |
| 935 | self.monitor._get_pidfile_info() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 936 | self._check_monitor(expected_pid, expected_exit_status, |
| 937 | expected_num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 938 | |
| 939 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 940 | def test_get_pidfile_info(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 941 | """ |
| 942 | normal cases for get_pidfile_info |
| 943 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 944 | # running |
| 945 | self.set_running() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 946 | self.setup_is_running(True) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 947 | self._test_get_pidfile_info_helper(self.pid, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 948 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 949 | # exited during check |
| 950 | self.set_running() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 951 | self.setup_is_running(False) |
| 952 | self.set_complete(123, use_second_read=True) # pidfile gets read again |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 953 | self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 954 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 955 | # completed |
| 956 | self.set_complete(123) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 957 | self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 958 | |
| 959 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 960 | def test_get_pidfile_info_running_no_proc(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 961 | """ |
| 962 | pidfile shows process running, but no proc exists |
| 963 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 964 | # running but no proc |
| 965 | self.set_running() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 966 | self.setup_is_running(False) |
| 967 | self.set_running(use_second_read=True) |
| 968 | email_manager.manager.enqueue_notify_email.expect_call( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 969 | mock.is_string_comparator(), mock.is_string_comparator()) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 970 | self._test_get_pidfile_info_helper(self.pid, 1, 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 971 | self.assertTrue(self.monitor.lost_process) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 972 | |
| 973 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 974 | def test_get_pidfile_info_not_yet_run(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 975 | """ |
| 976 | pidfile hasn't been written yet |
| 977 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 978 | self.set_not_yet_run() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 979 | self._test_get_pidfile_info_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 980 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 981 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 982 | def test_process_failed_to_write_pidfile(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 983 | self.set_not_yet_run() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 984 | email_manager.manager.enqueue_notify_email.expect_call( |
| 985 | mock.is_string_comparator(), mock.is_string_comparator()) |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 986 | self.monitor._start_time = (time.time() - |
| 987 | monitor_db._get_pidfile_timeout_secs() - 1) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 988 | self._test_get_pidfile_info_helper(None, 1, 0) |
| 989 | self.assertTrue(self.monitor.lost_process) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 990 | |
| 991 | |
| 992 | class AgentTest(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 993 | def setUp(self): |
| 994 | self.god = mock.mock_god() |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 995 | self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher, |
| 996 | 'dispatcher') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 997 | |
| 998 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 999 | def tearDown(self): |
| 1000 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1001 | |
| 1002 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1003 | def _create_mock_task(self, name): |
| 1004 | task = self.god.create_mock_class(monitor_db.AgentTask, name) |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1005 | task.num_processes = 1 |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1006 | _set_host_and_qe_ids(task) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1007 | return task |
| 1008 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1009 | def _create_agent(self, task): |
| 1010 | agent = monitor_db.Agent(task) |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1011 | agent.dispatcher = self._dispatcher |
| 1012 | return agent |
| 1013 | |
| 1014 | |
| 1015 | def _finish_agent(self, agent): |
| 1016 | while not agent.is_done(): |
| 1017 | agent.tick() |
| 1018 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1019 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1020 | def test_agent_abort(self): |
| 1021 | task = self._create_mock_task('task') |
| 1022 | task.poll.expect_call() |
| 1023 | task.is_done.expect_call().and_return(False) |
| 1024 | task.abort.expect_call() |
| 1025 | task.aborted = True |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1026 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1027 | agent = self._create_agent(task) |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1028 | agent.tick() |
| 1029 | agent.abort() |
| 1030 | self._finish_agent(agent) |
| 1031 | self.god.check_playback() |
| 1032 | |
| 1033 | |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1034 | def _test_agent_abort_before_started_helper(self, ignore_abort=False): |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1035 | task = self._create_mock_task('task') |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1036 | task.abort.expect_call() |
| 1037 | if ignore_abort: |
| 1038 | task.aborted = False |
| 1039 | task.poll.expect_call() |
| 1040 | task.is_done.expect_call().and_return(True) |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1041 | task.success = True |
| 1042 | else: |
| 1043 | task.aborted = True |
| 1044 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1045 | agent = self._create_agent(task) |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1046 | agent.abort() |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1047 | self._finish_agent(agent) |
| 1048 | self.god.check_playback() |
| 1049 | |
| 1050 | |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1051 | def test_agent_abort_before_started(self): |
| 1052 | self._test_agent_abort_before_started_helper() |
| 1053 | self._test_agent_abort_before_started_helper(True) |
| 1054 | |
| 1055 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1056 | class JobSchedulingTest(BaseSchedulerTest): |
showard | e58e3f8 | 2008-11-20 19:04:59 +0000 | [diff] [blame] | 1057 | def _test_run_helper(self, expect_agent=True, expect_starting=False, |
| 1058 | expect_pending=False): |
| 1059 | if expect_starting: |
| 1060 | expected_status = models.HostQueueEntry.Status.STARTING |
| 1061 | elif expect_pending: |
| 1062 | expected_status = models.HostQueueEntry.Status.PENDING |
| 1063 | else: |
| 1064 | expected_status = models.HostQueueEntry.Status.VERIFYING |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1065 | job = scheduler_models.Job.fetch('id = 1')[0] |
| 1066 | queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1067 | assert queue_entry.job is job |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1068 | job.run_if_ready(queue_entry) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1069 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1070 | self.god.check_playback() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1071 | |
| 1072 | self._dispatcher._schedule_delay_tasks() |
| 1073 | self._dispatcher._schedule_running_host_queue_entries() |
| 1074 | agent = self._dispatcher._agents[0] |
| 1075 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1076 | actual_status = models.HostQueueEntry.smart_get(1).status |
| 1077 | self.assertEquals(expected_status, actual_status) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1078 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1079 | if not expect_agent: |
| 1080 | self.assertEquals(agent, None) |
| 1081 | return |
| 1082 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1083 | self.assert_(isinstance(agent, monitor_db.Agent)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1084 | self.assert_(agent.task) |
| 1085 | return agent.task |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 1086 | |
| 1087 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1088 | def test_run_if_ready_delays(self): |
| 1089 | # Also tests Job.run_with_ready_delay() on atomic group jobs. |
| 1090 | django_job = self._create_job(hosts=[5, 6], atomic_group=1) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1091 | job = scheduler_models.Job(django_job.id) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1092 | self.assertEqual(1, job.synch_count) |
| 1093 | django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id)) |
| 1094 | self.assertEqual(2, len(django_hqes)) |
| 1095 | self.assertEqual(2, django_hqes[0].atomic_group.max_number_of_machines) |
| 1096 | |
| 1097 | def set_hqe_status(django_hqe, status): |
| 1098 | django_hqe.status = status |
| 1099 | django_hqe.save() |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1100 | scheduler_models.HostQueueEntry(django_hqe.id).host.set_status(status) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1101 | |
| 1102 | # An initial state, our synch_count is 1 |
| 1103 | set_hqe_status(django_hqes[0], models.HostQueueEntry.Status.VERIFYING) |
| 1104 | set_hqe_status(django_hqes[1], models.HostQueueEntry.Status.PENDING) |
| 1105 | |
| 1106 | # So that we don't depend on the config file value during the test. |
| 1107 | self.assert_(scheduler_config.config |
| 1108 | .secs_to_wait_for_atomic_group_hosts is not None) |
| 1109 | self.god.stub_with(scheduler_config.config, |
| 1110 | 'secs_to_wait_for_atomic_group_hosts', 123456) |
| 1111 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1112 | # Get the pending one as a scheduler_models.HostQueueEntry object. |
| 1113 | hqe = scheduler_models.HostQueueEntry(django_hqes[1].id) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1114 | self.assert_(not job._delay_ready_task) |
| 1115 | self.assertTrue(job.is_ready()) |
| 1116 | |
| 1117 | # Ready with one pending, one verifying and an atomic group should |
| 1118 | # result in a DelayCallTask to re-check if we're ready a while later. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1119 | job.run_if_ready(hqe) |
| 1120 | self.assertEquals('Waiting', hqe.status) |
| 1121 | self._dispatcher._schedule_delay_tasks() |
| 1122 | self.assertEquals('Pending', hqe.status) |
| 1123 | agent = self._dispatcher._agents[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1124 | self.assert_(job._delay_ready_task) |
| 1125 | self.assert_(isinstance(agent, monitor_db.Agent)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1126 | self.assert_(agent.task) |
| 1127 | delay_task = agent.task |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1128 | self.assert_(isinstance(delay_task, scheduler_models.DelayedCallTask)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1129 | self.assert_(not delay_task.is_done()) |
| 1130 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1131 | self.god.stub_function(delay_task, 'abort') |
| 1132 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1133 | self.god.stub_function(job, 'run') |
| 1134 | |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1135 | self.god.stub_function(job, '_pending_count') |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame] | 1136 | self.god.stub_with(job, 'synch_count', 9) |
| 1137 | self.god.stub_function(job, 'request_abort') |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1138 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1139 | # Test that the DelayedCallTask's callback queued up above does the |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1140 | # correct thing and does not call run if there are not enough hosts |
| 1141 | # in pending after the delay. |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1142 | job._pending_count.expect_call().and_return(0) |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame] | 1143 | job.request_abort.expect_call() |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1144 | delay_task._callback() |
| 1145 | self.god.check_playback() |
| 1146 | |
| 1147 | # Test that the DelayedCallTask's callback queued up above does the |
| 1148 | # correct thing and returns the Agent returned by job.run() if |
| 1149 | # there are still enough hosts pending after the delay. |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame] | 1150 | job.synch_count = 4 |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1151 | job._pending_count.expect_call().and_return(4) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1152 | job.run.expect_call(hqe) |
| 1153 | delay_task._callback() |
| 1154 | self.god.check_playback() |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1155 | |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1156 | job._pending_count.expect_call().and_return(4) |
| 1157 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1158 | # Adjust the delay deadline so that enough time has passed. |
| 1159 | job._delay_ready_task.end_time = time.time() - 111111 |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1160 | job.run.expect_call(hqe) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1161 | # ...the delay_expired condition should cause us to call run() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1162 | self._dispatcher._handle_agents() |
| 1163 | self.god.check_playback() |
| 1164 | delay_task.success = False |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1165 | |
| 1166 | # Adjust the delay deadline back so that enough time has not passed. |
| 1167 | job._delay_ready_task.end_time = time.time() + 111111 |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1168 | self._dispatcher._handle_agents() |
| 1169 | self.god.check_playback() |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1170 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1171 | # Now max_number_of_machines HQEs are in pending state. Remaining |
| 1172 | # delay will now be ignored. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1173 | other_hqe = scheduler_models.HostQueueEntry(django_hqes[0].id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1174 | self.god.unstub(job, 'run') |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1175 | self.god.unstub(job, '_pending_count') |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame] | 1176 | self.god.unstub(job, 'synch_count') |
| 1177 | self.god.unstub(job, 'request_abort') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1178 | # ...the over_max_threshold test should cause us to call run() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1179 | delay_task.abort.expect_call() |
| 1180 | other_hqe.on_pending() |
| 1181 | self.assertEquals('Starting', other_hqe.status) |
| 1182 | self.assertEquals('Starting', hqe.status) |
| 1183 | self.god.stub_function(job, 'run') |
| 1184 | self.god.unstub(delay_task, 'abort') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1185 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1186 | hqe.set_status('Pending') |
| 1187 | other_hqe.set_status('Pending') |
showard | 708b352 | 2009-08-20 23:26:15 +0000 | [diff] [blame] | 1188 | # Now we're not over the max for the atomic group. But all assigned |
| 1189 | # hosts are in pending state. over_max_threshold should make us run(). |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1190 | hqe.atomic_group.max_number_of_machines += 1 |
| 1191 | hqe.atomic_group.save() |
| 1192 | job.run.expect_call(hqe) |
| 1193 | hqe.on_pending() |
| 1194 | self.god.check_playback() |
| 1195 | hqe.atomic_group.max_number_of_machines -= 1 |
| 1196 | hqe.atomic_group.save() |
showard | 708b352 | 2009-08-20 23:26:15 +0000 | [diff] [blame] | 1197 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1198 | other_hqe = scheduler_models.HostQueueEntry(django_hqes[0].id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1199 | self.assertTrue(hqe.job is other_hqe.job) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1200 | # DBObject classes should reuse instances so these should be the same. |
| 1201 | self.assertEqual(job, other_hqe.job) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1202 | self.assertEqual(other_hqe.job, hqe.job) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1203 | # Be sure our delay was not lost during the other_hqe construction. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1204 | self.assertEqual(job._delay_ready_task, delay_task) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1205 | self.assert_(job._delay_ready_task) |
| 1206 | self.assertFalse(job._delay_ready_task.is_done()) |
| 1207 | self.assertFalse(job._delay_ready_task.aborted) |
| 1208 | |
| 1209 | # We want the real run() to be called below. |
| 1210 | self.god.unstub(job, 'run') |
| 1211 | |
| 1212 | # We pass in the other HQE this time the same way it would happen |
| 1213 | # for real when one host finishes verifying and enters pending. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1214 | job.run_if_ready(other_hqe) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1215 | |
| 1216 | # The delayed task must be aborted by the actual run() call above. |
| 1217 | self.assertTrue(job._delay_ready_task.aborted) |
| 1218 | self.assertFalse(job._delay_ready_task.success) |
| 1219 | self.assertTrue(job._delay_ready_task.is_done()) |
| 1220 | |
| 1221 | # Check that job run() and _finish_run() were called by the above: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1222 | self._dispatcher._schedule_running_host_queue_entries() |
| 1223 | agent = self._dispatcher._agents[0] |
| 1224 | self.assert_(agent.task) |
| 1225 | task = agent.task |
| 1226 | self.assert_(isinstance(task, monitor_db.QueueTask)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1227 | # Requery these hqes in order to verify the status from the DB. |
| 1228 | django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id)) |
| 1229 | for entry in django_hqes: |
| 1230 | self.assertEqual(models.HostQueueEntry.Status.STARTING, |
| 1231 | entry.status) |
| 1232 | |
| 1233 | # We're already running, but more calls to run_with_ready_delay can |
| 1234 | # continue to come in due to straggler hosts enter Pending. Make |
| 1235 | # sure we don't do anything. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1236 | self.god.stub_function(job, 'run') |
| 1237 | job.run_with_ready_delay(hqe) |
| 1238 | self.god.check_playback() |
| 1239 | self.god.unstub(job, 'run') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1240 | |
| 1241 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1242 | def test_run_synchronous_atomic_group_ready(self): |
| 1243 | self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True) |
| 1244 | self._update_hqe("status='Pending', execution_subdir=''") |
| 1245 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1246 | queue_task = self._test_run_helper(expect_starting=True) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1247 | |
| 1248 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1249 | # Atomic group jobs that do not depend on a specific label in the |
| 1250 | # atomic group will use the atomic group name as their group name. |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1251 | self.assertEquals(queue_task.queue_entries[0].get_group_name(), |
| 1252 | 'atomic1') |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1253 | |
| 1254 | |
| 1255 | def test_run_synchronous_atomic_group_with_label_ready(self): |
| 1256 | job = self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True) |
| 1257 | job.dependency_labels.add(self.label4) |
| 1258 | self._update_hqe("status='Pending', execution_subdir=''") |
| 1259 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1260 | queue_task = self._test_run_helper(expect_starting=True) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1261 | |
| 1262 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
| 1263 | # Atomic group jobs that also specify a label in the atomic group |
| 1264 | # will use the label name as their group name. |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1265 | self.assertEquals(queue_task.queue_entries[0].get_group_name(), |
| 1266 | 'label4') |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1267 | |
| 1268 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1269 | def test_run_synchronous_ready(self): |
| 1270 | self._create_job(hosts=[1, 2], synchronous=True) |
| 1271 | self._update_hqe("status='Pending', execution_subdir=''") |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1272 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1273 | queue_task = self._test_run_helper(expect_starting=True) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1274 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1275 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
| 1276 | self.assertEquals(queue_task.job.id, 1) |
| 1277 | hqe_ids = [hqe.id for hqe in queue_task.queue_entries] |
| 1278 | self.assertEquals(hqe_ids, [1, 2]) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1279 | |
| 1280 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1281 | def test_schedule_running_host_queue_entries_fail(self): |
| 1282 | self._create_job(hosts=[2]) |
| 1283 | self._update_hqe("status='%s', execution_subdir=''" % |
| 1284 | models.HostQueueEntry.Status.PENDING) |
| 1285 | job = scheduler_models.Job.fetch('id = 1')[0] |
| 1286 | queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0] |
| 1287 | assert queue_entry.job is job |
| 1288 | job.run_if_ready(queue_entry) |
| 1289 | self.assertEqual(queue_entry.status, |
| 1290 | models.HostQueueEntry.Status.STARTING) |
| 1291 | self.assert_(queue_entry.execution_subdir) |
| 1292 | self.god.check_playback() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1293 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1294 | class dummy_test_agent(object): |
| 1295 | task = 'dummy_test_agent' |
| 1296 | self._dispatcher._register_agent_for_ids( |
| 1297 | self._dispatcher._host_agents, [queue_entry.host.id], |
| 1298 | dummy_test_agent) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1299 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1300 | # Attempted to schedule on a host that already has an agent. |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 1301 | self.assertRaises(host_scheduler.SchedulerError, |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1302 | self._dispatcher._schedule_running_host_queue_entries) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1303 | |
| 1304 | |
jamesren | 47bd737 | 2010-03-13 00:58:17 +0000 | [diff] [blame] | 1305 | def test_schedule_hostless_job(self): |
| 1306 | job = self._create_job(hostless=True) |
| 1307 | self.assertEqual(1, job.hostqueueentry_set.count()) |
| 1308 | hqe_query = scheduler_models.HostQueueEntry.fetch( |
| 1309 | 'id = %s' % job.hostqueueentry_set.all()[0].id) |
| 1310 | self.assertEqual(1, len(hqe_query)) |
| 1311 | hqe = hqe_query[0] |
| 1312 | |
| 1313 | self.assertEqual(models.HostQueueEntry.Status.QUEUED, hqe.status) |
| 1314 | self.assertEqual(0, len(self._dispatcher._agents)) |
| 1315 | |
| 1316 | self._dispatcher._schedule_new_jobs() |
| 1317 | |
| 1318 | self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status) |
| 1319 | self.assertEqual(1, len(self._dispatcher._agents)) |
| 1320 | |
| 1321 | self._dispatcher._schedule_new_jobs() |
| 1322 | |
| 1323 | # No change to previously schedule hostless job, and no additional agent |
| 1324 | self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status) |
| 1325 | self.assertEqual(1, len(self._dispatcher._agents)) |
| 1326 | |
| 1327 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1328 | class TopLevelFunctionsTest(unittest.TestCase): |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 1329 | def setUp(self): |
| 1330 | self.god = mock.mock_god() |
| 1331 | |
| 1332 | |
| 1333 | def tearDown(self): |
| 1334 | self.god.unstub_all() |
| 1335 | |
| 1336 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1337 | def test_autoserv_command_line(self): |
| 1338 | machines = 'abcd12,efgh34' |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1339 | extra_args = ['-Z', 'hello'] |
showard | f65b740 | 2009-12-18 22:44:35 +0000 | [diff] [blame] | 1340 | expected_command_line_base = set((monitor_db._autoserv_path, '-p', |
| 1341 | '-m', machines, '-r', |
| 1342 | drone_manager.WORKING_DIRECTORY)) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1343 | |
showard | f65b740 | 2009-12-18 22:44:35 +0000 | [diff] [blame] | 1344 | expected_command_line = expected_command_line_base.union( |
| 1345 | ['--verbose']).union(extra_args) |
| 1346 | command_line = set( |
| 1347 | monitor_db._autoserv_command_line(machines, extra_args)) |
| 1348 | self.assertEqual(expected_command_line, command_line) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1349 | |
| 1350 | class FakeJob(object): |
| 1351 | owner = 'Bob' |
| 1352 | name = 'fake job name' |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 1353 | id = 1337 |
| 1354 | |
| 1355 | class FakeHQE(object): |
| 1356 | job = FakeJob |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1357 | |
showard | f65b740 | 2009-12-18 22:44:35 +0000 | [diff] [blame] | 1358 | expected_command_line = expected_command_line_base.union( |
| 1359 | ['-u', FakeJob.owner, '-l', FakeJob.name]) |
| 1360 | command_line = set(monitor_db._autoserv_command_line( |
| 1361 | machines, extra_args=[], queue_entry=FakeHQE, verbose=False)) |
| 1362 | self.assertEqual(expected_command_line, command_line) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1363 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1364 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1365 | class AgentTaskTest(unittest.TestCase, |
| 1366 | frontend_test_utils.FrontendTestMixin): |
| 1367 | def setUp(self): |
| 1368 | self._frontend_common_setup() |
| 1369 | |
| 1370 | |
| 1371 | def tearDown(self): |
| 1372 | self._frontend_common_teardown() |
| 1373 | |
| 1374 | |
| 1375 | def _setup_drones(self): |
| 1376 | self.god.stub_function(models.DroneSet, 'drone_sets_enabled') |
| 1377 | models.DroneSet.drone_sets_enabled.expect_call().and_return(True) |
| 1378 | |
| 1379 | drones = [] |
| 1380 | for x in xrange(4): |
| 1381 | drones.append(models.Drone.objects.create(hostname=str(x))) |
| 1382 | |
| 1383 | drone_set_1 = models.DroneSet.objects.create(name='1') |
| 1384 | drone_set_1.drones.add(*drones[0:2]) |
| 1385 | drone_set_2 = models.DroneSet.objects.create(name='2') |
| 1386 | drone_set_2.drones.add(*drones[2:4]) |
| 1387 | drone_set_3 = models.DroneSet.objects.create(name='3') |
| 1388 | |
| 1389 | job_1 = self._create_job_simple([self.hosts[0].id], |
| 1390 | drone_set=drone_set_1) |
| 1391 | job_2 = self._create_job_simple([self.hosts[0].id], |
| 1392 | drone_set=drone_set_2) |
| 1393 | job_3 = self._create_job_simple([self.hosts[0].id], |
| 1394 | drone_set=drone_set_3) |
| 1395 | |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1396 | job_4 = self._create_job_simple([self.hosts[0].id]) |
| 1397 | job_4.drone_set = None |
| 1398 | job_4.save() |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1399 | |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1400 | hqe_1 = job_1.hostqueueentry_set.all()[0] |
| 1401 | hqe_2 = job_2.hostqueueentry_set.all()[0] |
| 1402 | hqe_3 = job_3.hostqueueentry_set.all()[0] |
| 1403 | hqe_4 = job_4.hostqueueentry_set.all()[0] |
| 1404 | |
| 1405 | return (hqe_1, hqe_2, hqe_3, hqe_4), monitor_db.AgentTask() |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1406 | |
| 1407 | |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1408 | def test_get_drone_hostnames_allowed_no_drones_in_set(self): |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1409 | hqes, task = self._setup_drones() |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1410 | task.queue_entry_ids = (hqes[2].id,) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1411 | self.assertEqual(set(), task.get_drone_hostnames_allowed()) |
| 1412 | self.god.check_playback() |
| 1413 | |
| 1414 | |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1415 | def test_get_drone_hostnames_allowed_no_drone_set(self): |
| 1416 | hqes, task = self._setup_drones() |
| 1417 | hqe = hqes[3] |
| 1418 | task.queue_entry_ids = (hqe.id,) |
| 1419 | |
| 1420 | result = object() |
| 1421 | |
| 1422 | self.god.stub_function(task, '_user_or_global_default_drone_set') |
| 1423 | task._user_or_global_default_drone_set.expect_call( |
| 1424 | hqe.job, hqe.job.user()).and_return(result) |
| 1425 | |
| 1426 | self.assertEqual(result, task.get_drone_hostnames_allowed()) |
| 1427 | self.god.check_playback() |
| 1428 | |
| 1429 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1430 | def test_get_drone_hostnames_allowed_success(self): |
| 1431 | hqes, task = self._setup_drones() |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1432 | task.queue_entry_ids = (hqes[0].id,) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1433 | self.assertEqual(set(('0','1')), task.get_drone_hostnames_allowed()) |
| 1434 | self.god.check_playback() |
| 1435 | |
| 1436 | |
| 1437 | def test_get_drone_hostnames_allowed_multiple_jobs(self): |
| 1438 | hqes, task = self._setup_drones() |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1439 | task.queue_entry_ids = (hqes[0].id, hqes[1].id) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1440 | self.assertRaises(AssertionError, |
| 1441 | task.get_drone_hostnames_allowed) |
| 1442 | self.god.check_playback() |
| 1443 | |
| 1444 | |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1445 | def test_get_drone_hostnames_allowed_no_hqe(self): |
| 1446 | class MockSpecialTask(object): |
| 1447 | requested_by = object() |
| 1448 | |
| 1449 | class MockSpecialAgentTask(monitor_db.SpecialAgentTask): |
| 1450 | task = MockSpecialTask() |
| 1451 | queue_entry_ids = [] |
| 1452 | def __init__(self, *args, **kwargs): |
| 1453 | pass |
| 1454 | |
| 1455 | task = MockSpecialAgentTask() |
| 1456 | self.god.stub_function(models.DroneSet, 'drone_sets_enabled') |
| 1457 | self.god.stub_function(task, '_user_or_global_default_drone_set') |
| 1458 | |
| 1459 | result = object() |
| 1460 | models.DroneSet.drone_sets_enabled.expect_call().and_return(True) |
| 1461 | task._user_or_global_default_drone_set.expect_call( |
| 1462 | task.task, MockSpecialTask.requested_by).and_return(result) |
| 1463 | |
| 1464 | self.assertEqual(result, task.get_drone_hostnames_allowed()) |
| 1465 | self.god.check_playback() |
| 1466 | |
| 1467 | |
| 1468 | def _setup_test_user_or_global_default_drone_set(self): |
| 1469 | result = object() |
| 1470 | class MockDroneSet(object): |
| 1471 | def get_drone_hostnames(self): |
| 1472 | return result |
| 1473 | |
| 1474 | self.god.stub_function(models.DroneSet, 'get_default') |
| 1475 | models.DroneSet.get_default.expect_call().and_return(MockDroneSet()) |
| 1476 | return result |
| 1477 | |
| 1478 | |
| 1479 | def test_user_or_global_default_drone_set(self): |
| 1480 | expected = object() |
| 1481 | class MockDroneSet(object): |
| 1482 | def get_drone_hostnames(self): |
| 1483 | return expected |
| 1484 | class MockUser(object): |
| 1485 | drone_set = MockDroneSet() |
| 1486 | |
| 1487 | self._setup_test_user_or_global_default_drone_set() |
| 1488 | |
| 1489 | actual = monitor_db.AgentTask()._user_or_global_default_drone_set( |
| 1490 | None, MockUser()) |
| 1491 | |
| 1492 | self.assertEqual(expected, actual) |
| 1493 | self.god.check_playback() |
| 1494 | |
| 1495 | |
| 1496 | def test_user_or_global_default_drone_set_no_user(self): |
| 1497 | expected = self._setup_test_user_or_global_default_drone_set() |
| 1498 | actual = monitor_db.AgentTask()._user_or_global_default_drone_set( |
| 1499 | None, None) |
| 1500 | |
| 1501 | self.assertEqual(expected, actual) |
| 1502 | self.god.check_playback() |
| 1503 | |
| 1504 | |
| 1505 | def test_user_or_global_default_drone_set_no_user_drone_set(self): |
| 1506 | class MockUser(object): |
| 1507 | drone_set = None |
| 1508 | login = None |
| 1509 | |
| 1510 | expected = self._setup_test_user_or_global_default_drone_set() |
| 1511 | actual = monitor_db.AgentTask()._user_or_global_default_drone_set( |
| 1512 | None, MockUser()) |
| 1513 | |
| 1514 | self.assertEqual(expected, actual) |
| 1515 | self.god.check_playback() |
| 1516 | |
| 1517 | |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 1518 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1519 | unittest.main() |