blob: 22e213a9e5c86874de6617cee6437dd2f2bc1679 [file] [log] [blame]
jamesrenc44ae992010-02-19 00:12:54 +00001#!/usr/bin/python
Dan Shi76af8022013-10-19 01:59:49 -07002#pylint: disable-msg=C0111
jamesrenc44ae992010-02-19 00:12:54 +00003
4import datetime
5import common
6from autotest_lib.frontend import setup_django_environment
7from autotest_lib.frontend.afe import frontend_test_utils
8from autotest_lib.client.common_lib.test_utils import mock
9from autotest_lib.client.common_lib.test_utils import unittest
10from autotest_lib.database import database_connection
jamesrendd855242010-03-02 22:23:44 +000011from autotest_lib.frontend.afe import models, model_attributes
Dan Shi76af8022013-10-19 01:59:49 -070012from autotest_lib.scheduler import monitor_db
jamesrenc44ae992010-02-19 00:12:54 +000013from autotest_lib.scheduler import monitor_db_functional_test
14from autotest_lib.scheduler import scheduler_models
15
16_DEBUG = False
17
18
19class BaseSchedulerModelsTest(unittest.TestCase,
20 frontend_test_utils.FrontendTestMixin):
21 _config_section = 'AUTOTEST_WEB'
22
23 def _do_query(self, sql):
24 self._database.execute(sql)
25
26
27 def _set_monitor_stubs(self):
28 # Clear the instance cache as this is a brand new database.
29 scheduler_models.DBObject._clear_instance_cache()
30
31 self._database = (
32 database_connection.TranslatingDatabase.get_test_database(
33 translators=monitor_db_functional_test._DB_TRANSLATORS))
34 self._database.connect(db_type='django')
35 self._database.debug = _DEBUG
36
37 self.god.stub_with(scheduler_models, '_db', self._database)
38
39
40 def setUp(self):
41 self._frontend_common_setup()
42 self._set_monitor_stubs()
43
44
45 def tearDown(self):
46 self._database.disconnect()
47 self._frontend_common_teardown()
48
49
50 def _update_hqe(self, set, where=''):
51 query = 'UPDATE afe_host_queue_entries SET ' + set
52 if where:
53 query += ' WHERE ' + where
54 self._do_query(query)
55
56
57class DelayedCallTaskTest(unittest.TestCase):
58 def setUp(self):
59 self.god = mock.mock_god()
60
61
62 def tearDown(self):
63 self.god.unstub_all()
64
65
66 def test_delayed_call(self):
67 test_time = self.god.create_mock_function('time')
68 test_time.expect_call().and_return(33)
69 test_time.expect_call().and_return(34.01)
70 test_time.expect_call().and_return(34.99)
71 test_time.expect_call().and_return(35.01)
72 def test_callback():
73 test_callback.calls += 1
74 test_callback.calls = 0
75 delay_task = scheduler_models.DelayedCallTask(
76 delay_seconds=2, callback=test_callback,
77 now_func=test_time) # time 33
78 self.assertEqual(35, delay_task.end_time)
79 delay_task.poll() # activates the task and polls it once, time 34.01
80 self.assertEqual(0, test_callback.calls, "callback called early")
81 delay_task.poll() # time 34.99
82 self.assertEqual(0, test_callback.calls, "callback called early")
83 delay_task.poll() # time 35.01
84 self.assertEqual(1, test_callback.calls)
85 self.assert_(delay_task.is_done())
86 self.assert_(delay_task.success)
87 self.assert_(not delay_task.aborted)
88 self.god.check_playback()
89
90
91 def test_delayed_call_abort(self):
92 delay_task = scheduler_models.DelayedCallTask(
93 delay_seconds=987654, callback=lambda : None)
94 delay_task.abort()
95 self.assert_(delay_task.aborted)
96 self.assert_(delay_task.is_done())
97 self.assert_(not delay_task.success)
98 self.god.check_playback()
99
100
101class DBObjectTest(BaseSchedulerModelsTest):
102 def test_compare_fields_in_row(self):
103 host = scheduler_models.Host(id=1)
104 fields = list(host._fields)
105 row_data = [getattr(host, fieldname) for fieldname in fields]
106 self.assertEqual({}, host._compare_fields_in_row(row_data))
107 row_data[fields.index('hostname')] = 'spam'
108 self.assertEqual({'hostname': ('host1', 'spam')},
109 host._compare_fields_in_row(row_data))
110 row_data[fields.index('id')] = 23
111 self.assertEqual({'hostname': ('host1', 'spam'), 'id': (1, 23)},
112 host._compare_fields_in_row(row_data))
113
114
115 def test_compare_fields_in_row_datetime_ignores_microseconds(self):
116 datetime_with_us = datetime.datetime(2009, 10, 07, 12, 34, 56, 7890)
117 datetime_without_us = datetime.datetime(2009, 10, 07, 12, 34, 56, 0)
118 class TestTable(scheduler_models.DBObject):
119 _table_name = 'test_table'
120 _fields = ('id', 'test_datetime')
121 tt = TestTable(row=[1, datetime_without_us])
122 self.assertEqual({}, tt._compare_fields_in_row([1, datetime_with_us]))
123
124
125 def test_always_query(self):
126 host_a = scheduler_models.Host(id=2)
127 self.assertEqual(host_a.hostname, 'host2')
128 self._do_query('UPDATE afe_hosts SET hostname="host2-updated" '
129 'WHERE id=2')
130 host_b = scheduler_models.Host(id=2, always_query=True)
131 self.assert_(host_a is host_b, 'Cached instance not returned.')
132 self.assertEqual(host_a.hostname, 'host2-updated',
133 'Database was not re-queried')
134
135 # If either of these are called, a query was made when it shouldn't be.
136 host_a._compare_fields_in_row = lambda _: self.fail('eek! a query!')
137 host_a._update_fields_from_row = host_a._compare_fields_in_row
138 host_c = scheduler_models.Host(id=2, always_query=False)
139 self.assert_(host_a is host_c, 'Cached instance not returned')
140
141
142 def test_delete(self):
143 host = scheduler_models.Host(id=3)
144 host.delete()
145 host = self.assertRaises(scheduler_models.DBError, scheduler_models.Host, id=3,
146 always_query=False)
147 host = self.assertRaises(scheduler_models.DBError, scheduler_models.Host, id=3,
148 always_query=True)
149
150 def test_save(self):
151 # Dummy Job to avoid creating a one in the HostQueueEntry __init__.
152 class MockJob(object):
153 def __init__(self, id):
154 pass
155 def tag(self):
156 return 'MockJob'
157 self.god.stub_with(scheduler_models, 'Job', MockJob)
158 hqe = scheduler_models.HostQueueEntry(
159 new_record=True,
160 row=[0, 1, 2, 'Queued', None, 0, 0, 0, '.', None, False, None])
161 hqe.save()
162 new_id = hqe.id
163 # Force a re-query and verify that the correct data was stored.
164 scheduler_models.DBObject._clear_instance_cache()
165 hqe = scheduler_models.HostQueueEntry(id=new_id)
166 self.assertEqual(hqe.id, new_id)
167 self.assertEqual(hqe.job_id, 1)
168 self.assertEqual(hqe.host_id, 2)
169 self.assertEqual(hqe.status, 'Queued')
170 self.assertEqual(hqe.meta_host, None)
171 self.assertEqual(hqe.active, False)
172 self.assertEqual(hqe.complete, False)
173 self.assertEqual(hqe.deleted, False)
174 self.assertEqual(hqe.execution_subdir, '.')
175 self.assertEqual(hqe.atomic_group_id, None)
176 self.assertEqual(hqe.started_on, None)
177
178
179class HostTest(BaseSchedulerModelsTest):
180 def test_cmp_for_sort(self):
181 expected_order = [
182 'alice', 'Host1', 'host2', 'host3', 'host09', 'HOST010',
183 'host10', 'host11', 'yolkfolk']
184 hostname_idx = list(scheduler_models.Host._fields).index('hostname')
185 row = [None] * len(scheduler_models.Host._fields)
186 hosts = []
187 for hostname in expected_order:
188 row[hostname_idx] = hostname
189 hosts.append(scheduler_models.Host(row=row, new_record=True))
190
191 host1 = hosts[expected_order.index('Host1')]
192 host010 = hosts[expected_order.index('HOST010')]
193 host10 = hosts[expected_order.index('host10')]
194 host3 = hosts[expected_order.index('host3')]
195 alice = hosts[expected_order.index('alice')]
196 self.assertEqual(0, scheduler_models.Host.cmp_for_sort(host10, host10))
197 self.assertEqual(1, scheduler_models.Host.cmp_for_sort(host10, host010))
198 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(host010, host10))
199 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(host1, host10))
200 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(host1, host010))
201 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(host3, host10))
202 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(host3, host010))
203 self.assertEqual(1, scheduler_models.Host.cmp_for_sort(host3, host1))
204 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(host1, host3))
205 self.assertEqual(-1, scheduler_models.Host.cmp_for_sort(alice, host3))
206 self.assertEqual(1, scheduler_models.Host.cmp_for_sort(host3, alice))
207 self.assertEqual(0, scheduler_models.Host.cmp_for_sort(alice, alice))
208
209 hosts.sort(cmp=scheduler_models.Host.cmp_for_sort)
210 self.assertEqual(expected_order, [h.hostname for h in hosts])
211
212 hosts.reverse()
213 hosts.sort(cmp=scheduler_models.Host.cmp_for_sort)
214 self.assertEqual(expected_order, [h.hostname for h in hosts])
215
216
217class HostQueueEntryTest(BaseSchedulerModelsTest):
218 def _create_hqe(self, dependency_labels=(), **create_job_kwargs):
219 job = self._create_job(**create_job_kwargs)
220 for label in dependency_labels:
221 job.dependency_labels.add(label)
222 hqes = list(scheduler_models.HostQueueEntry.fetch(where='job_id=%d' % job.id))
223 self.assertEqual(1, len(hqes))
224 return hqes[0]
225
226
227 def _check_hqe_labels(self, hqe, expected_labels):
228 expected_labels = set(expected_labels)
229 label_names = set(label.name for label in hqe.get_labels())
230 self.assertEqual(expected_labels, label_names)
231
232
233 def test_get_labels_empty(self):
234 hqe = self._create_hqe(hosts=[1])
235 labels = list(hqe.get_labels())
236 self.assertEqual([], labels)
237
238
239 def test_get_labels_metahost(self):
240 hqe = self._create_hqe(metahosts=[2])
241 self._check_hqe_labels(hqe, ['label2'])
242
243
244 def test_get_labels_dependancies(self):
245 hqe = self._create_hqe(dependency_labels=(self.label3, self.label4),
246 metahosts=[1])
247 self._check_hqe_labels(hqe, ['label1', 'label3', 'label4'])
248
249
Dan Shi76af8022013-10-19 01:59:49 -0700250 def setup_abort_test(self, agent_finished=True):
251 """Setup the variables for testing abort method.
252
253 @param agent_finished: True to mock agent is finished before aborting
254 the hqe.
255 @return hqe, dispatcher: Mock object of hqe and dispatcher to be used
256 to test abort method.
257 """
258 hqe = self._create_hqe(hosts=[1])
259 hqe.aborted = True
260 hqe.complete = False
261 hqe.status = models.HostQueueEntry.Status.STARTING
262
263 dispatcher = self.god.create_mock_class(monitor_db.BaseDispatcher,
264 'BaseDispatcher')
265 agent = self.god.create_mock_class(monitor_db.Agent, 'Agent')
266 dispatcher.get_agents_for_entry.expect_call(hqe).and_return([agent])
267 agent.is_done.expect_call().and_return(agent_finished)
268 return hqe, dispatcher
269
270
271 def test_abort_fail_with_unfinished_agent(self):
272 """abort should fail if the hqe still has agent not finished.
273 """
274 hqe, dispatcher = self.setup_abort_test(agent_finished=False)
275 with self.assertRaises(AssertionError):
276 hqe.abort(dispatcher)
277 self.god.check_playback()
278
279
280 def test_abort_success(self):
281 """abort should succeed if all agents for the hqe are finished.
282 """
283 hqe, dispatcher = self.setup_abort_test(agent_finished=True)
284 hqe.abort(dispatcher)
285 self.god.check_playback()
286
287
jamesrenc44ae992010-02-19 00:12:54 +0000288class JobTest(BaseSchedulerModelsTest):
289 def setUp(self):
290 super(JobTest, self).setUp()
291
292 def _mock_create(**kwargs):
293 task = models.SpecialTask(**kwargs)
294 task.save()
Alex Miller16e4d6c2013-06-27 14:04:13 -0700295 self._tasks.append(task)
jamesrenc44ae992010-02-19 00:12:54 +0000296 self.god.stub_with(models.SpecialTask.objects, 'create', _mock_create)
297
298
Dan Shi07e09af2013-04-12 09:31:29 -0700299 def _test_pre_job_tasks_helper(self,
300 reboot_before=model_attributes.RebootBefore.ALWAYS):
jamesrenc44ae992010-02-19 00:12:54 +0000301 """
302 Calls HQE._do_schedule_pre_job_tasks() and returns the created special
303 task
304 """
Alex Miller16e4d6c2013-06-27 14:04:13 -0700305 self._tasks = []
jamesrenc44ae992010-02-19 00:12:54 +0000306 queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0]
Dan Shi07e09af2013-04-12 09:31:29 -0700307 queue_entry.job.reboot_before = reboot_before
jamesrenc44ae992010-02-19 00:12:54 +0000308 queue_entry._do_schedule_pre_job_tasks()
Alex Miller16e4d6c2013-06-27 14:04:13 -0700309 return self._tasks
jamesrenc44ae992010-02-19 00:12:54 +0000310
311
312 def test_job_request_abort(self):
313 django_job = self._create_job(hosts=[5, 6], atomic_group=1)
314 job = scheduler_models.Job(django_job.id)
315 job.request_abort()
316 django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id))
317 for hqe in django_hqes:
318 self.assertTrue(hqe.aborted)
319
320
321 def test__atomic_and_has_started__on_atomic(self):
322 self._create_job(hosts=[5, 6], atomic_group=1)
323 job = scheduler_models.Job.fetch('id = 1')[0]
324 self.assertFalse(job._atomic_and_has_started())
325
326 self._update_hqe("status='Pending'")
327 self.assertFalse(job._atomic_and_has_started())
328 self._update_hqe("status='Verifying'")
329 self.assertFalse(job._atomic_and_has_started())
330 self.assertFalse(job._atomic_and_has_started())
331 self._update_hqe("status='Failed'")
332 self.assertFalse(job._atomic_and_has_started())
333 self._update_hqe("status='Stopped'")
334 self.assertFalse(job._atomic_and_has_started())
335
336 self._update_hqe("status='Starting'")
337 self.assertTrue(job._atomic_and_has_started())
338 self._update_hqe("status='Completed'")
339 self.assertTrue(job._atomic_and_has_started())
340 self._update_hqe("status='Aborted'")
341
342
343 def test__atomic_and_has_started__not_atomic(self):
344 self._create_job(hosts=[1, 2])
345 job = scheduler_models.Job.fetch('id = 1')[0]
346 self.assertFalse(job._atomic_and_has_started())
347 self._update_hqe("status='Starting'")
348 self.assertFalse(job._atomic_and_has_started())
349
350
Alex Miller16e4d6c2013-06-27 14:04:13 -0700351 def _check_special_tasks(self, tasks, task_types):
352 self.assertEquals(len(tasks), len(task_types))
353 for task, (task_type, queue_entry_id) in zip(tasks, task_types):
354 self.assertEquals(task.task, task_type)
355 self.assertEquals(task.host.id, 1)
356 if queue_entry_id:
357 self.assertEquals(task.queue_entry.id, queue_entry_id)
jamesrenc44ae992010-02-19 00:12:54 +0000358
359
360 def test_run_asynchronous(self):
361 self._create_job(hosts=[1, 2])
362
Alex Miller16e4d6c2013-06-27 14:04:13 -0700363 tasks = self._test_pre_job_tasks_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000364
Dan Shi07e09af2013-04-12 09:31:29 -0700365 self._check_special_tasks(tasks, [(models.SpecialTask.Task.RESET, 1)])
jamesrenc44ae992010-02-19 00:12:54 +0000366
367
368 def test_run_asynchronous_skip_verify(self):
369 job = self._create_job(hosts=[1, 2])
370 job.run_verify = False
371 job.save()
372
Alex Miller16e4d6c2013-06-27 14:04:13 -0700373 tasks = self._test_pre_job_tasks_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000374
Dan Shi07e09af2013-04-12 09:31:29 -0700375 self._check_special_tasks(tasks, [(models.SpecialTask.Task.RESET, 1)])
jamesrenc44ae992010-02-19 00:12:54 +0000376
377
378 def test_run_synchronous_verify(self):
379 self._create_job(hosts=[1, 2], synchronous=True)
380
Alex Miller16e4d6c2013-06-27 14:04:13 -0700381 tasks = self._test_pre_job_tasks_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000382
Dan Shi07e09af2013-04-12 09:31:29 -0700383 self._check_special_tasks(tasks, [(models.SpecialTask.Task.RESET, 1)])
jamesrenc44ae992010-02-19 00:12:54 +0000384
385
386 def test_run_synchronous_skip_verify(self):
387 job = self._create_job(hosts=[1, 2], synchronous=True)
388 job.run_verify = False
389 job.save()
390
Alex Miller16e4d6c2013-06-27 14:04:13 -0700391 tasks = self._test_pre_job_tasks_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000392
Dan Shi07e09af2013-04-12 09:31:29 -0700393 self._check_special_tasks(tasks, [(models.SpecialTask.Task.RESET, 1)])
394
395
396 def test_run_asynchronous_do_not_reset(self):
397 job = self._create_job(hosts=[1, 2])
398 job.run_reset = False
399 job.run_verify = False
400 job.save()
401
402 tasks = self._test_pre_job_tasks_helper()
403
Alex Miller16e4d6c2013-06-27 14:04:13 -0700404 self.assertEquals(tasks, [])
jamesrenc44ae992010-02-19 00:12:54 +0000405
406
Dan Shi07e09af2013-04-12 09:31:29 -0700407 def test_run_synchronous_do_not_reset_no_RebootBefore(self):
408 job = self._create_job(hosts=[1, 2], synchronous=True)
409 job.reboot_before = model_attributes.RebootBefore.NEVER
410 job.save()
411
412 tasks = self._test_pre_job_tasks_helper(
413 reboot_before=model_attributes.RebootBefore.NEVER)
414
Alex Miller6ee996f2013-02-28 13:53:52 -0800415 self._check_special_tasks(tasks, [(models.SpecialTask.Task.VERIFY, 1)])
Dan Shi07e09af2013-04-12 09:31:29 -0700416
417
418 def test_run_asynchronous_do_not_reset(self):
419 job = self._create_job(hosts=[1, 2], synchronous=False)
420 job.reboot_before = model_attributes.RebootBefore.NEVER
421 job.save()
422
423 tasks = self._test_pre_job_tasks_helper(
424 reboot_before=model_attributes.RebootBefore.NEVER)
425
Alex Miller6ee996f2013-02-28 13:53:52 -0800426 self._check_special_tasks(tasks, [(models.SpecialTask.Task.VERIFY, 1)])
Dan Shi07e09af2013-04-12 09:31:29 -0700427
428
jamesrenc44ae992010-02-19 00:12:54 +0000429 def test_run_atomic_group_already_started(self):
430 self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True)
431 self._update_hqe("status='Starting', execution_subdir=''")
432
433 job = scheduler_models.Job.fetch('id = 1')[0]
434 queue_entry = scheduler_models.HostQueueEntry.fetch('id = 1')[0]
435 assert queue_entry.job is job
436 self.assertEqual(None, job.run(queue_entry))
437
438 self.god.check_playback()
439
440
441 def test_reboot_before_always(self):
442 job = self._create_job(hosts=[1])
jamesrendd855242010-03-02 22:23:44 +0000443 job.reboot_before = model_attributes.RebootBefore.ALWAYS
jamesrenc44ae992010-02-19 00:12:54 +0000444 job.save()
445
Alex Miller16e4d6c2013-06-27 14:04:13 -0700446 tasks = self._test_pre_job_tasks_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000447
Alex Miller16e4d6c2013-06-27 14:04:13 -0700448 self._check_special_tasks(tasks, [
Dan Shi07e09af2013-04-12 09:31:29 -0700449 (models.SpecialTask.Task.RESET, None)
Alex Miller16e4d6c2013-06-27 14:04:13 -0700450 ])
jamesrenc44ae992010-02-19 00:12:54 +0000451
452
Dan Shi07e09af2013-04-12 09:31:29 -0700453 def _test_reboot_before_if_dirty_helper(self):
jamesrenc44ae992010-02-19 00:12:54 +0000454 job = self._create_job(hosts=[1])
jamesrendd855242010-03-02 22:23:44 +0000455 job.reboot_before = model_attributes.RebootBefore.IF_DIRTY
jamesrenc44ae992010-02-19 00:12:54 +0000456 job.save()
457
Alex Miller16e4d6c2013-06-27 14:04:13 -0700458 tasks = self._test_pre_job_tasks_helper()
Dan Shi07e09af2013-04-12 09:31:29 -0700459 task_types = [(models.SpecialTask.Task.RESET, None)]
Alex Miller16e4d6c2013-06-27 14:04:13 -0700460
461 self._check_special_tasks(tasks, task_types)
jamesrenc44ae992010-02-19 00:12:54 +0000462
463
464 def test_reboot_before_if_dirty(self):
465 models.Host.smart_get(1).update_object(dirty=True)
Dan Shi07e09af2013-04-12 09:31:29 -0700466 self._test_reboot_before_if_dirty_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000467
468
469 def test_reboot_before_not_dirty(self):
470 models.Host.smart_get(1).update_object(dirty=False)
Dan Shi07e09af2013-04-12 09:31:29 -0700471 self._test_reboot_before_if_dirty_helper()
jamesrenc44ae992010-02-19 00:12:54 +0000472
473
474 def test_next_group_name(self):
475 django_job = self._create_job(metahosts=[1])
476 job = scheduler_models.Job(id=django_job.id)
477 self.assertEqual('group0', job._next_group_name())
478
479 for hqe in django_job.hostqueueentry_set.filter():
480 hqe.execution_subdir = 'my_rack.group0'
481 hqe.save()
482 self.assertEqual('my_rack.group1', job._next_group_name('my/rack'))
483
484
485if __name__ == '__main__':
486 unittest.main()