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