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