blob: 13ccfa728ab9ec6414892583e3535a2eaf230bc9 [file] [log] [blame]
showardce38e0c2008-05-29 19:36:16 +00001#!/usr/bin/python
2
showard8fe93b52008-11-18 17:53:22 +00003import unittest, time, subprocess, os, StringIO, tempfile, datetime, shutil
showardce38e0c2008-05-29 19:36:16 +00004import MySQLdb
5import common
showard364fe862008-10-17 02:01:16 +00006from autotest_lib.frontend import setup_django_environment
7from autotest_lib.frontend import setup_test_environment
jadmanskifb7cfb12008-07-09 14:13:21 +00008from autotest_lib.client.common_lib import global_config, host_protections
jadmanski3d161b02008-06-06 15:43:36 +00009from autotest_lib.client.common_lib.test_utils import mock
showard442e71e2008-10-06 10:05:20 +000010from autotest_lib.database import database_connection, migrate
showard21baa452008-10-21 00:08:39 +000011from autotest_lib.frontend import thread_local
showardb1e51872008-10-07 11:08:18 +000012from autotest_lib.frontend.afe import models
showard170873e2009-01-07 00:22:26 +000013from autotest_lib.scheduler import monitor_db, drone_manager, email_manager
showardd1ee1dd2009-01-07 21:33:08 +000014from autotest_lib.scheduler import scheduler_config
showardce38e0c2008-05-29 19:36:16 +000015
16_DEBUG = False
17
showard170873e2009-01-07 00:22:26 +000018class DummyAgent(object):
19 _is_running = False
20 _is_done = False
21 num_processes = 1
22 host_ids = []
23 queue_entry_ids = []
24
25 def is_running(self):
26 return self._is_running
27
28
29 def tick(self):
30 self._is_running = True
31
32
33 def is_done(self):
34 return self._is_done
35
36
37 def set_done(self, done):
38 self._is_done = done
39 self._is_running = not done
showard04c82c52008-05-29 19:38:12 +000040
showard56193bb2008-08-13 20:07:41 +000041
42class IsRow(mock.argument_comparator):
43 def __init__(self, row_id):
44 self.row_id = row_id
showardce38e0c2008-05-29 19:36:16 +000045
46
showard56193bb2008-08-13 20:07:41 +000047 def is_satisfied_by(self, parameter):
48 return list(parameter)[0] == self.row_id
49
50
51 def __str__(self):
52 return 'row with id %s' % self.row_id
53
54
showardb2e2c322008-10-14 17:33:55 +000055class BaseSchedulerTest(unittest.TestCase):
showard50c0e712008-09-22 16:20:37 +000056 _config_section = 'AUTOTEST_WEB'
showardb1e51872008-10-07 11:08:18 +000057 _test_db_initialized = False
showardce38e0c2008-05-29 19:36:16 +000058
jadmanski0afbb632008-06-06 21:10:57 +000059 def _do_query(self, sql):
showardb1e51872008-10-07 11:08:18 +000060 self._database.execute(sql)
showardce38e0c2008-05-29 19:36:16 +000061
62
showardb1e51872008-10-07 11:08:18 +000063 @classmethod
64 def _initialize_test_db(cls):
65 if cls._test_db_initialized:
66 return
67 temp_fd, cls._test_db_file = tempfile.mkstemp(suffix='.monitor_test')
68 os.close(temp_fd)
showard364fe862008-10-17 02:01:16 +000069 setup_test_environment.set_test_database(cls._test_db_file)
70 setup_test_environment.run_syncdb()
71 cls._test_db_backup = setup_test_environment.backup_test_database()
showardb1e51872008-10-07 11:08:18 +000072 cls._test_db_initialized = True
showardce38e0c2008-05-29 19:36:16 +000073
74
showard50c0e712008-09-22 16:20:37 +000075 def _open_test_db(self):
showardb1e51872008-10-07 11:08:18 +000076 self._initialize_test_db()
showard364fe862008-10-17 02:01:16 +000077 setup_test_environment.restore_test_database(self._test_db_backup)
showardb1e51872008-10-07 11:08:18 +000078 self._database = (
79 database_connection.DatabaseConnection.get_test_database(
80 self._test_db_file))
81 self._database.connect()
82 self._database.debug = _DEBUG
showardce38e0c2008-05-29 19:36:16 +000083
84
jadmanski0afbb632008-06-06 21:10:57 +000085 def _close_test_db(self):
showardb1e51872008-10-07 11:08:18 +000086 self._database.disconnect()
showardce38e0c2008-05-29 19:36:16 +000087
88
showard56193bb2008-08-13 20:07:41 +000089 def _set_monitor_stubs(self):
showardb1e51872008-10-07 11:08:18 +000090 monitor_db._db = self._database
showard170873e2009-01-07 00:22:26 +000091 monitor_db._drone_manager._results_dir = '/test/path'
92 monitor_db._drone_manager._temporary_directory = '/test/path/tmp'
showard56193bb2008-08-13 20:07:41 +000093
94
jadmanski0afbb632008-06-06 21:10:57 +000095 def _fill_in_test_data(self):
showardb1e51872008-10-07 11:08:18 +000096 user = models.User.objects.create(login='my_user')
97 acl_group = models.AclGroup.objects.create(name='my_acl')
98 acl_group.users.add(user)
99
100 hosts = [models.Host.objects.create(hostname=hostname) for hostname in
101 ('host1', 'host2', 'host3', 'host4')]
102 acl_group.hosts = hosts
showard98863972008-10-29 21:14:56 +0000103 models.AclGroup.smart_get('Everyone').hosts = []
showardb1e51872008-10-07 11:08:18 +0000104
105 labels = [models.Label.objects.create(name=name) for name in
106 ('label1', 'label2', 'label3')]
107 labels[2].only_if_needed = True
108 labels[2].save()
109 hosts[0].labels.add(labels[0])
110 hosts[1].labels.add(labels[1])
showardce38e0c2008-05-29 19:36:16 +0000111
112
showard21baa452008-10-21 00:08:39 +0000113 def _setup_dummy_user(self):
114 user = models.User.objects.create(login='dummy', access_level=100)
115 thread_local.set_user(user)
116
117
showard56193bb2008-08-13 20:07:41 +0000118 def setUp(self):
119 self.god = mock.mock_god()
showard50c0e712008-09-22 16:20:37 +0000120 self._open_test_db()
showard56193bb2008-08-13 20:07:41 +0000121 self._fill_in_test_data()
122 self._set_monitor_stubs()
123 self._dispatcher = monitor_db.Dispatcher()
showard21baa452008-10-21 00:08:39 +0000124 self._setup_dummy_user()
showardce38e0c2008-05-29 19:36:16 +0000125
126
showard56193bb2008-08-13 20:07:41 +0000127 def tearDown(self):
128 self._close_test_db()
129 self.god.unstub_all()
showardce38e0c2008-05-29 19:36:16 +0000130
131
showard2bab8f42008-11-12 18:15:22 +0000132 def _create_job(self, hosts=[], metahosts=[], priority=0, active=False,
showard4c5374f2008-09-04 17:02:56 +0000133 synchronous=False):
showard2bab8f42008-11-12 18:15:22 +0000134 synch_count = synchronous and 2 or 1
showard12bc8a82008-10-09 16:49:53 +0000135 created_on = datetime.datetime(2008, 1, 1)
showard2bab8f42008-11-12 18:15:22 +0000136 status = models.HostQueueEntry.Status.QUEUED
137 if active:
138 status = models.HostQueueEntry.Status.RUNNING
showard21baa452008-10-21 00:08:39 +0000139 job = models.Job.objects.create(
140 name='test', owner='my_user', priority=priority,
showard2bab8f42008-11-12 18:15:22 +0000141 synch_count=synch_count, created_on=created_on,
showard0fc38302008-10-23 00:44:07 +0000142 reboot_before=models.RebootBefore.NEVER)
jadmanski0afbb632008-06-06 21:10:57 +0000143 for host_id in hosts:
showardb1e51872008-10-07 11:08:18 +0000144 models.HostQueueEntry.objects.create(job=job, priority=priority,
showard2bab8f42008-11-12 18:15:22 +0000145 host_id=host_id, status=status)
showardb1e51872008-10-07 11:08:18 +0000146 models.IneligibleHostQueue.objects.create(job=job, host_id=host_id)
jadmanski0afbb632008-06-06 21:10:57 +0000147 for label_id in metahosts:
showardb1e51872008-10-07 11:08:18 +0000148 models.HostQueueEntry.objects.create(job=job, priority=priority,
149 meta_host_id=label_id,
showard2bab8f42008-11-12 18:15:22 +0000150 status=status)
showardb1e51872008-10-07 11:08:18 +0000151 return job
showardce38e0c2008-05-29 19:36:16 +0000152
153
jadmanski0afbb632008-06-06 21:10:57 +0000154 def _create_job_simple(self, hosts, use_metahost=False,
showard2bab8f42008-11-12 18:15:22 +0000155 priority=0, active=False):
jadmanski0afbb632008-06-06 21:10:57 +0000156 'An alternative interface to _create_job'
157 args = {'hosts' : [], 'metahosts' : []}
158 if use_metahost:
159 args['metahosts'] = hosts
160 else:
161 args['hosts'] = hosts
showardb1e51872008-10-07 11:08:18 +0000162 return self._create_job(priority=priority, active=active, **args)
showardce38e0c2008-05-29 19:36:16 +0000163
164
showard56193bb2008-08-13 20:07:41 +0000165 def _update_hqe(self, set, where=''):
166 query = 'UPDATE host_queue_entries SET ' + set
167 if where:
168 query += ' WHERE ' + where
169 self._do_query(query)
170
171
showardb2e2c322008-10-14 17:33:55 +0000172class DispatcherSchedulingTest(BaseSchedulerTest):
showard56193bb2008-08-13 20:07:41 +0000173 _jobs_scheduled = []
174
175 def _set_monitor_stubs(self):
176 super(DispatcherSchedulingTest, self)._set_monitor_stubs()
177 def run_stub(hqe_self, assigned_host=None):
178 hqe_self.set_status('Starting')
179 if hqe_self.meta_host:
180 host = assigned_host
181 else:
182 host = hqe_self.host
183 self._record_job_scheduled(hqe_self.job.id, host.id)
showard170873e2009-01-07 00:22:26 +0000184 return DummyAgent()
showard56193bb2008-08-13 20:07:41 +0000185 monitor_db.HostQueueEntry.run = run_stub
186
187
188 def _record_job_scheduled(self, job_id, host_id):
189 record = (job_id, host_id)
190 self.assert_(record not in self._jobs_scheduled,
191 'Job %d scheduled on host %d twice' %
192 (job_id, host_id))
193 self._jobs_scheduled.append(record)
194
195
196 def _assert_job_scheduled_on(self, job_id, host_id):
197 record = (job_id, host_id)
198 self.assert_(record in self._jobs_scheduled,
199 'Job %d not scheduled on host %d as expected\n'
200 'Jobs scheduled: %s' %
201 (job_id, host_id, self._jobs_scheduled))
202 self._jobs_scheduled.remove(record)
203
204
205 def _check_for_extra_schedulings(self):
206 if len(self._jobs_scheduled) != 0:
207 self.fail('Extra jobs scheduled: ' +
208 str(self._jobs_scheduled))
209
210
jadmanski0afbb632008-06-06 21:10:57 +0000211 def _convert_jobs_to_metahosts(self, *job_ids):
212 sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')'
213 self._do_query('UPDATE host_queue_entries SET '
214 'meta_host=host_id, host_id=NULL '
215 'WHERE job_id IN ' + sql_tuple)
showardce38e0c2008-05-29 19:36:16 +0000216
217
jadmanski0afbb632008-06-06 21:10:57 +0000218 def _lock_host(self, host_id):
219 self._do_query('UPDATE hosts SET locked=1 WHERE id=' +
220 str(host_id))
showardce38e0c2008-05-29 19:36:16 +0000221
222
jadmanski0afbb632008-06-06 21:10:57 +0000223 def setUp(self):
showard56193bb2008-08-13 20:07:41 +0000224 super(DispatcherSchedulingTest, self).setUp()
jadmanski0afbb632008-06-06 21:10:57 +0000225 self._jobs_scheduled = []
showardce38e0c2008-05-29 19:36:16 +0000226
227
jadmanski0afbb632008-06-06 21:10:57 +0000228 def _test_basic_scheduling_helper(self, use_metahosts):
229 'Basic nonmetahost scheduling'
230 self._create_job_simple([1], use_metahosts)
231 self._create_job_simple([2], use_metahosts)
232 self._dispatcher._schedule_new_jobs()
233 self._assert_job_scheduled_on(1, 1)
234 self._assert_job_scheduled_on(2, 2)
235 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000236
237
jadmanski0afbb632008-06-06 21:10:57 +0000238 def _test_priorities_helper(self, use_metahosts):
239 'Test prioritization ordering'
240 self._create_job_simple([1], use_metahosts)
241 self._create_job_simple([2], use_metahosts)
242 self._create_job_simple([1,2], use_metahosts)
243 self._create_job_simple([1], use_metahosts, priority=1)
244 self._dispatcher._schedule_new_jobs()
245 self._assert_job_scheduled_on(4, 1) # higher priority
246 self._assert_job_scheduled_on(2, 2) # earlier job over later
247 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000248
249
jadmanski0afbb632008-06-06 21:10:57 +0000250 def _test_hosts_ready_helper(self, use_metahosts):
251 """
252 Only hosts that are status=Ready, unlocked and not invalid get
253 scheduled.
254 """
255 self._create_job_simple([1], use_metahosts)
256 self._do_query('UPDATE hosts SET status="Running" WHERE id=1')
257 self._dispatcher._schedule_new_jobs()
258 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000259
jadmanski0afbb632008-06-06 21:10:57 +0000260 self._do_query('UPDATE hosts SET status="Ready", locked=1 '
261 'WHERE id=1')
262 self._dispatcher._schedule_new_jobs()
263 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000264
jadmanski0afbb632008-06-06 21:10:57 +0000265 self._do_query('UPDATE hosts SET locked=0, invalid=1 '
266 'WHERE id=1')
267 self._dispatcher._schedule_new_jobs()
showard5df2b192008-07-03 19:51:57 +0000268 if not use_metahosts:
269 self._assert_job_scheduled_on(1, 1)
jadmanski0afbb632008-06-06 21:10:57 +0000270 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000271
272
jadmanski0afbb632008-06-06 21:10:57 +0000273 def _test_hosts_idle_helper(self, use_metahosts):
274 'Only idle hosts get scheduled'
showard2bab8f42008-11-12 18:15:22 +0000275 self._create_job(hosts=[1], active=True)
jadmanski0afbb632008-06-06 21:10:57 +0000276 self._create_job_simple([1], use_metahosts)
277 self._dispatcher._schedule_new_jobs()
278 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000279
280
showard63a34772008-08-18 19:32:50 +0000281 def _test_obey_ACLs_helper(self, use_metahosts):
282 self._do_query('DELETE FROM acl_groups_hosts WHERE host_id=1')
283 self._create_job_simple([1], use_metahosts)
284 self._dispatcher._schedule_new_jobs()
285 self._check_for_extra_schedulings()
286
287
showard989f25d2008-10-01 11:38:11 +0000288 def _test_only_if_needed_labels_helper(self, use_metahosts):
289 # apply only_if_needed label3 to host1
showardb1e51872008-10-07 11:08:18 +0000290 label3 = models.Label.smart_get('label3')
291 models.Host.smart_get('host1').labels.add(label3)
292
293 job = self._create_job_simple([1], use_metahosts)
showard989f25d2008-10-01 11:38:11 +0000294 # if the job doesn't depend on label3, there should be no scheduling
295 self._dispatcher._schedule_new_jobs()
296 self._check_for_extra_schedulings()
297
298 # now make the job depend on label3
showardb1e51872008-10-07 11:08:18 +0000299 job.dependency_labels.add(label3)
showard989f25d2008-10-01 11:38:11 +0000300 self._dispatcher._schedule_new_jobs()
301 self._assert_job_scheduled_on(1, 1)
302 self._check_for_extra_schedulings()
303
304 if use_metahosts:
305 # should also work if the metahost is the only_if_needed label
306 self._do_query('DELETE FROM jobs_dependency_labels')
307 self._create_job(metahosts=[3])
308 self._dispatcher._schedule_new_jobs()
309 self._assert_job_scheduled_on(2, 1)
310 self._check_for_extra_schedulings()
311
312
jadmanski0afbb632008-06-06 21:10:57 +0000313 def test_basic_scheduling(self):
314 self._test_basic_scheduling_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000315
316
jadmanski0afbb632008-06-06 21:10:57 +0000317 def test_priorities(self):
318 self._test_priorities_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000319
320
jadmanski0afbb632008-06-06 21:10:57 +0000321 def test_hosts_ready(self):
322 self._test_hosts_ready_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000323
324
jadmanski0afbb632008-06-06 21:10:57 +0000325 def test_hosts_idle(self):
326 self._test_hosts_idle_helper(False)
showardce38e0c2008-05-29 19:36:16 +0000327
328
showard63a34772008-08-18 19:32:50 +0000329 def test_obey_ACLs(self):
330 self._test_obey_ACLs_helper(False)
331
332
showard989f25d2008-10-01 11:38:11 +0000333 def test_only_if_needed_labels(self):
334 self._test_only_if_needed_labels_helper(False)
335
336
showard63a34772008-08-18 19:32:50 +0000337 def test_non_metahost_on_invalid_host(self):
338 """
339 Non-metahost entries can get scheduled on invalid hosts (this is how
340 one-time hosts work).
341 """
342 self._do_query('UPDATE hosts SET invalid=1')
343 self._test_basic_scheduling_helper(False)
344
345
jadmanski0afbb632008-06-06 21:10:57 +0000346 def test_metahost_scheduling(self):
showard63a34772008-08-18 19:32:50 +0000347 """
348 Basic metahost scheduling
349 """
jadmanski0afbb632008-06-06 21:10:57 +0000350 self._test_basic_scheduling_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000351
352
jadmanski0afbb632008-06-06 21:10:57 +0000353 def test_metahost_priorities(self):
354 self._test_priorities_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000355
356
jadmanski0afbb632008-06-06 21:10:57 +0000357 def test_metahost_hosts_ready(self):
358 self._test_hosts_ready_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000359
360
jadmanski0afbb632008-06-06 21:10:57 +0000361 def test_metahost_hosts_idle(self):
362 self._test_hosts_idle_helper(True)
showardce38e0c2008-05-29 19:36:16 +0000363
364
showard63a34772008-08-18 19:32:50 +0000365 def test_metahost_obey_ACLs(self):
366 self._test_obey_ACLs_helper(True)
367
368
showard989f25d2008-10-01 11:38:11 +0000369 def test_metahost_only_if_needed_labels(self):
370 self._test_only_if_needed_labels_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])
380 self._dispatcher._schedule_new_jobs()
381 self._assert_job_scheduled_on(2, 1)
382 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000383
384
jadmanski0afbb632008-06-06 21:10:57 +0000385 def test_metahosts_obey_blocks(self):
386 """
387 Metahosts can't get scheduled on hosts already scheduled for
388 that job.
389 """
390 self._create_job(metahosts=[1], hosts=[1])
391 # make the nonmetahost entry complete, so the metahost can try
392 # to get scheduled
showard56193bb2008-08-13 20:07:41 +0000393 self._update_hqe(set='complete = 1', where='host_id=1')
jadmanski0afbb632008-06-06 21:10:57 +0000394 self._dispatcher._schedule_new_jobs()
395 self._check_for_extra_schedulings()
showardce38e0c2008-05-29 19:36:16 +0000396
397
showard56193bb2008-08-13 20:07:41 +0000398 def test_only_schedule_queued_entries(self):
399 self._create_job(metahosts=[1])
400 self._update_hqe(set='active=1, host_id=2')
401 self._dispatcher._schedule_new_jobs()
402 self._check_for_extra_schedulings()
403
404
showardfa8629c2008-11-04 16:51:23 +0000405 def test_no_ready_hosts(self):
406 self._create_job(hosts=[1])
407 self._do_query('UPDATE hosts SET status="Repair Failed"')
408 self._dispatcher._schedule_new_jobs()
409 self._check_for_extra_schedulings()
410
411
showardb2e2c322008-10-14 17:33:55 +0000412class DispatcherThrottlingTest(BaseSchedulerTest):
showard4c5374f2008-09-04 17:02:56 +0000413 """
414 Test that the dispatcher throttles:
415 * total number of running processes
416 * number of processes started per cycle
417 """
418 _MAX_RUNNING = 3
419 _MAX_STARTED = 2
420
421 def setUp(self):
422 super(DispatcherThrottlingTest, self).setUp()
showardd1ee1dd2009-01-07 21:33:08 +0000423 scheduler_config.config.max_running_processes = self._MAX_RUNNING
424 scheduler_config.config.max_processes_started_per_cycle = (
425 self._MAX_STARTED)
showard4c5374f2008-09-04 17:02:56 +0000426
427
showard4c5374f2008-09-04 17:02:56 +0000428 def _setup_some_agents(self, num_agents):
showard170873e2009-01-07 00:22:26 +0000429 self._agents = [DummyAgent() for i in xrange(num_agents)]
showard4c5374f2008-09-04 17:02:56 +0000430 self._dispatcher._agents = list(self._agents)
431
432
433 def _run_a_few_cycles(self):
434 for i in xrange(4):
435 self._dispatcher._handle_agents()
436
437
438 def _assert_agents_started(self, indexes, is_started=True):
439 for i in indexes:
440 self.assert_(self._agents[i].is_running() == is_started,
441 'Agent %d %sstarted' %
442 (i, is_started and 'not ' or ''))
443
444
445 def _assert_agents_not_started(self, indexes):
446 self._assert_agents_started(indexes, False)
447
448
449 def test_throttle_total(self):
450 self._setup_some_agents(4)
451 self._run_a_few_cycles()
452 self._assert_agents_started([0, 1, 2])
453 self._assert_agents_not_started([3])
454
455
456 def test_throttle_per_cycle(self):
457 self._setup_some_agents(3)
458 self._dispatcher._handle_agents()
459 self._assert_agents_started([0, 1])
460 self._assert_agents_not_started([2])
461
462
463 def test_throttle_with_synchronous(self):
464 self._setup_some_agents(2)
465 self._agents[0].num_processes = 3
466 self._run_a_few_cycles()
467 self._assert_agents_started([0])
468 self._assert_agents_not_started([1])
469
470
471 def test_large_agent_starvation(self):
472 """
473 Ensure large agents don't get starved by lower-priority agents.
474 """
475 self._setup_some_agents(3)
476 self._agents[1].num_processes = 3
477 self._run_a_few_cycles()
478 self._assert_agents_started([0])
479 self._assert_agents_not_started([1, 2])
480
481 self._agents[0].set_done(True)
482 self._run_a_few_cycles()
483 self._assert_agents_started([1])
484 self._assert_agents_not_started([2])
485
486
487 def test_zero_process_agent(self):
488 self._setup_some_agents(5)
489 self._agents[4].num_processes = 0
490 self._run_a_few_cycles()
491 self._assert_agents_started([0, 1, 2, 4])
492 self._assert_agents_not_started([3])
493
494
showard1be97432008-10-17 15:30:45 +0000495class FindAbortTest(BaseSchedulerTest):
showard56193bb2008-08-13 20:07:41 +0000496 """
showard1be97432008-10-17 15:30:45 +0000497 Test the dispatcher abort functionality.
showard56193bb2008-08-13 20:07:41 +0000498 """
showard170873e2009-01-07 00:22:26 +0000499 def _check_abort_agent(self, agent, entry_id):
showard1be97432008-10-17 15:30:45 +0000500 self.assert_(isinstance(agent, monitor_db.Agent))
501 tasks = list(agent.queue.queue)
showard170873e2009-01-07 00:22:26 +0000502 self.assertEquals(len(tasks), 1)
showard9d9ffd52008-11-09 23:14:35 +0000503 abort = tasks[0]
showard170873e2009-01-07 00:22:26 +0000504
showard1be97432008-10-17 15:30:45 +0000505 self.assert_(isinstance(abort, monitor_db.AbortTask))
showard170873e2009-01-07 00:22:26 +0000506 self.assertEquals(abort.queue_entry.id, entry_id)
showard1be97432008-10-17 15:30:45 +0000507
showard9d9ffd52008-11-09 23:14:35 +0000508
showard170873e2009-01-07 00:22:26 +0000509 def _check_host_agent(self, agent, host_id):
510 self.assert_(isinstance(agent, monitor_db.Agent))
511 tasks = list(agent.queue.queue)
512 self.assertEquals(len(tasks), 2)
513 cleanup, verify = tasks
showard9d9ffd52008-11-09 23:14:35 +0000514
showard45ae8192008-11-05 19:32:53 +0000515 self.assert_(isinstance(cleanup, monitor_db.CleanupTask))
showard170873e2009-01-07 00:22:26 +0000516 self.assertEquals(cleanup.host.id, host_id)
showard1be97432008-10-17 15:30:45 +0000517
518 self.assert_(isinstance(verify, monitor_db.VerifyTask))
showard170873e2009-01-07 00:22:26 +0000519 self.assertEquals(verify.host.id, host_id)
showard56193bb2008-08-13 20:07:41 +0000520
521
showard9d9ffd52008-11-09 23:14:35 +0000522 def _check_agents(self, agents, include_host_tasks):
showard170873e2009-01-07 00:22:26 +0000523 agents = list(agents)
524 if include_host_tasks:
525 self.assertEquals(len(agents), 4)
526 self._check_host_agent(agents.pop(0), 1)
527 self._check_host_agent(agents.pop(1), 2)
528
showard1be97432008-10-17 15:30:45 +0000529 self.assertEquals(len(agents), 2)
showard170873e2009-01-07 00:22:26 +0000530 self._check_abort_agent(agents[0], 1)
531 self._check_abort_agent(agents[1], 2)
showard56193bb2008-08-13 20:07:41 +0000532
533
534 def test_find_aborting_inactive(self):
535 self._create_job(hosts=[1, 2])
536 self._update_hqe(set='status="Abort"')
537
showard56193bb2008-08-13 20:07:41 +0000538 self._dispatcher._find_aborting()
539
showard9d9ffd52008-11-09 23:14:35 +0000540 self._check_agents(self._dispatcher._agents, include_host_tasks=False)
showard56193bb2008-08-13 20:07:41 +0000541 self.god.check_playback()
542
543
544 def test_find_aborting_active(self):
545 self._create_job(hosts=[1, 2])
546 self._update_hqe(set='status="Abort", active=1')
547 # have to make an Agent for the active HQEs
showard1be97432008-10-17 15:30:45 +0000548 agent = self.god.create_mock_class(monitor_db.Agent, 'old_agent')
showard170873e2009-01-07 00:22:26 +0000549 agent.host_ids = agent.queue_entry_ids = [1, 2]
showard56193bb2008-08-13 20:07:41 +0000550 self._dispatcher.add_agent(agent)
551
showard56193bb2008-08-13 20:07:41 +0000552 self._dispatcher._find_aborting()
553
showard9d9ffd52008-11-09 23:14:35 +0000554 self._check_agents(self._dispatcher._agents, include_host_tasks=True)
showard56193bb2008-08-13 20:07:41 +0000555 self.god.check_playback()
556
showard1be97432008-10-17 15:30:45 +0000557 # ensure agent gets aborted
showard170873e2009-01-07 00:22:26 +0000558 abort1 = self._dispatcher._agents[1].queue.queue[0]
showard1be97432008-10-17 15:30:45 +0000559 self.assertEquals(abort1.agents_to_abort, [agent])
showard170873e2009-01-07 00:22:26 +0000560 abort2 = self._dispatcher._agents[3].queue.queue[0]
showard1be97432008-10-17 15:30:45 +0000561 self.assertEquals(abort2.agents_to_abort, [])
562
showard56193bb2008-08-13 20:07:41 +0000563
showard98863972008-10-29 21:14:56 +0000564class JobTimeoutTest(BaseSchedulerTest):
showard2bab8f42008-11-12 18:15:22 +0000565 def _test_synch_start_timeout_helper(self, expect_abort,
566 set_created_on=True, set_active=True,
567 set_acl=True):
showardd1ee1dd2009-01-07 21:33:08 +0000568 scheduler_config.config.synch_job_start_timeout_minutes = 60
showard98863972008-10-29 21:14:56 +0000569 job = self._create_job(hosts=[1, 2])
showard98863972008-10-29 21:14:56 +0000570 if set_active:
571 hqe = job.hostqueueentry_set.filter(host__id=1)[0]
572 hqe.status = 'Pending'
573 hqe.active = 1
574 hqe.save()
575
576 everyone_acl = models.AclGroup.smart_get('Everyone')
577 host1 = models.Host.smart_get(1)
578 if set_acl:
579 everyone_acl.hosts.add(host1)
580 else:
581 everyone_acl.hosts.remove(host1)
582
583 job.created_on = datetime.datetime.now()
584 if set_created_on:
585 job.created_on -= datetime.timedelta(minutes=100)
586 job.save()
587
588 self._dispatcher._abort_jobs_past_synch_start_timeout()
589
590 for hqe in job.hostqueueentry_set.all():
591 if expect_abort:
592 self.assert_(hqe.status in ('Abort', 'Aborted'), hqe.status)
593 else:
594 self.assert_(hqe.status not in ('Abort', 'Aborted'), hqe.status)
595
596
597 def test_synch_start_timeout_helper(self):
598 # no abort if any of the condition aren't met
showard2bab8f42008-11-12 18:15:22 +0000599 self._test_synch_start_timeout_helper(False, set_created_on=False)
600 self._test_synch_start_timeout_helper(False, set_active=False)
601 self._test_synch_start_timeout_helper(False, set_acl=False)
showard98863972008-10-29 21:14:56 +0000602 # abort if all conditions are met
showard2bab8f42008-11-12 18:15:22 +0000603 self._test_synch_start_timeout_helper(True)
showard98863972008-10-29 21:14:56 +0000604
605
jadmanski3d161b02008-06-06 15:43:36 +0000606class PidfileRunMonitorTest(unittest.TestCase):
showard170873e2009-01-07 00:22:26 +0000607 execution_tag = 'test_tag'
jadmanski0afbb632008-06-06 21:10:57 +0000608 pid = 12345
showard170873e2009-01-07 00:22:26 +0000609 process = drone_manager.Process('myhost', pid)
showard21baa452008-10-21 00:08:39 +0000610 num_tests_failed = 1
jadmanski3d161b02008-06-06 15:43:36 +0000611
jadmanski0afbb632008-06-06 21:10:57 +0000612 def setUp(self):
613 self.god = mock.mock_god()
showard170873e2009-01-07 00:22:26 +0000614 self.mock_drone_manager = self.god.create_mock_class(
615 drone_manager.DroneManager, 'drone_manager')
616 self.god.stub_with(monitor_db, '_drone_manager',
617 self.mock_drone_manager)
618 self.god.stub_function(email_manager.manager, 'enqueue_notify_email')
619
620 self.pidfile_id = object()
621
622 self.mock_drone_manager.get_pidfile_id_from.expect_call(
623 self.execution_tag).and_return(self.pidfile_id)
624 self.mock_drone_manager.register_pidfile.expect_call(self.pidfile_id)
625
626 self.monitor = monitor_db.PidfileRunMonitor()
627 self.monitor.attach_to_existing_process(self.execution_tag)
jadmanski3d161b02008-06-06 15:43:36 +0000628
629
jadmanski0afbb632008-06-06 21:10:57 +0000630 def tearDown(self):
631 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +0000632
633
showard170873e2009-01-07 00:22:26 +0000634 def setup_pidfile(self, pid=None, exit_code=None, tests_failed=None,
635 use_second_read=False):
636 contents = drone_manager.PidfileContents()
637 if pid is not None:
638 contents.process = drone_manager.Process('myhost', pid)
639 contents.exit_status = exit_code
640 contents.num_tests_failed = tests_failed
641 self.mock_drone_manager.get_pidfile_contents.expect_call(
642 self.pidfile_id, use_second_read=use_second_read).and_return(
643 contents)
644
645
jadmanski0afbb632008-06-06 21:10:57 +0000646 def set_not_yet_run(self):
showard170873e2009-01-07 00:22:26 +0000647 self.setup_pidfile()
jadmanski3d161b02008-06-06 15:43:36 +0000648
649
showard3dd6b882008-10-27 19:21:39 +0000650 def set_empty_pidfile(self):
showard170873e2009-01-07 00:22:26 +0000651 self.setup_pidfile()
showard3dd6b882008-10-27 19:21:39 +0000652
653
showard170873e2009-01-07 00:22:26 +0000654 def set_running(self, use_second_read=False):
655 self.setup_pidfile(self.pid, use_second_read=use_second_read)
jadmanski3d161b02008-06-06 15:43:36 +0000656
657
showard170873e2009-01-07 00:22:26 +0000658 def set_complete(self, error_code, use_second_read=False):
659 self.setup_pidfile(self.pid, error_code, self.num_tests_failed,
660 use_second_read=use_second_read)
661
662
663 def _check_monitor(self, expected_pid, expected_exit_status,
664 expected_num_tests_failed):
665 if expected_pid is None:
666 self.assertEquals(self.monitor._state.process, None)
667 else:
668 self.assertEquals(self.monitor._state.process.pid, expected_pid)
669 self.assertEquals(self.monitor._state.exit_status, expected_exit_status)
670 self.assertEquals(self.monitor._state.num_tests_failed,
671 expected_num_tests_failed)
672
673
674 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000675
676
showard21baa452008-10-21 00:08:39 +0000677 def _test_read_pidfile_helper(self, expected_pid, expected_exit_status,
678 expected_num_tests_failed):
679 self.monitor._read_pidfile()
showard170873e2009-01-07 00:22:26 +0000680 self._check_monitor(expected_pid, expected_exit_status,
681 expected_num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000682
683
showard21baa452008-10-21 00:08:39 +0000684 def _get_expected_tests_failed(self, expected_exit_status):
685 if expected_exit_status is None:
686 expected_tests_failed = None
687 else:
688 expected_tests_failed = self.num_tests_failed
689 return expected_tests_failed
690
691
jadmanski0afbb632008-06-06 21:10:57 +0000692 def test_read_pidfile(self):
693 self.set_not_yet_run()
showard21baa452008-10-21 00:08:39 +0000694 self._test_read_pidfile_helper(None, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000695
showard3dd6b882008-10-27 19:21:39 +0000696 self.set_empty_pidfile()
697 self._test_read_pidfile_helper(None, None, None)
698
jadmanski0afbb632008-06-06 21:10:57 +0000699 self.set_running()
showard21baa452008-10-21 00:08:39 +0000700 self._test_read_pidfile_helper(self.pid, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000701
jadmanski0afbb632008-06-06 21:10:57 +0000702 self.set_complete(123)
showard21baa452008-10-21 00:08:39 +0000703 self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000704
705
jadmanski0afbb632008-06-06 21:10:57 +0000706 def test_read_pidfile_error(self):
showard170873e2009-01-07 00:22:26 +0000707 self.mock_drone_manager.get_pidfile_contents.expect_call(
708 self.pidfile_id, use_second_read=False).and_return(
709 drone_manager.InvalidPidfile('error'))
710 self.assertRaises(monitor_db.PidfileRunMonitor._PidfileException,
showard21baa452008-10-21 00:08:39 +0000711 self.monitor._read_pidfile)
jadmanski0afbb632008-06-06 21:10:57 +0000712 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000713
714
showard170873e2009-01-07 00:22:26 +0000715 def setup_is_running(self, is_running):
716 self.mock_drone_manager.is_process_running.expect_call(
717 self.process).and_return(is_running)
jadmanski3d161b02008-06-06 15:43:36 +0000718
719
showard21baa452008-10-21 00:08:39 +0000720 def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status,
721 expected_num_tests_failed):
722 self.monitor._get_pidfile_info()
showard170873e2009-01-07 00:22:26 +0000723 self._check_monitor(expected_pid, expected_exit_status,
724 expected_num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000725
726
jadmanski0afbb632008-06-06 21:10:57 +0000727 def test_get_pidfile_info(self):
showard21baa452008-10-21 00:08:39 +0000728 """
729 normal cases for get_pidfile_info
730 """
jadmanski0afbb632008-06-06 21:10:57 +0000731 # running
732 self.set_running()
showard170873e2009-01-07 00:22:26 +0000733 self.setup_is_running(True)
showard21baa452008-10-21 00:08:39 +0000734 self._test_get_pidfile_info_helper(self.pid, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000735
jadmanski0afbb632008-06-06 21:10:57 +0000736 # exited during check
737 self.set_running()
showard170873e2009-01-07 00:22:26 +0000738 self.setup_is_running(False)
739 self.set_complete(123, use_second_read=True) # pidfile gets read again
showard21baa452008-10-21 00:08:39 +0000740 self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000741
jadmanski0afbb632008-06-06 21:10:57 +0000742 # completed
743 self.set_complete(123)
showard21baa452008-10-21 00:08:39 +0000744 self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)
jadmanski3d161b02008-06-06 15:43:36 +0000745
746
jadmanski0afbb632008-06-06 21:10:57 +0000747 def test_get_pidfile_info_running_no_proc(self):
showard21baa452008-10-21 00:08:39 +0000748 """
749 pidfile shows process running, but no proc exists
750 """
jadmanski0afbb632008-06-06 21:10:57 +0000751 # running but no proc
752 self.set_running()
showard170873e2009-01-07 00:22:26 +0000753 self.setup_is_running(False)
754 self.set_running(use_second_read=True)
755 email_manager.manager.enqueue_notify_email.expect_call(
jadmanski0afbb632008-06-06 21:10:57 +0000756 mock.is_string_comparator(), mock.is_string_comparator())
showard21baa452008-10-21 00:08:39 +0000757 self._test_get_pidfile_info_helper(self.pid, 1, 0)
jadmanski0afbb632008-06-06 21:10:57 +0000758 self.assertTrue(self.monitor.lost_process)
jadmanski3d161b02008-06-06 15:43:36 +0000759
760
jadmanski0afbb632008-06-06 21:10:57 +0000761 def test_get_pidfile_info_not_yet_run(self):
showard21baa452008-10-21 00:08:39 +0000762 """
763 pidfile hasn't been written yet
764 """
jadmanski0afbb632008-06-06 21:10:57 +0000765 self.set_not_yet_run()
showard21baa452008-10-21 00:08:39 +0000766 self._test_get_pidfile_info_helper(None, None, None)
jadmanski3d161b02008-06-06 15:43:36 +0000767
jadmanski3d161b02008-06-06 15:43:36 +0000768
showard170873e2009-01-07 00:22:26 +0000769 def test_process_failed_to_write_pidfile(self):
jadmanski0afbb632008-06-06 21:10:57 +0000770 self.set_not_yet_run()
showard170873e2009-01-07 00:22:26 +0000771 email_manager.manager.enqueue_notify_email.expect_call(
772 mock.is_string_comparator(), mock.is_string_comparator())
773 dummy_process = drone_manager.Process('dummy', 12345)
774 self.mock_drone_manager.get_dummy_process.expect_call().and_return(
775 dummy_process)
776 self.monitor._start_time = time.time() - monitor_db.PIDFILE_TIMEOUT - 1
777 self._test_get_pidfile_info_helper(12345, 1, 0)
jadmanski3d161b02008-06-06 15:43:36 +0000778
779
780class AgentTest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +0000781 def setUp(self):
782 self.god = mock.mock_god()
jadmanski3d161b02008-06-06 15:43:36 +0000783
784
jadmanski0afbb632008-06-06 21:10:57 +0000785 def tearDown(self):
786 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +0000787
788
showard170873e2009-01-07 00:22:26 +0000789 def _create_mock_task(self, name):
790 task = self.god.create_mock_class(monitor_db.AgentTask, name)
791 task.host_ids = task.queue_entry_ids = []
792 return task
793
794
jadmanski0afbb632008-06-06 21:10:57 +0000795 def test_agent(self):
showard170873e2009-01-07 00:22:26 +0000796 task1 = self._create_mock_task('task1')
797 task2 = self._create_mock_task('task2')
798 task3 = self._create_mock_task('task3')
jadmanski3d161b02008-06-06 15:43:36 +0000799
jadmanski0afbb632008-06-06 21:10:57 +0000800 task1.start.expect_call()
801 task1.is_done.expect_call().and_return(False)
802 task1.poll.expect_call()
803 task1.is_done.expect_call().and_return(True)
804 task1.is_done.expect_call().and_return(True)
805 task1.success = True
jadmanski3d161b02008-06-06 15:43:36 +0000806
jadmanski0afbb632008-06-06 21:10:57 +0000807 task2.start.expect_call()
808 task2.is_done.expect_call().and_return(True)
809 task2.is_done.expect_call().and_return(True)
810 task2.success = False
811 task2.failure_tasks = [task3]
jadmanski3d161b02008-06-06 15:43:36 +0000812
jadmanski0afbb632008-06-06 21:10:57 +0000813 task3.start.expect_call()
814 task3.is_done.expect_call().and_return(True)
815 task3.is_done.expect_call().and_return(True)
816 task3.success = True
jadmanski3d161b02008-06-06 15:43:36 +0000817
jadmanski0afbb632008-06-06 21:10:57 +0000818 agent = monitor_db.Agent([task1, task2])
819 agent.dispatcher = object()
820 agent.start()
821 while not agent.is_done():
822 agent.tick()
823 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000824
825
826class AgentTasksTest(unittest.TestCase):
showard170873e2009-01-07 00:22:26 +0000827 TEMP_DIR = '/abspath/tempdir'
showard97aed502008-11-04 02:01:24 +0000828 RESULTS_DIR = '/results/dir'
jadmanski0afbb632008-06-06 21:10:57 +0000829 HOSTNAME = 'myhost'
showard170873e2009-01-07 00:22:26 +0000830 DUMMY_PROCESS = object()
jadmanskifb7cfb12008-07-09 14:13:21 +0000831 HOST_PROTECTION = host_protections.default
showard170873e2009-01-07 00:22:26 +0000832 PIDFILE_ID = object()
jadmanski3d161b02008-06-06 15:43:36 +0000833
jadmanski0afbb632008-06-06 21:10:57 +0000834 def setUp(self):
835 self.god = mock.mock_god()
showard170873e2009-01-07 00:22:26 +0000836 self.god.stub_with(drone_manager.DroneManager, 'get_temporary_path',
837 mock.mock_function('get_temporary_path',
838 default_return_val='tempdir'))
839 self.god.stub_function(drone_manager.DroneManager,
840 'copy_to_results_repository')
841 self.god.stub_function(drone_manager.DroneManager,
842 'get_pidfile_id_from')
843
844 def dummy_absolute_path(self, path):
845 return '/abspath/' + path
846 self.god.stub_with(drone_manager.DroneManager, 'absolute_path',
847 dummy_absolute_path)
848
849 self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'run')
850 self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'exit_code')
851 self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'get_process')
jadmanski0afbb632008-06-06 21:10:57 +0000852 self.host = self.god.create_mock_class(monitor_db.Host, 'host')
showard170873e2009-01-07 00:22:26 +0000853 self.host.id = 1
jadmanski0afbb632008-06-06 21:10:57 +0000854 self.host.hostname = self.HOSTNAME
jadmanskifb7cfb12008-07-09 14:13:21 +0000855 self.host.protection = self.HOST_PROTECTION
jadmanski0afbb632008-06-06 21:10:57 +0000856 self.queue_entry = self.god.create_mock_class(
857 monitor_db.HostQueueEntry, 'queue_entry')
showard97aed502008-11-04 02:01:24 +0000858 self.job = self.god.create_mock_class(monitor_db.Job, 'job')
showard170873e2009-01-07 00:22:26 +0000859 self.queue_entry.id = 1
showard97aed502008-11-04 02:01:24 +0000860 self.queue_entry.job = self.job
jadmanski0afbb632008-06-06 21:10:57 +0000861 self.queue_entry.host = self.host
862 self.queue_entry.meta_host = None
jadmanski3d161b02008-06-06 15:43:36 +0000863
864
jadmanski0afbb632008-06-06 21:10:57 +0000865 def tearDown(self):
866 self.god.unstub_all()
jadmanski3d161b02008-06-06 15:43:36 +0000867
868
jadmanski0afbb632008-06-06 21:10:57 +0000869 def run_task(self, task, success):
870 """
871 Do essentially what an Agent would do, but protect againt
872 infinite looping from test errors.
873 """
874 if not getattr(task, 'agent', None):
875 task.agent = object()
876 task.start()
877 count = 0
878 while not task.is_done():
879 count += 1
880 if count > 10:
881 print 'Task failed to finish'
882 # in case the playback has clues to why it
883 # failed
884 self.god.check_playback()
885 self.fail()
886 task.poll()
887 self.assertEquals(task.success, success)
jadmanski3d161b02008-06-06 15:43:36 +0000888
889
showard170873e2009-01-07 00:22:26 +0000890 def setup_run_monitor(self, exit_status, copy_log_file=True):
891 monitor_db.PidfileRunMonitor.run.expect_call(
892 mock.is_instance_comparator(list),
893 'tempdir',
894 nice_level=monitor_db.AUTOSERV_NICE_LEVEL,
895 log_file=mock.anything_comparator())
896 monitor_db.PidfileRunMonitor.exit_code.expect_call()
897 monitor_db.PidfileRunMonitor.exit_code.expect_call().and_return(
jadmanski0afbb632008-06-06 21:10:57 +0000898 exit_status)
jadmanski3d161b02008-06-06 15:43:36 +0000899
showard170873e2009-01-07 00:22:26 +0000900 if copy_log_file:
901 self._setup_move_logfile()
902
903
904 def _setup_move_logfile(self, include_destination=False):
905 monitor_db.PidfileRunMonitor.get_process.expect_call().and_return(
906 self.DUMMY_PROCESS)
907 if include_destination:
908 drone_manager.DroneManager.copy_to_results_repository.expect_call(
909 self.DUMMY_PROCESS, mock.is_string_comparator(),
910 destination_path=mock.is_string_comparator())
911 else:
912 drone_manager.DroneManager.copy_to_results_repository.expect_call(
913 self.DUMMY_PROCESS, mock.is_string_comparator())
914
jadmanski3d161b02008-06-06 15:43:36 +0000915
jadmanski0afbb632008-06-06 21:10:57 +0000916 def _test_repair_task_helper(self, success):
917 self.host.set_status.expect_call('Repairing')
918 if success:
919 self.setup_run_monitor(0)
920 self.host.set_status.expect_call('Ready')
921 else:
922 self.setup_run_monitor(1)
923 self.host.set_status.expect_call('Repair Failed')
jadmanski3d161b02008-06-06 15:43:36 +0000924
jadmanski0afbb632008-06-06 21:10:57 +0000925 task = monitor_db.RepairTask(self.host)
showard56193bb2008-08-13 20:07:41 +0000926 self.assertEquals(task.failure_tasks, [])
jadmanski0afbb632008-06-06 21:10:57 +0000927 self.run_task(task, success)
jadmanskifb7cfb12008-07-09 14:13:21 +0000928
929 expected_protection = host_protections.Protection.get_string(
930 host_protections.default)
mbligh3e0f7e02008-07-28 19:42:01 +0000931 expected_protection = host_protections.Protection.get_attr_name(
932 expected_protection)
933
showard170873e2009-01-07 00:22:26 +0000934 self.assertTrue(set(task.cmd) >=
935 set([monitor_db._autoserv_path, '-p', '-R', '-m',
936 self.HOSTNAME, '-r', self.TEMP_DIR,
937 '--host-protection', expected_protection]))
jadmanski0afbb632008-06-06 21:10:57 +0000938 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000939
940
jadmanski0afbb632008-06-06 21:10:57 +0000941 def test_repair_task(self):
942 self._test_repair_task_helper(True)
943 self._test_repair_task_helper(False)
jadmanski3d161b02008-06-06 15:43:36 +0000944
945
jadmanski0afbb632008-06-06 21:10:57 +0000946 def test_repair_task_with_queue_entry(self):
jadmanski0afbb632008-06-06 21:10:57 +0000947 self.host.set_status.expect_call('Repairing')
showarde788ea62008-11-17 21:02:47 +0000948 self.queue_entry.requeue.expect_call()
jadmanski0afbb632008-06-06 21:10:57 +0000949 self.setup_run_monitor(1)
950 self.host.set_status.expect_call('Repair Failed')
showarde788ea62008-11-17 21:02:47 +0000951 self.queue_entry.handle_host_failure.expect_call()
jadmanski3d161b02008-06-06 15:43:36 +0000952
showarde788ea62008-11-17 21:02:47 +0000953 task = monitor_db.RepairTask(self.host, self.queue_entry)
jadmanski0afbb632008-06-06 21:10:57 +0000954 self.run_task(task, False)
955 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +0000956
957
jadmanski0afbb632008-06-06 21:10:57 +0000958 def setup_verify_expects(self, success, use_queue_entry):
959 if use_queue_entry:
showard8fe93b52008-11-18 17:53:22 +0000960 self.queue_entry.set_status.expect_call('Verifying')
jadmanski0afbb632008-06-06 21:10:57 +0000961 self.host.set_status.expect_call('Verifying')
962 if success:
963 self.setup_run_monitor(0)
964 self.host.set_status.expect_call('Ready')
showard2bab8f42008-11-12 18:15:22 +0000965 if use_queue_entry:
966 self.queue_entry.on_pending.expect_call()
jadmanski0afbb632008-06-06 21:10:57 +0000967 else:
968 self.setup_run_monitor(1)
showard8fe93b52008-11-18 17:53:22 +0000969 if use_queue_entry and not self.queue_entry.meta_host:
970 self.queue_entry.set_execution_subdir.expect_call()
showard170873e2009-01-07 00:22:26 +0000971 self.queue_entry.execution_tag.expect_call().and_return('tag')
972 self._setup_move_logfile(include_destination=True)
jadmanski3d161b02008-06-06 15:43:36 +0000973
974
showard56193bb2008-08-13 20:07:41 +0000975 def _check_verify_failure_tasks(self, verify_task):
976 self.assertEquals(len(verify_task.failure_tasks), 1)
977 repair_task = verify_task.failure_tasks[0]
978 self.assert_(isinstance(repair_task, monitor_db.RepairTask))
979 self.assertEquals(verify_task.host, repair_task.host)
showard8fe93b52008-11-18 17:53:22 +0000980 if verify_task.queue_entry:
981 self.assertEquals(repair_task.queue_entry, verify_task.queue_entry)
showard56193bb2008-08-13 20:07:41 +0000982 else:
showarde788ea62008-11-17 21:02:47 +0000983 self.assertEquals(repair_task.queue_entry, None)
showard56193bb2008-08-13 20:07:41 +0000984
985
986 def _test_verify_task_helper(self, success, use_queue_entry=False,
987 use_meta_host=False):
jadmanski0afbb632008-06-06 21:10:57 +0000988 self.setup_verify_expects(success, use_queue_entry)
jadmanski3d161b02008-06-06 15:43:36 +0000989
jadmanski0afbb632008-06-06 21:10:57 +0000990 if use_queue_entry:
showard170873e2009-01-07 00:22:26 +0000991 task = monitor_db.VerifyTask(queue_entry=self.queue_entry)
jadmanski0afbb632008-06-06 21:10:57 +0000992 else:
993 task = monitor_db.VerifyTask(host=self.host)
showard56193bb2008-08-13 20:07:41 +0000994 self._check_verify_failure_tasks(task)
jadmanski0afbb632008-06-06 21:10:57 +0000995 self.run_task(task, success)
showard170873e2009-01-07 00:22:26 +0000996 self.assertTrue(set(task.cmd) >=
997 set([monitor_db._autoserv_path, '-p', '-v', '-m',
998 self.HOSTNAME, '-r', self.TEMP_DIR]))
jadmanski0afbb632008-06-06 21:10:57 +0000999 self.god.check_playback()
jadmanski3d161b02008-06-06 15:43:36 +00001000
1001
jadmanski0afbb632008-06-06 21:10:57 +00001002 def test_verify_task_with_host(self):
showard56193bb2008-08-13 20:07:41 +00001003 self._test_verify_task_helper(True)
1004 self._test_verify_task_helper(False)
jadmanski3d161b02008-06-06 15:43:36 +00001005
1006
jadmanski0afbb632008-06-06 21:10:57 +00001007 def test_verify_task_with_queue_entry(self):
showard56193bb2008-08-13 20:07:41 +00001008 self._test_verify_task_helper(True, use_queue_entry=True)
1009 self._test_verify_task_helper(False, use_queue_entry=True)
1010
1011
1012 def test_verify_task_with_metahost(self):
showard8fe93b52008-11-18 17:53:22 +00001013 self.queue_entry.meta_host = 1
1014 self.test_verify_task_with_queue_entry()
jadmanski3d161b02008-06-06 15:43:36 +00001015
1016
showard1be97432008-10-17 15:30:45 +00001017 def test_abort_task(self):
1018 queue_entry = self.god.create_mock_class(monitor_db.HostQueueEntry,
1019 'queue_entry')
showard170873e2009-01-07 00:22:26 +00001020 queue_entry.id = 1
showard1be97432008-10-17 15:30:45 +00001021 queue_entry.host_id, queue_entry.job_id = 1, 2
1022 task = self.god.create_mock_class(monitor_db.AgentTask, 'task')
1023 agent = self.god.create_mock_class(monitor_db.Agent, 'agent')
1024 agent.active_task = task
1025
showard1be97432008-10-17 15:30:45 +00001026 task.abort.expect_call()
1027 queue_entry.set_status.expect_call('Aborted')
1028
1029 abort_task = monitor_db.AbortTask(queue_entry, [agent])
1030 self.run_task(abort_task, True)
showard97aed502008-11-04 02:01:24 +00001031 self.god.check_playback()
1032
1033
showard170873e2009-01-07 00:22:26 +00001034 def _setup_pre_parse_expects(self, autoserv_success):
1035 self.queue_entry.execution_tag.expect_call().and_return('tag')
1036 self.pidfile_monitor = monitor_db.PidfileRunMonitor.expect_new()
1037 self.pidfile_monitor.pidfile_id = self.PIDFILE_ID
1038 self.pidfile_monitor.attach_to_existing_process.expect_call('tag')
1039 if autoserv_success:
1040 code = 0
1041 else:
1042 code = 1
1043 self.pidfile_monitor.exit_code.expect_call().and_return(code)
1044
showard97aed502008-11-04 02:01:24 +00001045 self.queue_entry.set_status.expect_call('Parsing')
1046
1047
1048 def _setup_post_parse_expects(self, autoserv_success):
showard97aed502008-11-04 02:01:24 +00001049 if autoserv_success:
showard170873e2009-01-07 00:22:26 +00001050 status = 'Completed'
showard97aed502008-11-04 02:01:24 +00001051 else:
showard170873e2009-01-07 00:22:26 +00001052 status = 'Failed'
showard97aed502008-11-04 02:01:24 +00001053 self.queue_entry.set_status.expect_call(status)
1054
1055
showard170873e2009-01-07 00:22:26 +00001056 def setup_reparse_run_monitor(self):
1057 autoserv_pidfile_id = object()
1058 monitor = monitor_db.PidfileRunMonitor.expect_new()
1059 monitor.run.expect_call(
1060 mock.is_instance_comparator(list),
1061 'tag',
1062 log_file=mock.anything_comparator(),
1063 pidfile_name='.parser_execute',
1064 paired_with_pidfile=self.PIDFILE_ID)
1065 monitor.exit_code.expect_call()
1066 monitor.exit_code.expect_call().and_return(0)
1067 monitor.get_process.expect_call().and_return(self.DUMMY_PROCESS)
1068 drone_manager.DroneManager.copy_to_results_repository.expect_call(
1069 self.DUMMY_PROCESS, mock.is_string_comparator())
showard97aed502008-11-04 02:01:24 +00001070
showard170873e2009-01-07 00:22:26 +00001071
1072 def _test_final_reparse_task_helper(self, autoserv_success=True):
1073 self._setup_pre_parse_expects(autoserv_success)
1074 self.setup_reparse_run_monitor()
showard97aed502008-11-04 02:01:24 +00001075 self._setup_post_parse_expects(autoserv_success)
1076
1077 task = monitor_db.FinalReparseTask([self.queue_entry])
1078 self.run_task(task, True)
1079
1080 self.god.check_playback()
showard170873e2009-01-07 00:22:26 +00001081 cmd = [monitor_db._parser_path, '--write-pidfile', '-l', '2', '-r',
1082 '-o', '/abspath/tag']
showard97aed502008-11-04 02:01:24 +00001083 self.assertEquals(task.cmd, cmd)
1084
1085
1086 def test_final_reparse_task(self):
1087 self.god.stub_class(monitor_db, 'PidfileRunMonitor')
1088 self._test_final_reparse_task_helper()
showard97aed502008-11-04 02:01:24 +00001089 self._test_final_reparse_task_helper(autoserv_success=False)
1090
1091
1092 def test_final_reparse_throttling(self):
1093 self.god.stub_class(monitor_db, 'PidfileRunMonitor')
1094 self.god.stub_function(monitor_db.FinalReparseTask,
1095 '_can_run_new_parse')
1096
showard170873e2009-01-07 00:22:26 +00001097 self._setup_pre_parse_expects(True)
showard97aed502008-11-04 02:01:24 +00001098 monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return(
1099 False)
1100 monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return(
1101 True)
showard170873e2009-01-07 00:22:26 +00001102 self.setup_reparse_run_monitor()
showard97aed502008-11-04 02:01:24 +00001103 self._setup_post_parse_expects(True)
1104
1105 task = monitor_db.FinalReparseTask([self.queue_entry])
1106 self.run_task(task, True)
1107 self.god.check_playback()
showard1be97432008-10-17 15:30:45 +00001108
1109
showard45ae8192008-11-05 19:32:53 +00001110 def _test_cleanup_task_helper(self, success, use_queue_entry=False):
showardfa8629c2008-11-04 16:51:23 +00001111 if use_queue_entry:
1112 self.queue_entry.get_host.expect_call().and_return(self.host)
showard45ae8192008-11-05 19:32:53 +00001113 self.host.set_status.expect_call('Cleaning')
showardfa8629c2008-11-04 16:51:23 +00001114 if success:
1115 self.setup_run_monitor(0)
1116 self.host.set_status.expect_call('Ready')
1117 self.host.update_field.expect_call('dirty', 0)
1118 else:
1119 self.setup_run_monitor(1)
showard8fe93b52008-11-18 17:53:22 +00001120 if use_queue_entry and not self.queue_entry.meta_host:
1121 self.queue_entry.set_execution_subdir.expect_call()
showard170873e2009-01-07 00:22:26 +00001122 self.queue_entry.execution_tag.expect_call().and_return('tag')
1123 self._setup_move_logfile(include_destination=True)
showardfa8629c2008-11-04 16:51:23 +00001124
1125 if use_queue_entry:
showard45ae8192008-11-05 19:32:53 +00001126 task = monitor_db.CleanupTask(queue_entry=self.queue_entry)
showardfa8629c2008-11-04 16:51:23 +00001127 else:
showard45ae8192008-11-05 19:32:53 +00001128 task = monitor_db.CleanupTask(host=self.host)
showardfa8629c2008-11-04 16:51:23 +00001129 self.assertEquals(len(task.failure_tasks), 1)
1130 repair_task = task.failure_tasks[0]
1131 self.assert_(isinstance(repair_task, monitor_db.RepairTask))
1132 if use_queue_entry:
showarde788ea62008-11-17 21:02:47 +00001133 self.assertEquals(repair_task.queue_entry, self.queue_entry)
showardfa8629c2008-11-04 16:51:23 +00001134
1135 self.run_task(task, success)
1136
1137 self.god.check_playback()
showard170873e2009-01-07 00:22:26 +00001138 self.assert_(set(task.cmd) >=
1139 set([monitor_db._autoserv_path, '-p', '--cleanup', '-m',
1140 self.HOSTNAME, '-r', self.TEMP_DIR]))
showardfa8629c2008-11-04 16:51:23 +00001141
showard45ae8192008-11-05 19:32:53 +00001142 def test_cleanup_task(self):
1143 self._test_cleanup_task_helper(True)
1144 self._test_cleanup_task_helper(False)
showardfa8629c2008-11-04 16:51:23 +00001145
1146
showard45ae8192008-11-05 19:32:53 +00001147 def test_cleanup_task_with_queue_entry(self):
1148 self._test_cleanup_task_helper(False, True)
showardfa8629c2008-11-04 16:51:23 +00001149
1150
showardb2e2c322008-10-14 17:33:55 +00001151class JobTest(BaseSchedulerTest):
showard2bab8f42008-11-12 18:15:22 +00001152 def setUp(self):
1153 super(JobTest, self).setUp()
showard170873e2009-01-07 00:22:26 +00001154 self.god.stub_with(
1155 drone_manager.DroneManager, 'attach_file_to_execution',
1156 mock.mock_function('attach_file_to_execution',
1157 default_return_val='/test/path/tmp/foo'))
showard2bab8f42008-11-12 18:15:22 +00001158
1159
1160 def _setup_directory_expects(self, execution_subdir):
1161 job_path = os.path.join('.', '1-my_user')
1162 results_dir = os.path.join(job_path, execution_subdir)
showard2bab8f42008-11-12 18:15:22 +00001163
1164
showarde58e3f82008-11-20 19:04:59 +00001165 def _test_run_helper(self, expect_agent=True, expect_starting=False,
1166 expect_pending=False):
1167 if expect_starting:
1168 expected_status = models.HostQueueEntry.Status.STARTING
1169 elif expect_pending:
1170 expected_status = models.HostQueueEntry.Status.PENDING
1171 else:
1172 expected_status = models.HostQueueEntry.Status.VERIFYING
showardb2e2c322008-10-14 17:33:55 +00001173 job = monitor_db.Job.fetch('id = 1').next()
1174 queue_entry = monitor_db.HostQueueEntry.fetch('id = 1').next()
1175 agent = job.run(queue_entry)
1176
showard2bab8f42008-11-12 18:15:22 +00001177 self.god.check_playback()
showarde77ac672008-11-14 22:42:33 +00001178 self.assertEquals(models.HostQueueEntry.smart_get(1).status,
1179 expected_status)
showard2bab8f42008-11-12 18:15:22 +00001180
showard9976ce92008-10-15 20:28:13 +00001181 if not expect_agent:
1182 self.assertEquals(agent, None)
1183 return
1184
showardb2e2c322008-10-14 17:33:55 +00001185 self.assert_(isinstance(agent, monitor_db.Agent))
1186 tasks = list(agent.queue.queue)
1187 return tasks
1188
1189
1190 def test_run_asynchronous(self):
1191 self._create_job(hosts=[1, 2])
1192
1193 tasks = self._test_run_helper()
1194
showard2bab8f42008-11-12 18:15:22 +00001195 self.assertEquals(len(tasks), 1)
1196 verify_task = tasks[0]
showardb2e2c322008-10-14 17:33:55 +00001197
1198 self.assert_(isinstance(verify_task, monitor_db.VerifyTask))
1199 self.assertEquals(verify_task.queue_entry.id, 1)
1200
showardb2e2c322008-10-14 17:33:55 +00001201
showard9976ce92008-10-15 20:28:13 +00001202 def test_run_asynchronous_skip_verify(self):
1203 job = self._create_job(hosts=[1, 2])
1204 job.run_verify = False
1205 job.save()
showard2bab8f42008-11-12 18:15:22 +00001206 self._setup_directory_expects('host1')
showard9976ce92008-10-15 20:28:13 +00001207
showarde58e3f82008-11-20 19:04:59 +00001208 tasks = self._test_run_helper(expect_starting=True)
showard9976ce92008-10-15 20:28:13 +00001209
1210 self.assertEquals(len(tasks), 1)
1211 queue_task = tasks[0]
1212
1213 self.assert_(isinstance(queue_task, monitor_db.QueueTask))
1214 self.assertEquals(queue_task.job.id, 1)
1215
1216
showardb2e2c322008-10-14 17:33:55 +00001217 def test_run_synchronous_verify(self):
1218 self._create_job(hosts=[1, 2], synchronous=True)
1219
1220 tasks = self._test_run_helper()
1221 self.assertEquals(len(tasks), 1)
1222 verify_task = tasks[0]
1223
showard2bab8f42008-11-12 18:15:22 +00001224 self.assert_(isinstance(verify_task, monitor_db.VerifyTask))
showardb2e2c322008-10-14 17:33:55 +00001225 self.assertEquals(verify_task.queue_entry.id, 1)
1226
1227
showard9976ce92008-10-15 20:28:13 +00001228 def test_run_synchronous_skip_verify(self):
1229 job = self._create_job(hosts=[1, 2], synchronous=True)
1230 job.run_verify = False
1231 job.save()
1232
showarde58e3f82008-11-20 19:04:59 +00001233 self._test_run_helper(expect_agent=False, expect_pending=True)
showard9976ce92008-10-15 20:28:13 +00001234
1235 queue_entry = models.HostQueueEntry.smart_get(1)
1236 self.assertEquals(queue_entry.status, 'Pending')
1237
1238
showardb2e2c322008-10-14 17:33:55 +00001239 def test_run_synchronous_ready(self):
1240 self._create_job(hosts=[1, 2], synchronous=True)
showard2bab8f42008-11-12 18:15:22 +00001241 self._update_hqe("status='Pending', execution_subdir='")
1242 self._setup_directory_expects('group0')
showardb2e2c322008-10-14 17:33:55 +00001243
showarde58e3f82008-11-20 19:04:59 +00001244 tasks = self._test_run_helper(expect_starting=True)
showardb2e2c322008-10-14 17:33:55 +00001245 self.assertEquals(len(tasks), 1)
1246 queue_task = tasks[0]
1247
1248 self.assert_(isinstance(queue_task, monitor_db.QueueTask))
1249 self.assertEquals(queue_task.job.id, 1)
1250 hqe_ids = [hqe.id for hqe in queue_task.queue_entries]
1251 self.assertEquals(hqe_ids, [1, 2])
1252
1253
showard21baa452008-10-21 00:08:39 +00001254 def test_reboot_before_always(self):
1255 job = self._create_job(hosts=[1])
showard0fc38302008-10-23 00:44:07 +00001256 job.reboot_before = models.RebootBefore.ALWAYS
showard21baa452008-10-21 00:08:39 +00001257 job.save()
1258
1259 tasks = self._test_run_helper()
showard2bab8f42008-11-12 18:15:22 +00001260 self.assertEquals(len(tasks), 2)
showard45ae8192008-11-05 19:32:53 +00001261 cleanup_task = tasks[0]
1262 self.assert_(isinstance(cleanup_task, monitor_db.CleanupTask))
1263 self.assertEquals(cleanup_task.host.id, 1)
showard21baa452008-10-21 00:08:39 +00001264
1265
1266 def _test_reboot_before_if_dirty_helper(self, expect_reboot):
1267 job = self._create_job(hosts=[1])
showard0fc38302008-10-23 00:44:07 +00001268 job.reboot_before = models.RebootBefore.IF_DIRTY
showard21baa452008-10-21 00:08:39 +00001269 job.save()
1270
1271 tasks = self._test_run_helper()
showard2bab8f42008-11-12 18:15:22 +00001272 self.assertEquals(len(tasks), expect_reboot and 2 or 1)
showard21baa452008-10-21 00:08:39 +00001273 if expect_reboot:
showard45ae8192008-11-05 19:32:53 +00001274 cleanup_task = tasks[0]
1275 self.assert_(isinstance(cleanup_task, monitor_db.CleanupTask))
1276 self.assertEquals(cleanup_task.host.id, 1)
showard21baa452008-10-21 00:08:39 +00001277
1278 def test_reboot_before_if_dirty(self):
1279 models.Host.smart_get(1).update_object(dirty=True)
1280 self._test_reboot_before_if_dirty_helper(True)
1281
1282
1283 def test_reboot_before_not_dirty(self):
1284 models.Host.smart_get(1).update_object(dirty=False)
1285 self._test_reboot_before_if_dirty_helper(False)
1286
1287
1288
showardce38e0c2008-05-29 19:36:16 +00001289if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +00001290 unittest.main()