blob: 566098b162cd6d5016627bb23779c8a957da5e86 [file] [log] [blame]
Prashanth Bf66d51b2014-05-06 12:42:25 -07001#!/usr/bin/python
2#pylint: disable-msg=C0111
3
4# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8import mock
9
10import common
11
12from autotest_lib.client.common_lib.test_utils import unittest
13from autotest_lib.frontend import setup_django_environment
14from autotest_lib.frontend.afe import frontend_test_utils
15from autotest_lib.frontend.afe import models
Prashanth B4ec98672014-05-15 10:44:54 -070016from autotest_lib.scheduler import email_manager
17from autotest_lib.scheduler import host_scheduler
18from autotest_lib.scheduler import monitor_db
Prashanth Bf66d51b2014-05-06 12:42:25 -070019from autotest_lib.scheduler import rdb
20from autotest_lib.scheduler import rdb_lib
21from autotest_lib.scheduler import rdb_testing_utils
22
23
24class QueryManagerTests(rdb_testing_utils.AbstractBaseRDBTester,
25 unittest.TestCase):
26 """Verify scheduler behavior when pending jobs are already given hosts."""
27
28 _config_section = 'AUTOTEST_WEB'
29
30
31 def testPendingQueueEntries(self):
32 """Test retrieval of pending queue entries."""
33 job = self.create_job(deps=set(['a']))
34
35 # Check that we don't pull the job we just created with only_hostless.
36 jobs_with_hosts = self.job_query_manager.get_pending_queue_entries(
37 only_hostless=True)
38 self.assertTrue(len(jobs_with_hosts) == 0)
39
40 # Check that only_hostless=False pulls new jobs, as always.
41 jobs_without_hosts = self.job_query_manager.get_pending_queue_entries(
42 only_hostless=False)
43 self.assertTrue(jobs_without_hosts[0].id == job.id and
44 jobs_without_hosts[0].host_id is None)
45
46
Jakob Juelichefa95312014-08-27 18:29:52 -070047 def testPendingQueueEntriesForShard(self):
48 """Test queue entries for shards aren't executed by master scheduler"""
49 job1 = self.create_job(deps=set(['a']))
50 job2 = self.create_job(deps=set(['b']))
51 shard = models.Shard.objects.create()
52 # Assign the job's label to a shard
53 shard.labels.add(job1.dependency_labels.all()[0])
54
55 # Check that we only pull jobs which are not assigned to a shard.
56 jobs_with_hosts = self.job_query_manager.get_pending_queue_entries()
57 self.assertTrue(len(jobs_with_hosts) == 1)
58 self.assertEqual(jobs_with_hosts[0].id, job2.id)
59
60
Prashanth Bf66d51b2014-05-06 12:42:25 -070061 def testHostQueries(self):
62 """Verify that the host query manager maintains its data structures."""
63 # Create a job and use the host_query_managers internal datastructures
64 # to retrieve its job info.
65 job = self.create_job(
66 deps=rdb_testing_utils.DEFAULT_DEPS,
67 acls=rdb_testing_utils.DEFAULT_ACLS)
68 queue_entries = self._dispatcher._refresh_pending_queue_entries()
69 job_manager = rdb_lib.JobQueryManager(queue_entries)
70 job_info = job_manager.get_job_info(queue_entries[0])
71 default_dep_ids = set([label.id for label in self.db_helper.get_labels(
72 name__in=rdb_testing_utils.DEFAULT_DEPS)])
73 default_acl_ids = set([acl.id for acl in self.db_helper.get_acls(
74 name__in=rdb_testing_utils.DEFAULT_ACLS)])
75 self.assertTrue(set(job_info['deps']) == default_dep_ids)
76 self.assertTrue(set(job_info['acls']) == default_acl_ids)
77
78
79 def testNewJobsWithHosts(self):
80 """Test that we handle inactive hqes with unleased hosts correctly."""
81 # Create a job and assign it an unleased host, then check that the
82 # HQE becomes active and the host remains assigned to it.
83 job = self.create_job(deps=['a'])
84 host = self.db_helper.create_host('h1', deps=['a'])
85 self.db_helper.add_host_to_job(host, job.id)
86
87 queue_entries = self._dispatcher._refresh_pending_queue_entries()
88 self._dispatcher._schedule_new_jobs()
89
90 host = self.db_helper.get_host(hostname='h1')[0]
91 self.assertTrue(host.leased == True and
92 host.status == models.Host.Status.READY)
93 hqes = list(self.db_helper.get_hqes(host_id=host.id))
94 self.assertTrue(len(hqes) == 1 and hqes[0].active and
95 hqes[0].status == models.HostQueueEntry.Status.QUEUED)
96
97
98 def testNewJobsWithInvalidHost(self):
99 """Test handling of inactive hqes assigned invalid, unleased hosts."""
100 # Create a job and assign it an unleased host, then check that the
101 # HQE becomes DOES NOT become active, because we validate the
102 # assignment again.
103 job = self.create_job(deps=['a'])
104 host = self.db_helper.create_host('h1', deps=['b'])
105 self.db_helper.add_host_to_job(host, job.id)
106
107 queue_entries = self._dispatcher._refresh_pending_queue_entries()
108 self._dispatcher._schedule_new_jobs()
109
110 host = self.db_helper.get_host(hostname='h1')[0]
111 self.assertTrue(host.leased == False and
112 host.status == models.Host.Status.READY)
113 hqes = list(self.db_helper.get_hqes(host_id=host.id))
114 self.assertTrue(len(hqes) == 1 and not hqes[0].active and
115 hqes[0].status == models.HostQueueEntry.Status.QUEUED)
116
117
118 def testNewJobsWithLeasedHost(self):
119 """Test handling of inactive hqes assigned leased hosts."""
120 # Create a job and assign it a leased host, then check that the
121 # HQE does not become active through the scheduler, and that the
122 # host gets released.
123 job = self.create_job(deps=['a'])
124 host = self.db_helper.create_host('h1', deps=['b'])
125 self.db_helper.add_host_to_job(host, job.id)
126 host.leased = 1
127 host.save()
128
129 rdb.batch_acquire_hosts = mock.MagicMock()
130 queue_entries = self._dispatcher._refresh_pending_queue_entries()
131 self._dispatcher._schedule_new_jobs()
132 self.assertTrue(rdb.batch_acquire_hosts.call_count == 0)
133 host = self.db_helper.get_host(hostname='h1')[0]
134 self.assertTrue(host.leased == True and
135 host.status == models.Host.Status.READY)
136 hqes = list(self.db_helper.get_hqes(host_id=host.id))
137 self.assertTrue(len(hqes) == 1 and not hqes[0].active and
138 hqes[0].status == models.HostQueueEntry.Status.QUEUED)
139 self.host_scheduler._release_hosts()
140 self.assertTrue(self.db_helper.get_host(hostname='h1')[0].leased == 0)
141
142
143 def testSpecialTaskOrdering(self):
144 """Test priority ordering of special tasks."""
145
146 # Create 2 special tasks, one with and one without an hqe.
Prashanth B4ec98672014-05-15 10:44:54 -0700147 # Activate the hqe and make sure it gets scheduled before the other.
Prashanth Bf66d51b2014-05-06 12:42:25 -0700148 host = self.db_helper.create_host('h1', deps=['a'])
Prashanth B4ec98672014-05-15 10:44:54 -0700149 job1 = self.create_job(deps=['a'])
150 self.db_helper.add_host_to_job(host, job1.id)
151 task1 = self.db_helper.create_special_task(job1.id)
152 hqe = self.db_helper.get_hqes(job=job1.id)[0]
Prashanth Bf66d51b2014-05-06 12:42:25 -0700153
Prashanth B4ec98672014-05-15 10:44:54 -0700154 # This task has no queue entry.
155 task2 = self.db_helper.create_special_task(host_id=host.id)
156
157 # Since the hqe task isn't active we get both back.
Prashanth Bf66d51b2014-05-06 12:42:25 -0700158 tasks = self.job_query_manager.get_prioritized_special_tasks()
Prashanth B4ec98672014-05-15 10:44:54 -0700159 self.assertTrue(tasks[1].queue_entry_id is None and
160 tasks[0].queue_entry_id == hqe.id)
161
162 # Activate the hqe and make sure the frontned task isn't returned.
163 self.db_helper.update_hqe(hqe.id, active=True)
164 tasks = self.job_query_manager.get_prioritized_special_tasks()
165 self.assertTrue(tasks[0].id == task1.id)
166
167
168class HostSchedulerTests(rdb_testing_utils.AbstractBaseRDBTester,
169 unittest.TestCase):
170 """Verify scheduler behavior when pending jobs are already given hosts."""
171
172 _config_section = 'AUTOTEST_WEB'
173
174
175 def setUp(self):
176 super(HostSchedulerTests, self).setUp()
177 self.host_scheduler = host_scheduler.HostScheduler()
178
179
180 def testSpecialTaskLocking(self):
181 """Test that frontend special tasks lock hosts."""
182 # Create multiple tasks with hosts and make sure the hosts get locked.
183 host = self.db_helper.create_host('h')
184 host1 = self.db_helper.create_host('h1')
185 task = self.db_helper.create_special_task(host_id=host.id)
186 task1 = self.db_helper.create_special_task(host_id=host1.id)
187 self.host_scheduler._lease_hosts_of_frontend_tasks()
188 self.assertTrue(self.db_helper.get_host(hostname='h')[0].leased == 1 and
189 self.db_helper.get_host(hostname='h1')[0].leased == 1)
190
191
192 def testJobScheduling(self):
193 """Test new host acquisitions."""
194 # Create a job that will find a host through the host scheduler, and
195 # make sure the hqe is activated, and a special task is created.
196 job = self.create_job(deps=set(['a']))
197 host = self.db_helper.create_host('h1', deps=set(['a']))
198 self.host_scheduler._schedule_jobs()
199 hqe = self.db_helper.get_hqes(job_id=job.id)[0]
200 self.assertTrue(hqe.active and hqe.host_id == host.id and
201 hqe.status == models.HostQueueEntry.Status.QUEUED)
202 task = self.db_helper.get_tasks(queue_entry_id=hqe.id)[0]
203 self.assertTrue(task.is_active == 0 and task.host_id == host.id)
204
205
206 def testOverlappingJobs(self):
207 """Check that we catch all cases of an overlapping job."""
208 job_1 = self.create_job(deps=set(['a']))
209 job_2 = self.create_job(deps=set(['a']))
210 host = self.db_helper.create_host('h1', deps=set(['a']))
211 self.db_helper.add_host_to_job(host, job_1.id, activate=True)
212 self.db_helper.add_host_to_job(host, job_2.id, activate=True)
213 jobs = (self.host_scheduler.job_query_manager.get_overlapping_jobs())
214 self.assertTrue(len(jobs) == 2 and
215 jobs[0]['host_id'] == jobs[1]['host_id'])
216 email_manager.manager.enqueue_notify_email = mock.MagicMock()
217 self.host_scheduler._check_host_assignments()
218 self.assertTrue(
219 email_manager.manager.enqueue_notify_email.call_count == 1)
220
221
222 def _check_agent_invariants(self, host, agent):
223 host_agents = list(self._dispatcher._host_agents[host.id])
224 self.assertTrue(len(host_agents) == 1)
225 self.assertTrue(host_agents[0].task.task.id == agent.id)
226 return host_agents[0]
227
228
229 def testLeasedFrontendTaskHost(self):
230 """Check that we don't scheduler a special task on an unleased host."""
231 # Create a special task without an hqe and make sure it isn't returned
232 # for scheduling till its host is leased.
233 host = self.db_helper.create_host('h1', deps=['a'])
234 task = self.db_helper.create_special_task(host_id=host.id)
235
236 tasks = self.job_query_manager.get_prioritized_special_tasks(
237 only_tasks_with_leased_hosts=True)
Prashanth Bf66d51b2014-05-06 12:42:25 -0700238 self.assertTrue(tasks == [])
Prashanth B4ec98672014-05-15 10:44:54 -0700239 tasks = self.job_query_manager.get_prioritized_special_tasks(
240 only_tasks_with_leased_hosts=False)
241 self.assertTrue(tasks[0].id == task.id)
242 self.host_scheduler._lease_hosts_of_frontend_tasks()
243 tasks = self.job_query_manager.get_prioritized_special_tasks(
244 only_tasks_with_leased_hosts=True)
245 self.assertTrue(tasks[0].id == task.id)
Prashanth Bf66d51b2014-05-06 12:42:25 -0700246
247
Prashanth B4ec98672014-05-15 10:44:54 -0700248 def testTickLockStep(self):
249 """Check that a frontend task and an hqe never run simultaneously."""
250
251 self.god.stub_with(monitor_db, '_inline_host_acquisition', False)
252
253 # Create a frontend special task against a host.
254 host = self.db_helper.create_host('h1', deps=set(['a']))
255 frontend_task = self.db_helper.create_special_task(host_id=host.id)
256 self._dispatcher._schedule_special_tasks()
257 # The frontend special task shouldn't get scheduled on the host till
258 # the host is leased.
259 self.assertFalse(self._dispatcher.host_has_agent(host))
260
261 # Create a job for the same host and make the host scheduler lease the
262 # host out to that job.
263 job = self.create_job(deps=set(['a']))
264 self.host_scheduler._schedule_jobs()
265 hqe = self.db_helper.get_hqes(job_id=job.id)[0]
266 tasks = self.job_query_manager.get_prioritized_special_tasks(
267 only_tasks_with_leased_hosts=True)
268 # We should not find the frontend special task, even though its host is
269 # now leased, because its leased by an active hqe.
270 self.assertTrue(len(tasks) == 1 and tasks[0].queue_entry_id == hqe.id)
271 self._dispatcher._schedule_special_tasks()
272 self.assertTrue(self._dispatcher.host_has_agent(host))
273
274 # Deactivate the hqe task and make sure the frontend task gets the host.
275 task = tasks[0]
276 self._dispatcher.remove_agent(self._check_agent_invariants(host, task))
277 task.is_complete = 1
278 task.is_active = 0
279 task.save()
280 self.db_helper.update_hqe(hqe.id, active=False)
281 self._dispatcher._schedule_special_tasks()
282 self.assertTrue(self._dispatcher.host_has_agent(host))
283 self._check_agent_invariants(host, frontend_task)
284
285 # Make sure we don't release the host being used by the incomplete task.
286 self.host_scheduler._release_hosts()
287 host = self.db_helper.get_host(hostname='h1')[0]
288 self.assertTrue(host.leased == True)
289