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