blob: cc655736fce616d0fe293d7339a28b421164ce34 [file] [log] [blame]
showardce38e0c2008-05-29 19:36:16 +00001#!/usr/bin/python
2
jamesrenc44ae992010-02-19 00:12:54 +00003import gc, logging, time
showardce38e0c2008-05-29 19:36:16 +00004import common
showard364fe862008-10-17 02:01:16 +00005from autotest_lib.frontend import setup_django_environment
showardb6d16622009-05-26 19:35:29 +00006from autotest_lib.frontend.afe import frontend_test_utils
jadmanski3d161b02008-06-06 15:43:36 +00007from autotest_lib.client.common_lib.test_utils import mock
showardf13a9e22009-12-18 22:54:09 +00008from autotest_lib.client.common_lib.test_utils import unittest
jamesrenc44ae992010-02-19 00:12:54 +00009from autotest_lib.database import database_connection
showardb1e51872008-10-07 11:08:18 +000010from autotest_lib.frontend.afe import models
showard170873e2009-01-07 00:22:26 +000011from autotest_lib.scheduler import monitor_db, drone_manager, email_manager
Dale Curtisaa513362011-03-01 17:27:44 -080012from autotest_lib.scheduler import scheduler_config, gc_stats, host_scheduler
showard78f5b012009-12-23 00:05:59 +000013from autotest_lib.scheduler import monitor_db_functional_test
jamesrenc44ae992010-02-19 00:12:54 +000014from autotest_lib.scheduler import scheduler_models
showardce38e0c2008-05-29 19:36:16 +000015
16_DEBUG = False
17
showarda3c58572009-03-12 20:36:59 +000018
showard9bb960b2009-11-19 01:02:11 +000019class DummyAgentTask(object):
showardd1195652009-12-08 22:21:02 +000020 num_processes = 1
21 owner_username = 'my_user'
showard9bb960b2009-11-19 01:02:11 +000022
jamesren76fcf192010-04-21 20:39:50 +000023 def get_drone_hostnames_allowed(self):
24 return None
25
showard9bb960b2009-11-19 01:02:11 +000026
showard170873e2009-01-07 00:22:26 +000027class DummyAgent(object):
showard8cc058f2009-09-08 16:26:33 +000028 started = False
showard170873e2009-01-07 00:22:26 +000029 _is_done = False
showardd1195652009-12-08 22:21:02 +000030 host_ids = ()
31 queue_entry_ids = ()
32
33 def __init__(self):
34 self.task = DummyAgentTask()
showard170873e2009-01-07 00:22:26 +000035
showard170873e2009-01-07 00:22:26 +000036
37 def tick(self):
showard8cc058f2009-09-08 16:26:33 +000038 self.started = True
showard170873e2009-01-07 00:22:26 +000039
40
41 def is_done(self):
42 return self._is_done
43
44
45 def set_done(self, done):
46 self._is_done = done
showard04c82c52008-05-29 19:38:12 +000047
showard56193bb2008-08-13 20:07:41 +000048
49class IsRow(mock.argument_comparator):
50 def __init__(self, row_id):
51 self.row_id = row_id
showardce38e0c2008-05-29 19:36:16 +000052
53
showard56193bb2008-08-13 20:07:41 +000054 def is_satisfied_by(self, parameter):
55 return list(parameter)[0] == self.row_id
56
57
58 def __str__(self):
59 return 'row with id %s' % self.row_id
60
61
showardd3dc1992009-04-22 21:01:40 +000062class IsAgentWithTask(mock.argument_comparator):
mbligh1ef218d2009-08-03 16:57:56 +000063 def __init__(self, task):
64 self._task = task
showardd3dc1992009-04-22 21:01:40 +000065
66
mbligh1ef218d2009-08-03 16:57:56 +000067 def is_satisfied_by(self, parameter):
68 if not isinstance(parameter, monitor_db.Agent):
69 return False
70 tasks = list(parameter.queue.queue)
71 if len(tasks) != 1:
72 return False
73 return tasks[0] == self._task
showardd3dc1992009-04-22 21:01:40 +000074
75
showard6b733412009-04-27 20:09:18 +000076def _set_host_and_qe_ids(agent_or_task, id_list=None):
77 if id_list is None:
78 id_list = []
79 agent_or_task.host_ids = agent_or_task.queue_entry_ids = id_list
80
81
showardb6d16622009-05-26 19:35:29 +000082class BaseSchedulerTest(unittest.TestCase,
83 frontend_test_utils.FrontendTestMixin):
showard50c0e712008-09-22 16:20:37 +000084 _config_section = 'AUTOTEST_WEB'
showardce38e0c2008-05-29 19:36:16 +000085
jadmanski0afbb632008-06-06 21:10:57 +000086 def _do_query(self, sql):
showardb1e51872008-10-07 11:08:18 +000087 self._database.execute(sql)
showardce38e0c2008-05-29 19:36:16 +000088
89
showardb6d16622009-05-26 19:35:29 +000090 def _set_monitor_stubs(self):
91 # Clear the instance cache as this is a brand new database.
jamesrenc44ae992010-02-19 00:12:54 +000092 scheduler_models.DBObject._clear_instance_cache()
showardce38e0c2008-05-29 19:36:16 +000093
showardb1e51872008-10-07 11:08:18 +000094 self._database = (
showard78f5b012009-12-23 00:05:59 +000095 database_connection.TranslatingDatabase.get_test_database(
96 translators=monitor_db_functional_test._DB_TRANSLATORS))
97 self._database.connect(db_type='django')
showardb1e51872008-10-07 11:08:18 +000098 self._database.debug = _DEBUG
showardce38e0c2008-05-29 19:36:16 +000099
showard78f5b012009-12-23 00:05:59 +0000100 self.god.stub_with(monitor_db, '_db', self._database)
jamesrenc44ae992010-02-19 00:12:54 +0000101 self.god.stub_with(scheduler_models, '_db', self._database)
102 self.god.stub_with(drone_manager.instance(), '_results_dir',
showard78f5b012009-12-23 00:05:59 +0000103 '/test/path')
jamesrenc44ae992010-02-19 00:12:54 +0000104 self.god.stub_with(drone_manager.instance(), '_temporary_directory',
showard78f5b012009-12-23 00:05:59 +0000105 '/test/path/tmp')
showard56193bb2008-08-13 20:07:41 +0000106
jamesrenc44ae992010-02-19 00:12:54 +0000107 monitor_db.initialize_globals()
108 scheduler_models.initialize_globals()
109
showard56193bb2008-08-13 20:07:41 +0000110
showard56193bb2008-08-13 20:07:41 +0000111 def setUp(self):
showardb6d16622009-05-26 19:35:29 +0000112 self._frontend_common_setup()
showard56193bb2008-08-13 20:07:41 +0000113 self._set_monitor_stubs()
114 self._dispatcher = monitor_db.Dispatcher()
showardce38e0c2008-05-29 19:36:16 +0000115
116
showard56193bb2008-08-13 20:07:41 +0000117 def tearDown(self):
showardb6d16622009-05-26 19:35:29 +0000118 self._database.disconnect()
119 self._frontend_common_teardown()
showardce38e0c2008-05-29 19:36:16 +0000120
121
showard56193bb2008-08-13 20:07:41 +0000122 def _update_hqe(self, set, where=''):
showardeab66ce2009-12-23 00:03:56 +0000123 query = 'UPDATE afe_host_queue_entries SET ' + set
showard56193bb2008-08-13 20:07:41 +0000124 if where:
125 query += ' WHERE ' + where
126 self._do_query(query)
127
128
showardb2e2c322008-10-14 17:33:55 +0000129class DispatcherSchedulingTest(BaseSchedulerTest):
showard56193bb2008-08-13 20:07:41 +0000130 _jobs_scheduled = []
131
showard89f84db2009-03-12 20:39:13 +0000132
133 def tearDown(self):
134 super(DispatcherSchedulingTest, self).tearDown()
135
136
showard56193bb2008-08-13 20:07:41 +0000137 def _set_monitor_stubs(self):
138 super(DispatcherSchedulingTest, self)._set_monitor_stubs()
showard89f84db2009-03-12 20:39:13 +0000139
showard8cc058f2009-09-08 16:26:33 +0000140 def hqe__do_schedule_pre_job_tasks_stub(queue_entry):
141 """Called by HostQueueEntry.run()."""
showard77182562009-06-10 00:16:05 +0000142 self._record_job_scheduled(queue_entry.job.id, queue_entry.host.id)
showard89f84db2009-03-12 20:39:13 +0000143 queue_entry.set_status('Starting')
showard89f84db2009-03-12 20:39:13 +0000144
jamesrenc44ae992010-02-19 00:12:54 +0000145 self.god.stub_with(scheduler_models.HostQueueEntry,
showard8cc058f2009-09-08 16:26:33 +0000146 '_do_schedule_pre_job_tasks',
147 hqe__do_schedule_pre_job_tasks_stub)
showard89f84db2009-03-12 20:39:13 +0000148
149 def hqe_queue_log_record_stub(self, log_line):
150 """No-Op to avoid calls down to the _drone_manager during tests."""
151
jamesrenc44ae992010-02-19 00:12:54 +0000152 self.god.stub_with(scheduler_models.HostQueueEntry, 'queue_log_record',
showard89f84db2009-03-12 20:39:13 +0000153 hqe_queue_log_record_stub)
showard56193bb2008-08-13 20:07:41 +0000154
155
156 def _record_job_scheduled(self, job_id, host_id):
157 record = (job_id, host_id)
158 self.assert_(record not in self._jobs_scheduled,
159 'Job %d scheduled on host %d twice' %
160 (job_id, host_id))
161 self._jobs_scheduled.append(record)
162
163
164 def _assert_job_scheduled_on(self, job_id, host_id):
165 record = (job_id, host_id)
166 self.assert_(record in self._jobs_scheduled,
167 'Job %d not scheduled on host %d as expected\n'
168 'Jobs scheduled: %s' %
169 (job_id, host_id, self._jobs_scheduled))
170 self._jobs_scheduled.remove(record)
171
172
showard89f84db2009-03-12 20:39:13 +0000173 def _assert_job_scheduled_on_number_of(self, job_id, host_ids, number):
174 """Assert job was scheduled on exactly number hosts out of a set."""
175 found = []
176 for host_id in host_ids:
177 record = (job_id, host_id)
178 if record in self._jobs_scheduled:
179 found.append(record)
180 self._jobs_scheduled.remove(record)
181 if len(found) < number:
182 self.fail('Job %d scheduled on fewer than %d hosts in %s.\n'
183 'Jobs scheduled: %s' % (job_id, number, host_ids, found))
184 elif len(found) > number:
185 self.fail('Job %d scheduled on more than %d hosts in %s.\n'
186 'Jobs scheduled: %s' % (job_id, number, host_ids, found))
187
188
showard56193bb2008-08-13 20:07:41 +0000189 def _check_for_extra_schedulings(self):
190 if len(self._jobs_scheduled) != 0:
191 self.fail('Extra jobs scheduled: ' +
192 str(self._jobs_scheduled))
193
194
jadmanski0afbb632008-06-06 21:10:57 +0000195 def _convert_jobs_to_metahosts(self, *job_ids):
196 sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')'
showardeab66ce2009-12-23 00:03:56 +0000197 self._do_query('UPDATE afe_host_queue_entries SET '
jadmanski0afbb632008-06-06 21:10:57 +0000198 'meta_host=host_id, host_id=NULL '
199 'WHERE job_id IN ' + sql_tuple)
showardce38e0c2008-05-29 19:36:16 +0000200
201
jadmanski0afbb632008-06-06 21:10:57 +0000202 def _lock_host(self, host_id):
showardeab66ce2009-12-23 00:03:56 +0000203 self._do_query('UPDATE afe_hosts SET locked=1 WHERE id=' +
jadmanski0afbb632008-06-06 21:10:57 +0000204 str(host_id))
showardce38e0c2008-05-29 19:36:16 +0000205
206
jadmanski0afbb632008-06-06 21:10:57 +0000207 def setUp(self):
showard56193bb2008-08-13 20:07:41 +0000208 super(DispatcherSchedulingTest, self).setUp()
jadmanski0afbb632008-06-06 21:10:57 +0000209 self._jobs_scheduled = []
showardce38e0c2008-05-29 19:36:16 +0000210
211
jamesren883492a2010-02-12 00:45:18 +0000212 def _run_scheduler(self):
213 for _ in xrange(2): # metahost scheduling can take two cycles
214 self._dispatcher._schedule_new_jobs()
215
216
jadmanski0afbb632008-06-06 21:10:57 +0000217 def _test_basic_scheduling_helper(self, use_metahosts):
218 'Basic nonmetahost scheduling'
219 self._create_job_simple([1], use_metahosts)
220 self._create_job_simple([2], use_metahosts)
jamesren883492a2010-02-12 00:45:18 +0000221 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000222 self._assert_job_scheduled_on(1, 1)
223 self._assert_job_scheduled_on(2, 2)
224 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000225
226
jadmanski0afbb632008-06-06 21:10:57 +0000227 def _test_priorities_helper(self, use_metahosts):
228 'Test prioritization ordering'
229 self._create_job_simple([1], use_metahosts)
230 self._create_job_simple([2], use_metahosts)
231 self._create_job_simple([1,2], use_metahosts)
232 self._create_job_simple([1], use_metahosts, priority=1)
jamesren883492a2010-02-12 00:45:18 +0000233 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000234 self._assert_job_scheduled_on(4, 1) # higher priority
235 self._assert_job_scheduled_on(2, 2) # earlier job over later
236 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000237
238
jadmanski0afbb632008-06-06 21:10:57 +0000239 def _test_hosts_ready_helper(self, use_metahosts):
240 """
241 Only hosts that are status=Ready, unlocked and not invalid get
242 scheduled.
243 """
244 self._create_job_simple([1], use_metahosts)
showardeab66ce2009-12-23 00:03:56 +0000245 self._do_query('UPDATE afe_hosts SET status="Running" WHERE id=1')
jamesren883492a2010-02-12 00:45:18 +0000246 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000247 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000248
showardeab66ce2009-12-23 00:03:56 +0000249 self._do_query('UPDATE afe_hosts SET status="Ready", locked=1 '
jadmanski0afbb632008-06-06 21:10:57 +0000250 'WHERE id=1')
jamesren883492a2010-02-12 00:45:18 +0000251 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000252 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000253
showardeab66ce2009-12-23 00:03:56 +0000254 self._do_query('UPDATE afe_hosts SET locked=0, invalid=1 '
jadmanski0afbb632008-06-06 21:10:57 +0000255 'WHERE id=1')
jamesren883492a2010-02-12 00:45:18 +0000256 self._run_scheduler()
showard5df2b192008-07-03 19:51:57 +0000257 if not use_metahosts:
258 self._assert_job_scheduled_on(1, 1)
jadmanski0afbb632008-06-06 21:10:57 +0000259 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000260
261
jadmanski0afbb632008-06-06 21:10:57 +0000262 def _test_hosts_idle_helper(self, use_metahosts):
263 'Only idle hosts get scheduled'
showard2bab8f42008-11-12 18:15:22 +0000264 self._create_job(hosts=[1], active=True)
jadmanski0afbb632008-06-06 21:10:57 +0000265 self._create_job_simple([1], use_metahosts)
jamesren883492a2010-02-12 00:45:18 +0000266 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000267 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000268
269
showard63a34772008-08-18 19:32:50 +0000270 def _test_obey_ACLs_helper(self, use_metahosts):
showardeab66ce2009-12-23 00:03:56 +0000271 self._do_query('DELETE FROM afe_acl_groups_hosts WHERE host_id=1')
showard63a34772008-08-18 19:32:50 +0000272 self._create_job_simple([1], use_metahosts)
jamesren883492a2010-02-12 00:45:18 +0000273 self._run_scheduler()
showard63a34772008-08-18 19:32:50 +0000274 self._check_for_extra_schedulings()
275
276
jadmanski0afbb632008-06-06 21:10:57 +0000277 def test_basic_scheduling(self):
278 self._test_basic_scheduling_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000279
280
jadmanski0afbb632008-06-06 21:10:57 +0000281 def test_priorities(self):
282 self._test_priorities_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000283
284
jadmanski0afbb632008-06-06 21:10:57 +0000285 def test_hosts_ready(self):
286 self._test_hosts_ready_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000287
288
jadmanski0afbb632008-06-06 21:10:57 +0000289 def test_hosts_idle(self):
290 self._test_hosts_idle_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000291
292
showard63a34772008-08-18 19:32:50 +0000293 def test_obey_ACLs(self):
294 self._test_obey_ACLs_helper(False)
295
296
showard2924b0a2009-06-18 23:16:15 +0000297 def test_one_time_hosts_ignore_ACLs(self):
showardeab66ce2009-12-23 00:03:56 +0000298 self._do_query('DELETE FROM afe_acl_groups_hosts WHERE host_id=1')
299 self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=1')
showard2924b0a2009-06-18 23:16:15 +0000300 self._create_job_simple([1])
jamesren883492a2010-02-12 00:45:18 +0000301 self._run_scheduler()
showard2924b0a2009-06-18 23:16:15 +0000302 self._assert_job_scheduled_on(1, 1)
303 self._check_for_extra_schedulings()
304
305
showard63a34772008-08-18 19:32:50 +0000306 def test_non_metahost_on_invalid_host(self):
307 """
308 Non-metahost entries can get scheduled on invalid hosts (this is how
309 one-time hosts work).
310 """
showardeab66ce2009-12-23 00:03:56 +0000311 self._do_query('UPDATE afe_hosts SET invalid=1')
showard63a34772008-08-18 19:32:50 +0000312 self._test_basic_scheduling_helper(False)
313
314
jadmanski0afbb632008-06-06 21:10:57 +0000315 def test_metahost_scheduling(self):
showard63a34772008-08-18 19:32:50 +0000316 """
317 Basic metahost scheduling
318 """
jadmanski0afbb632008-06-06 21:10:57 +0000319 self._test_basic_scheduling_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000320
321
jadmanski0afbb632008-06-06 21:10:57 +0000322 def test_metahost_priorities(self):
323 self._test_priorities_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000324
325
jadmanski0afbb632008-06-06 21:10:57 +0000326 def test_metahost_hosts_ready(self):
327 self._test_hosts_ready_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000328
329
jadmanski0afbb632008-06-06 21:10:57 +0000330 def test_metahost_hosts_idle(self):
331 self._test_hosts_idle_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000332
333
showard63a34772008-08-18 19:32:50 +0000334 def test_metahost_obey_ACLs(self):
335 self._test_obey_ACLs_helper(True)
336
337
showard89f84db2009-03-12 20:39:13 +0000338 def _setup_test_only_if_needed_labels(self):
showardade14e22009-01-26 22:38:32 +0000339 # apply only_if_needed label3 to host1
showard89f84db2009-03-12 20:39:13 +0000340 models.Host.smart_get('host1').labels.add(self.label3)
341 return self._create_job_simple([1], use_metahost=True)
showardade14e22009-01-26 22:38:32 +0000342
showard89f84db2009-03-12 20:39:13 +0000343
344 def test_only_if_needed_labels_avoids_host(self):
345 job = self._setup_test_only_if_needed_labels()
showardade14e22009-01-26 22:38:32 +0000346 # if the job doesn't depend on label3, there should be no scheduling
jamesren883492a2010-02-12 00:45:18 +0000347 self._run_scheduler()
showardade14e22009-01-26 22:38:32 +0000348 self._check_for_extra_schedulings()
349
showard89f84db2009-03-12 20:39:13 +0000350
351 def test_only_if_needed_labels_schedules(self):
352 job = self._setup_test_only_if_needed_labels()
353 job.dependency_labels.add(self.label3)
jamesren883492a2010-02-12 00:45:18 +0000354 self._run_scheduler()
showardade14e22009-01-26 22:38:32 +0000355 self._assert_job_scheduled_on(1, 1)
356 self._check_for_extra_schedulings()
357
showard89f84db2009-03-12 20:39:13 +0000358
359 def test_only_if_needed_labels_via_metahost(self):
360 job = self._setup_test_only_if_needed_labels()
361 job.dependency_labels.add(self.label3)
showardade14e22009-01-26 22:38:32 +0000362 # should also work if the metahost is the only_if_needed label
showardeab66ce2009-12-23 00:03:56 +0000363 self._do_query('DELETE FROM afe_jobs_dependency_labels')
showardade14e22009-01-26 22:38:32 +0000364 self._create_job(metahosts=[3])
jamesren883492a2010-02-12 00:45:18 +0000365 self._run_scheduler()
showardade14e22009-01-26 22:38:32 +0000366 self._assert_job_scheduled_on(2, 1)
367 self._check_for_extra_schedulings()
showard989f25d2008-10-01 11:38:11 +0000368
369
jadmanski0afbb632008-06-06 21:10:57 +0000370 def test_nonmetahost_over_metahost(self):
371 """
372 Non-metahost entries should take priority over metahost entries
373 for the same host
374 """
375 self._create_job(metahosts=[1])
376 self._create_job(hosts=[1])
jamesren883492a2010-02-12 00:45:18 +0000377 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000378 self._assert_job_scheduled_on(2, 1)
379 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000380
381
jadmanski0afbb632008-06-06 21:10:57 +0000382 def test_metahosts_obey_blocks(self):
383 """
384 Metahosts can't get scheduled on hosts already scheduled for
385 that job.
386 """
387 self._create_job(metahosts=[1], hosts=[1])
388 # make the nonmetahost entry complete, so the metahost can try
389 # to get scheduled
showard56193bb2008-08-13 20:07:41 +0000390 self._update_hqe(set='complete = 1', where='host_id=1')
jamesren883492a2010-02-12 00:45:18 +0000391 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000392 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000393
394
showard89f84db2009-03-12 20:39:13 +0000395 # TODO(gps): These should probably live in their own TestCase class
396 # specific to testing HostScheduler methods directly. It was convenient
397 # to put it here for now to share existing test environment setup code.
398 def test_HostScheduler_check_atomic_group_labels(self):
399 normal_job = self._create_job(metahosts=[0])
400 atomic_job = self._create_job(atomic_group=1)
401 # Indirectly initialize the internal state of the host scheduler.
402 self._dispatcher._refresh_pending_queue_entries()
403
jamesrenc44ae992010-02-19 00:12:54 +0000404 atomic_hqe = scheduler_models.HostQueueEntry.fetch(where='job_id=%d' %
showard8cc058f2009-09-08 16:26:33 +0000405 atomic_job.id)[0]
jamesrenc44ae992010-02-19 00:12:54 +0000406 normal_hqe = scheduler_models.HostQueueEntry.fetch(where='job_id=%d' %
showard8cc058f2009-09-08 16:26:33 +0000407 normal_job.id)[0]
showard89f84db2009-03-12 20:39:13 +0000408
409 host_scheduler = self._dispatcher._host_scheduler
410 self.assertTrue(host_scheduler._check_atomic_group_labels(
411 [self.label4.id], atomic_hqe))
412 self.assertFalse(host_scheduler._check_atomic_group_labels(
413 [self.label4.id], normal_hqe))
414 self.assertFalse(host_scheduler._check_atomic_group_labels(
415 [self.label5.id, self.label6.id, self.label7.id], normal_hqe))
416 self.assertTrue(host_scheduler._check_atomic_group_labels(
417 [self.label4.id, self.label6.id], atomic_hqe))
showard6157c632009-07-06 20:19:31 +0000418 self.assertTrue(host_scheduler._check_atomic_group_labels(
419 [self.label4.id, self.label5.id],
420 atomic_hqe))
showard89f84db2009-03-12 20:39:13 +0000421
422
423 def test_HostScheduler_get_host_atomic_group_id(self):
showard6157c632009-07-06 20:19:31 +0000424 job = self._create_job(metahosts=[self.label6.id])
jamesrenc44ae992010-02-19 00:12:54 +0000425 queue_entry = scheduler_models.HostQueueEntry.fetch(
showard8cc058f2009-09-08 16:26:33 +0000426 where='job_id=%d' % job.id)[0]
showard89f84db2009-03-12 20:39:13 +0000427 # Indirectly initialize the internal state of the host scheduler.
428 self._dispatcher._refresh_pending_queue_entries()
429
430 # Test the host scheduler
431 host_scheduler = self._dispatcher._host_scheduler
showard6157c632009-07-06 20:19:31 +0000432
433 # Two labels each in a different atomic group. This should log an
434 # error and continue.
435 orig_logging_error = logging.error
436 def mock_logging_error(message, *args):
437 mock_logging_error._num_calls += 1
438 # Test the logging call itself, we just wrapped it to count it.
439 orig_logging_error(message, *args)
440 mock_logging_error._num_calls = 0
441 self.god.stub_with(logging, 'error', mock_logging_error)
442 self.assertNotEquals(None, host_scheduler._get_host_atomic_group_id(
443 [self.label4.id, self.label8.id], queue_entry))
444 self.assertTrue(mock_logging_error._num_calls > 0)
445 self.god.unstub(logging, 'error')
446
447 # Two labels both in the same atomic group, this should not raise an
448 # error, it will merely cause the job to schedule on the intersection.
449 self.assertEquals(1, host_scheduler._get_host_atomic_group_id(
450 [self.label4.id, self.label5.id]))
451
452 self.assertEquals(None, host_scheduler._get_host_atomic_group_id([]))
453 self.assertEquals(None, host_scheduler._get_host_atomic_group_id(
showard89f84db2009-03-12 20:39:13 +0000454 [self.label3.id, self.label7.id, self.label6.id]))
showard6157c632009-07-06 20:19:31 +0000455 self.assertEquals(1, host_scheduler._get_host_atomic_group_id(
showard89f84db2009-03-12 20:39:13 +0000456 [self.label4.id, self.label7.id, self.label6.id]))
showard6157c632009-07-06 20:19:31 +0000457 self.assertEquals(1, host_scheduler._get_host_atomic_group_id(
showard89f84db2009-03-12 20:39:13 +0000458 [self.label7.id, self.label5.id]))
459
460
461 def test_atomic_group_hosts_blocked_from_non_atomic_jobs(self):
462 # Create a job scheduled to run on label6.
463 self._create_job(metahosts=[self.label6.id])
jamesren883492a2010-02-12 00:45:18 +0000464 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000465 # label6 only has hosts that are in atomic groups associated with it,
466 # there should be no scheduling.
467 self._check_for_extra_schedulings()
468
469
470 def test_atomic_group_hosts_blocked_from_non_atomic_jobs_explicit(self):
471 # Create a job scheduled to run on label5. This is an atomic group
472 # label but this job does not request atomic group scheduling.
473 self._create_job(metahosts=[self.label5.id])
jamesren883492a2010-02-12 00:45:18 +0000474 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000475 # label6 only has hosts that are in atomic groups associated with it,
476 # there should be no scheduling.
477 self._check_for_extra_schedulings()
478
479
480 def test_atomic_group_scheduling_basics(self):
481 # Create jobs scheduled to run on an atomic group.
482 job_a = self._create_job(synchronous=True, metahosts=[self.label4.id],
483 atomic_group=1)
484 job_b = self._create_job(synchronous=True, metahosts=[self.label5.id],
485 atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000486 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000487 # atomic_group.max_number_of_machines was 2 so we should run on 2.
488 self._assert_job_scheduled_on_number_of(job_a.id, (5, 6, 7), 2)
489 self._assert_job_scheduled_on(job_b.id, 8) # label5
490 self._assert_job_scheduled_on(job_b.id, 9) # label5
491 self._check_for_extra_schedulings()
492
493 # The three host label4 atomic group still has one host available.
494 # That means a job with a synch_count of 1 asking to be scheduled on
495 # the atomic group can still use the final machine.
496 #
497 # This may seem like a somewhat odd use case. It allows the use of an
498 # atomic group as a set of machines to run smaller jobs within (a set
499 # of hosts configured for use in network tests with eachother perhaps?)
500 onehost_job = self._create_job(atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000501 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000502 self._assert_job_scheduled_on_number_of(onehost_job.id, (5, 6, 7), 1)
503 self._check_for_extra_schedulings()
504
505 # No more atomic groups have hosts available, no more jobs should
506 # be scheduled.
507 self._create_job(atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000508 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000509 self._check_for_extra_schedulings()
510
511
512 def test_atomic_group_scheduling_obeys_acls(self):
513 # Request scheduling on a specific atomic label but be denied by ACLs.
showardeab66ce2009-12-23 00:03:56 +0000514 self._do_query('DELETE FROM afe_acl_groups_hosts '
515 'WHERE host_id in (8,9)')
showard89f84db2009-03-12 20:39:13 +0000516 job = self._create_job(metahosts=[self.label5.id], atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000517 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000518 self._check_for_extra_schedulings()
519
520
521 def test_atomic_group_scheduling_dependency_label_exclude(self):
522 # A dependency label that matches no hosts in the atomic group.
523 job_a = self._create_job(atomic_group=1)
524 job_a.dependency_labels.add(self.label3)
jamesren883492a2010-02-12 00:45:18 +0000525 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000526 self._check_for_extra_schedulings()
527
528
529 def test_atomic_group_scheduling_metahost_dependency_label_exclude(self):
530 # A metahost and dependency label that excludes too many hosts.
531 job_b = self._create_job(synchronous=True, metahosts=[self.label4.id],
532 atomic_group=1)
533 job_b.dependency_labels.add(self.label7)
jamesren883492a2010-02-12 00:45:18 +0000534 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000535 self._check_for_extra_schedulings()
536
537
538 def test_atomic_group_scheduling_dependency_label_match(self):
539 # A dependency label that exists on enough atomic group hosts in only
540 # one of the two atomic group labels.
541 job_c = self._create_job(synchronous=True, atomic_group=1)
542 job_c.dependency_labels.add(self.label7)
jamesren883492a2010-02-12 00:45:18 +0000543 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000544 self._assert_job_scheduled_on_number_of(job_c.id, (8, 9), 2)
545 self._check_for_extra_schedulings()
546
547
548 def test_atomic_group_scheduling_no_metahost(self):
549 # Force it to schedule on the other group for a reliable test.
showardeab66ce2009-12-23 00:03:56 +0000550 self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=9')
showard89f84db2009-03-12 20:39:13 +0000551 # An atomic job without a metahost.
552 job = self._create_job(synchronous=True, atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000553 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000554 self._assert_job_scheduled_on_number_of(job.id, (5, 6, 7), 2)
555 self._check_for_extra_schedulings()
556
557
558 def test_atomic_group_scheduling_partial_group(self):
559 # Make one host in labels[3] unavailable so that there are only two
560 # hosts left in the group.
showardeab66ce2009-12-23 00:03:56 +0000561 self._do_query('UPDATE afe_hosts SET status="Repair Failed" WHERE id=5')
showard89f84db2009-03-12 20:39:13 +0000562 job = self._create_job(synchronous=True, metahosts=[self.label4.id],
563 atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000564 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000565 # Verify that it was scheduled on the 2 ready hosts in that group.
566 self._assert_job_scheduled_on(job.id, 6)
567 self._assert_job_scheduled_on(job.id, 7)
568 self._check_for_extra_schedulings()
569
570
571 def test_atomic_group_scheduling_not_enough_available(self):
572 # Mark some hosts in each atomic group label as not usable.
573 # One host running, another invalid in the first group label.
showardeab66ce2009-12-23 00:03:56 +0000574 self._do_query('UPDATE afe_hosts SET status="Running" WHERE id=5')
575 self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=6')
showard89f84db2009-03-12 20:39:13 +0000576 # One host invalid in the second group label.
showardeab66ce2009-12-23 00:03:56 +0000577 self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id=9')
showard89f84db2009-03-12 20:39:13 +0000578 # Nothing to schedule when no group label has enough (2) good hosts..
579 self._create_job(atomic_group=1, synchronous=True)
jamesren883492a2010-02-12 00:45:18 +0000580 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000581 # There are not enough hosts in either atomic group,
582 # No more scheduling should occur.
583 self._check_for_extra_schedulings()
584
585 # Now create an atomic job that has a synch count of 1. It should
586 # schedule on exactly one of the hosts.
587 onehost_job = self._create_job(atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000588 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000589 self._assert_job_scheduled_on_number_of(onehost_job.id, (7, 8), 1)
590
591
592 def test_atomic_group_scheduling_no_valid_hosts(self):
showardeab66ce2009-12-23 00:03:56 +0000593 self._do_query('UPDATE afe_hosts SET invalid=1 WHERE id in (8,9)')
showard89f84db2009-03-12 20:39:13 +0000594 self._create_job(synchronous=True, metahosts=[self.label5.id],
595 atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000596 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000597 # no hosts in the selected group and label are valid. no schedulings.
598 self._check_for_extra_schedulings()
599
600
601 def test_atomic_group_scheduling_metahost_works(self):
602 # Test that atomic group scheduling also obeys metahosts.
603 self._create_job(metahosts=[0], atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000604 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000605 # There are no atomic group hosts that also have that metahost.
606 self._check_for_extra_schedulings()
607
608 job_b = self._create_job(metahosts=[self.label5.id], atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000609 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000610 self._assert_job_scheduled_on(job_b.id, 8)
611 self._assert_job_scheduled_on(job_b.id, 9)
612 self._check_for_extra_schedulings()
613
614
615 def test_atomic_group_skips_ineligible_hosts(self):
616 # Test hosts marked ineligible for this job are not eligible.
617 # How would this ever happen anyways?
618 job = self._create_job(metahosts=[self.label4.id], atomic_group=1)
619 models.IneligibleHostQueue.objects.create(job=job, host_id=5)
620 models.IneligibleHostQueue.objects.create(job=job, host_id=6)
621 models.IneligibleHostQueue.objects.create(job=job, host_id=7)
jamesren883492a2010-02-12 00:45:18 +0000622 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000623 # No scheduling should occur as all desired hosts were ineligible.
624 self._check_for_extra_schedulings()
625
626
627 def test_atomic_group_scheduling_fail(self):
628 # If synch_count is > the atomic group number of machines, the job
629 # should be aborted immediately.
630 model_job = self._create_job(synchronous=True, atomic_group=1)
631 model_job.synch_count = 4
632 model_job.save()
jamesrenc44ae992010-02-19 00:12:54 +0000633 job = scheduler_models.Job(id=model_job.id)
jamesren883492a2010-02-12 00:45:18 +0000634 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000635 self._check_for_extra_schedulings()
636 queue_entries = job.get_host_queue_entries()
637 self.assertEqual(1, len(queue_entries))
638 self.assertEqual(queue_entries[0].status,
639 models.HostQueueEntry.Status.ABORTED)
640
641
showard205fd602009-03-21 00:17:35 +0000642 def test_atomic_group_no_labels_no_scheduling(self):
643 # Never schedule on atomic groups marked invalid.
644 job = self._create_job(metahosts=[self.label5.id], synchronous=True,
645 atomic_group=1)
646 # Deleting an atomic group via the frontend marks it invalid and
647 # removes all label references to the group. The job now references
648 # an invalid atomic group with no labels associated with it.
649 self.label5.atomic_group.invalid = True
650 self.label5.atomic_group.save()
651 self.label5.atomic_group = None
652 self.label5.save()
653
jamesren883492a2010-02-12 00:45:18 +0000654 self._run_scheduler()
showard205fd602009-03-21 00:17:35 +0000655 self._check_for_extra_schedulings()
656
657
showard89f84db2009-03-12 20:39:13 +0000658 def test_schedule_directly_on_atomic_group_host_fail(self):
659 # Scheduling a job directly on hosts in an atomic group must
660 # fail to avoid users inadvertently holding up the use of an
661 # entire atomic group by using the machines individually.
662 job = self._create_job(hosts=[5])
jamesren883492a2010-02-12 00:45:18 +0000663 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000664 self._check_for_extra_schedulings()
665
666
667 def test_schedule_directly_on_atomic_group_host(self):
668 # Scheduling a job directly on one host in an atomic group will
669 # work when the atomic group is listed on the HQE in addition
670 # to the host (assuming the sync count is 1).
671 job = self._create_job(hosts=[5], atomic_group=1)
jamesren883492a2010-02-12 00:45:18 +0000672 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000673 self._assert_job_scheduled_on(job.id, 5)
674 self._check_for_extra_schedulings()
675
676
677 def test_schedule_directly_on_atomic_group_hosts_sync2(self):
678 job = self._create_job(hosts=[5,8], atomic_group=1, synchronous=True)
jamesren883492a2010-02-12 00:45:18 +0000679 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000680 self._assert_job_scheduled_on(job.id, 5)
681 self._assert_job_scheduled_on(job.id, 8)
682 self._check_for_extra_schedulings()
683
684
685 def test_schedule_directly_on_atomic_group_hosts_wrong_group(self):
686 job = self._create_job(hosts=[5,8], atomic_group=2, synchronous=True)
jamesren883492a2010-02-12 00:45:18 +0000687 self._run_scheduler()
showard89f84db2009-03-12 20:39:13 +0000688 self._check_for_extra_schedulings()
689
690
showard56193bb2008-08-13 20:07:41 +0000691 def test_only_schedule_queued_entries(self):
692 self._create_job(metahosts=[1])
693 self._update_hqe(set='active=1, host_id=2')
jamesren883492a2010-02-12 00:45:18 +0000694 self._run_scheduler()
showard56193bb2008-08-13 20:07:41 +0000695 self._check_for_extra_schedulings()
696
697
showardfa8629c2008-11-04 16:51:23 +0000698 def test_no_ready_hosts(self):
699 self._create_job(hosts=[1])
showardeab66ce2009-12-23 00:03:56 +0000700 self._do_query('UPDATE afe_hosts SET status="Repair Failed"')
jamesren883492a2010-02-12 00:45:18 +0000701 self._run_scheduler()
showardfa8629c2008-11-04 16:51:23 +0000702 self._check_for_extra_schedulings()
703
704
showardf13a9e22009-12-18 22:54:09 +0000705 def test_garbage_collection(self):
706 self.god.stub_with(self._dispatcher, '_seconds_between_garbage_stats',
707 999999)
708 self.god.stub_function(gc, 'collect')
709 self.god.stub_function(gc_stats, '_log_garbage_collector_stats')
710 gc.collect.expect_call().and_return(0)
711 gc_stats._log_garbage_collector_stats.expect_call()
712 # Force a garbage collection run
713 self._dispatcher._last_garbage_stats_time = 0
714 self._dispatcher._garbage_collection()
715 # The previous call should have reset the time, it won't do anything
716 # the second time. If it does, we'll get an unexpected call.
717 self._dispatcher._garbage_collection()
718
719
720
showardb2e2c322008-10-14 17:33:55 +0000721class DispatcherThrottlingTest(BaseSchedulerTest):
showard4c5374f2008-09-04 17:02:56 +0000722 """
723 Test that the dispatcher throttles:
724 * total number of running processes
725 * number of processes started per cycle
726 """
727 _MAX_RUNNING = 3
728 _MAX_STARTED = 2
729
730 def setUp(self):
731 super(DispatcherThrottlingTest, self).setUp()
showard324bf812009-01-20 23:23:38 +0000732 scheduler_config.config.max_processes_per_drone = self._MAX_RUNNING
showardd1ee1dd2009-01-07 21:33:08 +0000733 scheduler_config.config.max_processes_started_per_cycle = (
734 self._MAX_STARTED)
showard4c5374f2008-09-04 17:02:56 +0000735
jamesren76fcf192010-04-21 20:39:50 +0000736 def fake_max_runnable_processes(fake_self, username,
737 drone_hostnames_allowed):
showardd1195652009-12-08 22:21:02 +0000738 running = sum(agent.task.num_processes
showard324bf812009-01-20 23:23:38 +0000739 for agent in self._agents
showard8cc058f2009-09-08 16:26:33 +0000740 if agent.started and not agent.is_done())
showard324bf812009-01-20 23:23:38 +0000741 return self._MAX_RUNNING - running
742 self.god.stub_with(drone_manager.DroneManager, 'max_runnable_processes',
743 fake_max_runnable_processes)
showard2fa51692009-01-13 23:48:08 +0000744
showard4c5374f2008-09-04 17:02:56 +0000745
showard4c5374f2008-09-04 17:02:56 +0000746 def _setup_some_agents(self, num_agents):
showard170873e2009-01-07 00:22:26 +0000747 self._agents = [DummyAgent() for i in xrange(num_agents)]
showard4c5374f2008-09-04 17:02:56 +0000748 self._dispatcher._agents = list(self._agents)
749
750
751 def _run_a_few_cycles(self):
752 for i in xrange(4):
753 self._dispatcher._handle_agents()
754
755
756 def _assert_agents_started(self, indexes, is_started=True):
757 for i in indexes:
showard8cc058f2009-09-08 16:26:33 +0000758 self.assert_(self._agents[i].started == is_started,
showard4c5374f2008-09-04 17:02:56 +0000759 'Agent %d %sstarted' %
760 (i, is_started and 'not ' or ''))
761
762
763 def _assert_agents_not_started(self, indexes):
764 self._assert_agents_started(indexes, False)
765
766
767 def test_throttle_total(self):
768 self._setup_some_agents(4)
769 self._run_a_few_cycles()
770 self._assert_agents_started([0, 1, 2])
771 self._assert_agents_not_started([3])
772
773
774 def test_throttle_per_cycle(self):
775 self._setup_some_agents(3)
776 self._dispatcher._handle_agents()
777 self._assert_agents_started([0, 1])
778 self._assert_agents_not_started([2])
779
780
781 def test_throttle_with_synchronous(self):
782 self._setup_some_agents(2)
showardd1195652009-12-08 22:21:02 +0000783 self._agents[0].task.num_processes = 3
showard4c5374f2008-09-04 17:02:56 +0000784 self._run_a_few_cycles()
785 self._assert_agents_started([0])
786 self._assert_agents_not_started([1])
787
788
789 def test_large_agent_starvation(self):
790 """
791 Ensure large agents don't get starved by lower-priority agents.
792 """
793 self._setup_some_agents(3)
showardd1195652009-12-08 22:21:02 +0000794 self._agents[1].task.num_processes = 3
showard4c5374f2008-09-04 17:02:56 +0000795 self._run_a_few_cycles()
796 self._assert_agents_started([0])
797 self._assert_agents_not_started([1, 2])
798
799 self._agents[0].set_done(True)
800 self._run_a_few_cycles()
801 self._assert_agents_started([1])
802 self._assert_agents_not_started([2])
803
804
805 def test_zero_process_agent(self):
806 self._setup_some_agents(5)
showardd1195652009-12-08 22:21:02 +0000807 self._agents[4].task.num_processes = 0
showard4c5374f2008-09-04 17:02:56 +0000808 self._run_a_few_cycles()
809 self._assert_agents_started([0, 1, 2, 4])
810 self._assert_agents_not_started([3])
811
812
jadmanski3d161b02008-06-06 15:43:36 +0000813class PidfileRunMonitorTest(unittest.TestCase):
showard170873e2009-01-07 00:22:26 +0000814 execution_tag = 'test_tag'
jadmanski0afbb632008-06-06 21:10:57 +0000815 pid = 12345
showard170873e2009-01-07 00:22:26 +0000816 process = drone_manager.Process('myhost', pid)
showard21baa452008-10-21 00:08:39 +0000817 num_tests_failed = 1
jadmanski3d161b02008-06-06 15:43:36 +0000818
jadmanski0afbb632008-06-06 21:10:57 +0000819 def setUp(self):
820 self.god = mock.mock_god()
showard170873e2009-01-07 00:22:26 +0000821 self.mock_drone_manager = self.god.create_mock_class(
822 drone_manager.DroneManager, 'drone_manager')
823 self.god.stub_with(monitor_db, '_drone_manager',
824 self.mock_drone_manager)
825 self.god.stub_function(email_manager.manager, 'enqueue_notify_email')
showardec6a3b92009-09-25 20:29:13 +0000826 self.god.stub_with(monitor_db, '_get_pidfile_timeout_secs',
827 self._mock_get_pidfile_timeout_secs)
showard170873e2009-01-07 00:22:26 +0000828
829 self.pidfile_id = object()
830
showardd3dc1992009-04-22 21:01:40 +0000831 (self.mock_drone_manager.get_pidfile_id_from
832 .expect_call(self.execution_tag,
jamesrenc44ae992010-02-19 00:12:54 +0000833 pidfile_name=drone_manager.AUTOSERV_PID_FILE)
showardd3dc1992009-04-22 21:01:40 +0000834 .and_return(self.pidfile_id))
showard170873e2009-01-07 00:22:26 +0000835
836 self.monitor = monitor_db.PidfileRunMonitor()
837 self.monitor.attach_to_existing_process(self.execution_tag)
jadmanski3d161b02008-06-06 15:43:36 +0000838
jadmanski0afbb632008-06-06 21:10:57 +0000839 def tearDown(self):
840 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +0000841
842
showardec6a3b92009-09-25 20:29:13 +0000843 def _mock_get_pidfile_timeout_secs(self):
844 return 300
845
846
showard170873e2009-01-07 00:22:26 +0000847 def setup_pidfile(self, pid=None, exit_code=None, tests_failed=None,
848 use_second_read=False):
849 contents = drone_manager.PidfileContents()
850 if pid is not None:
851 contents.process = drone_manager.Process('myhost', pid)
852 contents.exit_status = exit_code
853 contents.num_tests_failed = tests_failed
854 self.mock_drone_manager.get_pidfile_contents.expect_call(
855 self.pidfile_id, use_second_read=use_second_read).and_return(
856 contents)
857
858
jadmanski0afbb632008-06-06 21:10:57 +0000859 def set_not_yet_run(self):
showard170873e2009-01-07 00:22:26 +0000860 self.setup_pidfile()
jadmanski3d161b02008-06-06 15:43:36 +0000861
862
showard3dd6b882008-10-27 19:21:39 +0000863 def set_empty_pidfile(self):
showard170873e2009-01-07 00:22:26 +0000864 self.setup_pidfile()
showard3dd6b882008-10-27 19:21:39 +0000865
866
showard170873e2009-01-07 00:22:26 +0000867 def set_running(self, use_second_read=False):
868 self.setup_pidfile(self.pid, use_second_read=use_second_read)
jadmanski3d161b02008-06-06 15:43:36 +0000869
870
showard170873e2009-01-07 00:22:26 +0000871 def set_complete(self, error_code, use_second_read=False):
872 self.setup_pidfile(self.pid, error_code, self.num_tests_failed,
873 use_second_read=use_second_read)
874
875
876 def _check_monitor(self, expected_pid, expected_exit_status,
877 expected_num_tests_failed):
878 if expected_pid is None:
879 self.assertEquals(self.monitor._state.process, None)
880 else:
881 self.assertEquals(self.monitor._state.process.pid, expected_pid)
882 self.assertEquals(self.monitor._state.exit_status, expected_exit_status)
883 self.assertEquals(self.monitor._state.num_tests_failed,
884 expected_num_tests_failed)
885
886
887 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000888
889
showard21baa452008-10-21 00:08:39 +0000890 def _test_read_pidfile_helper(self, expected_pid, expected_exit_status,
891 expected_num_tests_failed):
892 self.monitor._read_pidfile()
showard170873e2009-01-07 00:22:26 +0000893 self._check_monitor(expected_pid, expected_exit_status,
894 expected_num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000895
896
showard21baa452008-10-21 00:08:39 +0000897 def _get_expected_tests_failed(self, expected_exit_status):
898 if expected_exit_status is None:
899 expected_tests_failed = None
900 else:
901 expected_tests_failed = self.num_tests_failed
902 return expected_tests_failed
903
904
jadmanski0afbb632008-06-06 21:10:57 +0000905 def test_read_pidfile(self):
906 self.set_not_yet_run()
showard21baa452008-10-21 00:08:39 +0000907 self._test_read_pidfile_helper(None, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000908
showard3dd6b882008-10-27 19:21:39 +0000909 self.set_empty_pidfile()
910 self._test_read_pidfile_helper(None, None, None)
911
jadmanski0afbb632008-06-06 21:10:57 +0000912 self.set_running()
showard21baa452008-10-21 00:08:39 +0000913 self._test_read_pidfile_helper(self.pid, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000914
jadmanski0afbb632008-06-06 21:10:57 +0000915 self.set_complete(123)
showard21baa452008-10-21 00:08:39 +0000916 self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000917
918
jadmanski0afbb632008-06-06 21:10:57 +0000919 def test_read_pidfile_error(self):
showard170873e2009-01-07 00:22:26 +0000920 self.mock_drone_manager.get_pidfile_contents.expect_call(
921 self.pidfile_id, use_second_read=False).and_return(
922 drone_manager.InvalidPidfile('error'))
923 self.assertRaises(monitor_db.PidfileRunMonitor._PidfileException,
showard21baa452008-10-21 00:08:39 +0000924 self.monitor._read_pidfile)
jadmanski0afbb632008-06-06 21:10:57 +0000925 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000926
927
showard170873e2009-01-07 00:22:26 +0000928 def setup_is_running(self, is_running):
929 self.mock_drone_manager.is_process_running.expect_call(
930 self.process).and_return(is_running)
jadmanski3d161b02008-06-06 15:43:36 +0000931
932
showard21baa452008-10-21 00:08:39 +0000933 def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status,
934 expected_num_tests_failed):
935 self.monitor._get_pidfile_info()
showard170873e2009-01-07 00:22:26 +0000936 self._check_monitor(expected_pid, expected_exit_status,
937 expected_num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000938
939
jadmanski0afbb632008-06-06 21:10:57 +0000940 def test_get_pidfile_info(self):
showard21baa452008-10-21 00:08:39 +0000941 """
942 normal cases for get_pidfile_info
943 """
jadmanski0afbb632008-06-06 21:10:57 +0000944 # running
945 self.set_running()
showard170873e2009-01-07 00:22:26 +0000946 self.setup_is_running(True)
showard21baa452008-10-21 00:08:39 +0000947 self._test_get_pidfile_info_helper(self.pid, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000948
jadmanski0afbb632008-06-06 21:10:57 +0000949 # exited during check
950 self.set_running()
showard170873e2009-01-07 00:22:26 +0000951 self.setup_is_running(False)
952 self.set_complete(123, use_second_read=True) # pidfile gets read again
showard21baa452008-10-21 00:08:39 +0000953 self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000954
jadmanski0afbb632008-06-06 21:10:57 +0000955 # completed
956 self.set_complete(123)
showard21baa452008-10-21 00:08:39 +0000957 self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000958
959
jadmanski0afbb632008-06-06 21:10:57 +0000960 def test_get_pidfile_info_running_no_proc(self):
showard21baa452008-10-21 00:08:39 +0000961 """
962 pidfile shows process running, but no proc exists
963 """
jadmanski0afbb632008-06-06 21:10:57 +0000964 # running but no proc
965 self.set_running()
showard170873e2009-01-07 00:22:26 +0000966 self.setup_is_running(False)
967 self.set_running(use_second_read=True)
968 email_manager.manager.enqueue_notify_email.expect_call(
jadmanski0afbb632008-06-06 21:10:57 +0000969 mock.is_string_comparator(), mock.is_string_comparator())
showard21baa452008-10-21 00:08:39 +0000970 self._test_get_pidfile_info_helper(self.pid, 1, 0)
jadmanski0afbb632008-06-06 21:10:57 +0000971 self.assertTrue(self.monitor.lost_process)
jadmanski3d161b02008-06-06 15:43:36 +0000972
973
jadmanski0afbb632008-06-06 21:10:57 +0000974 def test_get_pidfile_info_not_yet_run(self):
showard21baa452008-10-21 00:08:39 +0000975 """
976 pidfile hasn't been written yet
977 """
jadmanski0afbb632008-06-06 21:10:57 +0000978 self.set_not_yet_run()
showard21baa452008-10-21 00:08:39 +0000979 self._test_get_pidfile_info_helper(None, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000980
jadmanski3d161b02008-06-06 15:43:36 +0000981
showard170873e2009-01-07 00:22:26 +0000982 def test_process_failed_to_write_pidfile(self):
jadmanski0afbb632008-06-06 21:10:57 +0000983 self.set_not_yet_run()
showard170873e2009-01-07 00:22:26 +0000984 email_manager.manager.enqueue_notify_email.expect_call(
985 mock.is_string_comparator(), mock.is_string_comparator())
showardec6a3b92009-09-25 20:29:13 +0000986 self.monitor._start_time = (time.time() -
987 monitor_db._get_pidfile_timeout_secs() - 1)
showard35162b02009-03-03 02:17:30 +0000988 self._test_get_pidfile_info_helper(None, 1, 0)
989 self.assertTrue(self.monitor.lost_process)
jadmanski3d161b02008-06-06 15:43:36 +0000990
991
992class AgentTest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +0000993 def setUp(self):
994 self.god = mock.mock_god()
showard6b733412009-04-27 20:09:18 +0000995 self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher,
996 'dispatcher')
jadmanski3d161b02008-06-06 15:43:36 +0000997
998
jadmanski0afbb632008-06-06 21:10:57 +0000999 def tearDown(self):
1000 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +00001001
1002
showard170873e2009-01-07 00:22:26 +00001003 def _create_mock_task(self, name):
1004 task = self.god.create_mock_class(monitor_db.AgentTask, name)
showard418785b2009-11-23 20:19:59 +00001005 task.num_processes = 1
showard6b733412009-04-27 20:09:18 +00001006 _set_host_and_qe_ids(task)
showard170873e2009-01-07 00:22:26 +00001007 return task
1008
showard8cc058f2009-09-08 16:26:33 +00001009 def _create_agent(self, task):
1010 agent = monitor_db.Agent(task)
showard6b733412009-04-27 20:09:18 +00001011 agent.dispatcher = self._dispatcher
1012 return agent
1013
1014
1015 def _finish_agent(self, agent):
1016 while not agent.is_done():
1017 agent.tick()
1018
showard170873e2009-01-07 00:22:26 +00001019
showard8cc058f2009-09-08 16:26:33 +00001020 def test_agent_abort(self):
1021 task = self._create_mock_task('task')
1022 task.poll.expect_call()
1023 task.is_done.expect_call().and_return(False)
1024 task.abort.expect_call()
1025 task.aborted = True
jadmanski3d161b02008-06-06 15:43:36 +00001026
showard8cc058f2009-09-08 16:26:33 +00001027 agent = self._create_agent(task)
showard6b733412009-04-27 20:09:18 +00001028 agent.tick()
1029 agent.abort()
1030 self._finish_agent(agent)
1031 self.god.check_playback()
1032
1033
showard08a36412009-05-05 01:01:13 +00001034 def _test_agent_abort_before_started_helper(self, ignore_abort=False):
showard20f9bdd2009-04-29 19:48:33 +00001035 task = self._create_mock_task('task')
showard08a36412009-05-05 01:01:13 +00001036 task.abort.expect_call()
1037 if ignore_abort:
1038 task.aborted = False
1039 task.poll.expect_call()
1040 task.is_done.expect_call().and_return(True)
showard08a36412009-05-05 01:01:13 +00001041 task.success = True
1042 else:
1043 task.aborted = True
1044
showard8cc058f2009-09-08 16:26:33 +00001045 agent = self._create_agent(task)
showard20f9bdd2009-04-29 19:48:33 +00001046 agent.abort()
showard20f9bdd2009-04-29 19:48:33 +00001047 self._finish_agent(agent)
1048 self.god.check_playback()
1049
1050
showard08a36412009-05-05 01:01:13 +00001051 def test_agent_abort_before_started(self):
1052 self._test_agent_abort_before_started_helper()
1053 self._test_agent_abort_before_started_helper(True)
1054
1055
jamesrenc44ae992010-02-19 00:12:54 +00001056class JobSchedulingTest(BaseSchedulerTest):
showarde58e3f82008-11-20 19:04:59 +00001057 def _test_run_helper(self, expect_agent=True, expect_starting=False,
1058 expect_pending=False):
1059 if expect_starting:
1060 expected_status = models.HostQueueEntry.Status.STARTING
1061 elif expect_pending:
1062 expected_status = models.HostQueueEntry.Status.PENDING
1063 else:
1064 expected_status = models.HostQueueEntry.Status.VERIFYING
jamesrenc44ae992010-02-19 00:12:54 +00001065 job = scheduler_models.Job.fetch('id = 1')[0]
1066 queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0]
showard77182562009-06-10 00:16:05 +00001067 assert queue_entry.job is job
showard8cc058f2009-09-08 16:26:33 +00001068 job.run_if_ready(queue_entry)
showardb2e2c322008-10-14 17:33:55 +00001069
showard2bab8f42008-11-12 18:15:22 +00001070 self.god.check_playback()
showard8cc058f2009-09-08 16:26:33 +00001071
1072 self._dispatcher._schedule_delay_tasks()
1073 self._dispatcher._schedule_running_host_queue_entries()
1074 agent = self._dispatcher._agents[0]
1075
showard77182562009-06-10 00:16:05 +00001076 actual_status = models.HostQueueEntry.smart_get(1).status
1077 self.assertEquals(expected_status, actual_status)
showard2bab8f42008-11-12 18:15:22 +00001078
showard9976ce92008-10-15 20:28:13 +00001079 if not expect_agent:
1080 self.assertEquals(agent, None)
1081 return
1082
showardb2e2c322008-10-14 17:33:55 +00001083 self.assert_(isinstance(agent, monitor_db.Agent))
showard8cc058f2009-09-08 16:26:33 +00001084 self.assert_(agent.task)
1085 return agent.task
showardc9ae1782009-01-30 01:42:37 +00001086
1087
showard77182562009-06-10 00:16:05 +00001088 def test_run_if_ready_delays(self):
1089 # Also tests Job.run_with_ready_delay() on atomic group jobs.
1090 django_job = self._create_job(hosts=[5, 6], atomic_group=1)
jamesrenc44ae992010-02-19 00:12:54 +00001091 job = scheduler_models.Job(django_job.id)
showard77182562009-06-10 00:16:05 +00001092 self.assertEqual(1, job.synch_count)
1093 django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id))
1094 self.assertEqual(2, len(django_hqes))
1095 self.assertEqual(2, django_hqes[0].atomic_group.max_number_of_machines)
1096
1097 def set_hqe_status(django_hqe, status):
1098 django_hqe.status = status
1099 django_hqe.save()
jamesrenc44ae992010-02-19 00:12:54 +00001100 scheduler_models.HostQueueEntry(django_hqe.id).host.set_status(status)
showard77182562009-06-10 00:16:05 +00001101
1102 # An initial state, our synch_count is 1
1103 set_hqe_status(django_hqes[0], models.HostQueueEntry.Status.VERIFYING)
1104 set_hqe_status(django_hqes[1], models.HostQueueEntry.Status.PENDING)
1105
1106 # So that we don't depend on the config file value during the test.
1107 self.assert_(scheduler_config.config
1108 .secs_to_wait_for_atomic_group_hosts is not None)
1109 self.god.stub_with(scheduler_config.config,
1110 'secs_to_wait_for_atomic_group_hosts', 123456)
1111
jamesrenc44ae992010-02-19 00:12:54 +00001112 # Get the pending one as a scheduler_models.HostQueueEntry object.
1113 hqe = scheduler_models.HostQueueEntry(django_hqes[1].id)
showard77182562009-06-10 00:16:05 +00001114 self.assert_(not job._delay_ready_task)
1115 self.assertTrue(job.is_ready())
1116
1117 # Ready with one pending, one verifying and an atomic group should
1118 # result in a DelayCallTask to re-check if we're ready a while later.
showard8cc058f2009-09-08 16:26:33 +00001119 job.run_if_ready(hqe)
1120 self.assertEquals('Waiting', hqe.status)
1121 self._dispatcher._schedule_delay_tasks()
1122 self.assertEquals('Pending', hqe.status)
1123 agent = self._dispatcher._agents[0]
showard77182562009-06-10 00:16:05 +00001124 self.assert_(job._delay_ready_task)
1125 self.assert_(isinstance(agent, monitor_db.Agent))
showard8cc058f2009-09-08 16:26:33 +00001126 self.assert_(agent.task)
1127 delay_task = agent.task
jamesrenc44ae992010-02-19 00:12:54 +00001128 self.assert_(isinstance(delay_task, scheduler_models.DelayedCallTask))
showard77182562009-06-10 00:16:05 +00001129 self.assert_(not delay_task.is_done())
1130
showard8cc058f2009-09-08 16:26:33 +00001131 self.god.stub_function(delay_task, 'abort')
1132
showard77182562009-06-10 00:16:05 +00001133 self.god.stub_function(job, 'run')
1134
showardd2014822009-10-12 20:26:58 +00001135 self.god.stub_function(job, '_pending_count')
showardd07a5f32009-12-07 19:36:20 +00001136 self.god.stub_with(job, 'synch_count', 9)
1137 self.god.stub_function(job, 'request_abort')
showardd2014822009-10-12 20:26:58 +00001138
showard77182562009-06-10 00:16:05 +00001139 # Test that the DelayedCallTask's callback queued up above does the
showardd2014822009-10-12 20:26:58 +00001140 # correct thing and does not call run if there are not enough hosts
1141 # in pending after the delay.
showardd2014822009-10-12 20:26:58 +00001142 job._pending_count.expect_call().and_return(0)
showardd07a5f32009-12-07 19:36:20 +00001143 job.request_abort.expect_call()
showardd2014822009-10-12 20:26:58 +00001144 delay_task._callback()
1145 self.god.check_playback()
1146
1147 # Test that the DelayedCallTask's callback queued up above does the
1148 # correct thing and returns the Agent returned by job.run() if
1149 # there are still enough hosts pending after the delay.
showardd07a5f32009-12-07 19:36:20 +00001150 job.synch_count = 4
showardd2014822009-10-12 20:26:58 +00001151 job._pending_count.expect_call().and_return(4)
showard8cc058f2009-09-08 16:26:33 +00001152 job.run.expect_call(hqe)
1153 delay_task._callback()
1154 self.god.check_playback()
showard77182562009-06-10 00:16:05 +00001155
showardd2014822009-10-12 20:26:58 +00001156 job._pending_count.expect_call().and_return(4)
1157
showard77182562009-06-10 00:16:05 +00001158 # Adjust the delay deadline so that enough time has passed.
1159 job._delay_ready_task.end_time = time.time() - 111111
showard8cc058f2009-09-08 16:26:33 +00001160 job.run.expect_call(hqe)
showard77182562009-06-10 00:16:05 +00001161 # ...the delay_expired condition should cause us to call run()
showard8cc058f2009-09-08 16:26:33 +00001162 self._dispatcher._handle_agents()
1163 self.god.check_playback()
1164 delay_task.success = False
showard77182562009-06-10 00:16:05 +00001165
1166 # Adjust the delay deadline back so that enough time has not passed.
1167 job._delay_ready_task.end_time = time.time() + 111111
showard8cc058f2009-09-08 16:26:33 +00001168 self._dispatcher._handle_agents()
1169 self.god.check_playback()
showard77182562009-06-10 00:16:05 +00001170
showard77182562009-06-10 00:16:05 +00001171 # Now max_number_of_machines HQEs are in pending state. Remaining
1172 # delay will now be ignored.
jamesrenc44ae992010-02-19 00:12:54 +00001173 other_hqe = scheduler_models.HostQueueEntry(django_hqes[0].id)
showard8cc058f2009-09-08 16:26:33 +00001174 self.god.unstub(job, 'run')
showardd2014822009-10-12 20:26:58 +00001175 self.god.unstub(job, '_pending_count')
showardd07a5f32009-12-07 19:36:20 +00001176 self.god.unstub(job, 'synch_count')
1177 self.god.unstub(job, 'request_abort')
showard77182562009-06-10 00:16:05 +00001178 # ...the over_max_threshold test should cause us to call run()
showard8cc058f2009-09-08 16:26:33 +00001179 delay_task.abort.expect_call()
1180 other_hqe.on_pending()
1181 self.assertEquals('Starting', other_hqe.status)
1182 self.assertEquals('Starting', hqe.status)
1183 self.god.stub_function(job, 'run')
1184 self.god.unstub(delay_task, 'abort')
showard77182562009-06-10 00:16:05 +00001185
showard8cc058f2009-09-08 16:26:33 +00001186 hqe.set_status('Pending')
1187 other_hqe.set_status('Pending')
showard708b3522009-08-20 23:26:15 +00001188 # Now we're not over the max for the atomic group. But all assigned
1189 # hosts are in pending state. over_max_threshold should make us run().
showard8cc058f2009-09-08 16:26:33 +00001190 hqe.atomic_group.max_number_of_machines += 1
1191 hqe.atomic_group.save()
1192 job.run.expect_call(hqe)
1193 hqe.on_pending()
1194 self.god.check_playback()
1195 hqe.atomic_group.max_number_of_machines -= 1
1196 hqe.atomic_group.save()
showard708b3522009-08-20 23:26:15 +00001197
jamesrenc44ae992010-02-19 00:12:54 +00001198 other_hqe = scheduler_models.HostQueueEntry(django_hqes[0].id)
showard8cc058f2009-09-08 16:26:33 +00001199 self.assertTrue(hqe.job is other_hqe.job)
showard77182562009-06-10 00:16:05 +00001200 # DBObject classes should reuse instances so these should be the same.
1201 self.assertEqual(job, other_hqe.job)
showard8cc058f2009-09-08 16:26:33 +00001202 self.assertEqual(other_hqe.job, hqe.job)
showard77182562009-06-10 00:16:05 +00001203 # Be sure our delay was not lost during the other_hqe construction.
showard8cc058f2009-09-08 16:26:33 +00001204 self.assertEqual(job._delay_ready_task, delay_task)
showard77182562009-06-10 00:16:05 +00001205 self.assert_(job._delay_ready_task)
1206 self.assertFalse(job._delay_ready_task.is_done())
1207 self.assertFalse(job._delay_ready_task.aborted)
1208
1209 # We want the real run() to be called below.
1210 self.god.unstub(job, 'run')
1211
1212 # We pass in the other HQE this time the same way it would happen
1213 # for real when one host finishes verifying and enters pending.
showard8cc058f2009-09-08 16:26:33 +00001214 job.run_if_ready(other_hqe)
showard77182562009-06-10 00:16:05 +00001215
1216 # The delayed task must be aborted by the actual run() call above.
1217 self.assertTrue(job._delay_ready_task.aborted)
1218 self.assertFalse(job._delay_ready_task.success)
1219 self.assertTrue(job._delay_ready_task.is_done())
1220
1221 # Check that job run() and _finish_run() were called by the above:
showard8cc058f2009-09-08 16:26:33 +00001222 self._dispatcher._schedule_running_host_queue_entries()
1223 agent = self._dispatcher._agents[0]
1224 self.assert_(agent.task)
1225 task = agent.task
1226 self.assert_(isinstance(task, monitor_db.QueueTask))
showard77182562009-06-10 00:16:05 +00001227 # Requery these hqes in order to verify the status from the DB.
1228 django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id))
1229 for entry in django_hqes:
1230 self.assertEqual(models.HostQueueEntry.Status.STARTING,
1231 entry.status)
1232
1233 # We're already running, but more calls to run_with_ready_delay can
1234 # continue to come in due to straggler hosts enter Pending. Make
1235 # sure we don't do anything.
showard8cc058f2009-09-08 16:26:33 +00001236 self.god.stub_function(job, 'run')
1237 job.run_with_ready_delay(hqe)
1238 self.god.check_playback()
1239 self.god.unstub(job, 'run')
showard77182562009-06-10 00:16:05 +00001240
1241
showardf1ae3542009-05-11 19:26:02 +00001242 def test_run_synchronous_atomic_group_ready(self):
1243 self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True)
1244 self._update_hqe("status='Pending', execution_subdir=''")
1245
showard8cc058f2009-09-08 16:26:33 +00001246 queue_task = self._test_run_helper(expect_starting=True)
showardf1ae3542009-05-11 19:26:02 +00001247
1248 self.assert_(isinstance(queue_task, monitor_db.QueueTask))
showard77182562009-06-10 00:16:05 +00001249 # Atomic group jobs that do not depend on a specific label in the
1250 # atomic group will use the atomic group name as their group name.
showardd1195652009-12-08 22:21:02 +00001251 self.assertEquals(queue_task.queue_entries[0].get_group_name(),
1252 'atomic1')
showardf1ae3542009-05-11 19:26:02 +00001253
1254
1255 def test_run_synchronous_atomic_group_with_label_ready(self):
1256 job = self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True)
1257 job.dependency_labels.add(self.label4)
1258 self._update_hqe("status='Pending', execution_subdir=''")
1259
showard8cc058f2009-09-08 16:26:33 +00001260 queue_task = self._test_run_helper(expect_starting=True)
showardf1ae3542009-05-11 19:26:02 +00001261
1262 self.assert_(isinstance(queue_task, monitor_db.QueueTask))
1263 # Atomic group jobs that also specify a label in the atomic group
1264 # will use the label name as their group name.
showardd1195652009-12-08 22:21:02 +00001265 self.assertEquals(queue_task.queue_entries[0].get_group_name(),
1266 'label4')
showardf1ae3542009-05-11 19:26:02 +00001267
1268
jamesrenc44ae992010-02-19 00:12:54 +00001269 def test_run_synchronous_ready(self):
1270 self._create_job(hosts=[1, 2], synchronous=True)
1271 self._update_hqe("status='Pending', execution_subdir=''")
showard21baa452008-10-21 00:08:39 +00001272
jamesrenc44ae992010-02-19 00:12:54 +00001273 queue_task = self._test_run_helper(expect_starting=True)
showard8cc058f2009-09-08 16:26:33 +00001274
jamesrenc44ae992010-02-19 00:12:54 +00001275 self.assert_(isinstance(queue_task, monitor_db.QueueTask))
1276 self.assertEquals(queue_task.job.id, 1)
1277 hqe_ids = [hqe.id for hqe in queue_task.queue_entries]
1278 self.assertEquals(hqe_ids, [1, 2])
showard21baa452008-10-21 00:08:39 +00001279
1280
jamesrenc44ae992010-02-19 00:12:54 +00001281 def test_schedule_running_host_queue_entries_fail(self):
1282 self._create_job(hosts=[2])
1283 self._update_hqe("status='%s', execution_subdir=''" %
1284 models.HostQueueEntry.Status.PENDING)
1285 job = scheduler_models.Job.fetch('id = 1')[0]
1286 queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0]
1287 assert queue_entry.job is job
1288 job.run_if_ready(queue_entry)
1289 self.assertEqual(queue_entry.status,
1290 models.HostQueueEntry.Status.STARTING)
1291 self.assert_(queue_entry.execution_subdir)
1292 self.god.check_playback()
showard21baa452008-10-21 00:08:39 +00001293
jamesrenc44ae992010-02-19 00:12:54 +00001294 class dummy_test_agent(object):
1295 task = 'dummy_test_agent'
1296 self._dispatcher._register_agent_for_ids(
1297 self._dispatcher._host_agents, [queue_entry.host.id],
1298 dummy_test_agent)
showard21baa452008-10-21 00:08:39 +00001299
jamesrenc44ae992010-02-19 00:12:54 +00001300 # Attempted to schedule on a host that already has an agent.
Dale Curtisaa513362011-03-01 17:27:44 -08001301 self.assertRaises(host_scheduler.SchedulerError,
jamesrenc44ae992010-02-19 00:12:54 +00001302 self._dispatcher._schedule_running_host_queue_entries)
showardf1ae3542009-05-11 19:26:02 +00001303
1304
jamesren47bd7372010-03-13 00:58:17 +00001305 def test_schedule_hostless_job(self):
1306 job = self._create_job(hostless=True)
1307 self.assertEqual(1, job.hostqueueentry_set.count())
1308 hqe_query = scheduler_models.HostQueueEntry.fetch(
1309 'id = %s' % job.hostqueueentry_set.all()[0].id)
1310 self.assertEqual(1, len(hqe_query))
1311 hqe = hqe_query[0]
1312
1313 self.assertEqual(models.HostQueueEntry.Status.QUEUED, hqe.status)
1314 self.assertEqual(0, len(self._dispatcher._agents))
1315
1316 self._dispatcher._schedule_new_jobs()
1317
1318 self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status)
1319 self.assertEqual(1, len(self._dispatcher._agents))
1320
1321 self._dispatcher._schedule_new_jobs()
1322
1323 # No change to previously schedule hostless job, and no additional agent
1324 self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status)
1325 self.assertEqual(1, len(self._dispatcher._agents))
1326
1327
showardf1ae3542009-05-11 19:26:02 +00001328class TopLevelFunctionsTest(unittest.TestCase):
mblighe7d9c602009-07-02 19:02:33 +00001329 def setUp(self):
1330 self.god = mock.mock_god()
1331
1332
1333 def tearDown(self):
1334 self.god.unstub_all()
1335
1336
showardf1ae3542009-05-11 19:26:02 +00001337 def test_autoserv_command_line(self):
1338 machines = 'abcd12,efgh34'
showardf1ae3542009-05-11 19:26:02 +00001339 extra_args = ['-Z', 'hello']
showardf65b7402009-12-18 22:44:35 +00001340 expected_command_line_base = set((monitor_db._autoserv_path, '-p',
1341 '-m', machines, '-r',
1342 drone_manager.WORKING_DIRECTORY))
showardf1ae3542009-05-11 19:26:02 +00001343
showardf65b7402009-12-18 22:44:35 +00001344 expected_command_line = expected_command_line_base.union(
1345 ['--verbose']).union(extra_args)
1346 command_line = set(
1347 monitor_db._autoserv_command_line(machines, extra_args))
1348 self.assertEqual(expected_command_line, command_line)
showardf1ae3542009-05-11 19:26:02 +00001349
1350 class FakeJob(object):
1351 owner = 'Bob'
1352 name = 'fake job name'
mblighe7d9c602009-07-02 19:02:33 +00001353 id = 1337
1354
1355 class FakeHQE(object):
1356 job = FakeJob
showardf1ae3542009-05-11 19:26:02 +00001357
showardf65b7402009-12-18 22:44:35 +00001358 expected_command_line = expected_command_line_base.union(
1359 ['-u', FakeJob.owner, '-l', FakeJob.name])
1360 command_line = set(monitor_db._autoserv_command_line(
1361 machines, extra_args=[], queue_entry=FakeHQE, verbose=False))
1362 self.assertEqual(expected_command_line, command_line)
showardf1ae3542009-05-11 19:26:02 +00001363
showard21baa452008-10-21 00:08:39 +00001364
jamesren76fcf192010-04-21 20:39:50 +00001365class AgentTaskTest(unittest.TestCase,
1366 frontend_test_utils.FrontendTestMixin):
1367 def setUp(self):
1368 self._frontend_common_setup()
1369
1370
1371 def tearDown(self):
1372 self._frontend_common_teardown()
1373
1374
1375 def _setup_drones(self):
1376 self.god.stub_function(models.DroneSet, 'drone_sets_enabled')
1377 models.DroneSet.drone_sets_enabled.expect_call().and_return(True)
1378
1379 drones = []
1380 for x in xrange(4):
1381 drones.append(models.Drone.objects.create(hostname=str(x)))
1382
1383 drone_set_1 = models.DroneSet.objects.create(name='1')
1384 drone_set_1.drones.add(*drones[0:2])
1385 drone_set_2 = models.DroneSet.objects.create(name='2')
1386 drone_set_2.drones.add(*drones[2:4])
1387 drone_set_3 = models.DroneSet.objects.create(name='3')
1388
1389 job_1 = self._create_job_simple([self.hosts[0].id],
1390 drone_set=drone_set_1)
1391 job_2 = self._create_job_simple([self.hosts[0].id],
1392 drone_set=drone_set_2)
1393 job_3 = self._create_job_simple([self.hosts[0].id],
1394 drone_set=drone_set_3)
1395
jamesrendd77e012010-04-28 18:07:30 +00001396 job_4 = self._create_job_simple([self.hosts[0].id])
1397 job_4.drone_set = None
1398 job_4.save()
jamesren76fcf192010-04-21 20:39:50 +00001399
jamesrendd77e012010-04-28 18:07:30 +00001400 hqe_1 = job_1.hostqueueentry_set.all()[0]
1401 hqe_2 = job_2.hostqueueentry_set.all()[0]
1402 hqe_3 = job_3.hostqueueentry_set.all()[0]
1403 hqe_4 = job_4.hostqueueentry_set.all()[0]
1404
1405 return (hqe_1, hqe_2, hqe_3, hqe_4), monitor_db.AgentTask()
jamesren76fcf192010-04-21 20:39:50 +00001406
1407
jamesrendd77e012010-04-28 18:07:30 +00001408 def test_get_drone_hostnames_allowed_no_drones_in_set(self):
jamesren76fcf192010-04-21 20:39:50 +00001409 hqes, task = self._setup_drones()
jamesrendd77e012010-04-28 18:07:30 +00001410 task.queue_entry_ids = (hqes[2].id,)
jamesren76fcf192010-04-21 20:39:50 +00001411 self.assertEqual(set(), task.get_drone_hostnames_allowed())
1412 self.god.check_playback()
1413
1414
jamesrendd77e012010-04-28 18:07:30 +00001415 def test_get_drone_hostnames_allowed_no_drone_set(self):
1416 hqes, task = self._setup_drones()
1417 hqe = hqes[3]
1418 task.queue_entry_ids = (hqe.id,)
1419
1420 result = object()
1421
1422 self.god.stub_function(task, '_user_or_global_default_drone_set')
1423 task._user_or_global_default_drone_set.expect_call(
1424 hqe.job, hqe.job.user()).and_return(result)
1425
1426 self.assertEqual(result, task.get_drone_hostnames_allowed())
1427 self.god.check_playback()
1428
1429
jamesren76fcf192010-04-21 20:39:50 +00001430 def test_get_drone_hostnames_allowed_success(self):
1431 hqes, task = self._setup_drones()
jamesrendd77e012010-04-28 18:07:30 +00001432 task.queue_entry_ids = (hqes[0].id,)
jamesren76fcf192010-04-21 20:39:50 +00001433 self.assertEqual(set(('0','1')), task.get_drone_hostnames_allowed())
1434 self.god.check_playback()
1435
1436
1437 def test_get_drone_hostnames_allowed_multiple_jobs(self):
1438 hqes, task = self._setup_drones()
jamesrendd77e012010-04-28 18:07:30 +00001439 task.queue_entry_ids = (hqes[0].id, hqes[1].id)
jamesren76fcf192010-04-21 20:39:50 +00001440 self.assertRaises(AssertionError,
1441 task.get_drone_hostnames_allowed)
1442 self.god.check_playback()
1443
1444
jamesrendd77e012010-04-28 18:07:30 +00001445 def test_get_drone_hostnames_allowed_no_hqe(self):
1446 class MockSpecialTask(object):
1447 requested_by = object()
1448
1449 class MockSpecialAgentTask(monitor_db.SpecialAgentTask):
1450 task = MockSpecialTask()
1451 queue_entry_ids = []
1452 def __init__(self, *args, **kwargs):
1453 pass
1454
1455 task = MockSpecialAgentTask()
1456 self.god.stub_function(models.DroneSet, 'drone_sets_enabled')
1457 self.god.stub_function(task, '_user_or_global_default_drone_set')
1458
1459 result = object()
1460 models.DroneSet.drone_sets_enabled.expect_call().and_return(True)
1461 task._user_or_global_default_drone_set.expect_call(
1462 task.task, MockSpecialTask.requested_by).and_return(result)
1463
1464 self.assertEqual(result, task.get_drone_hostnames_allowed())
1465 self.god.check_playback()
1466
1467
1468 def _setup_test_user_or_global_default_drone_set(self):
1469 result = object()
1470 class MockDroneSet(object):
1471 def get_drone_hostnames(self):
1472 return result
1473
1474 self.god.stub_function(models.DroneSet, 'get_default')
1475 models.DroneSet.get_default.expect_call().and_return(MockDroneSet())
1476 return result
1477
1478
1479 def test_user_or_global_default_drone_set(self):
1480 expected = object()
1481 class MockDroneSet(object):
1482 def get_drone_hostnames(self):
1483 return expected
1484 class MockUser(object):
1485 drone_set = MockDroneSet()
1486
1487 self._setup_test_user_or_global_default_drone_set()
1488
1489 actual = monitor_db.AgentTask()._user_or_global_default_drone_set(
1490 None, MockUser())
1491
1492 self.assertEqual(expected, actual)
1493 self.god.check_playback()
1494
1495
1496 def test_user_or_global_default_drone_set_no_user(self):
1497 expected = self._setup_test_user_or_global_default_drone_set()
1498 actual = monitor_db.AgentTask()._user_or_global_default_drone_set(
1499 None, None)
1500
1501 self.assertEqual(expected, actual)
1502 self.god.check_playback()
1503
1504
1505 def test_user_or_global_default_drone_set_no_user_drone_set(self):
1506 class MockUser(object):
1507 drone_set = None
1508 login = None
1509
1510 expected = self._setup_test_user_or_global_default_drone_set()
1511 actual = monitor_db.AgentTask()._user_or_global_default_drone_set(
1512 None, MockUser())
1513
1514 self.assertEqual(expected, actual)
1515 self.god.check_playback()
1516
1517
showardce38e0c2008-05-29 19:36:16 +00001518if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +00001519 unittest.main()