blob: 99e0fa08fed1ef03443429bd253e0cdcc2078c06 [file] [log] [blame]
showardce38e0c2008-05-29 19:36:16 +00001#!/usr/bin/python
2
Justin Giorgi67ad67d2016-06-29 14:41:04 -07003import gc
4import time
5import unittest
6
showardce38e0c2008-05-29 19:36:16 +00007import common
showard364fe862008-10-17 02:01:16 +00008from autotest_lib.frontend import setup_django_environment
showardb6d16622009-05-26 19:35:29 +00009from autotest_lib.frontend.afe import frontend_test_utils
Dan Shi114e1722016-01-10 18:12:53 -080010from autotest_lib.client.common_lib import global_config
jadmanski3d161b02008-06-06 15:43:36 +000011from autotest_lib.client.common_lib.test_utils import mock
jamesrenc44ae992010-02-19 00:12:54 +000012from autotest_lib.database import database_connection
showardb1e51872008-10-07 11:08:18 +000013from autotest_lib.frontend.afe import models
beeps5e2bb4a2013-10-28 11:26:45 -070014from autotest_lib.scheduler import agent_task
Dan Shi80f7c532015-08-25 10:23:14 -070015from autotest_lib.scheduler import monitor_db, drone_manager
beeps5e2bb4a2013-10-28 11:26:45 -070016from autotest_lib.scheduler import pidfile_monitor
Prashanth B0e960282014-05-13 19:38:28 -070017from autotest_lib.scheduler import scheduler_config, gc_stats
Prashanth B0e960282014-05-13 19:38:28 -070018from autotest_lib.scheduler import scheduler_lib
jamesrenc44ae992010-02-19 00:12:54 +000019from autotest_lib.scheduler import scheduler_models
showardce38e0c2008-05-29 19:36:16 +000020
21_DEBUG = False
22
showarda3c58572009-03-12 20:36:59 +000023
showard9bb960b2009-11-19 01:02:11 +000024class DummyAgentTask(object):
showardd1195652009-12-08 22:21:02 +000025 num_processes = 1
26 owner_username = 'my_user'
showard9bb960b2009-11-19 01:02:11 +000027
jamesren76fcf192010-04-21 20:39:50 +000028 def get_drone_hostnames_allowed(self):
29 return None
30
showard9bb960b2009-11-19 01:02:11 +000031
showard170873e2009-01-07 00:22:26 +000032class DummyAgent(object):
showard8cc058f2009-09-08 16:26:33 +000033 started = False
showard170873e2009-01-07 00:22:26 +000034 _is_done = False
showardd1195652009-12-08 22:21:02 +000035 host_ids = ()
Dan Shi114e1722016-01-10 18:12:53 -080036 hostnames = {}
showardd1195652009-12-08 22:21:02 +000037 queue_entry_ids = ()
38
39 def __init__(self):
40 self.task = DummyAgentTask()
showard170873e2009-01-07 00:22:26 +000041
showard170873e2009-01-07 00:22:26 +000042
43 def tick(self):
showard8cc058f2009-09-08 16:26:33 +000044 self.started = True
showard170873e2009-01-07 00:22:26 +000045
46
47 def is_done(self):
48 return self._is_done
49
50
51 def set_done(self, done):
52 self._is_done = done
showard04c82c52008-05-29 19:38:12 +000053
showard56193bb2008-08-13 20:07:41 +000054
55class IsRow(mock.argument_comparator):
56 def __init__(self, row_id):
57 self.row_id = row_id
showardce38e0c2008-05-29 19:36:16 +000058
59
showard56193bb2008-08-13 20:07:41 +000060 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
showardd3dc1992009-04-22 21:01:40 +000068class IsAgentWithTask(mock.argument_comparator):
mbligh1ef218d2009-08-03 16:57:56 +000069 def __init__(self, task):
70 self._task = task
showardd3dc1992009-04-22 21:01:40 +000071
72
mbligh1ef218d2009-08-03 16:57:56 +000073 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
showardd3dc1992009-04-22 21:01:40 +000080
81
showard6b733412009-04-27 20:09:18 +000082def _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 Shi114e1722016-01-10 18:12:53 -080086 agent_or_task.hostnames = dict((host_id, '192.168.1.1')
87 for host_id in id_list)
showard6b733412009-04-27 20:09:18 +000088
89
showardb6d16622009-05-26 19:35:29 +000090class BaseSchedulerTest(unittest.TestCase,
91 frontend_test_utils.FrontendTestMixin):
showard50c0e712008-09-22 16:20:37 +000092 _config_section = 'AUTOTEST_WEB'
showardce38e0c2008-05-29 19:36:16 +000093
jadmanski0afbb632008-06-06 21:10:57 +000094 def _do_query(self, sql):
showardb1e51872008-10-07 11:08:18 +000095 self._database.execute(sql)
showardce38e0c2008-05-29 19:36:16 +000096
97
showardb6d16622009-05-26 19:35:29 +000098 def _set_monitor_stubs(self):
Prathmesh Prabhu47bd1dd2017-01-06 15:04:46 -080099 self.mock_config = global_config.FakeGlobalConfig()
100 self.god.stub_with(global_config, 'global_config', self.mock_config)
101
showardb6d16622009-05-26 19:35:29 +0000102 # Clear the instance cache as this is a brand new database.
jamesrenc44ae992010-02-19 00:12:54 +0000103 scheduler_models.DBObject._clear_instance_cache()
showardce38e0c2008-05-29 19:36:16 +0000104
showardb1e51872008-10-07 11:08:18 +0000105 self._database = (
showard78f5b012009-12-23 00:05:59 +0000106 database_connection.TranslatingDatabase.get_test_database(
Prashanth B4ec98672014-05-15 10:44:54 -0700107 translators=scheduler_lib._DB_TRANSLATORS))
showard78f5b012009-12-23 00:05:59 +0000108 self._database.connect(db_type='django')
showardb1e51872008-10-07 11:08:18 +0000109 self._database.debug = _DEBUG
showardce38e0c2008-05-29 19:36:16 +0000110
Prashanth B0e960282014-05-13 19:38:28 -0700111 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 Juelichd615a1e2014-09-04 11:48:36 -0700114 self.god.stub_with(monitor_db, '_db', self._database)
Prashanth Bf66d51b2014-05-06 12:42:25 -0700115
Allen Li7bea6a02017-02-06 17:01:20 -0800116 self.god.stub_with(monitor_db.Dispatcher,
beeps7d8a1b12013-10-29 17:58:34 -0700117 '_get_pending_queue_entries',
118 self._get_pending_hqes)
jamesrenc44ae992010-02-19 00:12:54 +0000119 self.god.stub_with(scheduler_models, '_db', self._database)
120 self.god.stub_with(drone_manager.instance(), '_results_dir',
showard78f5b012009-12-23 00:05:59 +0000121 '/test/path')
jamesrenc44ae992010-02-19 00:12:54 +0000122 self.god.stub_with(drone_manager.instance(), '_temporary_directory',
showard78f5b012009-12-23 00:05:59 +0000123 '/test/path/tmp')
Jakob Juelichd615a1e2014-09-04 11:48:36 -0700124 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)
showard56193bb2008-08-13 20:07:41 +0000128
jamesrenc44ae992010-02-19 00:12:54 +0000129 monitor_db.initialize_globals()
130 scheduler_models.initialize_globals()
131
showard56193bb2008-08-13 20:07:41 +0000132
showard56193bb2008-08-13 20:07:41 +0000133 def setUp(self):
showardb6d16622009-05-26 19:35:29 +0000134 self._frontend_common_setup()
showard56193bb2008-08-13 20:07:41 +0000135 self._set_monitor_stubs()
Prathmesh Prabhu688b9672017-01-06 15:05:58 -0800136 self._set_global_config_values()
showard56193bb2008-08-13 20:07:41 +0000137 self._dispatcher = monitor_db.Dispatcher()
showardce38e0c2008-05-29 19:36:16 +0000138
139
showard56193bb2008-08-13 20:07:41 +0000140 def tearDown(self):
showardb6d16622009-05-26 19:35:29 +0000141 self._database.disconnect()
142 self._frontend_common_teardown()
showardce38e0c2008-05-29 19:36:16 +0000143
144
Prathmesh Prabhu688b9672017-01-06 15:05:58 -0800145 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
showard56193bb2008-08-13 20:07:41 +0000151 def _update_hqe(self, set, where=''):
showardeab66ce2009-12-23 00:03:56 +0000152 query = 'UPDATE afe_host_queue_entries SET ' + set
showard56193bb2008-08-13 20:07:41 +0000153 if where:
154 query += ' WHERE ' + where
155 self._do_query(query)
156
157
beeps7d8a1b12013-10-29 17:58:34 -0700158 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
showardb2e2c322008-10-14 17:33:55 +0000169class DispatcherSchedulingTest(BaseSchedulerTest):
showard56193bb2008-08-13 20:07:41 +0000170 _jobs_scheduled = []
171
showard89f84db2009-03-12 20:39:13 +0000172
173 def tearDown(self):
174 super(DispatcherSchedulingTest, self).tearDown()
175
176
showard56193bb2008-08-13 20:07:41 +0000177 def _set_monitor_stubs(self):
178 super(DispatcherSchedulingTest, self)._set_monitor_stubs()
showard89f84db2009-03-12 20:39:13 +0000179
showard8cc058f2009-09-08 16:26:33 +0000180 def hqe__do_schedule_pre_job_tasks_stub(queue_entry):
181 """Called by HostQueueEntry.run()."""
showard77182562009-06-10 00:16:05 +0000182 self._record_job_scheduled(queue_entry.job.id, queue_entry.host.id)
showard89f84db2009-03-12 20:39:13 +0000183 queue_entry.set_status('Starting')
showard89f84db2009-03-12 20:39:13 +0000184
jamesrenc44ae992010-02-19 00:12:54 +0000185 self.god.stub_with(scheduler_models.HostQueueEntry,
showard8cc058f2009-09-08 16:26:33 +0000186 '_do_schedule_pre_job_tasks',
187 hqe__do_schedule_pre_job_tasks_stub)
showard89f84db2009-03-12 20:39:13 +0000188
showard56193bb2008-08-13 20:07:41 +0000189
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
showard89f84db2009-03-12 20:39:13 +0000207 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
showard56193bb2008-08-13 20:07:41 +0000223 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
jadmanski0afbb632008-06-06 21:10:57 +0000229 def _convert_jobs_to_metahosts(self, *job_ids):
230 sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')'
showardeab66ce2009-12-23 00:03:56 +0000231 self._do_query('UPDATE afe_host_queue_entries SET '
jadmanski0afbb632008-06-06 21:10:57 +0000232 'meta_host=host_id, host_id=NULL '
233 'WHERE job_id IN ' + sql_tuple)
showardce38e0c2008-05-29 19:36:16 +0000234
235
jadmanski0afbb632008-06-06 21:10:57 +0000236 def _lock_host(self, host_id):
showardeab66ce2009-12-23 00:03:56 +0000237 self._do_query('UPDATE afe_hosts SET locked=1 WHERE id=' +
jadmanski0afbb632008-06-06 21:10:57 +0000238 str(host_id))
showardce38e0c2008-05-29 19:36:16 +0000239
240
jadmanski0afbb632008-06-06 21:10:57 +0000241 def setUp(self):
showard56193bb2008-08-13 20:07:41 +0000242 super(DispatcherSchedulingTest, self).setUp()
jadmanski0afbb632008-06-06 21:10:57 +0000243 self._jobs_scheduled = []
showardce38e0c2008-05-29 19:36:16 +0000244
245
jamesren883492a2010-02-12 00:45:18 +0000246 def _run_scheduler(self):
beepscc9fc702013-12-02 12:45:38 -0800247 self._dispatcher._host_scheduler.tick()
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700248 for _ in xrange(2): # metahost scheduling can take two ticks
jamesren883492a2010-02-12 00:45:18 +0000249 self._dispatcher._schedule_new_jobs()
250
251
jadmanski0afbb632008-06-06 21:10:57 +0000252 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)
jamesren883492a2010-02-12 00:45:18 +0000256 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000257 self._assert_job_scheduled_on(1, 1)
258 self._assert_job_scheduled_on(2, 2)
259 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000260
261
jadmanski0afbb632008-06-06 21:10:57 +0000262 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)
jamesren883492a2010-02-12 00:45:18 +0000268 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000269 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()
showardce38e0c2008-05-29 19:36:16 +0000272
273
jadmanski0afbb632008-06-06 21:10:57 +0000274 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)
showardeab66ce2009-12-23 00:03:56 +0000280 self._do_query('UPDATE afe_hosts SET status="Running" WHERE id=1')
jamesren883492a2010-02-12 00:45:18 +0000281 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000282 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000283
showardeab66ce2009-12-23 00:03:56 +0000284 self._do_query('UPDATE afe_hosts SET status="Ready", locked=1 '
jadmanski0afbb632008-06-06 21:10:57 +0000285 'WHERE id=1')
jamesren883492a2010-02-12 00:45:18 +0000286 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000287 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000288
showardeab66ce2009-12-23 00:03:56 +0000289 self._do_query('UPDATE afe_hosts SET locked=0, invalid=1 '
jadmanski0afbb632008-06-06 21:10:57 +0000290 'WHERE id=1')
jamesren883492a2010-02-12 00:45:18 +0000291 self._run_scheduler()
showard5df2b192008-07-03 19:51:57 +0000292 if not use_metahosts:
293 self._assert_job_scheduled_on(1, 1)
jadmanski0afbb632008-06-06 21:10:57 +0000294 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000295
296
jadmanski0afbb632008-06-06 21:10:57 +0000297 def _test_hosts_idle_helper(self, use_metahosts):
298 'Only idle hosts get scheduled'
showard2bab8f42008-11-12 18:15:22 +0000299 self._create_job(hosts=[1], active=True)
jadmanski0afbb632008-06-06 21:10:57 +0000300 self._create_job_simple([1], use_metahosts)
jamesren883492a2010-02-12 00:45:18 +0000301 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000302 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000303
304
showard63a34772008-08-18 19:32:50 +0000305 def _test_obey_ACLs_helper(self, use_metahosts):
showardeab66ce2009-12-23 00:03:56 +0000306 self._do_query('DELETE FROM afe_acl_groups_hosts WHERE host_id=1')
showard63a34772008-08-18 19:32:50 +0000307 self._create_job_simple([1], use_metahosts)
jamesren883492a2010-02-12 00:45:18 +0000308 self._run_scheduler()
showard63a34772008-08-18 19:32:50 +0000309 self._check_for_extra_schedulings()
310
311
jadmanski0afbb632008-06-06 21:10:57 +0000312 def test_basic_scheduling(self):
313 self._test_basic_scheduling_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000314
315
jadmanski0afbb632008-06-06 21:10:57 +0000316 def test_priorities(self):
317 self._test_priorities_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000318
319
jadmanski0afbb632008-06-06 21:10:57 +0000320 def test_hosts_ready(self):
321 self._test_hosts_ready_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000322
323
jadmanski0afbb632008-06-06 21:10:57 +0000324 def test_hosts_idle(self):
325 self._test_hosts_idle_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000326
327
showard63a34772008-08-18 19:32:50 +0000328 def test_obey_ACLs(self):
329 self._test_obey_ACLs_helper(False)
330
331
showard2924b0a2009-06-18 23:16:15 +0000332 def test_one_time_hosts_ignore_ACLs(self):
showardeab66ce2009-12-23 00:03:56 +0000333 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')
showard2924b0a2009-06-18 23:16:15 +0000335 self._create_job_simple([1])
jamesren883492a2010-02-12 00:45:18 +0000336 self._run_scheduler()
showard2924b0a2009-06-18 23:16:15 +0000337 self._assert_job_scheduled_on(1, 1)
338 self._check_for_extra_schedulings()
339
340
showard63a34772008-08-18 19:32:50 +0000341 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 """
showardeab66ce2009-12-23 00:03:56 +0000346 self._do_query('UPDATE afe_hosts SET invalid=1')
showard63a34772008-08-18 19:32:50 +0000347 self._test_basic_scheduling_helper(False)
348
349
jadmanski0afbb632008-06-06 21:10:57 +0000350 def test_metahost_scheduling(self):
showard63a34772008-08-18 19:32:50 +0000351 """
352 Basic metahost scheduling
353 """
jadmanski0afbb632008-06-06 21:10:57 +0000354 self._test_basic_scheduling_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000355
356
jadmanski0afbb632008-06-06 21:10:57 +0000357 def test_metahost_priorities(self):
358 self._test_priorities_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000359
360
jadmanski0afbb632008-06-06 21:10:57 +0000361 def test_metahost_hosts_ready(self):
362 self._test_hosts_ready_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000363
364
jadmanski0afbb632008-06-06 21:10:57 +0000365 def test_metahost_hosts_idle(self):
366 self._test_hosts_idle_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000367
368
showard63a34772008-08-18 19:32:50 +0000369 def test_metahost_obey_ACLs(self):
370 self._test_obey_ACLs_helper(True)
371
372
jadmanski0afbb632008-06-06 21:10:57 +0000373 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])
jamesren883492a2010-02-12 00:45:18 +0000380 self._run_scheduler()
jadmanski0afbb632008-06-06 21:10:57 +0000381 self._assert_job_scheduled_on(2, 1)
382 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000383
384
Jakob Juelichd615a1e2014-09-04 11:48:36 -0700385 def test_no_execution_subdir_not_found(self):
386 """Reproduce bug crosbug.com/334353 and recover from it."""
387
Prathmesh Prabhu47bd1dd2017-01-06 15:04:46 -0800388 self.mock_config.set_config_value('SCHEDULER', 'drones', 'localhost')
Jakob Juelichd615a1e2014-09-04 11:48:36 -0700389
390 job = self._create_job(hostless=True)
391
392 # Ensure execution_subdir is set before status
393 original_set_status = scheduler_models.HostQueueEntry.set_status
394 def fake_set_status(hqe, *args, **kwargs):
395 self.assertEqual(hqe.execution_subdir, 'hostless')
396 original_set_status(hqe, *args, **kwargs)
397
398 self.god.stub_with(scheduler_models.HostQueueEntry, 'set_status',
399 fake_set_status)
400
401 self._dispatcher._schedule_new_jobs()
402
403 hqe = job.hostqueueentry_set.all()[0]
404 self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status)
405 self.assertEqual('hostless', hqe.execution_subdir)
406
407
showard56193bb2008-08-13 20:07:41 +0000408 def test_only_schedule_queued_entries(self):
409 self._create_job(metahosts=[1])
410 self._update_hqe(set='active=1, host_id=2')
jamesren883492a2010-02-12 00:45:18 +0000411 self._run_scheduler()
showard56193bb2008-08-13 20:07:41 +0000412 self._check_for_extra_schedulings()
413
414
showardfa8629c2008-11-04 16:51:23 +0000415 def test_no_ready_hosts(self):
416 self._create_job(hosts=[1])
showardeab66ce2009-12-23 00:03:56 +0000417 self._do_query('UPDATE afe_hosts SET status="Repair Failed"')
jamesren883492a2010-02-12 00:45:18 +0000418 self._run_scheduler()
showardfa8629c2008-11-04 16:51:23 +0000419 self._check_for_extra_schedulings()
420
421
showardf13a9e22009-12-18 22:54:09 +0000422 def test_garbage_collection(self):
423 self.god.stub_with(self._dispatcher, '_seconds_between_garbage_stats',
424 999999)
425 self.god.stub_function(gc, 'collect')
426 self.god.stub_function(gc_stats, '_log_garbage_collector_stats')
427 gc.collect.expect_call().and_return(0)
428 gc_stats._log_garbage_collector_stats.expect_call()
429 # Force a garbage collection run
430 self._dispatcher._last_garbage_stats_time = 0
431 self._dispatcher._garbage_collection()
432 # The previous call should have reset the time, it won't do anything
433 # the second time. If it does, we'll get an unexpected call.
434 self._dispatcher._garbage_collection()
435
436
showardb2e2c322008-10-14 17:33:55 +0000437class DispatcherThrottlingTest(BaseSchedulerTest):
showard4c5374f2008-09-04 17:02:56 +0000438 """
439 Test that the dispatcher throttles:
440 * total number of running processes
441 * number of processes started per cycle
442 """
443 _MAX_RUNNING = 3
444 _MAX_STARTED = 2
445
446 def setUp(self):
447 super(DispatcherThrottlingTest, self).setUp()
showard324bf812009-01-20 23:23:38 +0000448 scheduler_config.config.max_processes_per_drone = self._MAX_RUNNING
showard4c5374f2008-09-04 17:02:56 +0000449
jamesren76fcf192010-04-21 20:39:50 +0000450 def fake_max_runnable_processes(fake_self, username,
451 drone_hostnames_allowed):
showardd1195652009-12-08 22:21:02 +0000452 running = sum(agent.task.num_processes
showard324bf812009-01-20 23:23:38 +0000453 for agent in self._agents
showard8cc058f2009-09-08 16:26:33 +0000454 if agent.started and not agent.is_done())
showard324bf812009-01-20 23:23:38 +0000455 return self._MAX_RUNNING - running
456 self.god.stub_with(drone_manager.DroneManager, 'max_runnable_processes',
457 fake_max_runnable_processes)
showard2fa51692009-01-13 23:48:08 +0000458
showard4c5374f2008-09-04 17:02:56 +0000459
showard4c5374f2008-09-04 17:02:56 +0000460 def _setup_some_agents(self, num_agents):
showard170873e2009-01-07 00:22:26 +0000461 self._agents = [DummyAgent() for i in xrange(num_agents)]
showard4c5374f2008-09-04 17:02:56 +0000462 self._dispatcher._agents = list(self._agents)
463
464
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700465 def _run_a_few_ticks(self):
showard4c5374f2008-09-04 17:02:56 +0000466 for i in xrange(4):
467 self._dispatcher._handle_agents()
468
469
470 def _assert_agents_started(self, indexes, is_started=True):
471 for i in indexes:
showard8cc058f2009-09-08 16:26:33 +0000472 self.assert_(self._agents[i].started == is_started,
showard4c5374f2008-09-04 17:02:56 +0000473 'Agent %d %sstarted' %
474 (i, is_started and 'not ' or ''))
475
476
477 def _assert_agents_not_started(self, indexes):
478 self._assert_agents_started(indexes, False)
479
480
481 def test_throttle_total(self):
482 self._setup_some_agents(4)
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700483 self._run_a_few_ticks()
showard4c5374f2008-09-04 17:02:56 +0000484 self._assert_agents_started([0, 1, 2])
485 self._assert_agents_not_started([3])
486
487
showard4c5374f2008-09-04 17:02:56 +0000488 def test_throttle_with_synchronous(self):
489 self._setup_some_agents(2)
showardd1195652009-12-08 22:21:02 +0000490 self._agents[0].task.num_processes = 3
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700491 self._run_a_few_ticks()
showard4c5374f2008-09-04 17:02:56 +0000492 self._assert_agents_started([0])
493 self._assert_agents_not_started([1])
494
495
496 def test_large_agent_starvation(self):
497 """
498 Ensure large agents don't get starved by lower-priority agents.
499 """
500 self._setup_some_agents(3)
showardd1195652009-12-08 22:21:02 +0000501 self._agents[1].task.num_processes = 3
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700502 self._run_a_few_ticks()
showard4c5374f2008-09-04 17:02:56 +0000503 self._assert_agents_started([0])
504 self._assert_agents_not_started([1, 2])
505
506 self._agents[0].set_done(True)
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700507 self._run_a_few_ticks()
showard4c5374f2008-09-04 17:02:56 +0000508 self._assert_agents_started([1])
509 self._assert_agents_not_started([2])
510
511
512 def test_zero_process_agent(self):
513 self._setup_some_agents(5)
showardd1195652009-12-08 22:21:02 +0000514 self._agents[4].task.num_processes = 0
Paul Hobbsb92af21b2015-04-09 15:12:41 -0700515 self._run_a_few_ticks()
showard4c5374f2008-09-04 17:02:56 +0000516 self._assert_agents_started([0, 1, 2, 4])
517 self._assert_agents_not_started([3])
518
519
jadmanski3d161b02008-06-06 15:43:36 +0000520class PidfileRunMonitorTest(unittest.TestCase):
showard170873e2009-01-07 00:22:26 +0000521 execution_tag = 'test_tag'
jadmanski0afbb632008-06-06 21:10:57 +0000522 pid = 12345
showard170873e2009-01-07 00:22:26 +0000523 process = drone_manager.Process('myhost', pid)
showard21baa452008-10-21 00:08:39 +0000524 num_tests_failed = 1
jadmanski3d161b02008-06-06 15:43:36 +0000525
jadmanski0afbb632008-06-06 21:10:57 +0000526 def setUp(self):
527 self.god = mock.mock_god()
showard170873e2009-01-07 00:22:26 +0000528 self.mock_drone_manager = self.god.create_mock_class(
529 drone_manager.DroneManager, 'drone_manager')
Jakob Jülich36accc62014-07-23 10:26:55 -0700530 self.god.stub_with(drone_manager, '_the_instance',
showard170873e2009-01-07 00:22:26 +0000531 self.mock_drone_manager)
beeps5e2bb4a2013-10-28 11:26:45 -0700532 self.god.stub_with(pidfile_monitor, '_get_pidfile_timeout_secs',
showardec6a3b92009-09-25 20:29:13 +0000533 self._mock_get_pidfile_timeout_secs)
showard170873e2009-01-07 00:22:26 +0000534
535 self.pidfile_id = object()
536
showardd3dc1992009-04-22 21:01:40 +0000537 (self.mock_drone_manager.get_pidfile_id_from
538 .expect_call(self.execution_tag,
jamesrenc44ae992010-02-19 00:12:54 +0000539 pidfile_name=drone_manager.AUTOSERV_PID_FILE)
showardd3dc1992009-04-22 21:01:40 +0000540 .and_return(self.pidfile_id))
showard170873e2009-01-07 00:22:26 +0000541
beeps5e2bb4a2013-10-28 11:26:45 -0700542 self.monitor = pidfile_monitor.PidfileRunMonitor()
showard170873e2009-01-07 00:22:26 +0000543 self.monitor.attach_to_existing_process(self.execution_tag)
jadmanski3d161b02008-06-06 15:43:36 +0000544
jadmanski0afbb632008-06-06 21:10:57 +0000545 def tearDown(self):
546 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +0000547
548
showardec6a3b92009-09-25 20:29:13 +0000549 def _mock_get_pidfile_timeout_secs(self):
550 return 300
551
552
showard170873e2009-01-07 00:22:26 +0000553 def setup_pidfile(self, pid=None, exit_code=None, tests_failed=None,
554 use_second_read=False):
555 contents = drone_manager.PidfileContents()
556 if pid is not None:
557 contents.process = drone_manager.Process('myhost', pid)
558 contents.exit_status = exit_code
559 contents.num_tests_failed = tests_failed
560 self.mock_drone_manager.get_pidfile_contents.expect_call(
561 self.pidfile_id, use_second_read=use_second_read).and_return(
562 contents)
563
564
jadmanski0afbb632008-06-06 21:10:57 +0000565 def set_not_yet_run(self):
showard170873e2009-01-07 00:22:26 +0000566 self.setup_pidfile()
jadmanski3d161b02008-06-06 15:43:36 +0000567
568
showard3dd6b882008-10-27 19:21:39 +0000569 def set_empty_pidfile(self):
showard170873e2009-01-07 00:22:26 +0000570 self.setup_pidfile()
showard3dd6b882008-10-27 19:21:39 +0000571
572
showard170873e2009-01-07 00:22:26 +0000573 def set_running(self, use_second_read=False):
574 self.setup_pidfile(self.pid, use_second_read=use_second_read)
jadmanski3d161b02008-06-06 15:43:36 +0000575
576
showard170873e2009-01-07 00:22:26 +0000577 def set_complete(self, error_code, use_second_read=False):
578 self.setup_pidfile(self.pid, error_code, self.num_tests_failed,
579 use_second_read=use_second_read)
580
581
582 def _check_monitor(self, expected_pid, expected_exit_status,
583 expected_num_tests_failed):
584 if expected_pid is None:
585 self.assertEquals(self.monitor._state.process, None)
586 else:
587 self.assertEquals(self.monitor._state.process.pid, expected_pid)
588 self.assertEquals(self.monitor._state.exit_status, expected_exit_status)
589 self.assertEquals(self.monitor._state.num_tests_failed,
590 expected_num_tests_failed)
591
592
593 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000594
595
showard21baa452008-10-21 00:08:39 +0000596 def _test_read_pidfile_helper(self, expected_pid, expected_exit_status,
597 expected_num_tests_failed):
598 self.monitor._read_pidfile()
showard170873e2009-01-07 00:22:26 +0000599 self._check_monitor(expected_pid, expected_exit_status,
600 expected_num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000601
602
showard21baa452008-10-21 00:08:39 +0000603 def _get_expected_tests_failed(self, expected_exit_status):
604 if expected_exit_status is None:
605 expected_tests_failed = None
606 else:
607 expected_tests_failed = self.num_tests_failed
608 return expected_tests_failed
609
610
jadmanski0afbb632008-06-06 21:10:57 +0000611 def test_read_pidfile(self):
612 self.set_not_yet_run()
showard21baa452008-10-21 00:08:39 +0000613 self._test_read_pidfile_helper(None, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000614
showard3dd6b882008-10-27 19:21:39 +0000615 self.set_empty_pidfile()
616 self._test_read_pidfile_helper(None, None, None)
617
jadmanski0afbb632008-06-06 21:10:57 +0000618 self.set_running()
showard21baa452008-10-21 00:08:39 +0000619 self._test_read_pidfile_helper(self.pid, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000620
jadmanski0afbb632008-06-06 21:10:57 +0000621 self.set_complete(123)
showard21baa452008-10-21 00:08:39 +0000622 self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000623
624
jadmanski0afbb632008-06-06 21:10:57 +0000625 def test_read_pidfile_error(self):
showard170873e2009-01-07 00:22:26 +0000626 self.mock_drone_manager.get_pidfile_contents.expect_call(
627 self.pidfile_id, use_second_read=False).and_return(
628 drone_manager.InvalidPidfile('error'))
beeps5e2bb4a2013-10-28 11:26:45 -0700629 self.assertRaises(pidfile_monitor.PidfileRunMonitor._PidfileException,
showard21baa452008-10-21 00:08:39 +0000630 self.monitor._read_pidfile)
jadmanski0afbb632008-06-06 21:10:57 +0000631 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000632
633
showard170873e2009-01-07 00:22:26 +0000634 def setup_is_running(self, is_running):
635 self.mock_drone_manager.is_process_running.expect_call(
636 self.process).and_return(is_running)
jadmanski3d161b02008-06-06 15:43:36 +0000637
638
showard21baa452008-10-21 00:08:39 +0000639 def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status,
640 expected_num_tests_failed):
641 self.monitor._get_pidfile_info()
showard170873e2009-01-07 00:22:26 +0000642 self._check_monitor(expected_pid, expected_exit_status,
643 expected_num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000644
645
jadmanski0afbb632008-06-06 21:10:57 +0000646 def test_get_pidfile_info(self):
showard21baa452008-10-21 00:08:39 +0000647 """
648 normal cases for get_pidfile_info
649 """
jadmanski0afbb632008-06-06 21:10:57 +0000650 # running
651 self.set_running()
showard170873e2009-01-07 00:22:26 +0000652 self.setup_is_running(True)
showard21baa452008-10-21 00:08:39 +0000653 self._test_get_pidfile_info_helper(self.pid, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000654
jadmanski0afbb632008-06-06 21:10:57 +0000655 # exited during check
656 self.set_running()
showard170873e2009-01-07 00:22:26 +0000657 self.setup_is_running(False)
658 self.set_complete(123, use_second_read=True) # pidfile gets read again
showard21baa452008-10-21 00:08:39 +0000659 self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000660
jadmanski0afbb632008-06-06 21:10:57 +0000661 # completed
662 self.set_complete(123)
showard21baa452008-10-21 00:08:39 +0000663 self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000664
665
jadmanski0afbb632008-06-06 21:10:57 +0000666 def test_get_pidfile_info_running_no_proc(self):
showard21baa452008-10-21 00:08:39 +0000667 """
668 pidfile shows process running, but no proc exists
669 """
jadmanski0afbb632008-06-06 21:10:57 +0000670 # running but no proc
671 self.set_running()
showard170873e2009-01-07 00:22:26 +0000672 self.setup_is_running(False)
673 self.set_running(use_second_read=True)
showard21baa452008-10-21 00:08:39 +0000674 self._test_get_pidfile_info_helper(self.pid, 1, 0)
jadmanski0afbb632008-06-06 21:10:57 +0000675 self.assertTrue(self.monitor.lost_process)
jadmanski3d161b02008-06-06 15:43:36 +0000676
677
jadmanski0afbb632008-06-06 21:10:57 +0000678 def test_get_pidfile_info_not_yet_run(self):
showard21baa452008-10-21 00:08:39 +0000679 """
680 pidfile hasn't been written yet
681 """
jadmanski0afbb632008-06-06 21:10:57 +0000682 self.set_not_yet_run()
showard21baa452008-10-21 00:08:39 +0000683 self._test_get_pidfile_info_helper(None, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000684
jadmanski3d161b02008-06-06 15:43:36 +0000685
showard170873e2009-01-07 00:22:26 +0000686 def test_process_failed_to_write_pidfile(self):
jadmanski0afbb632008-06-06 21:10:57 +0000687 self.set_not_yet_run()
showardec6a3b92009-09-25 20:29:13 +0000688 self.monitor._start_time = (time.time() -
beeps5e2bb4a2013-10-28 11:26:45 -0700689 pidfile_monitor._get_pidfile_timeout_secs() - 1)
showard35162b02009-03-03 02:17:30 +0000690 self._test_get_pidfile_info_helper(None, 1, 0)
691 self.assertTrue(self.monitor.lost_process)
jadmanski3d161b02008-06-06 15:43:36 +0000692
693
694class AgentTest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +0000695 def setUp(self):
696 self.god = mock.mock_god()
showard6b733412009-04-27 20:09:18 +0000697 self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher,
698 'dispatcher')
jadmanski3d161b02008-06-06 15:43:36 +0000699
700
jadmanski0afbb632008-06-06 21:10:57 +0000701 def tearDown(self):
702 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +0000703
704
showard170873e2009-01-07 00:22:26 +0000705 def _create_mock_task(self, name):
beeps5e2bb4a2013-10-28 11:26:45 -0700706 task = self.god.create_mock_class(agent_task.AgentTask, name)
showard418785b2009-11-23 20:19:59 +0000707 task.num_processes = 1
showard6b733412009-04-27 20:09:18 +0000708 _set_host_and_qe_ids(task)
showard170873e2009-01-07 00:22:26 +0000709 return task
710
showard8cc058f2009-09-08 16:26:33 +0000711 def _create_agent(self, task):
712 agent = monitor_db.Agent(task)
showard6b733412009-04-27 20:09:18 +0000713 agent.dispatcher = self._dispatcher
714 return agent
715
716
717 def _finish_agent(self, agent):
718 while not agent.is_done():
719 agent.tick()
720
showard170873e2009-01-07 00:22:26 +0000721
showard8cc058f2009-09-08 16:26:33 +0000722 def test_agent_abort(self):
723 task = self._create_mock_task('task')
724 task.poll.expect_call()
725 task.is_done.expect_call().and_return(False)
726 task.abort.expect_call()
727 task.aborted = True
jadmanski3d161b02008-06-06 15:43:36 +0000728
showard8cc058f2009-09-08 16:26:33 +0000729 agent = self._create_agent(task)
showard6b733412009-04-27 20:09:18 +0000730 agent.tick()
731 agent.abort()
732 self._finish_agent(agent)
733 self.god.check_playback()
734
735
showard08a36412009-05-05 01:01:13 +0000736 def _test_agent_abort_before_started_helper(self, ignore_abort=False):
showard20f9bdd2009-04-29 19:48:33 +0000737 task = self._create_mock_task('task')
showard08a36412009-05-05 01:01:13 +0000738 task.abort.expect_call()
739 if ignore_abort:
740 task.aborted = False
741 task.poll.expect_call()
742 task.is_done.expect_call().and_return(True)
showard08a36412009-05-05 01:01:13 +0000743 task.success = True
744 else:
745 task.aborted = True
746
showard8cc058f2009-09-08 16:26:33 +0000747 agent = self._create_agent(task)
showard20f9bdd2009-04-29 19:48:33 +0000748 agent.abort()
showard20f9bdd2009-04-29 19:48:33 +0000749 self._finish_agent(agent)
750 self.god.check_playback()
751
752
showard08a36412009-05-05 01:01:13 +0000753 def test_agent_abort_before_started(self):
754 self._test_agent_abort_before_started_helper()
755 self._test_agent_abort_before_started_helper(True)
756
757
jamesrenc44ae992010-02-19 00:12:54 +0000758class JobSchedulingTest(BaseSchedulerTest):
showarde58e3f82008-11-20 19:04:59 +0000759 def _test_run_helper(self, expect_agent=True, expect_starting=False,
760 expect_pending=False):
761 if expect_starting:
762 expected_status = models.HostQueueEntry.Status.STARTING
763 elif expect_pending:
764 expected_status = models.HostQueueEntry.Status.PENDING
765 else:
766 expected_status = models.HostQueueEntry.Status.VERIFYING
jamesrenc44ae992010-02-19 00:12:54 +0000767 job = scheduler_models.Job.fetch('id = 1')[0]
768 queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0]
showard77182562009-06-10 00:16:05 +0000769 assert queue_entry.job is job
showard8cc058f2009-09-08 16:26:33 +0000770 job.run_if_ready(queue_entry)
showardb2e2c322008-10-14 17:33:55 +0000771
showard2bab8f42008-11-12 18:15:22 +0000772 self.god.check_playback()
showard8cc058f2009-09-08 16:26:33 +0000773
showard8cc058f2009-09-08 16:26:33 +0000774 self._dispatcher._schedule_running_host_queue_entries()
775 agent = self._dispatcher._agents[0]
776
showard77182562009-06-10 00:16:05 +0000777 actual_status = models.HostQueueEntry.smart_get(1).status
778 self.assertEquals(expected_status, actual_status)
showard2bab8f42008-11-12 18:15:22 +0000779
showard9976ce92008-10-15 20:28:13 +0000780 if not expect_agent:
781 self.assertEquals(agent, None)
782 return
783
showardb2e2c322008-10-14 17:33:55 +0000784 self.assert_(isinstance(agent, monitor_db.Agent))
showard8cc058f2009-09-08 16:26:33 +0000785 self.assert_(agent.task)
786 return agent.task
showardc9ae1782009-01-30 01:42:37 +0000787
788
jamesrenc44ae992010-02-19 00:12:54 +0000789 def test_run_synchronous_ready(self):
790 self._create_job(hosts=[1, 2], synchronous=True)
791 self._update_hqe("status='Pending', execution_subdir=''")
showard21baa452008-10-21 00:08:39 +0000792
jamesrenc44ae992010-02-19 00:12:54 +0000793 queue_task = self._test_run_helper(expect_starting=True)
showard8cc058f2009-09-08 16:26:33 +0000794
jamesrenc44ae992010-02-19 00:12:54 +0000795 self.assert_(isinstance(queue_task, monitor_db.QueueTask))
796 self.assertEquals(queue_task.job.id, 1)
797 hqe_ids = [hqe.id for hqe in queue_task.queue_entries]
798 self.assertEquals(hqe_ids, [1, 2])
showard21baa452008-10-21 00:08:39 +0000799
800
jamesrenc44ae992010-02-19 00:12:54 +0000801 def test_schedule_running_host_queue_entries_fail(self):
802 self._create_job(hosts=[2])
803 self._update_hqe("status='%s', execution_subdir=''" %
804 models.HostQueueEntry.Status.PENDING)
805 job = scheduler_models.Job.fetch('id = 1')[0]
806 queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0]
807 assert queue_entry.job is job
808 job.run_if_ready(queue_entry)
809 self.assertEqual(queue_entry.status,
810 models.HostQueueEntry.Status.STARTING)
811 self.assert_(queue_entry.execution_subdir)
812 self.god.check_playback()
showard21baa452008-10-21 00:08:39 +0000813
jamesrenc44ae992010-02-19 00:12:54 +0000814 class dummy_test_agent(object):
815 task = 'dummy_test_agent'
816 self._dispatcher._register_agent_for_ids(
817 self._dispatcher._host_agents, [queue_entry.host.id],
818 dummy_test_agent)
showard21baa452008-10-21 00:08:39 +0000819
jamesrenc44ae992010-02-19 00:12:54 +0000820 # Attempted to schedule on a host that already has an agent.
Xixuan Wuaabc22f2018-02-08 17:01:08 -0800821 # Verify that it doesn't raise any error.
822 self._dispatcher._schedule_running_host_queue_entries()
showardf1ae3542009-05-11 19:26:02 +0000823
824
jamesren47bd7372010-03-13 00:58:17 +0000825 def test_schedule_hostless_job(self):
826 job = self._create_job(hostless=True)
827 self.assertEqual(1, job.hostqueueentry_set.count())
828 hqe_query = scheduler_models.HostQueueEntry.fetch(
829 'id = %s' % job.hostqueueentry_set.all()[0].id)
830 self.assertEqual(1, len(hqe_query))
831 hqe = hqe_query[0]
832
833 self.assertEqual(models.HostQueueEntry.Status.QUEUED, hqe.status)
834 self.assertEqual(0, len(self._dispatcher._agents))
835
836 self._dispatcher._schedule_new_jobs()
837
838 self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status)
839 self.assertEqual(1, len(self._dispatcher._agents))
840
841 self._dispatcher._schedule_new_jobs()
842
843 # No change to previously schedule hostless job, and no additional agent
844 self.assertEqual(models.HostQueueEntry.Status.STARTING, hqe.status)
845 self.assertEqual(1, len(self._dispatcher._agents))
846
847
showardf1ae3542009-05-11 19:26:02 +0000848class TopLevelFunctionsTest(unittest.TestCase):
mblighe7d9c602009-07-02 19:02:33 +0000849 def setUp(self):
850 self.god = mock.mock_god()
851
852
853 def tearDown(self):
854 self.god.unstub_all()
855
856
showardf1ae3542009-05-11 19:26:02 +0000857 def test_autoserv_command_line(self):
858 machines = 'abcd12,efgh34'
showardf1ae3542009-05-11 19:26:02 +0000859 extra_args = ['-Z', 'hello']
showardf65b7402009-12-18 22:44:35 +0000860 expected_command_line_base = set((monitor_db._autoserv_path, '-p',
861 '-m', machines, '-r',
Simran Basi1bf60eb2015-12-01 16:39:29 -0800862 '--lab', 'True',
showardf65b7402009-12-18 22:44:35 +0000863 drone_manager.WORKING_DIRECTORY))
showardf1ae3542009-05-11 19:26:02 +0000864
showardf65b7402009-12-18 22:44:35 +0000865 expected_command_line = expected_command_line_base.union(
866 ['--verbose']).union(extra_args)
867 command_line = set(
868 monitor_db._autoserv_command_line(machines, extra_args))
869 self.assertEqual(expected_command_line, command_line)
showardf1ae3542009-05-11 19:26:02 +0000870
871 class FakeJob(object):
872 owner = 'Bob'
873 name = 'fake job name'
Aviv Keshet1f23b692013-05-14 11:13:55 -0700874 test_retry = 0
mblighe7d9c602009-07-02 19:02:33 +0000875 id = 1337
876
877 class FakeHQE(object):
878 job = FakeJob
showardf1ae3542009-05-11 19:26:02 +0000879
showardf65b7402009-12-18 22:44:35 +0000880 expected_command_line = expected_command_line_base.union(
881 ['-u', FakeJob.owner, '-l', FakeJob.name])
882 command_line = set(monitor_db._autoserv_command_line(
883 machines, extra_args=[], queue_entry=FakeHQE, verbose=False))
884 self.assertEqual(expected_command_line, command_line)
showardf1ae3542009-05-11 19:26:02 +0000885
showard21baa452008-10-21 00:08:39 +0000886
jamesren76fcf192010-04-21 20:39:50 +0000887class AgentTaskTest(unittest.TestCase,
888 frontend_test_utils.FrontendTestMixin):
889 def setUp(self):
890 self._frontend_common_setup()
891
892
893 def tearDown(self):
894 self._frontend_common_teardown()
895
896
897 def _setup_drones(self):
898 self.god.stub_function(models.DroneSet, 'drone_sets_enabled')
899 models.DroneSet.drone_sets_enabled.expect_call().and_return(True)
900
901 drones = []
902 for x in xrange(4):
903 drones.append(models.Drone.objects.create(hostname=str(x)))
904
905 drone_set_1 = models.DroneSet.objects.create(name='1')
906 drone_set_1.drones.add(*drones[0:2])
907 drone_set_2 = models.DroneSet.objects.create(name='2')
908 drone_set_2.drones.add(*drones[2:4])
909 drone_set_3 = models.DroneSet.objects.create(name='3')
910
911 job_1 = self._create_job_simple([self.hosts[0].id],
912 drone_set=drone_set_1)
913 job_2 = self._create_job_simple([self.hosts[0].id],
914 drone_set=drone_set_2)
915 job_3 = self._create_job_simple([self.hosts[0].id],
916 drone_set=drone_set_3)
917
jamesrendd77e012010-04-28 18:07:30 +0000918 job_4 = self._create_job_simple([self.hosts[0].id])
919 job_4.drone_set = None
920 job_4.save()
jamesren76fcf192010-04-21 20:39:50 +0000921
jamesrendd77e012010-04-28 18:07:30 +0000922 hqe_1 = job_1.hostqueueentry_set.all()[0]
923 hqe_2 = job_2.hostqueueentry_set.all()[0]
924 hqe_3 = job_3.hostqueueentry_set.all()[0]
925 hqe_4 = job_4.hostqueueentry_set.all()[0]
926
beeps5e2bb4a2013-10-28 11:26:45 -0700927 return (hqe_1, hqe_2, hqe_3, hqe_4), agent_task.AgentTask()
jamesren76fcf192010-04-21 20:39:50 +0000928
929
jamesrendd77e012010-04-28 18:07:30 +0000930 def test_get_drone_hostnames_allowed_no_drones_in_set(self):
jamesren76fcf192010-04-21 20:39:50 +0000931 hqes, task = self._setup_drones()
jamesrendd77e012010-04-28 18:07:30 +0000932 task.queue_entry_ids = (hqes[2].id,)
jamesren76fcf192010-04-21 20:39:50 +0000933 self.assertEqual(set(), task.get_drone_hostnames_allowed())
934 self.god.check_playback()
935
936
jamesrendd77e012010-04-28 18:07:30 +0000937 def test_get_drone_hostnames_allowed_no_drone_set(self):
938 hqes, task = self._setup_drones()
939 hqe = hqes[3]
940 task.queue_entry_ids = (hqe.id,)
941
942 result = object()
943
944 self.god.stub_function(task, '_user_or_global_default_drone_set')
945 task._user_or_global_default_drone_set.expect_call(
946 hqe.job, hqe.job.user()).and_return(result)
947
948 self.assertEqual(result, task.get_drone_hostnames_allowed())
949 self.god.check_playback()
950
951
jamesren76fcf192010-04-21 20:39:50 +0000952 def test_get_drone_hostnames_allowed_success(self):
953 hqes, task = self._setup_drones()
jamesrendd77e012010-04-28 18:07:30 +0000954 task.queue_entry_ids = (hqes[0].id,)
Dan Shi114e1722016-01-10 18:12:53 -0800955 self.assertEqual(set(('0','1')), task.get_drone_hostnames_allowed([]))
jamesren76fcf192010-04-21 20:39:50 +0000956 self.god.check_playback()
957
958
959 def test_get_drone_hostnames_allowed_multiple_jobs(self):
960 hqes, task = self._setup_drones()
jamesrendd77e012010-04-28 18:07:30 +0000961 task.queue_entry_ids = (hqes[0].id, hqes[1].id)
jamesren76fcf192010-04-21 20:39:50 +0000962 self.assertRaises(AssertionError,
963 task.get_drone_hostnames_allowed)
964 self.god.check_playback()
965
966
jamesrendd77e012010-04-28 18:07:30 +0000967 def test_get_drone_hostnames_allowed_no_hqe(self):
968 class MockSpecialTask(object):
969 requested_by = object()
970
beeps5e2bb4a2013-10-28 11:26:45 -0700971 class MockSpecialAgentTask(agent_task.SpecialAgentTask):
jamesrendd77e012010-04-28 18:07:30 +0000972 task = MockSpecialTask()
973 queue_entry_ids = []
974 def __init__(self, *args, **kwargs):
Dan Shi114e1722016-01-10 18:12:53 -0800975 super(agent_task.SpecialAgentTask, self).__init__()
jamesrendd77e012010-04-28 18:07:30 +0000976
977 task = MockSpecialAgentTask()
978 self.god.stub_function(models.DroneSet, 'drone_sets_enabled')
979 self.god.stub_function(task, '_user_or_global_default_drone_set')
980
981 result = object()
982 models.DroneSet.drone_sets_enabled.expect_call().and_return(True)
983 task._user_or_global_default_drone_set.expect_call(
984 task.task, MockSpecialTask.requested_by).and_return(result)
985
986 self.assertEqual(result, task.get_drone_hostnames_allowed())
987 self.god.check_playback()
988
989
990 def _setup_test_user_or_global_default_drone_set(self):
991 result = object()
992 class MockDroneSet(object):
993 def get_drone_hostnames(self):
994 return result
995
996 self.god.stub_function(models.DroneSet, 'get_default')
997 models.DroneSet.get_default.expect_call().and_return(MockDroneSet())
998 return result
999
1000
1001 def test_user_or_global_default_drone_set(self):
1002 expected = object()
1003 class MockDroneSet(object):
1004 def get_drone_hostnames(self):
1005 return expected
1006 class MockUser(object):
1007 drone_set = MockDroneSet()
1008
1009 self._setup_test_user_or_global_default_drone_set()
1010
beeps5e2bb4a2013-10-28 11:26:45 -07001011 actual = agent_task.AgentTask()._user_or_global_default_drone_set(
jamesrendd77e012010-04-28 18:07:30 +00001012 None, MockUser())
1013
1014 self.assertEqual(expected, actual)
1015 self.god.check_playback()
1016
1017
1018 def test_user_or_global_default_drone_set_no_user(self):
1019 expected = self._setup_test_user_or_global_default_drone_set()
beeps5e2bb4a2013-10-28 11:26:45 -07001020 actual = agent_task.AgentTask()._user_or_global_default_drone_set(
jamesrendd77e012010-04-28 18:07:30 +00001021 None, None)
1022
1023 self.assertEqual(expected, actual)
1024 self.god.check_playback()
1025
1026
1027 def test_user_or_global_default_drone_set_no_user_drone_set(self):
1028 class MockUser(object):
1029 drone_set = None
1030 login = None
1031
1032 expected = self._setup_test_user_or_global_default_drone_set()
beeps5e2bb4a2013-10-28 11:26:45 -07001033 actual = agent_task.AgentTask()._user_or_global_default_drone_set(
jamesrendd77e012010-04-28 18:07:30 +00001034 None, MockUser())
1035
1036 self.assertEqual(expected, actual)
1037 self.god.check_playback()
1038
1039
Dan Shi76af8022013-10-19 01:59:49 -07001040 def test_abort_HostlessQueueTask(self):
1041 hqe = self.god.create_mock_class(scheduler_models.HostQueueEntry,
1042 'HostQueueEntry')
1043 # If hqe is still in STARTING status, aborting the task should finish
1044 # without changing hqe's status.
1045 hqe.status = models.HostQueueEntry.Status.STARTING
1046 hqe.job = None
1047 hqe.id = 0
1048 task = monitor_db.HostlessQueueTask(hqe)
1049 task.abort()
1050
1051 # If hqe is in RUNNING status, aborting the task should change hqe's
1052 # status to Parsing, so FinalReparseTask can be scheduled.
1053 hqe.set_status.expect_call('Parsing')
1054 hqe.status = models.HostQueueEntry.Status.RUNNING
1055 hqe.job = None
1056 hqe.id = 0
1057 task = monitor_db.HostlessQueueTask(hqe)
1058 task.abort()
1059
1060
showardce38e0c2008-05-29 19:36:16 +00001061if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +00001062 unittest.main()