showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 3 | import unittest, time, subprocess, os, StringIO, tempfile, datetime, shutil |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 4 | import logging |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 5 | import common |
mbligh | 8bcd23a | 2009-02-03 19:14:06 +0000 | [diff] [blame] | 6 | import MySQLdb |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 7 | from autotest_lib.frontend import setup_django_environment |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 8 | from autotest_lib.frontend.afe import frontend_test_utils |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import global_config, host_protections |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib.test_utils import mock |
showard | 442e71e | 2008-10-06 10:05:20 +0000 | [diff] [blame] | 11 | from autotest_lib.database import database_connection, migrate |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 12 | from autotest_lib.frontend import thread_local |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 13 | from autotest_lib.frontend.afe import models |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 14 | from autotest_lib.scheduler import monitor_db, drone_manager, email_manager |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 15 | from autotest_lib.scheduler import scheduler_config |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 16 | |
| 17 | _DEBUG = False |
| 18 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 19 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 20 | class DummyAgent(object): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 21 | started = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 22 | _is_done = False |
| 23 | num_processes = 1 |
| 24 | host_ids = [] |
| 25 | queue_entry_ids = [] |
| 26 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 27 | |
| 28 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 29 | self.started = True |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def is_done(self): |
| 33 | return self._is_done |
| 34 | |
| 35 | |
| 36 | def set_done(self, done): |
| 37 | self._is_done = done |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 38 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 39 | |
| 40 | class IsRow(mock.argument_comparator): |
| 41 | def __init__(self, row_id): |
| 42 | self.row_id = row_id |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 43 | |
| 44 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 45 | def is_satisfied_by(self, parameter): |
| 46 | return list(parameter)[0] == self.row_id |
| 47 | |
| 48 | |
| 49 | def __str__(self): |
| 50 | return 'row with id %s' % self.row_id |
| 51 | |
| 52 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 53 | class IsAgentWithTask(mock.argument_comparator): |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 54 | def __init__(self, task): |
| 55 | self._task = task |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 56 | |
| 57 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 58 | def is_satisfied_by(self, parameter): |
| 59 | if not isinstance(parameter, monitor_db.Agent): |
| 60 | return False |
| 61 | tasks = list(parameter.queue.queue) |
| 62 | if len(tasks) != 1: |
| 63 | return False |
| 64 | return tasks[0] == self._task |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 65 | |
| 66 | |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 67 | def _set_host_and_qe_ids(agent_or_task, id_list=None): |
| 68 | if id_list is None: |
| 69 | id_list = [] |
| 70 | agent_or_task.host_ids = agent_or_task.queue_entry_ids = id_list |
| 71 | |
| 72 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 73 | class BaseSchedulerTest(unittest.TestCase, |
| 74 | frontend_test_utils.FrontendTestMixin): |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 75 | _config_section = 'AUTOTEST_WEB' |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 76 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 77 | def _do_query(self, sql): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 78 | self._database.execute(sql) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 79 | |
| 80 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 81 | def _set_monitor_stubs(self): |
| 82 | # Clear the instance cache as this is a brand new database. |
| 83 | monitor_db.DBObject._clear_instance_cache() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 84 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 85 | self._database = ( |
| 86 | database_connection.DatabaseConnection.get_test_database( |
| 87 | self._test_db_file)) |
| 88 | self._database.connect() |
| 89 | self._database.debug = _DEBUG |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 90 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 91 | monitor_db._db = self._database |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 92 | monitor_db._drone_manager._results_dir = '/test/path' |
| 93 | monitor_db._drone_manager._temporary_directory = '/test/path/tmp' |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 94 | |
| 95 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 96 | def setUp(self): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 97 | self._frontend_common_setup() |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 98 | self._set_monitor_stubs() |
| 99 | self._dispatcher = monitor_db.Dispatcher() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 100 | |
| 101 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 102 | def tearDown(self): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 103 | self._database.disconnect() |
| 104 | self._frontend_common_teardown() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 105 | |
| 106 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 107 | def _update_hqe(self, set, where=''): |
| 108 | query = 'UPDATE host_queue_entries SET ' + set |
| 109 | if where: |
| 110 | query += ' WHERE ' + where |
| 111 | self._do_query(query) |
| 112 | |
| 113 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 114 | class DBObjectTest(BaseSchedulerTest): |
| 115 | # It may seem odd to subclass BaseSchedulerTest for this but it saves us |
| 116 | # duplicating some setup work for what we want to test. |
| 117 | |
| 118 | |
| 119 | def test_compare_fields_in_row(self): |
| 120 | host = monitor_db.Host(id=1) |
| 121 | fields = list(host._fields) |
| 122 | row_data = [getattr(host, fieldname) for fieldname in fields] |
| 123 | self.assertEqual({}, host._compare_fields_in_row(row_data)) |
| 124 | row_data[fields.index('hostname')] = 'spam' |
| 125 | self.assertEqual({'hostname': ('host1', 'spam')}, |
| 126 | host._compare_fields_in_row(row_data)) |
| 127 | row_data[fields.index('id')] = 23 |
| 128 | self.assertEqual({'hostname': ('host1', 'spam'), 'id': (1, 23)}, |
| 129 | host._compare_fields_in_row(row_data)) |
| 130 | |
| 131 | |
| 132 | def test_always_query(self): |
| 133 | host_a = monitor_db.Host(id=2) |
| 134 | self.assertEqual(host_a.hostname, 'host2') |
| 135 | self._do_query('UPDATE hosts SET hostname="host2-updated" WHERE id=2') |
| 136 | host_b = monitor_db.Host(id=2, always_query=True) |
| 137 | self.assert_(host_a is host_b, 'Cached instance not returned.') |
| 138 | self.assertEqual(host_a.hostname, 'host2-updated', |
| 139 | 'Database was not re-queried') |
| 140 | |
| 141 | # If either of these are called, a query was made when it shouldn't be. |
| 142 | host_a._compare_fields_in_row = lambda _: self.fail('eek! a query!') |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 143 | host_a._update_fields_from_row = host_a._compare_fields_in_row |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 144 | host_c = monitor_db.Host(id=2, always_query=False) |
| 145 | self.assert_(host_a is host_c, 'Cached instance not returned') |
| 146 | |
| 147 | |
| 148 | def test_delete(self): |
| 149 | host = monitor_db.Host(id=3) |
| 150 | host.delete() |
| 151 | host = self.assertRaises(monitor_db.DBError, monitor_db.Host, id=3, |
| 152 | always_query=False) |
| 153 | host = self.assertRaises(monitor_db.DBError, monitor_db.Host, id=3, |
| 154 | always_query=True) |
| 155 | |
showard | 76e29d1 | 2009-04-15 21:53:10 +0000 | [diff] [blame] | 156 | def test_save(self): |
| 157 | # Dummy Job to avoid creating a one in the HostQueueEntry __init__. |
| 158 | class MockJob(object): |
| 159 | def __init__(self, id): |
| 160 | pass |
| 161 | def tag(self): |
| 162 | return 'MockJob' |
| 163 | self.god.stub_with(monitor_db, 'Job', MockJob) |
| 164 | hqe = monitor_db.HostQueueEntry( |
| 165 | new_record=True, |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 166 | row=[0, 1, 2, 'Queued', None, 0, 0, 0, '.', None, False, None]) |
showard | 76e29d1 | 2009-04-15 21:53:10 +0000 | [diff] [blame] | 167 | hqe.save() |
| 168 | new_id = hqe.id |
| 169 | # Force a re-query and verify that the correct data was stored. |
| 170 | monitor_db.DBObject._clear_instance_cache() |
| 171 | hqe = monitor_db.HostQueueEntry(id=new_id) |
| 172 | self.assertEqual(hqe.id, new_id) |
| 173 | self.assertEqual(hqe.job_id, 1) |
| 174 | self.assertEqual(hqe.host_id, 2) |
| 175 | self.assertEqual(hqe.status, 'Queued') |
| 176 | self.assertEqual(hqe.meta_host, None) |
| 177 | self.assertEqual(hqe.active, False) |
| 178 | self.assertEqual(hqe.complete, False) |
| 179 | self.assertEqual(hqe.deleted, False) |
| 180 | self.assertEqual(hqe.execution_subdir, '.') |
| 181 | self.assertEqual(hqe.atomic_group_id, None) |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 182 | self.assertEqual(hqe.started_on, None) |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 183 | |
| 184 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 185 | class DispatcherSchedulingTest(BaseSchedulerTest): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 186 | _jobs_scheduled = [] |
| 187 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 188 | |
| 189 | def tearDown(self): |
| 190 | super(DispatcherSchedulingTest, self).tearDown() |
| 191 | |
| 192 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 193 | def _set_monitor_stubs(self): |
| 194 | super(DispatcherSchedulingTest, self)._set_monitor_stubs() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 195 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 196 | def hqe__do_schedule_pre_job_tasks_stub(queue_entry): |
| 197 | """Called by HostQueueEntry.run().""" |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 198 | self._record_job_scheduled(queue_entry.job.id, queue_entry.host.id) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 199 | queue_entry.set_status('Starting') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 200 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 201 | self.god.stub_with(monitor_db.HostQueueEntry, |
| 202 | '_do_schedule_pre_job_tasks', |
| 203 | hqe__do_schedule_pre_job_tasks_stub) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 204 | |
| 205 | def hqe_queue_log_record_stub(self, log_line): |
| 206 | """No-Op to avoid calls down to the _drone_manager during tests.""" |
| 207 | |
| 208 | self.god.stub_with(monitor_db.HostQueueEntry, 'queue_log_record', |
| 209 | hqe_queue_log_record_stub) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | def _record_job_scheduled(self, job_id, host_id): |
| 213 | record = (job_id, host_id) |
| 214 | self.assert_(record not in self._jobs_scheduled, |
| 215 | 'Job %d scheduled on host %d twice' % |
| 216 | (job_id, host_id)) |
| 217 | self._jobs_scheduled.append(record) |
| 218 | |
| 219 | |
| 220 | def _assert_job_scheduled_on(self, job_id, host_id): |
| 221 | record = (job_id, host_id) |
| 222 | self.assert_(record in self._jobs_scheduled, |
| 223 | 'Job %d not scheduled on host %d as expected\n' |
| 224 | 'Jobs scheduled: %s' % |
| 225 | (job_id, host_id, self._jobs_scheduled)) |
| 226 | self._jobs_scheduled.remove(record) |
| 227 | |
| 228 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 229 | def _assert_job_scheduled_on_number_of(self, job_id, host_ids, number): |
| 230 | """Assert job was scheduled on exactly number hosts out of a set.""" |
| 231 | found = [] |
| 232 | for host_id in host_ids: |
| 233 | record = (job_id, host_id) |
| 234 | if record in self._jobs_scheduled: |
| 235 | found.append(record) |
| 236 | self._jobs_scheduled.remove(record) |
| 237 | if len(found) < number: |
| 238 | self.fail('Job %d scheduled on fewer than %d hosts in %s.\n' |
| 239 | 'Jobs scheduled: %s' % (job_id, number, host_ids, found)) |
| 240 | elif len(found) > number: |
| 241 | self.fail('Job %d scheduled on more than %d hosts in %s.\n' |
| 242 | 'Jobs scheduled: %s' % (job_id, number, host_ids, found)) |
| 243 | |
| 244 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 245 | def _check_for_extra_schedulings(self): |
| 246 | if len(self._jobs_scheduled) != 0: |
| 247 | self.fail('Extra jobs scheduled: ' + |
| 248 | str(self._jobs_scheduled)) |
| 249 | |
| 250 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 251 | def _convert_jobs_to_metahosts(self, *job_ids): |
| 252 | sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')' |
| 253 | self._do_query('UPDATE host_queue_entries SET ' |
| 254 | 'meta_host=host_id, host_id=NULL ' |
| 255 | 'WHERE job_id IN ' + sql_tuple) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 256 | |
| 257 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 258 | def _lock_host(self, host_id): |
| 259 | self._do_query('UPDATE hosts SET locked=1 WHERE id=' + |
| 260 | str(host_id)) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 261 | |
| 262 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 263 | def setUp(self): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 264 | super(DispatcherSchedulingTest, self).setUp() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 265 | self._jobs_scheduled = [] |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 266 | |
| 267 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 268 | def _test_basic_scheduling_helper(self, use_metahosts): |
| 269 | 'Basic nonmetahost scheduling' |
| 270 | self._create_job_simple([1], use_metahosts) |
| 271 | self._create_job_simple([2], use_metahosts) |
| 272 | self._dispatcher._schedule_new_jobs() |
| 273 | self._assert_job_scheduled_on(1, 1) |
| 274 | self._assert_job_scheduled_on(2, 2) |
| 275 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 276 | |
| 277 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 278 | def _test_priorities_helper(self, use_metahosts): |
| 279 | 'Test prioritization ordering' |
| 280 | self._create_job_simple([1], use_metahosts) |
| 281 | self._create_job_simple([2], use_metahosts) |
| 282 | self._create_job_simple([1,2], use_metahosts) |
| 283 | self._create_job_simple([1], use_metahosts, priority=1) |
| 284 | self._dispatcher._schedule_new_jobs() |
| 285 | self._assert_job_scheduled_on(4, 1) # higher priority |
| 286 | self._assert_job_scheduled_on(2, 2) # earlier job over later |
| 287 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 288 | |
| 289 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 290 | def _test_hosts_ready_helper(self, use_metahosts): |
| 291 | """ |
| 292 | Only hosts that are status=Ready, unlocked and not invalid get |
| 293 | scheduled. |
| 294 | """ |
| 295 | self._create_job_simple([1], use_metahosts) |
| 296 | self._do_query('UPDATE hosts SET status="Running" WHERE id=1') |
| 297 | self._dispatcher._schedule_new_jobs() |
| 298 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 299 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 300 | self._do_query('UPDATE hosts SET status="Ready", locked=1 ' |
| 301 | 'WHERE id=1') |
| 302 | self._dispatcher._schedule_new_jobs() |
| 303 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 304 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 305 | self._do_query('UPDATE hosts SET locked=0, invalid=1 ' |
| 306 | 'WHERE id=1') |
| 307 | self._dispatcher._schedule_new_jobs() |
showard | 5df2b19 | 2008-07-03 19:51:57 +0000 | [diff] [blame] | 308 | if not use_metahosts: |
| 309 | self._assert_job_scheduled_on(1, 1) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 310 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 311 | |
| 312 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 313 | def _test_hosts_idle_helper(self, use_metahosts): |
| 314 | 'Only idle hosts get scheduled' |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 315 | self._create_job(hosts=[1], active=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 316 | self._create_job_simple([1], use_metahosts) |
| 317 | self._dispatcher._schedule_new_jobs() |
| 318 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 319 | |
| 320 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 321 | def _test_obey_ACLs_helper(self, use_metahosts): |
| 322 | self._do_query('DELETE FROM acl_groups_hosts WHERE host_id=1') |
| 323 | self._create_job_simple([1], use_metahosts) |
| 324 | self._dispatcher._schedule_new_jobs() |
| 325 | self._check_for_extra_schedulings() |
| 326 | |
| 327 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 328 | def test_basic_scheduling(self): |
| 329 | self._test_basic_scheduling_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 330 | |
| 331 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 332 | def test_priorities(self): |
| 333 | self._test_priorities_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 334 | |
| 335 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 336 | def test_hosts_ready(self): |
| 337 | self._test_hosts_ready_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 338 | |
| 339 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 340 | def test_hosts_idle(self): |
| 341 | self._test_hosts_idle_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 342 | |
| 343 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 344 | def test_obey_ACLs(self): |
| 345 | self._test_obey_ACLs_helper(False) |
| 346 | |
| 347 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 348 | def test_one_time_hosts_ignore_ACLs(self): |
| 349 | self._do_query('DELETE FROM acl_groups_hosts WHERE host_id=1') |
| 350 | self._do_query('UPDATE hosts SET invalid=1 WHERE id=1') |
| 351 | self._create_job_simple([1]) |
| 352 | self._dispatcher._schedule_new_jobs() |
| 353 | self._assert_job_scheduled_on(1, 1) |
| 354 | self._check_for_extra_schedulings() |
| 355 | |
| 356 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 357 | def test_non_metahost_on_invalid_host(self): |
| 358 | """ |
| 359 | Non-metahost entries can get scheduled on invalid hosts (this is how |
| 360 | one-time hosts work). |
| 361 | """ |
| 362 | self._do_query('UPDATE hosts SET invalid=1') |
| 363 | self._test_basic_scheduling_helper(False) |
| 364 | |
| 365 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 366 | def test_metahost_scheduling(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 367 | """ |
| 368 | Basic metahost scheduling |
| 369 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 370 | self._test_basic_scheduling_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 371 | |
| 372 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 373 | def test_metahost_priorities(self): |
| 374 | self._test_priorities_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 375 | |
| 376 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 377 | def test_metahost_hosts_ready(self): |
| 378 | self._test_hosts_ready_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 379 | |
| 380 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 381 | def test_metahost_hosts_idle(self): |
| 382 | self._test_hosts_idle_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 383 | |
| 384 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 385 | def test_metahost_obey_ACLs(self): |
| 386 | self._test_obey_ACLs_helper(True) |
| 387 | |
| 388 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 389 | def _setup_test_only_if_needed_labels(self): |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 390 | # apply only_if_needed label3 to host1 |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 391 | models.Host.smart_get('host1').labels.add(self.label3) |
| 392 | return self._create_job_simple([1], use_metahost=True) |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 393 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 394 | |
| 395 | def test_only_if_needed_labels_avoids_host(self): |
| 396 | job = self._setup_test_only_if_needed_labels() |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 397 | # if the job doesn't depend on label3, there should be no scheduling |
| 398 | self._dispatcher._schedule_new_jobs() |
| 399 | self._check_for_extra_schedulings() |
| 400 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 401 | |
| 402 | def test_only_if_needed_labels_schedules(self): |
| 403 | job = self._setup_test_only_if_needed_labels() |
| 404 | job.dependency_labels.add(self.label3) |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 405 | self._dispatcher._schedule_new_jobs() |
| 406 | self._assert_job_scheduled_on(1, 1) |
| 407 | self._check_for_extra_schedulings() |
| 408 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 409 | |
| 410 | def test_only_if_needed_labels_via_metahost(self): |
| 411 | job = self._setup_test_only_if_needed_labels() |
| 412 | job.dependency_labels.add(self.label3) |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 413 | # should also work if the metahost is the only_if_needed label |
| 414 | self._do_query('DELETE FROM jobs_dependency_labels') |
| 415 | self._create_job(metahosts=[3]) |
| 416 | self._dispatcher._schedule_new_jobs() |
| 417 | self._assert_job_scheduled_on(2, 1) |
| 418 | self._check_for_extra_schedulings() |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 419 | |
| 420 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 421 | def test_nonmetahost_over_metahost(self): |
| 422 | """ |
| 423 | Non-metahost entries should take priority over metahost entries |
| 424 | for the same host |
| 425 | """ |
| 426 | self._create_job(metahosts=[1]) |
| 427 | self._create_job(hosts=[1]) |
| 428 | self._dispatcher._schedule_new_jobs() |
| 429 | self._assert_job_scheduled_on(2, 1) |
| 430 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 431 | |
| 432 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 433 | def test_metahosts_obey_blocks(self): |
| 434 | """ |
| 435 | Metahosts can't get scheduled on hosts already scheduled for |
| 436 | that job. |
| 437 | """ |
| 438 | self._create_job(metahosts=[1], hosts=[1]) |
| 439 | # make the nonmetahost entry complete, so the metahost can try |
| 440 | # to get scheduled |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 441 | self._update_hqe(set='complete = 1', where='host_id=1') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 442 | self._dispatcher._schedule_new_jobs() |
| 443 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 444 | |
| 445 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 446 | # TODO(gps): These should probably live in their own TestCase class |
| 447 | # specific to testing HostScheduler methods directly. It was convenient |
| 448 | # to put it here for now to share existing test environment setup code. |
| 449 | def test_HostScheduler_check_atomic_group_labels(self): |
| 450 | normal_job = self._create_job(metahosts=[0]) |
| 451 | atomic_job = self._create_job(atomic_group=1) |
| 452 | # Indirectly initialize the internal state of the host scheduler. |
| 453 | self._dispatcher._refresh_pending_queue_entries() |
| 454 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 455 | atomic_hqe = monitor_db.HostQueueEntry.fetch(where='job_id=%d' % |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 456 | atomic_job.id)[0] |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 457 | normal_hqe = monitor_db.HostQueueEntry.fetch(where='job_id=%d' % |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 458 | normal_job.id)[0] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 459 | |
| 460 | host_scheduler = self._dispatcher._host_scheduler |
| 461 | self.assertTrue(host_scheduler._check_atomic_group_labels( |
| 462 | [self.label4.id], atomic_hqe)) |
| 463 | self.assertFalse(host_scheduler._check_atomic_group_labels( |
| 464 | [self.label4.id], normal_hqe)) |
| 465 | self.assertFalse(host_scheduler._check_atomic_group_labels( |
| 466 | [self.label5.id, self.label6.id, self.label7.id], normal_hqe)) |
| 467 | self.assertTrue(host_scheduler._check_atomic_group_labels( |
| 468 | [self.label4.id, self.label6.id], atomic_hqe)) |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 469 | self.assertTrue(host_scheduler._check_atomic_group_labels( |
| 470 | [self.label4.id, self.label5.id], |
| 471 | atomic_hqe)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 472 | |
| 473 | |
| 474 | def test_HostScheduler_get_host_atomic_group_id(self): |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 475 | job = self._create_job(metahosts=[self.label6.id]) |
| 476 | queue_entry = monitor_db.HostQueueEntry.fetch( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 477 | where='job_id=%d' % job.id)[0] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 478 | # Indirectly initialize the internal state of the host scheduler. |
| 479 | self._dispatcher._refresh_pending_queue_entries() |
| 480 | |
| 481 | # Test the host scheduler |
| 482 | host_scheduler = self._dispatcher._host_scheduler |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 483 | |
| 484 | # Two labels each in a different atomic group. This should log an |
| 485 | # error and continue. |
| 486 | orig_logging_error = logging.error |
| 487 | def mock_logging_error(message, *args): |
| 488 | mock_logging_error._num_calls += 1 |
| 489 | # Test the logging call itself, we just wrapped it to count it. |
| 490 | orig_logging_error(message, *args) |
| 491 | mock_logging_error._num_calls = 0 |
| 492 | self.god.stub_with(logging, 'error', mock_logging_error) |
| 493 | self.assertNotEquals(None, host_scheduler._get_host_atomic_group_id( |
| 494 | [self.label4.id, self.label8.id], queue_entry)) |
| 495 | self.assertTrue(mock_logging_error._num_calls > 0) |
| 496 | self.god.unstub(logging, 'error') |
| 497 | |
| 498 | # Two labels both in the same atomic group, this should not raise an |
| 499 | # error, it will merely cause the job to schedule on the intersection. |
| 500 | self.assertEquals(1, host_scheduler._get_host_atomic_group_id( |
| 501 | [self.label4.id, self.label5.id])) |
| 502 | |
| 503 | self.assertEquals(None, host_scheduler._get_host_atomic_group_id([])) |
| 504 | self.assertEquals(None, host_scheduler._get_host_atomic_group_id( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 505 | [self.label3.id, self.label7.id, self.label6.id])) |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 506 | self.assertEquals(1, host_scheduler._get_host_atomic_group_id( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 507 | [self.label4.id, self.label7.id, self.label6.id])) |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 508 | self.assertEquals(1, host_scheduler._get_host_atomic_group_id( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 509 | [self.label7.id, self.label5.id])) |
| 510 | |
| 511 | |
| 512 | def test_atomic_group_hosts_blocked_from_non_atomic_jobs(self): |
| 513 | # Create a job scheduled to run on label6. |
| 514 | self._create_job(metahosts=[self.label6.id]) |
| 515 | self._dispatcher._schedule_new_jobs() |
| 516 | # label6 only has hosts that are in atomic groups associated with it, |
| 517 | # there should be no scheduling. |
| 518 | self._check_for_extra_schedulings() |
| 519 | |
| 520 | |
| 521 | def test_atomic_group_hosts_blocked_from_non_atomic_jobs_explicit(self): |
| 522 | # Create a job scheduled to run on label5. This is an atomic group |
| 523 | # label but this job does not request atomic group scheduling. |
| 524 | self._create_job(metahosts=[self.label5.id]) |
| 525 | self._dispatcher._schedule_new_jobs() |
| 526 | # label6 only has hosts that are in atomic groups associated with it, |
| 527 | # there should be no scheduling. |
| 528 | self._check_for_extra_schedulings() |
| 529 | |
| 530 | |
| 531 | def test_atomic_group_scheduling_basics(self): |
| 532 | # Create jobs scheduled to run on an atomic group. |
| 533 | job_a = self._create_job(synchronous=True, metahosts=[self.label4.id], |
| 534 | atomic_group=1) |
| 535 | job_b = self._create_job(synchronous=True, metahosts=[self.label5.id], |
| 536 | atomic_group=1) |
| 537 | self._dispatcher._schedule_new_jobs() |
| 538 | # atomic_group.max_number_of_machines was 2 so we should run on 2. |
| 539 | self._assert_job_scheduled_on_number_of(job_a.id, (5, 6, 7), 2) |
| 540 | self._assert_job_scheduled_on(job_b.id, 8) # label5 |
| 541 | self._assert_job_scheduled_on(job_b.id, 9) # label5 |
| 542 | self._check_for_extra_schedulings() |
| 543 | |
| 544 | # The three host label4 atomic group still has one host available. |
| 545 | # That means a job with a synch_count of 1 asking to be scheduled on |
| 546 | # the atomic group can still use the final machine. |
| 547 | # |
| 548 | # This may seem like a somewhat odd use case. It allows the use of an |
| 549 | # atomic group as a set of machines to run smaller jobs within (a set |
| 550 | # of hosts configured for use in network tests with eachother perhaps?) |
| 551 | onehost_job = self._create_job(atomic_group=1) |
| 552 | self._dispatcher._schedule_new_jobs() |
| 553 | self._assert_job_scheduled_on_number_of(onehost_job.id, (5, 6, 7), 1) |
| 554 | self._check_for_extra_schedulings() |
| 555 | |
| 556 | # No more atomic groups have hosts available, no more jobs should |
| 557 | # be scheduled. |
| 558 | self._create_job(atomic_group=1) |
| 559 | self._dispatcher._schedule_new_jobs() |
| 560 | self._check_for_extra_schedulings() |
| 561 | |
| 562 | |
| 563 | def test_atomic_group_scheduling_obeys_acls(self): |
| 564 | # Request scheduling on a specific atomic label but be denied by ACLs. |
| 565 | self._do_query('DELETE FROM acl_groups_hosts WHERE host_id in (8,9)') |
| 566 | job = self._create_job(metahosts=[self.label5.id], atomic_group=1) |
| 567 | self._dispatcher._schedule_new_jobs() |
| 568 | self._check_for_extra_schedulings() |
| 569 | |
| 570 | |
| 571 | def test_atomic_group_scheduling_dependency_label_exclude(self): |
| 572 | # A dependency label that matches no hosts in the atomic group. |
| 573 | job_a = self._create_job(atomic_group=1) |
| 574 | job_a.dependency_labels.add(self.label3) |
| 575 | self._dispatcher._schedule_new_jobs() |
| 576 | self._check_for_extra_schedulings() |
| 577 | |
| 578 | |
| 579 | def test_atomic_group_scheduling_metahost_dependency_label_exclude(self): |
| 580 | # A metahost and dependency label that excludes too many hosts. |
| 581 | job_b = self._create_job(synchronous=True, metahosts=[self.label4.id], |
| 582 | atomic_group=1) |
| 583 | job_b.dependency_labels.add(self.label7) |
| 584 | self._dispatcher._schedule_new_jobs() |
| 585 | self._check_for_extra_schedulings() |
| 586 | |
| 587 | |
| 588 | def test_atomic_group_scheduling_dependency_label_match(self): |
| 589 | # A dependency label that exists on enough atomic group hosts in only |
| 590 | # one of the two atomic group labels. |
| 591 | job_c = self._create_job(synchronous=True, atomic_group=1) |
| 592 | job_c.dependency_labels.add(self.label7) |
| 593 | self._dispatcher._schedule_new_jobs() |
| 594 | self._assert_job_scheduled_on_number_of(job_c.id, (8, 9), 2) |
| 595 | self._check_for_extra_schedulings() |
| 596 | |
| 597 | |
| 598 | def test_atomic_group_scheduling_no_metahost(self): |
| 599 | # Force it to schedule on the other group for a reliable test. |
| 600 | self._do_query('UPDATE hosts SET invalid=1 WHERE id=9') |
| 601 | # An atomic job without a metahost. |
| 602 | job = self._create_job(synchronous=True, atomic_group=1) |
| 603 | self._dispatcher._schedule_new_jobs() |
| 604 | self._assert_job_scheduled_on_number_of(job.id, (5, 6, 7), 2) |
| 605 | self._check_for_extra_schedulings() |
| 606 | |
| 607 | |
| 608 | def test_atomic_group_scheduling_partial_group(self): |
| 609 | # Make one host in labels[3] unavailable so that there are only two |
| 610 | # hosts left in the group. |
| 611 | self._do_query('UPDATE hosts SET status="Repair Failed" WHERE id=5') |
| 612 | job = self._create_job(synchronous=True, metahosts=[self.label4.id], |
| 613 | atomic_group=1) |
| 614 | self._dispatcher._schedule_new_jobs() |
| 615 | # Verify that it was scheduled on the 2 ready hosts in that group. |
| 616 | self._assert_job_scheduled_on(job.id, 6) |
| 617 | self._assert_job_scheduled_on(job.id, 7) |
| 618 | self._check_for_extra_schedulings() |
| 619 | |
| 620 | |
| 621 | def test_atomic_group_scheduling_not_enough_available(self): |
| 622 | # Mark some hosts in each atomic group label as not usable. |
| 623 | # One host running, another invalid in the first group label. |
| 624 | self._do_query('UPDATE hosts SET status="Running" WHERE id=5') |
| 625 | self._do_query('UPDATE hosts SET invalid=1 WHERE id=6') |
| 626 | # One host invalid in the second group label. |
| 627 | self._do_query('UPDATE hosts SET invalid=1 WHERE id=9') |
| 628 | # Nothing to schedule when no group label has enough (2) good hosts.. |
| 629 | self._create_job(atomic_group=1, synchronous=True) |
| 630 | self._dispatcher._schedule_new_jobs() |
| 631 | # There are not enough hosts in either atomic group, |
| 632 | # No more scheduling should occur. |
| 633 | self._check_for_extra_schedulings() |
| 634 | |
| 635 | # Now create an atomic job that has a synch count of 1. It should |
| 636 | # schedule on exactly one of the hosts. |
| 637 | onehost_job = self._create_job(atomic_group=1) |
| 638 | self._dispatcher._schedule_new_jobs() |
| 639 | self._assert_job_scheduled_on_number_of(onehost_job.id, (7, 8), 1) |
| 640 | |
| 641 | |
| 642 | def test_atomic_group_scheduling_no_valid_hosts(self): |
| 643 | self._do_query('UPDATE hosts SET invalid=1 WHERE id in (8,9)') |
| 644 | self._create_job(synchronous=True, metahosts=[self.label5.id], |
| 645 | atomic_group=1) |
| 646 | self._dispatcher._schedule_new_jobs() |
| 647 | # no hosts in the selected group and label are valid. no schedulings. |
| 648 | self._check_for_extra_schedulings() |
| 649 | |
| 650 | |
| 651 | def test_atomic_group_scheduling_metahost_works(self): |
| 652 | # Test that atomic group scheduling also obeys metahosts. |
| 653 | self._create_job(metahosts=[0], atomic_group=1) |
| 654 | self._dispatcher._schedule_new_jobs() |
| 655 | # There are no atomic group hosts that also have that metahost. |
| 656 | self._check_for_extra_schedulings() |
| 657 | |
| 658 | job_b = self._create_job(metahosts=[self.label5.id], atomic_group=1) |
| 659 | self._dispatcher._schedule_new_jobs() |
| 660 | self._assert_job_scheduled_on(job_b.id, 8) |
| 661 | self._assert_job_scheduled_on(job_b.id, 9) |
| 662 | self._check_for_extra_schedulings() |
| 663 | |
| 664 | |
| 665 | def test_atomic_group_skips_ineligible_hosts(self): |
| 666 | # Test hosts marked ineligible for this job are not eligible. |
| 667 | # How would this ever happen anyways? |
| 668 | job = self._create_job(metahosts=[self.label4.id], atomic_group=1) |
| 669 | models.IneligibleHostQueue.objects.create(job=job, host_id=5) |
| 670 | models.IneligibleHostQueue.objects.create(job=job, host_id=6) |
| 671 | models.IneligibleHostQueue.objects.create(job=job, host_id=7) |
| 672 | self._dispatcher._schedule_new_jobs() |
| 673 | # No scheduling should occur as all desired hosts were ineligible. |
| 674 | self._check_for_extra_schedulings() |
| 675 | |
| 676 | |
| 677 | def test_atomic_group_scheduling_fail(self): |
| 678 | # If synch_count is > the atomic group number of machines, the job |
| 679 | # should be aborted immediately. |
| 680 | model_job = self._create_job(synchronous=True, atomic_group=1) |
| 681 | model_job.synch_count = 4 |
| 682 | model_job.save() |
| 683 | job = monitor_db.Job(id=model_job.id) |
| 684 | self._dispatcher._schedule_new_jobs() |
| 685 | self._check_for_extra_schedulings() |
| 686 | queue_entries = job.get_host_queue_entries() |
| 687 | self.assertEqual(1, len(queue_entries)) |
| 688 | self.assertEqual(queue_entries[0].status, |
| 689 | models.HostQueueEntry.Status.ABORTED) |
| 690 | |
| 691 | |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 692 | def test_atomic_group_no_labels_no_scheduling(self): |
| 693 | # Never schedule on atomic groups marked invalid. |
| 694 | job = self._create_job(metahosts=[self.label5.id], synchronous=True, |
| 695 | atomic_group=1) |
| 696 | # Deleting an atomic group via the frontend marks it invalid and |
| 697 | # removes all label references to the group. The job now references |
| 698 | # an invalid atomic group with no labels associated with it. |
| 699 | self.label5.atomic_group.invalid = True |
| 700 | self.label5.atomic_group.save() |
| 701 | self.label5.atomic_group = None |
| 702 | self.label5.save() |
| 703 | |
| 704 | self._dispatcher._schedule_new_jobs() |
| 705 | self._check_for_extra_schedulings() |
| 706 | |
| 707 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 708 | def test_schedule_directly_on_atomic_group_host_fail(self): |
| 709 | # Scheduling a job directly on hosts in an atomic group must |
| 710 | # fail to avoid users inadvertently holding up the use of an |
| 711 | # entire atomic group by using the machines individually. |
| 712 | job = self._create_job(hosts=[5]) |
| 713 | self._dispatcher._schedule_new_jobs() |
| 714 | self._check_for_extra_schedulings() |
| 715 | |
| 716 | |
| 717 | def test_schedule_directly_on_atomic_group_host(self): |
| 718 | # Scheduling a job directly on one host in an atomic group will |
| 719 | # work when the atomic group is listed on the HQE in addition |
| 720 | # to the host (assuming the sync count is 1). |
| 721 | job = self._create_job(hosts=[5], atomic_group=1) |
| 722 | self._dispatcher._schedule_new_jobs() |
| 723 | self._assert_job_scheduled_on(job.id, 5) |
| 724 | self._check_for_extra_schedulings() |
| 725 | |
| 726 | |
| 727 | def test_schedule_directly_on_atomic_group_hosts_sync2(self): |
| 728 | job = self._create_job(hosts=[5,8], atomic_group=1, synchronous=True) |
| 729 | self._dispatcher._schedule_new_jobs() |
| 730 | self._assert_job_scheduled_on(job.id, 5) |
| 731 | self._assert_job_scheduled_on(job.id, 8) |
| 732 | self._check_for_extra_schedulings() |
| 733 | |
| 734 | |
| 735 | def test_schedule_directly_on_atomic_group_hosts_wrong_group(self): |
| 736 | job = self._create_job(hosts=[5,8], atomic_group=2, synchronous=True) |
| 737 | self._dispatcher._schedule_new_jobs() |
| 738 | self._check_for_extra_schedulings() |
| 739 | |
| 740 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 741 | def test_only_schedule_queued_entries(self): |
| 742 | self._create_job(metahosts=[1]) |
| 743 | self._update_hqe(set='active=1, host_id=2') |
| 744 | self._dispatcher._schedule_new_jobs() |
| 745 | self._check_for_extra_schedulings() |
| 746 | |
| 747 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 748 | def test_no_ready_hosts(self): |
| 749 | self._create_job(hosts=[1]) |
| 750 | self._do_query('UPDATE hosts SET status="Repair Failed"') |
| 751 | self._dispatcher._schedule_new_jobs() |
| 752 | self._check_for_extra_schedulings() |
| 753 | |
| 754 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 755 | class DispatcherThrottlingTest(BaseSchedulerTest): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 756 | """ |
| 757 | Test that the dispatcher throttles: |
| 758 | * total number of running processes |
| 759 | * number of processes started per cycle |
| 760 | """ |
| 761 | _MAX_RUNNING = 3 |
| 762 | _MAX_STARTED = 2 |
| 763 | |
| 764 | def setUp(self): |
| 765 | super(DispatcherThrottlingTest, self).setUp() |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 766 | scheduler_config.config.max_processes_per_drone = self._MAX_RUNNING |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 767 | scheduler_config.config.max_processes_started_per_cycle = ( |
| 768 | self._MAX_STARTED) |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 769 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 770 | def fake_max_runnable_processes(fake_self): |
| 771 | running = sum(agent.num_processes |
| 772 | for agent in self._agents |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 773 | if agent.started and not agent.is_done()) |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 774 | return self._MAX_RUNNING - running |
| 775 | self.god.stub_with(drone_manager.DroneManager, 'max_runnable_processes', |
| 776 | fake_max_runnable_processes) |
showard | 2fa5169 | 2009-01-13 23:48:08 +0000 | [diff] [blame] | 777 | |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 778 | |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 779 | def _setup_some_agents(self, num_agents): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 780 | self._agents = [DummyAgent() for i in xrange(num_agents)] |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 781 | self._dispatcher._agents = list(self._agents) |
| 782 | |
| 783 | |
| 784 | def _run_a_few_cycles(self): |
| 785 | for i in xrange(4): |
| 786 | self._dispatcher._handle_agents() |
| 787 | |
| 788 | |
| 789 | def _assert_agents_started(self, indexes, is_started=True): |
| 790 | for i in indexes: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 791 | self.assert_(self._agents[i].started == is_started, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 792 | 'Agent %d %sstarted' % |
| 793 | (i, is_started and 'not ' or '')) |
| 794 | |
| 795 | |
| 796 | def _assert_agents_not_started(self, indexes): |
| 797 | self._assert_agents_started(indexes, False) |
| 798 | |
| 799 | |
| 800 | def test_throttle_total(self): |
| 801 | self._setup_some_agents(4) |
| 802 | self._run_a_few_cycles() |
| 803 | self._assert_agents_started([0, 1, 2]) |
| 804 | self._assert_agents_not_started([3]) |
| 805 | |
| 806 | |
| 807 | def test_throttle_per_cycle(self): |
| 808 | self._setup_some_agents(3) |
| 809 | self._dispatcher._handle_agents() |
| 810 | self._assert_agents_started([0, 1]) |
| 811 | self._assert_agents_not_started([2]) |
| 812 | |
| 813 | |
| 814 | def test_throttle_with_synchronous(self): |
| 815 | self._setup_some_agents(2) |
| 816 | self._agents[0].num_processes = 3 |
| 817 | self._run_a_few_cycles() |
| 818 | self._assert_agents_started([0]) |
| 819 | self._assert_agents_not_started([1]) |
| 820 | |
| 821 | |
| 822 | def test_large_agent_starvation(self): |
| 823 | """ |
| 824 | Ensure large agents don't get starved by lower-priority agents. |
| 825 | """ |
| 826 | self._setup_some_agents(3) |
| 827 | self._agents[1].num_processes = 3 |
| 828 | self._run_a_few_cycles() |
| 829 | self._assert_agents_started([0]) |
| 830 | self._assert_agents_not_started([1, 2]) |
| 831 | |
| 832 | self._agents[0].set_done(True) |
| 833 | self._run_a_few_cycles() |
| 834 | self._assert_agents_started([1]) |
| 835 | self._assert_agents_not_started([2]) |
| 836 | |
| 837 | |
| 838 | def test_zero_process_agent(self): |
| 839 | self._setup_some_agents(5) |
| 840 | self._agents[4].num_processes = 0 |
| 841 | self._run_a_few_cycles() |
| 842 | self._assert_agents_started([0, 1, 2, 4]) |
| 843 | self._assert_agents_not_started([3]) |
| 844 | |
| 845 | |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 846 | class FindAbortTest(BaseSchedulerTest): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 847 | """ |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 848 | Test the dispatcher abort functionality. |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 849 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 850 | def _check_host_agent(self, agent, host_id): |
| 851 | self.assert_(isinstance(agent, monitor_db.Agent)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 852 | self.assert_(agent.task) |
| 853 | cleanup = agent.task |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 854 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 855 | self.assert_(isinstance(cleanup, monitor_db.CleanupTask)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 856 | self.assertEquals(cleanup.host.id, host_id) |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 857 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 858 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 859 | def _check_agents(self, agents): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 860 | agents = list(agents) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 861 | self.assertEquals(len(agents), 2) |
| 862 | self._check_host_agent(agents[0], 1) |
| 863 | self._check_host_agent(agents[1], 2) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 864 | |
| 865 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 866 | def _common_setup(self): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 867 | self._create_job(hosts=[1, 2]) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 868 | self._update_hqe(set='aborted=1') |
| 869 | self._agent = self.god.create_mock_class(monitor_db.Agent, 'old_agent') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 870 | self._agent.started = True |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 871 | _set_host_and_qe_ids(self._agent, [1, 2]) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 872 | self._agent.abort.expect_call() |
| 873 | self._agent.abort.expect_call() # gets called once for each HQE |
| 874 | self._dispatcher.add_agent(self._agent) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 875 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 876 | |
| 877 | def test_find_aborting(self): |
| 878 | self._common_setup() |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 879 | self._dispatcher._find_aborting() |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 880 | self.god.check_playback() |
| 881 | |
| 882 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 883 | def test_find_aborting_verifying(self): |
| 884 | self._common_setup() |
| 885 | self._update_hqe(set='active=1, status="Verifying"') |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 886 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 887 | self._agent.tick.expect_call() |
| 888 | self._agent.is_done.expect_call().and_return(True) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 889 | self._dispatcher._find_aborting() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 890 | self._dispatcher._handle_agents() |
| 891 | self._dispatcher._schedule_special_tasks() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 892 | self._check_agents(self._dispatcher._agents) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 893 | self.god.check_playback() |
| 894 | |
| 895 | |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 896 | class JobTimeoutTest(BaseSchedulerTest): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 897 | def _test_synch_start_timeout_helper(self, expect_abort, |
| 898 | set_created_on=True, set_active=True, |
| 899 | set_acl=True): |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 900 | scheduler_config.config.synch_job_start_timeout_minutes = 60 |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 901 | job = self._create_job(hosts=[1, 2]) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 902 | if set_active: |
| 903 | hqe = job.hostqueueentry_set.filter(host__id=1)[0] |
| 904 | hqe.status = 'Pending' |
| 905 | hqe.active = 1 |
| 906 | hqe.save() |
| 907 | |
| 908 | everyone_acl = models.AclGroup.smart_get('Everyone') |
| 909 | host1 = models.Host.smart_get(1) |
| 910 | if set_acl: |
| 911 | everyone_acl.hosts.add(host1) |
| 912 | else: |
| 913 | everyone_acl.hosts.remove(host1) |
| 914 | |
| 915 | job.created_on = datetime.datetime.now() |
| 916 | if set_created_on: |
| 917 | job.created_on -= datetime.timedelta(minutes=100) |
| 918 | job.save() |
| 919 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 920 | cleanup = self._dispatcher._periodic_cleanup |
| 921 | cleanup._abort_jobs_past_synch_start_timeout() |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 922 | |
| 923 | for hqe in job.hostqueueentry_set.all(): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 924 | self.assertEquals(hqe.aborted, expect_abort) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 925 | |
| 926 | |
| 927 | def test_synch_start_timeout_helper(self): |
| 928 | # no abort if any of the condition aren't met |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 929 | self._test_synch_start_timeout_helper(False, set_created_on=False) |
| 930 | self._test_synch_start_timeout_helper(False, set_active=False) |
| 931 | self._test_synch_start_timeout_helper(False, set_acl=False) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 932 | # abort if all conditions are met |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 933 | self._test_synch_start_timeout_helper(True) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 934 | |
| 935 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 936 | class PidfileRunMonitorTest(unittest.TestCase): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 937 | execution_tag = 'test_tag' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 938 | pid = 12345 |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 939 | process = drone_manager.Process('myhost', pid) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 940 | num_tests_failed = 1 |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 941 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 942 | def setUp(self): |
| 943 | self.god = mock.mock_god() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 944 | self.mock_drone_manager = self.god.create_mock_class( |
| 945 | drone_manager.DroneManager, 'drone_manager') |
| 946 | self.god.stub_with(monitor_db, '_drone_manager', |
| 947 | self.mock_drone_manager) |
| 948 | self.god.stub_function(email_manager.manager, 'enqueue_notify_email') |
| 949 | |
| 950 | self.pidfile_id = object() |
| 951 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 952 | (self.mock_drone_manager.get_pidfile_id_from |
| 953 | .expect_call(self.execution_tag, |
| 954 | pidfile_name=monitor_db._AUTOSERV_PID_FILE) |
| 955 | .and_return(self.pidfile_id)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 956 | self.mock_drone_manager.register_pidfile.expect_call(self.pidfile_id) |
| 957 | |
| 958 | self.monitor = monitor_db.PidfileRunMonitor() |
| 959 | self.monitor.attach_to_existing_process(self.execution_tag) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 960 | |
| 961 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 962 | def tearDown(self): |
| 963 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 964 | |
| 965 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 966 | def setup_pidfile(self, pid=None, exit_code=None, tests_failed=None, |
| 967 | use_second_read=False): |
| 968 | contents = drone_manager.PidfileContents() |
| 969 | if pid is not None: |
| 970 | contents.process = drone_manager.Process('myhost', pid) |
| 971 | contents.exit_status = exit_code |
| 972 | contents.num_tests_failed = tests_failed |
| 973 | self.mock_drone_manager.get_pidfile_contents.expect_call( |
| 974 | self.pidfile_id, use_second_read=use_second_read).and_return( |
| 975 | contents) |
| 976 | |
| 977 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 978 | def set_not_yet_run(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 979 | self.setup_pidfile() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 980 | |
| 981 | |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 982 | def set_empty_pidfile(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 983 | self.setup_pidfile() |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 984 | |
| 985 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 986 | def set_running(self, use_second_read=False): |
| 987 | self.setup_pidfile(self.pid, use_second_read=use_second_read) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 988 | |
| 989 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 990 | def set_complete(self, error_code, use_second_read=False): |
| 991 | self.setup_pidfile(self.pid, error_code, self.num_tests_failed, |
| 992 | use_second_read=use_second_read) |
| 993 | |
| 994 | |
| 995 | def _check_monitor(self, expected_pid, expected_exit_status, |
| 996 | expected_num_tests_failed): |
| 997 | if expected_pid is None: |
| 998 | self.assertEquals(self.monitor._state.process, None) |
| 999 | else: |
| 1000 | self.assertEquals(self.monitor._state.process.pid, expected_pid) |
| 1001 | self.assertEquals(self.monitor._state.exit_status, expected_exit_status) |
| 1002 | self.assertEquals(self.monitor._state.num_tests_failed, |
| 1003 | expected_num_tests_failed) |
| 1004 | |
| 1005 | |
| 1006 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1007 | |
| 1008 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1009 | def _test_read_pidfile_helper(self, expected_pid, expected_exit_status, |
| 1010 | expected_num_tests_failed): |
| 1011 | self.monitor._read_pidfile() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1012 | self._check_monitor(expected_pid, expected_exit_status, |
| 1013 | expected_num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1014 | |
| 1015 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1016 | def _get_expected_tests_failed(self, expected_exit_status): |
| 1017 | if expected_exit_status is None: |
| 1018 | expected_tests_failed = None |
| 1019 | else: |
| 1020 | expected_tests_failed = self.num_tests_failed |
| 1021 | return expected_tests_failed |
| 1022 | |
| 1023 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1024 | def test_read_pidfile(self): |
| 1025 | self.set_not_yet_run() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1026 | self._test_read_pidfile_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1027 | |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 1028 | self.set_empty_pidfile() |
| 1029 | self._test_read_pidfile_helper(None, None, None) |
| 1030 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1031 | self.set_running() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1032 | self._test_read_pidfile_helper(self.pid, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1033 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1034 | self.set_complete(123) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1035 | self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1036 | |
| 1037 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1038 | def test_read_pidfile_error(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1039 | self.mock_drone_manager.get_pidfile_contents.expect_call( |
| 1040 | self.pidfile_id, use_second_read=False).and_return( |
| 1041 | drone_manager.InvalidPidfile('error')) |
| 1042 | self.assertRaises(monitor_db.PidfileRunMonitor._PidfileException, |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1043 | self.monitor._read_pidfile) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1044 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1045 | |
| 1046 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1047 | def setup_is_running(self, is_running): |
| 1048 | self.mock_drone_manager.is_process_running.expect_call( |
| 1049 | self.process).and_return(is_running) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1050 | |
| 1051 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1052 | def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status, |
| 1053 | expected_num_tests_failed): |
| 1054 | self.monitor._get_pidfile_info() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1055 | self._check_monitor(expected_pid, expected_exit_status, |
| 1056 | expected_num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1057 | |
| 1058 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1059 | def test_get_pidfile_info(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1060 | """ |
| 1061 | normal cases for get_pidfile_info |
| 1062 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1063 | # running |
| 1064 | self.set_running() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1065 | self.setup_is_running(True) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1066 | self._test_get_pidfile_info_helper(self.pid, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1067 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1068 | # exited during check |
| 1069 | self.set_running() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1070 | self.setup_is_running(False) |
| 1071 | self.set_complete(123, use_second_read=True) # pidfile gets read again |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1072 | self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1073 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1074 | # completed |
| 1075 | self.set_complete(123) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1076 | self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1077 | |
| 1078 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1079 | def test_get_pidfile_info_running_no_proc(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1080 | """ |
| 1081 | pidfile shows process running, but no proc exists |
| 1082 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1083 | # running but no proc |
| 1084 | self.set_running() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1085 | self.setup_is_running(False) |
| 1086 | self.set_running(use_second_read=True) |
| 1087 | email_manager.manager.enqueue_notify_email.expect_call( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1088 | mock.is_string_comparator(), mock.is_string_comparator()) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1089 | self._test_get_pidfile_info_helper(self.pid, 1, 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1090 | self.assertTrue(self.monitor.lost_process) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1091 | |
| 1092 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1093 | def test_get_pidfile_info_not_yet_run(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1094 | """ |
| 1095 | pidfile hasn't been written yet |
| 1096 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1097 | self.set_not_yet_run() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1098 | self._test_get_pidfile_info_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1099 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1100 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1101 | def test_process_failed_to_write_pidfile(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1102 | self.set_not_yet_run() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1103 | email_manager.manager.enqueue_notify_email.expect_call( |
| 1104 | mock.is_string_comparator(), mock.is_string_comparator()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1105 | self.monitor._start_time = time.time() - monitor_db.PIDFILE_TIMEOUT - 1 |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1106 | self._test_get_pidfile_info_helper(None, 1, 0) |
| 1107 | self.assertTrue(self.monitor.lost_process) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1108 | |
| 1109 | |
| 1110 | class AgentTest(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1111 | def setUp(self): |
| 1112 | self.god = mock.mock_god() |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1113 | self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher, |
| 1114 | 'dispatcher') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1115 | |
| 1116 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1117 | def tearDown(self): |
| 1118 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1119 | |
| 1120 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1121 | def _create_mock_task(self, name): |
| 1122 | task = self.god.create_mock_class(monitor_db.AgentTask, name) |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1123 | _set_host_and_qe_ids(task) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1124 | return task |
| 1125 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1126 | def _create_agent(self, task): |
| 1127 | agent = monitor_db.Agent(task) |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1128 | agent.dispatcher = self._dispatcher |
| 1129 | return agent |
| 1130 | |
| 1131 | |
| 1132 | def _finish_agent(self, agent): |
| 1133 | while not agent.is_done(): |
| 1134 | agent.tick() |
| 1135 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1136 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1137 | def test_agent_abort(self): |
| 1138 | task = self._create_mock_task('task') |
| 1139 | task.poll.expect_call() |
| 1140 | task.is_done.expect_call().and_return(False) |
| 1141 | task.abort.expect_call() |
| 1142 | task.aborted = True |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1143 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1144 | agent = self._create_agent(task) |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1145 | agent.tick() |
| 1146 | agent.abort() |
| 1147 | self._finish_agent(agent) |
| 1148 | self.god.check_playback() |
| 1149 | |
| 1150 | |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1151 | def _test_agent_abort_before_started_helper(self, ignore_abort=False): |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1152 | task = self._create_mock_task('task') |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1153 | task.abort.expect_call() |
| 1154 | if ignore_abort: |
| 1155 | task.aborted = False |
| 1156 | task.poll.expect_call() |
| 1157 | task.is_done.expect_call().and_return(True) |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1158 | task.success = True |
| 1159 | else: |
| 1160 | task.aborted = True |
| 1161 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1162 | agent = self._create_agent(task) |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1163 | agent.abort() |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1164 | self._finish_agent(agent) |
| 1165 | self.god.check_playback() |
| 1166 | |
| 1167 | |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1168 | def test_agent_abort_before_started(self): |
| 1169 | self._test_agent_abort_before_started_helper() |
| 1170 | self._test_agent_abort_before_started_helper(True) |
| 1171 | |
| 1172 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1173 | class DelayedCallTaskTest(unittest.TestCase): |
| 1174 | def setUp(self): |
| 1175 | self.god = mock.mock_god() |
| 1176 | |
| 1177 | |
| 1178 | def tearDown(self): |
| 1179 | self.god.unstub_all() |
| 1180 | |
| 1181 | |
| 1182 | def test_delayed_call(self): |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 1183 | test_time = self.god.create_mock_function('time') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1184 | test_time.expect_call().and_return(33) |
| 1185 | test_time.expect_call().and_return(34.01) |
| 1186 | test_time.expect_call().and_return(34.99) |
| 1187 | test_time.expect_call().and_return(35.01) |
| 1188 | def test_callback(): |
| 1189 | test_callback.calls += 1 |
| 1190 | test_callback.calls = 0 |
| 1191 | delay_task = monitor_db.DelayedCallTask( |
| 1192 | delay_seconds=2, callback=test_callback, |
| 1193 | now_func=test_time) # time 33 |
| 1194 | self.assertEqual(35, delay_task.end_time) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1195 | agent = monitor_db.Agent(delay_task, num_processes=0) |
| 1196 | self.assert_(not agent.started) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1197 | agent.tick() # activates the task and polls it once, time 34.01 |
| 1198 | self.assertEqual(0, test_callback.calls, "callback called early") |
| 1199 | agent.tick() # time 34.99 |
| 1200 | self.assertEqual(0, test_callback.calls, "callback called early") |
| 1201 | agent.tick() # time 35.01 |
| 1202 | self.assertEqual(1, test_callback.calls) |
| 1203 | self.assert_(agent.is_done()) |
| 1204 | self.assert_(delay_task.is_done()) |
| 1205 | self.assert_(delay_task.success) |
| 1206 | self.assert_(not delay_task.aborted) |
| 1207 | self.god.check_playback() |
| 1208 | |
| 1209 | |
| 1210 | def test_delayed_call_abort(self): |
| 1211 | delay_task = monitor_db.DelayedCallTask( |
| 1212 | delay_seconds=987654, callback=lambda : None) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1213 | agent = monitor_db.Agent(delay_task, num_processes=0) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1214 | agent.abort() |
| 1215 | agent.tick() |
| 1216 | self.assert_(agent.is_done()) |
| 1217 | self.assert_(delay_task.aborted) |
| 1218 | self.assert_(delay_task.is_done()) |
| 1219 | self.assert_(not delay_task.success) |
| 1220 | self.god.check_playback() |
| 1221 | |
| 1222 | |
| 1223 | |
showard | 184a5e8 | 2009-05-29 18:42:20 +0000 | [diff] [blame] | 1224 | class AgentTasksTest(BaseSchedulerTest): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1225 | ABSPATH_BASE = '/abspath/' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1226 | HOSTNAME = 'myhost' |
showard | 381341a | 2009-07-15 14:28:56 +0000 | [diff] [blame] | 1227 | BASE_TASK_DIR = 'hosts/%s/' % HOSTNAME |
| 1228 | RESULTS_DIR = '/results/dir' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1229 | DUMMY_PROCESS = object() |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 1230 | HOST_PROTECTION = host_protections.default |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1231 | PIDFILE_ID = object() |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1232 | JOB_OWNER = 'test_owner' |
| 1233 | JOB_NAME = 'test_job_name' |
| 1234 | JOB_AUTOSERV_PARAMS = set(['-u', JOB_OWNER, '-l', JOB_NAME]) |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1235 | PLATFORM = 'test_platform' |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1236 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1237 | def setUp(self): |
showard | 184a5e8 | 2009-05-29 18:42:20 +0000 | [diff] [blame] | 1238 | super(AgentTasksTest, self).setUp() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1239 | self.god = mock.mock_god() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1240 | self.god.stub_with(drone_manager.DroneManager, 'get_temporary_path', |
| 1241 | mock.mock_function('get_temporary_path', |
| 1242 | default_return_val='tempdir')) |
| 1243 | self.god.stub_function(drone_manager.DroneManager, |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1244 | 'copy_results_on_drone') |
| 1245 | self.god.stub_function(drone_manager.DroneManager, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1246 | 'copy_to_results_repository') |
| 1247 | self.god.stub_function(drone_manager.DroneManager, |
| 1248 | 'get_pidfile_id_from') |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1249 | self.god.stub_function(drone_manager.DroneManager, |
| 1250 | 'attach_file_to_execution') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1251 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1252 | def dummy_absolute_path(drone_manager_self, path): |
| 1253 | return self.ABSPATH_BASE + path |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1254 | self.god.stub_with(drone_manager.DroneManager, 'absolute_path', |
| 1255 | dummy_absolute_path) |
| 1256 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1257 | def dummy_abspath(path): |
| 1258 | return self.ABSPATH_BASE + path |
| 1259 | self.god.stub_with(os.path, 'abspath', dummy_abspath) |
| 1260 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1261 | self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'run') |
| 1262 | self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'exit_code') |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1263 | self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'kill') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1264 | self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'get_process') |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1265 | self.god.stub_class_method(monitor_db.PidfileRunMonitor, |
| 1266 | 'try_copy_to_results_repository') |
| 1267 | self.god.stub_class_method(monitor_db.PidfileRunMonitor, |
| 1268 | 'try_copy_results_on_drone') |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1269 | def mock_has_process(unused): |
| 1270 | return True |
| 1271 | self.god.stub_with(monitor_db.PidfileRunMonitor, 'has_process', |
| 1272 | mock_has_process) |
showard | 381341a | 2009-07-15 14:28:56 +0000 | [diff] [blame] | 1273 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1274 | self.host = self.god.create_mock_class(monitor_db.Host, 'host') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1275 | self.host.id = 1 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1276 | self.host.hostname = self.HOSTNAME |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 1277 | self.host.protection = self.HOST_PROTECTION |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1278 | |
| 1279 | # For this (and other model creations), we must create the entry this |
| 1280 | # way; otherwise, if an entry matching the id already exists, Django 1.0 |
| 1281 | # will raise an exception complaining about a duplicate primary key. |
| 1282 | # This way, Django performs an UPDATE query if an entry matching the id |
| 1283 | # already exists. |
| 1284 | host = models.Host(id=self.host.id, hostname=self.host.hostname, |
| 1285 | protection=self.host.protection) |
| 1286 | host.save() |
showard | 381341a | 2009-07-15 14:28:56 +0000 | [diff] [blame] | 1287 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1288 | self.job = self.god.create_mock_class(monitor_db.Job, 'job') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1289 | self.job.owner = self.JOB_OWNER |
| 1290 | self.job.name = self.JOB_NAME |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 1291 | self.job.id = 1337 |
| 1292 | self.job.tag = lambda: 'fake-job-tag' |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1293 | job = models.Job(id=self.job.id, owner=self.job.owner, |
| 1294 | name=self.job.name, created_on=datetime.datetime.now()) |
| 1295 | job.save() |
showard | 381341a | 2009-07-15 14:28:56 +0000 | [diff] [blame] | 1296 | |
| 1297 | self.queue_entry = self.god.create_mock_class( |
| 1298 | monitor_db.HostQueueEntry, 'queue_entry') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1299 | self.queue_entry.id = 1 |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1300 | self.queue_entry.job = self.job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1301 | self.queue_entry.host = self.host |
| 1302 | self.queue_entry.meta_host = None |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 1303 | queue_entry = models.HostQueueEntry(id=self.queue_entry.id, job=job, |
| 1304 | host=host, meta_host=None) |
| 1305 | queue_entry.save() |
showard | 381341a | 2009-07-15 14:28:56 +0000 | [diff] [blame] | 1306 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1307 | self.task = models.SpecialTask(id=1, host=host, |
| 1308 | task=models.SpecialTask.Task.CLEANUP, |
| 1309 | is_active=False, is_complete=False, |
| 1310 | time_requested=datetime.datetime.now(), |
| 1311 | time_started=None, |
| 1312 | queue_entry=queue_entry) |
| 1313 | self.task.save() |
| 1314 | self.god.stub_function(self.task, 'activate') |
| 1315 | self.god.stub_function(self.task, 'finish') |
| 1316 | self.god.stub_function(models.SpecialTask.objects, 'create') |
| 1317 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1318 | self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher, |
| 1319 | 'dispatcher') |
| 1320 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1321 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1322 | def tearDown(self): |
showard | 184a5e8 | 2009-05-29 18:42:20 +0000 | [diff] [blame] | 1323 | super(AgentTasksTest, self).tearDown() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1324 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1325 | |
| 1326 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1327 | def run_task(self, task, success): |
| 1328 | """ |
| 1329 | Do essentially what an Agent would do, but protect againt |
| 1330 | infinite looping from test errors. |
| 1331 | """ |
| 1332 | if not getattr(task, 'agent', None): |
| 1333 | task.agent = object() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1334 | count = 0 |
| 1335 | while not task.is_done(): |
| 1336 | count += 1 |
| 1337 | if count > 10: |
| 1338 | print 'Task failed to finish' |
| 1339 | # in case the playback has clues to why it |
| 1340 | # failed |
| 1341 | self.god.check_playback() |
| 1342 | self.fail() |
| 1343 | task.poll() |
| 1344 | self.assertEquals(task.success, success) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1345 | |
| 1346 | |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1347 | def setup_run_monitor(self, exit_status, task_tag, copy_log_file=True, |
| 1348 | aborted=False): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1349 | monitor_db.PidfileRunMonitor.run.expect_call( |
| 1350 | mock.is_instance_comparator(list), |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1351 | self.BASE_TASK_DIR + task_tag, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1352 | nice_level=monitor_db.AUTOSERV_NICE_LEVEL, |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1353 | log_file=mock.anything_comparator(), |
| 1354 | pidfile_name=monitor_db._AUTOSERV_PID_FILE, |
| 1355 | paired_with_pidfile=None) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1356 | monitor_db.PidfileRunMonitor.exit_code.expect_call() |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1357 | if aborted: |
| 1358 | monitor_db.PidfileRunMonitor.kill.expect_call() |
| 1359 | else: |
| 1360 | monitor_db.PidfileRunMonitor.exit_code.expect_call().and_return( |
| 1361 | exit_status) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1362 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1363 | self.task.finish.expect_call() |
| 1364 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1365 | if copy_log_file: |
| 1366 | self._setup_move_logfile() |
| 1367 | |
| 1368 | |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1369 | def _setup_move_logfile(self, copy_on_drone=False, |
| 1370 | include_destination=False): |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1371 | monitor = monitor_db.PidfileRunMonitor |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1372 | if copy_on_drone: |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1373 | self.queue_entry.execution_path.expect_call().and_return('tag') |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1374 | monitor.try_copy_results_on_drone.expect_call( |
| 1375 | source_path=mock.is_string_comparator(), |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1376 | destination_path=mock.is_string_comparator()) |
| 1377 | elif include_destination: |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1378 | monitor.try_copy_to_results_repository.expect_call( |
| 1379 | mock.is_string_comparator(), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1380 | destination_path=mock.is_string_comparator()) |
| 1381 | else: |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1382 | monitor.try_copy_to_results_repository.expect_call( |
| 1383 | mock.is_string_comparator()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1384 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1385 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1386 | def _setup_special_task(self, task_id, task_type, use_queue_entry): |
| 1387 | self.task.id = task_id |
| 1388 | self.task.task = task_type |
| 1389 | if use_queue_entry: |
| 1390 | queue_entry = models.HostQueueEntry(id=self.queue_entry.id) |
| 1391 | else: |
| 1392 | queue_entry = None |
| 1393 | self.task.queue_entry = queue_entry |
| 1394 | self.task.save() |
| 1395 | |
| 1396 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1397 | def _setup_write_host_keyvals_expects(self, task_tag): |
| 1398 | self.host.platform_and_labels.expect_call().and_return( |
| 1399 | (self.PLATFORM, (self.PLATFORM,))) |
| 1400 | |
| 1401 | execution_path = os.path.join(self.BASE_TASK_DIR, task_tag) |
| 1402 | file_contents = ('platform=%(platform)s\nlabels=%(platform)s\n' |
| 1403 | % dict(platform=self.PLATFORM)) |
| 1404 | file_path = os.path.join(execution_path, 'host_keyvals', self.HOSTNAME) |
| 1405 | drone_manager.DroneManager.attach_file_to_execution.expect_call( |
| 1406 | execution_path, file_contents, file_path=file_path) |
| 1407 | |
| 1408 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1409 | def _test_repair_task_helper(self, success, task_id, use_queue_entry=False): |
| 1410 | self._setup_special_task(task_id, models.SpecialTask.Task.REPAIR, |
| 1411 | use_queue_entry) |
| 1412 | task_tag = '%d-repair' % task_id |
| 1413 | |
| 1414 | self.task.activate.expect_call() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1415 | self._setup_write_host_keyvals_expects(task_tag) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1416 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1417 | self.host.set_status.expect_call('Repairing') |
| 1418 | if success: |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1419 | self.setup_run_monitor(0, task_tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1420 | self.host.set_status.expect_call('Ready') |
| 1421 | else: |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1422 | self.setup_run_monitor(1, task_tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1423 | self.host.set_status.expect_call('Repair Failed') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1424 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1425 | task = monitor_db.RepairTask(task=self.task) |
| 1426 | task.host = self.host |
| 1427 | if use_queue_entry: |
| 1428 | task.queue_entry = self.queue_entry |
| 1429 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1430 | self.run_task(task, success) |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 1431 | |
| 1432 | expected_protection = host_protections.Protection.get_string( |
| 1433 | host_protections.default) |
mbligh | 3e0f7e0 | 2008-07-28 19:42:01 +0000 | [diff] [blame] | 1434 | expected_protection = host_protections.Protection.get_attr_name( |
| 1435 | expected_protection) |
| 1436 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1437 | self.assertTrue(set(task.cmd) >= |
| 1438 | set([monitor_db._autoserv_path, '-p', '-R', '-m', |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1439 | self.HOSTNAME, '-r', |
| 1440 | drone_manager.WORKING_DIRECTORY, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1441 | '--host-protection', expected_protection])) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1442 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1443 | |
| 1444 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1445 | def test_repair_task(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1446 | self._test_repair_task_helper(True, 1) |
| 1447 | self._test_repair_task_helper(False, 2) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1448 | |
| 1449 | |
showard | cfd4a7e | 2009-07-11 01:47:33 +0000 | [diff] [blame] | 1450 | def test_repair_task_with_hqe_already_requeued(self): |
| 1451 | # during recovery, a RepairTask can be passed a queue entry that has |
| 1452 | # already been requeued. ensure it leaves the HQE alone in that case. |
| 1453 | self.queue_entry.meta_host = 1 |
| 1454 | self.queue_entry.host = None |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1455 | self._test_repair_task_helper(False, 1, True) |
showard | cfd4a7e | 2009-07-11 01:47:33 +0000 | [diff] [blame] | 1456 | |
| 1457 | |
showard | 8ac6f2a | 2009-07-16 14:50:32 +0000 | [diff] [blame] | 1458 | def test_recovery_repair_task_working_directory(self): |
| 1459 | # ensure that a RepairTask recovering an existing SpecialTask picks up |
| 1460 | # the working directory immediately |
| 1461 | class MockSpecialTask(object): |
| 1462 | def execution_path(self): |
| 1463 | return '/my/path' |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1464 | host = models.Host(id=self.host.id) |
| 1465 | queue_entry = None |
showard | 8ac6f2a | 2009-07-16 14:50:32 +0000 | [diff] [blame] | 1466 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1467 | task = monitor_db.RepairTask(task=MockSpecialTask()) |
showard | 8ac6f2a | 2009-07-16 14:50:32 +0000 | [diff] [blame] | 1468 | |
| 1469 | self.assertEquals(task._working_directory, '/my/path') |
| 1470 | |
| 1471 | |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1472 | def test_repair_task_aborted(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1473 | self._setup_special_task(1, models.SpecialTask.Task.REPAIR, False) |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1474 | task_tag = '1-repair' |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1475 | |
| 1476 | self.task.activate.expect_call() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1477 | self._setup_write_host_keyvals_expects(task_tag) |
| 1478 | |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1479 | self.host.set_status.expect_call('Repairing') |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1480 | self.setup_run_monitor(0, task_tag, aborted=True) |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1481 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1482 | task = monitor_db.RepairTask(task=self.task) |
| 1483 | task.host = self.host |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1484 | task.agent = object() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1485 | |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1486 | task.poll() |
| 1487 | task.abort() |
| 1488 | |
| 1489 | self.assertTrue(task.done) |
| 1490 | self.assertTrue(task.aborted) |
showard | b6681aa | 2009-07-08 21:15:00 +0000 | [diff] [blame] | 1491 | self.god.check_playback() |
| 1492 | |
| 1493 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1494 | def _test_repair_task_with_queue_entry_helper(self, parse_failed_repair, |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1495 | task_id): |
| 1496 | self._setup_special_task(task_id, models.SpecialTask.Task.REPAIR, True) |
| 1497 | task_tag = '%d-repair' % task_id |
| 1498 | |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1499 | self.god.stub_class(monitor_db, 'FinalReparseTask') |
| 1500 | self.god.stub_class(monitor_db, 'Agent') |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1501 | self.god.stub_class_method(monitor_db.TaskWithJobKeyvals, |
| 1502 | '_write_keyval_after_job') |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1503 | agent = DummyAgent() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1504 | agent.dispatcher = self._dispatcher |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1505 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1506 | self.task.activate.expect_call() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1507 | self._setup_write_host_keyvals_expects(task_tag) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1508 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1509 | self.host.set_status.expect_call('Repairing') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1510 | self.setup_run_monitor(1, task_tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1511 | self.host.set_status.expect_call('Repair Failed') |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1512 | self.queue_entry.update_from_database.expect_call() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1513 | self.queue_entry.status = 'Queued' |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1514 | self.queue_entry.set_execution_subdir.expect_call() |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1515 | monitor_db.TaskWithJobKeyvals._write_keyval_after_job.expect_call( |
| 1516 | 'job_queued', mock.is_instance_comparator(int)) |
| 1517 | monitor_db.TaskWithJobKeyvals._write_keyval_after_job.expect_call( |
| 1518 | 'job_finished', mock.is_instance_comparator(int)) |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1519 | self._setup_move_logfile(copy_on_drone=True) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1520 | self.queue_entry.execution_path.expect_call().and_return('tag') |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1521 | self._setup_move_logfile() |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1522 | self.job.parse_failed_repair = parse_failed_repair |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 1523 | self.queue_entry.handle_host_failure.expect_call() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1524 | if parse_failed_repair: |
| 1525 | self.queue_entry.set_status.expect_call('Parsing') |
| 1526 | self.queue_entry.execution_path.expect_call().and_return('tag') |
| 1527 | drone_manager.DroneManager.get_pidfile_id_from.expect_call( |
| 1528 | 'tag', pidfile_name=monitor_db._AUTOSERV_PID_FILE).and_return( |
| 1529 | self.PIDFILE_ID) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1530 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1531 | task = monitor_db.RepairTask(task=self.task) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1532 | task.agent = agent |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1533 | task.host = self.host |
| 1534 | task.queue_entry = self.queue_entry |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1535 | self.queue_entry.status = 'Queued' |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1536 | self.job.created_on = datetime.datetime(2009, 1, 1) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1537 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1538 | self.run_task(task, False) |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1539 | self.assertTrue(set(task.cmd) >= self.JOB_AUTOSERV_PARAMS) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1540 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1541 | |
| 1542 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1543 | def test_repair_task_with_queue_entry(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1544 | self._test_repair_task_with_queue_entry_helper(True, 1) |
| 1545 | self._test_repair_task_with_queue_entry_helper(False, 2) |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1546 | |
| 1547 | |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 1548 | def _setup_prejob_task_failure(self, task_tag, use_queue_entry): |
| 1549 | self.setup_run_monitor(1, task_tag) |
| 1550 | if use_queue_entry: |
| 1551 | if not self.queue_entry.meta_host: |
| 1552 | self.queue_entry.set_execution_subdir.expect_call() |
| 1553 | self.queue_entry.execution_path.expect_call().and_return('tag') |
| 1554 | self._setup_move_logfile(include_destination=True) |
| 1555 | self.queue_entry.requeue.expect_call() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1556 | queue_entry = models.HostQueueEntry(id=self.queue_entry.id) |
| 1557 | else: |
| 1558 | queue_entry = None |
| 1559 | models.SpecialTask.objects.create.expect_call( |
| 1560 | host=models.Host(id=self.host.id), |
| 1561 | task=models.SpecialTask.Task.REPAIR, |
| 1562 | queue_entry=queue_entry) |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 1563 | |
| 1564 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1565 | def setup_verify_expects(self, success, use_queue_entry, task_tag): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1566 | self.task.activate.expect_call() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1567 | self._setup_write_host_keyvals_expects(task_tag) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1568 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1569 | if use_queue_entry: |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1570 | self.queue_entry.set_status.expect_call('Verifying') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1571 | self.host.set_status.expect_call('Verifying') |
| 1572 | if success: |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1573 | self.setup_run_monitor(0, task_tag) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1574 | if use_queue_entry: |
| 1575 | self.queue_entry.on_pending.expect_call() |
| 1576 | else: |
| 1577 | self.host.set_status.expect_call('Ready') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1578 | else: |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 1579 | self._setup_prejob_task_failure(task_tag, use_queue_entry) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1580 | |
| 1581 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1582 | def _test_verify_task_helper(self, success, task_id, use_queue_entry=False, |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 1583 | use_meta_host=False): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1584 | self._setup_special_task(task_id, models.SpecialTask.Task.VERIFY, |
| 1585 | use_queue_entry) |
| 1586 | task_tag = '%d-verify' % task_id |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1587 | self.setup_verify_expects(success, use_queue_entry, task_tag) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1588 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1589 | task = monitor_db.VerifyTask(task=self.task) |
| 1590 | task.host = self.host |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1591 | if use_queue_entry: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1592 | task.queue_entry = self.queue_entry |
| 1593 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1594 | self.run_task(task, success) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1595 | self.assertTrue(set(task.cmd) >= |
| 1596 | set([monitor_db._autoserv_path, '-p', '-v', '-m', |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1597 | self.HOSTNAME, '-r', |
| 1598 | drone_manager.WORKING_DIRECTORY])) |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1599 | if use_queue_entry: |
| 1600 | self.assertTrue(set(task.cmd) >= self.JOB_AUTOSERV_PARAMS) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1601 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1602 | |
| 1603 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1604 | def test_verify_task_with_host(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1605 | self._test_verify_task_helper(True, 1) |
| 1606 | self._test_verify_task_helper(False, 2) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1607 | |
| 1608 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1609 | def test_verify_task_with_queue_entry(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1610 | self._test_verify_task_helper(True, 1, use_queue_entry=True) |
| 1611 | self._test_verify_task_helper(False, 2, use_queue_entry=True) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 1612 | |
| 1613 | |
| 1614 | def test_verify_task_with_metahost(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1615 | self.queue_entry.meta_host = 1 |
| 1616 | self.test_verify_task_with_queue_entry() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 1617 | |
| 1618 | |
showard | a640b2d | 2009-07-20 22:37:06 +0000 | [diff] [blame] | 1619 | def test_specialtask_abort_before_prolog(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1620 | self._setup_special_task(1, models.SpecialTask.Task.REPAIR, False) |
| 1621 | task = monitor_db.RepairTask(task=self.task) |
| 1622 | self.task.finish.expect_call() |
showard | a640b2d | 2009-07-20 22:37:06 +0000 | [diff] [blame] | 1623 | task.abort() |
| 1624 | self.assertTrue(task.aborted) |
| 1625 | |
| 1626 | |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1627 | def _setup_post_job_task_expects(self, autoserv_success, hqe_status=None, |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1628 | hqe_aborted=False, host_status=None): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1629 | self.queue_entry.execution_path.expect_call().and_return('tag') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1630 | self.pidfile_monitor = monitor_db.PidfileRunMonitor.expect_new() |
| 1631 | self.pidfile_monitor.pidfile_id = self.PIDFILE_ID |
| 1632 | self.pidfile_monitor.attach_to_existing_process.expect_call('tag') |
| 1633 | if autoserv_success: |
| 1634 | code = 0 |
| 1635 | else: |
| 1636 | code = 1 |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1637 | self.queue_entry.update_from_database.expect_call() |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1638 | self.queue_entry.aborted = hqe_aborted |
| 1639 | if not hqe_aborted: |
| 1640 | self.pidfile_monitor.exit_code.expect_call().and_return(code) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1641 | |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1642 | if hqe_status: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1643 | self.queue_entry.status = hqe_status |
| 1644 | if host_status: |
| 1645 | self.host.status = host_status |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1646 | |
| 1647 | |
| 1648 | def _setup_pre_parse_expects(self, autoserv_success): |
| 1649 | self._setup_post_job_task_expects(autoserv_success, 'Parsing') |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1650 | |
| 1651 | |
| 1652 | def _setup_post_parse_expects(self, autoserv_success): |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1653 | if autoserv_success: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1654 | status = 'Completed' |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1655 | else: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1656 | status = 'Failed' |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1657 | self.queue_entry.set_status.expect_call(status) |
| 1658 | |
| 1659 | |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1660 | def _expect_execute_run_monitor(self): |
| 1661 | self.monitor.exit_code.expect_call() |
| 1662 | self.monitor.exit_code.expect_call().and_return(0) |
| 1663 | self._expect_copy_results() |
| 1664 | |
| 1665 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1666 | def _setup_post_job_run_monitor(self, pidfile_name): |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1667 | self.pidfile_monitor.has_process.expect_call().and_return(True) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1668 | autoserv_pidfile_id = object() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1669 | self.monitor = monitor_db.PidfileRunMonitor.expect_new() |
| 1670 | self.monitor.run.expect_call( |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1671 | mock.is_instance_comparator(list), |
| 1672 | 'tag', |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1673 | nice_level=monitor_db.AUTOSERV_NICE_LEVEL, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1674 | log_file=mock.anything_comparator(), |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1675 | pidfile_name=pidfile_name, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1676 | paired_with_pidfile=self.PIDFILE_ID) |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1677 | self._expect_execute_run_monitor() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1678 | |
| 1679 | |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1680 | def _expect_copy_results(self, monitor=None, queue_entry=None): |
| 1681 | if monitor is None: |
| 1682 | monitor = self.monitor |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1683 | if queue_entry: |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1684 | queue_entry.execution_path.expect_call().and_return('tag') |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1685 | monitor.try_copy_to_results_repository.expect_call( |
| 1686 | mock.is_string_comparator()) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1687 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1688 | |
| 1689 | def _test_final_reparse_task_helper(self, autoserv_success=True): |
| 1690 | self._setup_pre_parse_expects(autoserv_success) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1691 | self._setup_post_job_run_monitor(monitor_db._PARSER_PID_FILE) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1692 | self._setup_post_parse_expects(autoserv_success) |
| 1693 | |
| 1694 | task = monitor_db.FinalReparseTask([self.queue_entry]) |
| 1695 | self.run_task(task, True) |
| 1696 | |
| 1697 | self.god.check_playback() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1698 | cmd = [monitor_db._parser_path, '--write-pidfile', '-l', '2', '-r', |
mbligh | 2d7c8bd | 2009-05-13 20:42:50 +0000 | [diff] [blame] | 1699 | '-o', '-P', '/abspath/tag'] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1700 | self.assertEquals(task.cmd, cmd) |
| 1701 | |
| 1702 | |
| 1703 | def test_final_reparse_task(self): |
| 1704 | self.god.stub_class(monitor_db, 'PidfileRunMonitor') |
| 1705 | self._test_final_reparse_task_helper() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1706 | self._test_final_reparse_task_helper(autoserv_success=False) |
| 1707 | |
| 1708 | |
| 1709 | def test_final_reparse_throttling(self): |
| 1710 | self.god.stub_class(monitor_db, 'PidfileRunMonitor') |
| 1711 | self.god.stub_function(monitor_db.FinalReparseTask, |
| 1712 | '_can_run_new_parse') |
| 1713 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1714 | self._setup_pre_parse_expects(True) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1715 | monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return( |
| 1716 | False) |
| 1717 | monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return( |
| 1718 | True) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1719 | self._setup_post_job_run_monitor(monitor_db._PARSER_PID_FILE) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1720 | self._setup_post_parse_expects(True) |
| 1721 | |
| 1722 | task = monitor_db.FinalReparseTask([self.queue_entry]) |
| 1723 | self.run_task(task, True) |
| 1724 | self.god.check_playback() |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 1725 | |
| 1726 | |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1727 | def test_final_reparse_recovery(self): |
| 1728 | self.god.stub_class(monitor_db, 'PidfileRunMonitor') |
| 1729 | self.monitor = self.god.create_mock_class(monitor_db.PidfileRunMonitor, |
| 1730 | 'run_monitor') |
| 1731 | self._setup_post_job_task_expects(True) |
| 1732 | self._expect_execute_run_monitor() |
| 1733 | self._setup_post_parse_expects(True) |
| 1734 | |
| 1735 | task = monitor_db.FinalReparseTask([self.queue_entry], |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1736 | recover_run_monitor=self.monitor) |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1737 | self.run_task(task, True) |
| 1738 | self.god.check_playback() |
| 1739 | |
| 1740 | |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 1741 | def _setup_gather_logs_expects(self, autoserv_killed=True, |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1742 | hqe_aborted=False, has_process=True): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1743 | self.god.stub_class(monitor_db, 'PidfileRunMonitor') |
| 1744 | self.god.stub_class(monitor_db, 'FinalReparseTask') |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 1745 | self._setup_post_job_task_expects(not autoserv_killed, 'Gathering', |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1746 | hqe_aborted, host_status='Running') |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1747 | if hqe_aborted or not has_process: |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 1748 | exit_code = None |
| 1749 | elif autoserv_killed: |
| 1750 | exit_code = 271 |
| 1751 | else: |
| 1752 | exit_code = 0 |
| 1753 | self.pidfile_monitor.exit_code.expect_call().and_return(exit_code) |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1754 | |
| 1755 | if has_process: |
| 1756 | if exit_code != 0: |
| 1757 | self._setup_post_job_run_monitor('.collect_crashinfo_execute') |
| 1758 | self.pidfile_monitor.has_process.expect_call().and_return(True) |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1759 | self.pidfile_monitor.has_process.expect_call().and_return(True) |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1760 | self._expect_copy_results(monitor=self.pidfile_monitor, |
| 1761 | queue_entry=self.queue_entry) |
| 1762 | else: |
| 1763 | # The first has_process() is in GatherLogsTask.run(), and the second |
| 1764 | # is in AgentTask._copy_results() |
| 1765 | self.pidfile_monitor.has_process.expect_call().and_return(False) |
| 1766 | self.pidfile_monitor.has_process.expect_call().and_return(False) |
| 1767 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1768 | self.queue_entry.set_status.expect_call('Parsing') |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1769 | self.pidfile_monitor.has_process.expect_call().and_return(has_process) |
| 1770 | |
| 1771 | if has_process: |
| 1772 | self.pidfile_monitor.num_tests_failed.expect_call().and_return(0) |
showard | b562645 | 2009-06-30 01:57:28 +0000 | [diff] [blame] | 1773 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1774 | |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1775 | def _run_gather_logs_task(self, success=True): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1776 | task = monitor_db.GatherLogsTask(self.job, [self.queue_entry]) |
| 1777 | task.agent = DummyAgent() |
| 1778 | task.agent.dispatcher = self._dispatcher |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1779 | self.run_task(task, success) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1780 | self.god.check_playback() |
| 1781 | |
| 1782 | |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1783 | def test_gather_logs_task(self): |
| 1784 | self._setup_gather_logs_expects() |
| 1785 | # no rebooting for this basic test |
| 1786 | self.job.reboot_after = models.RebootAfter.NEVER |
| 1787 | self.host.set_status.expect_call('Ready') |
| 1788 | |
| 1789 | self._run_gather_logs_task() |
| 1790 | |
| 1791 | |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 1792 | def test_gather_logs_task_successful_autoserv(self): |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 1793 | # When Autoserv exits successfully, no collect_crashinfo stage runs |
| 1794 | self._setup_gather_logs_expects(autoserv_killed=False) |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 1795 | self.job.reboot_after = models.RebootAfter.NEVER |
| 1796 | self.host.set_status.expect_call('Ready') |
| 1797 | |
| 1798 | self._run_gather_logs_task() |
| 1799 | |
| 1800 | |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1801 | def _setup_gather_task_cleanup_expects(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1802 | models.SpecialTask.objects.create.expect_call( |
| 1803 | host=models.Host(id=self.host.id), |
| 1804 | task=models.SpecialTask.Task.CLEANUP) |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1805 | |
| 1806 | |
| 1807 | def test_gather_logs_reboot_hosts(self): |
| 1808 | self._setup_gather_logs_expects() |
| 1809 | self.job.reboot_after = models.RebootAfter.ALWAYS |
| 1810 | self._setup_gather_task_cleanup_expects() |
| 1811 | |
| 1812 | self._run_gather_logs_task() |
| 1813 | |
| 1814 | |
| 1815 | def test_gather_logs_reboot_on_abort(self): |
| 1816 | self._setup_gather_logs_expects(hqe_aborted=True) |
| 1817 | self.job.reboot_after = models.RebootAfter.NEVER |
| 1818 | self._setup_gather_task_cleanup_expects() |
| 1819 | |
| 1820 | self._run_gather_logs_task() |
| 1821 | |
| 1822 | |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1823 | def test_gather_logs_no_process(self): |
| 1824 | self._setup_gather_logs_expects(has_process=False) |
| 1825 | self.job.reboot_after = models.RebootAfter.NEVER |
| 1826 | self.host.set_status.expect_call('Ready') |
| 1827 | |
| 1828 | self._run_gather_logs_task(success=False) |
| 1829 | |
| 1830 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1831 | def _test_cleanup_task_helper(self, success, task_id, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1832 | use_queue_entry=False): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1833 | self._setup_special_task(task_id, models.SpecialTask.Task.CLEANUP, |
| 1834 | use_queue_entry) |
| 1835 | task_tag = '%d-cleanup' % task_id |
| 1836 | |
| 1837 | self.task.activate.expect_call() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame^] | 1838 | self._setup_write_host_keyvals_expects(task_tag) |
| 1839 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1840 | self.host.set_status.expect_call('Cleaning') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1841 | |
| 1842 | if use_queue_entry: |
| 1843 | self.queue_entry.set_status.expect_call('Verifying') |
| 1844 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1845 | if success: |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1846 | self.setup_run_monitor(0, task_tag) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1847 | self.host.update_field.expect_call('dirty', 0) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1848 | if use_queue_entry: |
| 1849 | queue_entry = models.HostQueueEntry(id=self.queue_entry.id) |
| 1850 | models.SpecialTask.objects.create.expect_call( |
| 1851 | host=models.Host(id=self.host.id), |
| 1852 | task=models.SpecialTask.Task.VERIFY, |
| 1853 | queue_entry=queue_entry) |
| 1854 | else: |
| 1855 | self.host.set_status.expect_call('Ready') |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1856 | else: |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 1857 | self._setup_prejob_task_failure(task_tag, use_queue_entry) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1858 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1859 | task = monitor_db.CleanupTask(task=self.task) |
| 1860 | task.host = self.host |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1861 | if use_queue_entry: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1862 | task.queue_entry = self.queue_entry |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1863 | |
| 1864 | self.run_task(task, success) |
| 1865 | |
| 1866 | self.god.check_playback() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1867 | self.assert_(set(task.cmd) >= |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1868 | set([monitor_db._autoserv_path, '-p', '--cleanup', '-m', |
| 1869 | self.HOSTNAME, '-r', |
| 1870 | drone_manager.WORKING_DIRECTORY])) |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1871 | if use_queue_entry: |
| 1872 | self.assertTrue(set(task.cmd) >= self.JOB_AUTOSERV_PARAMS) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1873 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1874 | def test_cleanup_task(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1875 | self._test_cleanup_task_helper(True, 1) |
| 1876 | self._test_cleanup_task_helper(False, 2) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1877 | |
| 1878 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1879 | def test_cleanup_task_with_queue_entry(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1880 | self._test_cleanup_task_helper(False, 1, use_queue_entry=True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1881 | |
| 1882 | |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1883 | def test_recovery_queue_task_aborted_early(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1884 | # abort a recovery QueueTask right after it's created |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1885 | self.god.stub_class_method(monitor_db.QueueTask, '_log_abort') |
| 1886 | self.god.stub_class_method(monitor_db.QueueTask, '_finish_task') |
| 1887 | run_monitor = self.god.create_mock_class(monitor_db.PidfileRunMonitor, |
| 1888 | 'run_monitor') |
| 1889 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1890 | self.queue_entry.get_group_name.expect_call().and_return('') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1891 | self.queue_entry.execution_path.expect_call().and_return('tag') |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1892 | run_monitor.kill.expect_call() |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1893 | monitor_db.QueueTask._log_abort.expect_call() |
| 1894 | monitor_db.QueueTask._finish_task.expect_call() |
| 1895 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1896 | task = monitor_db.QueueTask(self.job, [self.queue_entry], |
| 1897 | recover_run_monitor=run_monitor) |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1898 | task.abort() |
| 1899 | self.assert_(task.aborted) |
| 1900 | self.god.check_playback() |
| 1901 | |
| 1902 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 1903 | class HostTest(BaseSchedulerTest): |
| 1904 | def test_cmp_for_sort(self): |
| 1905 | expected_order = [ |
| 1906 | 'alice', 'Host1', 'host2', 'host3', 'host09', 'HOST010', |
| 1907 | 'host10', 'host11', 'yolkfolk'] |
| 1908 | hostname_idx = list(monitor_db.Host._fields).index('hostname') |
| 1909 | row = [None] * len(monitor_db.Host._fields) |
| 1910 | hosts = [] |
| 1911 | for hostname in expected_order: |
| 1912 | row[hostname_idx] = hostname |
| 1913 | hosts.append(monitor_db.Host(row=row, new_record=True)) |
| 1914 | |
| 1915 | host1 = hosts[expected_order.index('Host1')] |
| 1916 | host010 = hosts[expected_order.index('HOST010')] |
| 1917 | host10 = hosts[expected_order.index('host10')] |
| 1918 | host3 = hosts[expected_order.index('host3')] |
| 1919 | alice = hosts[expected_order.index('alice')] |
| 1920 | self.assertEqual(0, monitor_db.Host.cmp_for_sort(host10, host10)) |
| 1921 | self.assertEqual(1, monitor_db.Host.cmp_for_sort(host10, host010)) |
| 1922 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host010, host10)) |
| 1923 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host1, host10)) |
| 1924 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host1, host010)) |
| 1925 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host3, host10)) |
| 1926 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host3, host010)) |
| 1927 | self.assertEqual(1, monitor_db.Host.cmp_for_sort(host3, host1)) |
| 1928 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host1, host3)) |
| 1929 | self.assertEqual(-1, monitor_db.Host.cmp_for_sort(alice, host3)) |
| 1930 | self.assertEqual(1, monitor_db.Host.cmp_for_sort(host3, alice)) |
| 1931 | self.assertEqual(0, monitor_db.Host.cmp_for_sort(alice, alice)) |
| 1932 | |
| 1933 | hosts.sort(cmp=monitor_db.Host.cmp_for_sort) |
| 1934 | self.assertEqual(expected_order, [h.hostname for h in hosts]) |
| 1935 | |
| 1936 | hosts.reverse() |
| 1937 | hosts.sort(cmp=monitor_db.Host.cmp_for_sort) |
| 1938 | self.assertEqual(expected_order, [h.hostname for h in hosts]) |
| 1939 | |
| 1940 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1941 | class HostQueueEntryTest(BaseSchedulerTest): |
| 1942 | def _create_hqe(self, dependency_labels=(), **create_job_kwargs): |
| 1943 | job = self._create_job(**create_job_kwargs) |
| 1944 | for label in dependency_labels: |
| 1945 | job.dependency_labels.add(label) |
| 1946 | hqes = list(monitor_db.HostQueueEntry.fetch(where='job_id=%d' % job.id)) |
| 1947 | self.assertEqual(1, len(hqes)) |
| 1948 | return hqes[0] |
| 1949 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1950 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1951 | def _check_hqe_labels(self, hqe, expected_labels): |
| 1952 | expected_labels = set(expected_labels) |
| 1953 | label_names = set(label.name for label in hqe.get_labels()) |
| 1954 | self.assertEqual(expected_labels, label_names) |
| 1955 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1956 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1957 | def test_get_labels_empty(self): |
| 1958 | hqe = self._create_hqe(hosts=[1]) |
| 1959 | labels = list(hqe.get_labels()) |
| 1960 | self.assertEqual([], labels) |
| 1961 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1962 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1963 | def test_get_labels_metahost(self): |
| 1964 | hqe = self._create_hqe(metahosts=[2]) |
| 1965 | self._check_hqe_labels(hqe, ['label2']) |
| 1966 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1967 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1968 | def test_get_labels_dependancies(self): |
| 1969 | hqe = self._create_hqe(dependency_labels=(self.label3, self.label4), |
| 1970 | metahosts=[1]) |
| 1971 | self._check_hqe_labels(hqe, ['label1', 'label3', 'label4']) |
| 1972 | |
| 1973 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1974 | class JobTest(BaseSchedulerTest): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1975 | def setUp(self): |
| 1976 | super(JobTest, self).setUp() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1977 | self.god.stub_with( |
| 1978 | drone_manager.DroneManager, 'attach_file_to_execution', |
| 1979 | mock.mock_function('attach_file_to_execution', |
| 1980 | default_return_val='/test/path/tmp/foo')) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1981 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1982 | def _mock_create(**kwargs): |
| 1983 | task = models.SpecialTask(**kwargs) |
| 1984 | task.save() |
| 1985 | self._task = task |
| 1986 | self.god.stub_with(models.SpecialTask.objects, 'create', _mock_create) |
| 1987 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1988 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1989 | def _test_pre_job_tasks_helper(self): |
| 1990 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1991 | Calls HQE._do_schedule_pre_job_tasks() and returns the created special |
| 1992 | task |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1993 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1994 | self._task = None |
| 1995 | queue_entry = monitor_db.HostQueueEntry.fetch('id = 1')[0] |
| 1996 | queue_entry._do_schedule_pre_job_tasks() |
| 1997 | return self._task |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1998 | |
| 1999 | |
showard | e58e3f8 | 2008-11-20 19:04:59 +0000 | [diff] [blame] | 2000 | def _test_run_helper(self, expect_agent=True, expect_starting=False, |
| 2001 | expect_pending=False): |
| 2002 | if expect_starting: |
| 2003 | expected_status = models.HostQueueEntry.Status.STARTING |
| 2004 | elif expect_pending: |
| 2005 | expected_status = models.HostQueueEntry.Status.PENDING |
| 2006 | else: |
| 2007 | expected_status = models.HostQueueEntry.Status.VERIFYING |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2008 | job = monitor_db.Job.fetch('id = 1')[0] |
| 2009 | queue_entry = monitor_db.HostQueueEntry.fetch('id = 1')[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2010 | assert queue_entry.job is job |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2011 | job.run_if_ready(queue_entry) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2012 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2013 | self.god.check_playback() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2014 | |
| 2015 | self._dispatcher._schedule_delay_tasks() |
| 2016 | self._dispatcher._schedule_running_host_queue_entries() |
| 2017 | agent = self._dispatcher._agents[0] |
| 2018 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2019 | actual_status = models.HostQueueEntry.smart_get(1).status |
| 2020 | self.assertEquals(expected_status, actual_status) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2021 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 2022 | if not expect_agent: |
| 2023 | self.assertEquals(agent, None) |
| 2024 | return |
| 2025 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2026 | self.assert_(isinstance(agent, monitor_db.Agent)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2027 | self.assert_(agent.task) |
| 2028 | return agent.task |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2029 | |
| 2030 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2031 | def test_run_if_ready_delays(self): |
| 2032 | # Also tests Job.run_with_ready_delay() on atomic group jobs. |
| 2033 | django_job = self._create_job(hosts=[5, 6], atomic_group=1) |
| 2034 | job = monitor_db.Job(django_job.id) |
| 2035 | self.assertEqual(1, job.synch_count) |
| 2036 | django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id)) |
| 2037 | self.assertEqual(2, len(django_hqes)) |
| 2038 | self.assertEqual(2, django_hqes[0].atomic_group.max_number_of_machines) |
| 2039 | |
| 2040 | def set_hqe_status(django_hqe, status): |
| 2041 | django_hqe.status = status |
| 2042 | django_hqe.save() |
| 2043 | monitor_db.HostQueueEntry(django_hqe.id).host.set_status(status) |
| 2044 | |
| 2045 | # An initial state, our synch_count is 1 |
| 2046 | set_hqe_status(django_hqes[0], models.HostQueueEntry.Status.VERIFYING) |
| 2047 | set_hqe_status(django_hqes[1], models.HostQueueEntry.Status.PENDING) |
| 2048 | |
| 2049 | # So that we don't depend on the config file value during the test. |
| 2050 | self.assert_(scheduler_config.config |
| 2051 | .secs_to_wait_for_atomic_group_hosts is not None) |
| 2052 | self.god.stub_with(scheduler_config.config, |
| 2053 | 'secs_to_wait_for_atomic_group_hosts', 123456) |
| 2054 | |
| 2055 | # Get the pending one as a monitor_db.HostQueueEntry object. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2056 | hqe = monitor_db.HostQueueEntry(django_hqes[1].id) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2057 | self.assert_(not job._delay_ready_task) |
| 2058 | self.assertTrue(job.is_ready()) |
| 2059 | |
| 2060 | # Ready with one pending, one verifying and an atomic group should |
| 2061 | # result in a DelayCallTask to re-check if we're ready a while later. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2062 | job.run_if_ready(hqe) |
| 2063 | self.assertEquals('Waiting', hqe.status) |
| 2064 | self._dispatcher._schedule_delay_tasks() |
| 2065 | self.assertEquals('Pending', hqe.status) |
| 2066 | agent = self._dispatcher._agents[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2067 | self.assert_(job._delay_ready_task) |
| 2068 | self.assert_(isinstance(agent, monitor_db.Agent)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2069 | self.assert_(agent.task) |
| 2070 | delay_task = agent.task |
| 2071 | self.assert_(isinstance(delay_task, monitor_db.DelayedCallTask)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2072 | self.assert_(not delay_task.is_done()) |
| 2073 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2074 | self.god.stub_function(delay_task, 'abort') |
| 2075 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2076 | self.god.stub_function(job, 'run') |
| 2077 | |
| 2078 | # Test that the DelayedCallTask's callback queued up above does the |
| 2079 | # correct thing and returns the Agent returned by job.run(). |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2080 | job.run.expect_call(hqe) |
| 2081 | delay_task._callback() |
| 2082 | self.god.check_playback() |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2083 | |
| 2084 | # Adjust the delay deadline so that enough time has passed. |
| 2085 | job._delay_ready_task.end_time = time.time() - 111111 |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2086 | job.run.expect_call(hqe) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2087 | # ...the delay_expired condition should cause us to call run() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2088 | self._dispatcher._handle_agents() |
| 2089 | self.god.check_playback() |
| 2090 | delay_task.success = False |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2091 | |
| 2092 | # Adjust the delay deadline back so that enough time has not passed. |
| 2093 | job._delay_ready_task.end_time = time.time() + 111111 |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2094 | self._dispatcher._handle_agents() |
| 2095 | self.god.check_playback() |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2096 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2097 | # Now max_number_of_machines HQEs are in pending state. Remaining |
| 2098 | # delay will now be ignored. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2099 | other_hqe = monitor_db.HostQueueEntry(django_hqes[0].id) |
| 2100 | self.god.unstub(job, 'run') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2101 | # ...the over_max_threshold test should cause us to call run() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2102 | delay_task.abort.expect_call() |
| 2103 | other_hqe.on_pending() |
| 2104 | self.assertEquals('Starting', other_hqe.status) |
| 2105 | self.assertEquals('Starting', hqe.status) |
| 2106 | self.god.stub_function(job, 'run') |
| 2107 | self.god.unstub(delay_task, 'abort') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2108 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2109 | hqe.set_status('Pending') |
| 2110 | other_hqe.set_status('Pending') |
showard | 708b352 | 2009-08-20 23:26:15 +0000 | [diff] [blame] | 2111 | # Now we're not over the max for the atomic group. But all assigned |
| 2112 | # hosts are in pending state. over_max_threshold should make us run(). |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2113 | hqe.atomic_group.max_number_of_machines += 1 |
| 2114 | hqe.atomic_group.save() |
| 2115 | job.run.expect_call(hqe) |
| 2116 | hqe.on_pending() |
| 2117 | self.god.check_playback() |
| 2118 | hqe.atomic_group.max_number_of_machines -= 1 |
| 2119 | hqe.atomic_group.save() |
showard | 708b352 | 2009-08-20 23:26:15 +0000 | [diff] [blame] | 2120 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2121 | other_hqe = monitor_db.HostQueueEntry(django_hqes[0].id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2122 | self.assertTrue(hqe.job is other_hqe.job) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2123 | # DBObject classes should reuse instances so these should be the same. |
| 2124 | self.assertEqual(job, other_hqe.job) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2125 | self.assertEqual(other_hqe.job, hqe.job) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2126 | # Be sure our delay was not lost during the other_hqe construction. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2127 | self.assertEqual(job._delay_ready_task, delay_task) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2128 | self.assert_(job._delay_ready_task) |
| 2129 | self.assertFalse(job._delay_ready_task.is_done()) |
| 2130 | self.assertFalse(job._delay_ready_task.aborted) |
| 2131 | |
| 2132 | # We want the real run() to be called below. |
| 2133 | self.god.unstub(job, 'run') |
| 2134 | |
| 2135 | # We pass in the other HQE this time the same way it would happen |
| 2136 | # for real when one host finishes verifying and enters pending. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2137 | job.run_if_ready(other_hqe) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2138 | |
| 2139 | # The delayed task must be aborted by the actual run() call above. |
| 2140 | self.assertTrue(job._delay_ready_task.aborted) |
| 2141 | self.assertFalse(job._delay_ready_task.success) |
| 2142 | self.assertTrue(job._delay_ready_task.is_done()) |
| 2143 | |
| 2144 | # Check that job run() and _finish_run() were called by the above: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2145 | self._dispatcher._schedule_running_host_queue_entries() |
| 2146 | agent = self._dispatcher._agents[0] |
| 2147 | self.assert_(agent.task) |
| 2148 | task = agent.task |
| 2149 | self.assert_(isinstance(task, monitor_db.QueueTask)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2150 | # Requery these hqes in order to verify the status from the DB. |
| 2151 | django_hqes = list(models.HostQueueEntry.objects.filter(job=job.id)) |
| 2152 | for entry in django_hqes: |
| 2153 | self.assertEqual(models.HostQueueEntry.Status.STARTING, |
| 2154 | entry.status) |
| 2155 | |
| 2156 | # We're already running, but more calls to run_with_ready_delay can |
| 2157 | # continue to come in due to straggler hosts enter Pending. Make |
| 2158 | # sure we don't do anything. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2159 | self.god.stub_function(job, 'run') |
| 2160 | job.run_with_ready_delay(hqe) |
| 2161 | self.god.check_playback() |
| 2162 | self.god.unstub(job, 'run') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2163 | |
| 2164 | |
| 2165 | def test__atomic_and_has_started__on_atomic(self): |
| 2166 | self._create_job(hosts=[5, 6], atomic_group=1) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2167 | job = monitor_db.Job.fetch('id = 1')[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2168 | self.assertFalse(job._atomic_and_has_started()) |
showard | af8b4ca | 2009-06-16 18:47:26 +0000 | [diff] [blame] | 2169 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2170 | self._update_hqe("status='Pending'") |
| 2171 | self.assertFalse(job._atomic_and_has_started()) |
| 2172 | self._update_hqe("status='Verifying'") |
| 2173 | self.assertFalse(job._atomic_and_has_started()) |
showard | af8b4ca | 2009-06-16 18:47:26 +0000 | [diff] [blame] | 2174 | self.assertFalse(job._atomic_and_has_started()) |
| 2175 | self._update_hqe("status='Failed'") |
| 2176 | self.assertFalse(job._atomic_and_has_started()) |
| 2177 | self._update_hqe("status='Stopped'") |
| 2178 | self.assertFalse(job._atomic_and_has_started()) |
| 2179 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2180 | self._update_hqe("status='Starting'") |
| 2181 | self.assertTrue(job._atomic_and_has_started()) |
| 2182 | self._update_hqe("status='Completed'") |
| 2183 | self.assertTrue(job._atomic_and_has_started()) |
| 2184 | self._update_hqe("status='Aborted'") |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2185 | |
| 2186 | |
| 2187 | def test__atomic_and_has_started__not_atomic(self): |
| 2188 | self._create_job(hosts=[1, 2]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2189 | job = monitor_db.Job.fetch('id = 1')[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2190 | self.assertFalse(job._atomic_and_has_started()) |
| 2191 | self._update_hqe("status='Starting'") |
| 2192 | self.assertFalse(job._atomic_and_has_started()) |
| 2193 | |
| 2194 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2195 | def _check_special_task(self, task, task_type, queue_entry_id=None): |
| 2196 | self.assertEquals(task.task, task_type) |
| 2197 | self.assertEquals(task.host.id, 1) |
| 2198 | if queue_entry_id: |
| 2199 | self.assertEquals(task.queue_entry.id, queue_entry_id) |
| 2200 | |
| 2201 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2202 | def test_run_asynchronous(self): |
| 2203 | self._create_job(hosts=[1, 2]) |
| 2204 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2205 | task = self._test_pre_job_tasks_helper() |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2206 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2207 | self._check_special_task(task, models.SpecialTask.Task.VERIFY, 1) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2208 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2209 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 2210 | def test_run_asynchronous_skip_verify(self): |
| 2211 | job = self._create_job(hosts=[1, 2]) |
| 2212 | job.run_verify = False |
| 2213 | job.save() |
| 2214 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2215 | task = self._test_pre_job_tasks_helper() |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 2216 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2217 | self.assertEquals(task, None) |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 2218 | |
| 2219 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2220 | def test_run_synchronous_verify(self): |
| 2221 | self._create_job(hosts=[1, 2], synchronous=True) |
| 2222 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2223 | task = self._test_pre_job_tasks_helper() |
| 2224 | |
| 2225 | self._check_special_task(task, models.SpecialTask.Task.VERIFY, 1) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2226 | |
| 2227 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 2228 | def test_run_synchronous_skip_verify(self): |
| 2229 | job = self._create_job(hosts=[1, 2], synchronous=True) |
| 2230 | job.run_verify = False |
| 2231 | job.save() |
| 2232 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2233 | task = self._test_pre_job_tasks_helper() |
| 2234 | |
| 2235 | self.assertEquals(task, None) |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 2236 | |
| 2237 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2238 | def test_run_synchronous_ready(self): |
| 2239 | self._create_job(hosts=[1, 2], synchronous=True) |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 2240 | self._update_hqe("status='Pending', execution_subdir=''") |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2241 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2242 | queue_task = self._test_run_helper(expect_starting=True) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2243 | |
| 2244 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
| 2245 | self.assertEquals(queue_task.job.id, 1) |
| 2246 | hqe_ids = [hqe.id for hqe in queue_task.queue_entries] |
| 2247 | self.assertEquals(hqe_ids, [1, 2]) |
| 2248 | |
| 2249 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2250 | def test_run_atomic_group_already_started(self): |
| 2251 | self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True) |
| 2252 | self._update_hqe("status='Starting', execution_subdir=''") |
| 2253 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2254 | job = monitor_db.Job.fetch('id = 1')[0] |
| 2255 | queue_entry = monitor_db.HostQueueEntry.fetch('id = 1')[0] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2256 | assert queue_entry.job is job |
| 2257 | self.assertEqual(None, job.run(queue_entry)) |
| 2258 | |
| 2259 | self.god.check_playback() |
| 2260 | |
| 2261 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2262 | def test_run_synchronous_atomic_group_ready(self): |
| 2263 | self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True) |
| 2264 | self._update_hqe("status='Pending', execution_subdir=''") |
| 2265 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2266 | queue_task = self._test_run_helper(expect_starting=True) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2267 | |
| 2268 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2269 | # Atomic group jobs that do not depend on a specific label in the |
| 2270 | # atomic group will use the atomic group name as their group name. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2271 | self.assertEquals(queue_task.group_name, 'atomic1') |
| 2272 | |
| 2273 | |
| 2274 | def test_run_synchronous_atomic_group_with_label_ready(self): |
| 2275 | job = self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True) |
| 2276 | job.dependency_labels.add(self.label4) |
| 2277 | self._update_hqe("status='Pending', execution_subdir=''") |
| 2278 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2279 | queue_task = self._test_run_helper(expect_starting=True) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2280 | |
| 2281 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
| 2282 | # Atomic group jobs that also specify a label in the atomic group |
| 2283 | # will use the label name as their group name. |
| 2284 | self.assertEquals(queue_task.group_name, 'label4') |
| 2285 | |
| 2286 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2287 | def test_reboot_before_always(self): |
| 2288 | job = self._create_job(hosts=[1]) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 2289 | job.reboot_before = models.RebootBefore.ALWAYS |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2290 | job.save() |
| 2291 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2292 | task = self._test_pre_job_tasks_helper() |
| 2293 | |
| 2294 | self._check_special_task(task, models.SpecialTask.Task.CLEANUP) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2295 | |
| 2296 | |
| 2297 | def _test_reboot_before_if_dirty_helper(self, expect_reboot): |
| 2298 | job = self._create_job(hosts=[1]) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 2299 | job.reboot_before = models.RebootBefore.IF_DIRTY |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2300 | job.save() |
| 2301 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2302 | task = self._test_pre_job_tasks_helper() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2303 | if expect_reboot: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2304 | task_type = models.SpecialTask.Task.CLEANUP |
| 2305 | else: |
| 2306 | task_type = models.SpecialTask.Task.VERIFY |
| 2307 | self._check_special_task(task, task_type) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2308 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2309 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2310 | def test_reboot_before_if_dirty(self): |
| 2311 | models.Host.smart_get(1).update_object(dirty=True) |
| 2312 | self._test_reboot_before_if_dirty_helper(True) |
| 2313 | |
| 2314 | |
| 2315 | def test_reboot_before_not_dirty(self): |
| 2316 | models.Host.smart_get(1).update_object(dirty=False) |
| 2317 | self._test_reboot_before_if_dirty_helper(False) |
| 2318 | |
| 2319 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2320 | def test_next_group_name(self): |
| 2321 | django_job = self._create_job(metahosts=[1]) |
| 2322 | job = monitor_db.Job(id=django_job.id) |
| 2323 | self.assertEqual('group0', job._next_group_name()) |
| 2324 | |
| 2325 | for hqe in django_job.hostqueueentry_set.filter(): |
| 2326 | hqe.execution_subdir = 'my_rack.group0' |
| 2327 | hqe.save() |
| 2328 | self.assertEqual('my_rack.group1', job._next_group_name('my/rack')) |
| 2329 | |
| 2330 | |
| 2331 | class TopLevelFunctionsTest(unittest.TestCase): |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 2332 | def setUp(self): |
| 2333 | self.god = mock.mock_god() |
| 2334 | |
| 2335 | |
| 2336 | def tearDown(self): |
| 2337 | self.god.unstub_all() |
| 2338 | |
| 2339 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2340 | def test_autoserv_command_line(self): |
| 2341 | machines = 'abcd12,efgh34' |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2342 | extra_args = ['-Z', 'hello'] |
| 2343 | expected_command_line = [monitor_db._autoserv_path, '-p', |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2344 | '-m', machines, '-r', |
| 2345 | drone_manager.WORKING_DIRECTORY] |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2346 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2347 | command_line = monitor_db._autoserv_command_line(machines, extra_args) |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 2348 | self.assertEqual(expected_command_line + ['--verbose'] + extra_args, |
| 2349 | command_line) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2350 | |
| 2351 | class FakeJob(object): |
| 2352 | owner = 'Bob' |
| 2353 | name = 'fake job name' |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 2354 | id = 1337 |
| 2355 | |
| 2356 | class FakeHQE(object): |
| 2357 | job = FakeJob |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2358 | |
| 2359 | command_line = monitor_db._autoserv_command_line( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2360 | machines, extra_args=[], queue_entry=FakeHQE, verbose=False) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2361 | self.assertEqual(expected_command_line + |
| 2362 | ['-u', FakeJob.owner, '-l', FakeJob.name], |
| 2363 | command_line) |
| 2364 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2365 | |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 2366 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2367 | unittest.main() |