showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
showard | 12bc8a8 | 2008-10-09 16:49:53 +0000 | [diff] [blame] | 3 | import unittest, time, subprocess, os, StringIO, tempfile, datetime |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 4 | import MySQLdb |
| 5 | import common |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend import setup_django_environment |
| 7 | from autotest_lib.frontend import setup_test_environment |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import global_config, host_protections |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 9 | from autotest_lib.client.common_lib.test_utils import mock |
showard | 442e71e | 2008-10-06 10:05:20 +0000 | [diff] [blame] | 10 | from autotest_lib.database import database_connection, migrate |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 11 | from autotest_lib.frontend import thread_local |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 12 | from autotest_lib.frontend.afe import models |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 13 | from autotest_lib.scheduler import monitor_db |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 14 | |
| 15 | _DEBUG = False |
| 16 | |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 17 | class Dummy(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 18 | 'Dummy object that can have attribute assigned to it' |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 19 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 20 | |
| 21 | class IsRow(mock.argument_comparator): |
| 22 | def __init__(self, row_id): |
| 23 | self.row_id = row_id |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 24 | |
| 25 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 26 | def is_satisfied_by(self, parameter): |
| 27 | return list(parameter)[0] == self.row_id |
| 28 | |
| 29 | |
| 30 | def __str__(self): |
| 31 | return 'row with id %s' % self.row_id |
| 32 | |
| 33 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 34 | class BaseSchedulerTest(unittest.TestCase): |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 35 | _config_section = 'AUTOTEST_WEB' |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 36 | _test_db_initialized = False |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 37 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | def _do_query(self, sql): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 39 | self._database.execute(sql) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 40 | |
| 41 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 42 | @classmethod |
| 43 | def _initialize_test_db(cls): |
| 44 | if cls._test_db_initialized: |
| 45 | return |
| 46 | temp_fd, cls._test_db_file = tempfile.mkstemp(suffix='.monitor_test') |
| 47 | os.close(temp_fd) |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 48 | setup_test_environment.set_test_database(cls._test_db_file) |
| 49 | setup_test_environment.run_syncdb() |
| 50 | cls._test_db_backup = setup_test_environment.backup_test_database() |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 51 | cls._test_db_initialized = True |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 52 | |
| 53 | |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 54 | def _open_test_db(self): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 55 | self._initialize_test_db() |
showard | 364fe86 | 2008-10-17 02:01:16 +0000 | [diff] [blame] | 56 | setup_test_environment.restore_test_database(self._test_db_backup) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 57 | self._database = ( |
| 58 | database_connection.DatabaseConnection.get_test_database( |
| 59 | self._test_db_file)) |
| 60 | self._database.connect() |
| 61 | self._database.debug = _DEBUG |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 62 | |
| 63 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 64 | def _close_test_db(self): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 65 | self._database.disconnect() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 66 | |
| 67 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 68 | def _set_monitor_stubs(self): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 69 | monitor_db._db = self._database |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 70 | |
| 71 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 72 | def _fill_in_test_data(self): |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 73 | user = models.User.objects.create(login='my_user') |
| 74 | acl_group = models.AclGroup.objects.create(name='my_acl') |
| 75 | acl_group.users.add(user) |
| 76 | |
| 77 | hosts = [models.Host.objects.create(hostname=hostname) for hostname in |
| 78 | ('host1', 'host2', 'host3', 'host4')] |
| 79 | acl_group.hosts = hosts |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 80 | models.AclGroup.smart_get('Everyone').hosts = [] |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 81 | |
| 82 | labels = [models.Label.objects.create(name=name) for name in |
| 83 | ('label1', 'label2', 'label3')] |
| 84 | labels[2].only_if_needed = True |
| 85 | labels[2].save() |
| 86 | hosts[0].labels.add(labels[0]) |
| 87 | hosts[1].labels.add(labels[1]) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 88 | |
| 89 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 90 | def _setup_dummy_user(self): |
| 91 | user = models.User.objects.create(login='dummy', access_level=100) |
| 92 | thread_local.set_user(user) |
| 93 | |
| 94 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 95 | def setUp(self): |
| 96 | self.god = mock.mock_god() |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 97 | self._open_test_db() |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 98 | self._fill_in_test_data() |
| 99 | self._set_monitor_stubs() |
| 100 | self._dispatcher = monitor_db.Dispatcher() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 101 | self._setup_dummy_user() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 102 | |
| 103 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 104 | def tearDown(self): |
| 105 | self._close_test_db() |
| 106 | self.god.unstub_all() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 107 | |
| 108 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 109 | def _create_job(self, hosts=[], metahosts=[], priority=0, active=False, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 110 | synchronous=False): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 111 | synch_count = synchronous and 2 or 1 |
showard | 12bc8a8 | 2008-10-09 16:49:53 +0000 | [diff] [blame] | 112 | created_on = datetime.datetime(2008, 1, 1) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 113 | status = models.HostQueueEntry.Status.QUEUED |
| 114 | if active: |
| 115 | status = models.HostQueueEntry.Status.RUNNING |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 116 | job = models.Job.objects.create( |
| 117 | name='test', owner='my_user', priority=priority, |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 118 | synch_count=synch_count, created_on=created_on, |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 119 | reboot_before=models.RebootBefore.NEVER) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 120 | for host_id in hosts: |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 121 | models.HostQueueEntry.objects.create(job=job, priority=priority, |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 122 | host_id=host_id, status=status) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 123 | models.IneligibleHostQueue.objects.create(job=job, host_id=host_id) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 124 | for label_id in metahosts: |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 125 | models.HostQueueEntry.objects.create(job=job, priority=priority, |
| 126 | meta_host_id=label_id, |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 127 | status=status) |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 128 | return job |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 129 | |
| 130 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 131 | def _create_job_simple(self, hosts, use_metahost=False, |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 132 | priority=0, active=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 133 | 'An alternative interface to _create_job' |
| 134 | args = {'hosts' : [], 'metahosts' : []} |
| 135 | if use_metahost: |
| 136 | args['metahosts'] = hosts |
| 137 | else: |
| 138 | args['hosts'] = hosts |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 139 | return self._create_job(priority=priority, active=active, **args) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 140 | |
| 141 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 142 | def _update_hqe(self, set, where=''): |
| 143 | query = 'UPDATE host_queue_entries SET ' + set |
| 144 | if where: |
| 145 | query += ' WHERE ' + where |
| 146 | self._do_query(query) |
| 147 | |
| 148 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 149 | class DispatcherSchedulingTest(BaseSchedulerTest): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 150 | _jobs_scheduled = [] |
| 151 | |
| 152 | def _set_monitor_stubs(self): |
| 153 | super(DispatcherSchedulingTest, self)._set_monitor_stubs() |
| 154 | def run_stub(hqe_self, assigned_host=None): |
| 155 | hqe_self.set_status('Starting') |
| 156 | if hqe_self.meta_host: |
| 157 | host = assigned_host |
| 158 | else: |
| 159 | host = hqe_self.host |
| 160 | self._record_job_scheduled(hqe_self.job.id, host.id) |
| 161 | return Dummy() |
| 162 | monitor_db.HostQueueEntry.run = run_stub |
| 163 | |
| 164 | |
| 165 | def _record_job_scheduled(self, job_id, host_id): |
| 166 | record = (job_id, host_id) |
| 167 | self.assert_(record not in self._jobs_scheduled, |
| 168 | 'Job %d scheduled on host %d twice' % |
| 169 | (job_id, host_id)) |
| 170 | self._jobs_scheduled.append(record) |
| 171 | |
| 172 | |
| 173 | def _assert_job_scheduled_on(self, job_id, host_id): |
| 174 | record = (job_id, host_id) |
| 175 | self.assert_(record in self._jobs_scheduled, |
| 176 | 'Job %d not scheduled on host %d as expected\n' |
| 177 | 'Jobs scheduled: %s' % |
| 178 | (job_id, host_id, self._jobs_scheduled)) |
| 179 | self._jobs_scheduled.remove(record) |
| 180 | |
| 181 | |
| 182 | def _check_for_extra_schedulings(self): |
| 183 | if len(self._jobs_scheduled) != 0: |
| 184 | self.fail('Extra jobs scheduled: ' + |
| 185 | str(self._jobs_scheduled)) |
| 186 | |
| 187 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 188 | def _convert_jobs_to_metahosts(self, *job_ids): |
| 189 | sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')' |
| 190 | self._do_query('UPDATE host_queue_entries SET ' |
| 191 | 'meta_host=host_id, host_id=NULL ' |
| 192 | 'WHERE job_id IN ' + sql_tuple) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 193 | |
| 194 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 195 | def _lock_host(self, host_id): |
| 196 | self._do_query('UPDATE hosts SET locked=1 WHERE id=' + |
| 197 | str(host_id)) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 198 | |
| 199 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 200 | def setUp(self): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 201 | super(DispatcherSchedulingTest, self).setUp() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | self._jobs_scheduled = [] |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 203 | |
| 204 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 205 | def _test_basic_scheduling_helper(self, use_metahosts): |
| 206 | 'Basic nonmetahost scheduling' |
| 207 | self._create_job_simple([1], use_metahosts) |
| 208 | self._create_job_simple([2], use_metahosts) |
| 209 | self._dispatcher._schedule_new_jobs() |
| 210 | self._assert_job_scheduled_on(1, 1) |
| 211 | self._assert_job_scheduled_on(2, 2) |
| 212 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 213 | |
| 214 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 215 | def _test_priorities_helper(self, use_metahosts): |
| 216 | 'Test prioritization ordering' |
| 217 | self._create_job_simple([1], use_metahosts) |
| 218 | self._create_job_simple([2], use_metahosts) |
| 219 | self._create_job_simple([1,2], use_metahosts) |
| 220 | self._create_job_simple([1], use_metahosts, priority=1) |
| 221 | self._dispatcher._schedule_new_jobs() |
| 222 | self._assert_job_scheduled_on(4, 1) # higher priority |
| 223 | self._assert_job_scheduled_on(2, 2) # earlier job over later |
| 224 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 225 | |
| 226 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 227 | def _test_hosts_ready_helper(self, use_metahosts): |
| 228 | """ |
| 229 | Only hosts that are status=Ready, unlocked and not invalid get |
| 230 | scheduled. |
| 231 | """ |
| 232 | self._create_job_simple([1], use_metahosts) |
| 233 | self._do_query('UPDATE hosts SET status="Running" WHERE id=1') |
| 234 | self._dispatcher._schedule_new_jobs() |
| 235 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 236 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 237 | self._do_query('UPDATE hosts SET status="Ready", locked=1 ' |
| 238 | 'WHERE id=1') |
| 239 | self._dispatcher._schedule_new_jobs() |
| 240 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 241 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 242 | self._do_query('UPDATE hosts SET locked=0, invalid=1 ' |
| 243 | 'WHERE id=1') |
| 244 | self._dispatcher._schedule_new_jobs() |
showard | 5df2b19 | 2008-07-03 19:51:57 +0000 | [diff] [blame] | 245 | if not use_metahosts: |
| 246 | self._assert_job_scheduled_on(1, 1) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 247 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 248 | |
| 249 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 250 | def _test_hosts_idle_helper(self, use_metahosts): |
| 251 | 'Only idle hosts get scheduled' |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 252 | self._create_job(hosts=[1], active=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 253 | self._create_job_simple([1], use_metahosts) |
| 254 | self._dispatcher._schedule_new_jobs() |
| 255 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 256 | |
| 257 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 258 | def _test_obey_ACLs_helper(self, use_metahosts): |
| 259 | self._do_query('DELETE FROM acl_groups_hosts WHERE host_id=1') |
| 260 | self._create_job_simple([1], use_metahosts) |
| 261 | self._dispatcher._schedule_new_jobs() |
| 262 | self._check_for_extra_schedulings() |
| 263 | |
| 264 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 265 | def _test_only_if_needed_labels_helper(self, use_metahosts): |
| 266 | # apply only_if_needed label3 to host1 |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 267 | label3 = models.Label.smart_get('label3') |
| 268 | models.Host.smart_get('host1').labels.add(label3) |
| 269 | |
| 270 | job = self._create_job_simple([1], use_metahosts) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 271 | # if the job doesn't depend on label3, there should be no scheduling |
| 272 | self._dispatcher._schedule_new_jobs() |
| 273 | self._check_for_extra_schedulings() |
| 274 | |
| 275 | # now make the job depend on label3 |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 276 | job.dependency_labels.add(label3) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 277 | self._dispatcher._schedule_new_jobs() |
| 278 | self._assert_job_scheduled_on(1, 1) |
| 279 | self._check_for_extra_schedulings() |
| 280 | |
| 281 | if use_metahosts: |
| 282 | # should also work if the metahost is the only_if_needed label |
| 283 | self._do_query('DELETE FROM jobs_dependency_labels') |
| 284 | self._create_job(metahosts=[3]) |
| 285 | self._dispatcher._schedule_new_jobs() |
| 286 | self._assert_job_scheduled_on(2, 1) |
| 287 | self._check_for_extra_schedulings() |
| 288 | |
| 289 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 290 | def test_basic_scheduling(self): |
| 291 | self._test_basic_scheduling_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 292 | |
| 293 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 294 | def test_priorities(self): |
| 295 | self._test_priorities_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 296 | |
| 297 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 298 | def test_hosts_ready(self): |
| 299 | self._test_hosts_ready_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 300 | |
| 301 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 302 | def test_hosts_idle(self): |
| 303 | self._test_hosts_idle_helper(False) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 304 | |
| 305 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 306 | def test_obey_ACLs(self): |
| 307 | self._test_obey_ACLs_helper(False) |
| 308 | |
| 309 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 310 | def test_only_if_needed_labels(self): |
| 311 | self._test_only_if_needed_labels_helper(False) |
| 312 | |
| 313 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 314 | def test_non_metahost_on_invalid_host(self): |
| 315 | """ |
| 316 | Non-metahost entries can get scheduled on invalid hosts (this is how |
| 317 | one-time hosts work). |
| 318 | """ |
| 319 | self._do_query('UPDATE hosts SET invalid=1') |
| 320 | self._test_basic_scheduling_helper(False) |
| 321 | |
| 322 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 323 | def test_metahost_scheduling(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 324 | """ |
| 325 | Basic metahost scheduling |
| 326 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 327 | self._test_basic_scheduling_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 328 | |
| 329 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 330 | def test_metahost_priorities(self): |
| 331 | self._test_priorities_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 332 | |
| 333 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 334 | def test_metahost_hosts_ready(self): |
| 335 | self._test_hosts_ready_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 336 | |
| 337 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 338 | def test_metahost_hosts_idle(self): |
| 339 | self._test_hosts_idle_helper(True) |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 340 | |
| 341 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 342 | def test_metahost_obey_ACLs(self): |
| 343 | self._test_obey_ACLs_helper(True) |
| 344 | |
| 345 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 346 | def test_metahost_only_if_needed_labels(self): |
| 347 | self._test_only_if_needed_labels_helper(True) |
| 348 | |
| 349 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 350 | def test_nonmetahost_over_metahost(self): |
| 351 | """ |
| 352 | Non-metahost entries should take priority over metahost entries |
| 353 | for the same host |
| 354 | """ |
| 355 | self._create_job(metahosts=[1]) |
| 356 | self._create_job(hosts=[1]) |
| 357 | self._dispatcher._schedule_new_jobs() |
| 358 | self._assert_job_scheduled_on(2, 1) |
| 359 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 360 | |
| 361 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 362 | def test_metahosts_obey_blocks(self): |
| 363 | """ |
| 364 | Metahosts can't get scheduled on hosts already scheduled for |
| 365 | that job. |
| 366 | """ |
| 367 | self._create_job(metahosts=[1], hosts=[1]) |
| 368 | # make the nonmetahost entry complete, so the metahost can try |
| 369 | # to get scheduled |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 370 | self._update_hqe(set='complete = 1', where='host_id=1') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 371 | self._dispatcher._schedule_new_jobs() |
| 372 | self._check_for_extra_schedulings() |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 373 | |
| 374 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 375 | def test_only_schedule_queued_entries(self): |
| 376 | self._create_job(metahosts=[1]) |
| 377 | self._update_hqe(set='active=1, host_id=2') |
| 378 | self._dispatcher._schedule_new_jobs() |
| 379 | self._check_for_extra_schedulings() |
| 380 | |
| 381 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 382 | def test_no_ready_hosts(self): |
| 383 | self._create_job(hosts=[1]) |
| 384 | self._do_query('UPDATE hosts SET status="Repair Failed"') |
| 385 | self._dispatcher._schedule_new_jobs() |
| 386 | self._check_for_extra_schedulings() |
| 387 | |
| 388 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 389 | class DispatcherThrottlingTest(BaseSchedulerTest): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 390 | """ |
| 391 | Test that the dispatcher throttles: |
| 392 | * total number of running processes |
| 393 | * number of processes started per cycle |
| 394 | """ |
| 395 | _MAX_RUNNING = 3 |
| 396 | _MAX_STARTED = 2 |
| 397 | |
| 398 | def setUp(self): |
| 399 | super(DispatcherThrottlingTest, self).setUp() |
| 400 | self._dispatcher.max_running_processes = self._MAX_RUNNING |
| 401 | self._dispatcher.max_processes_started_per_cycle = self._MAX_STARTED |
| 402 | |
| 403 | |
| 404 | class DummyAgent(object): |
| 405 | _is_running = False |
| 406 | _is_done = False |
| 407 | num_processes = 1 |
| 408 | |
| 409 | def is_running(self): |
| 410 | return self._is_running |
| 411 | |
| 412 | |
| 413 | def tick(self): |
| 414 | self._is_running = True |
| 415 | |
| 416 | |
| 417 | def is_done(self): |
| 418 | return self._is_done |
| 419 | |
| 420 | |
| 421 | def set_done(self, done): |
| 422 | self._is_done = done |
| 423 | self._is_running = not done |
| 424 | |
| 425 | |
| 426 | def _setup_some_agents(self, num_agents): |
| 427 | self._agents = [self.DummyAgent() for i in xrange(num_agents)] |
| 428 | self._dispatcher._agents = list(self._agents) |
| 429 | |
| 430 | |
| 431 | def _run_a_few_cycles(self): |
| 432 | for i in xrange(4): |
| 433 | self._dispatcher._handle_agents() |
| 434 | |
| 435 | |
| 436 | def _assert_agents_started(self, indexes, is_started=True): |
| 437 | for i in indexes: |
| 438 | self.assert_(self._agents[i].is_running() == is_started, |
| 439 | 'Agent %d %sstarted' % |
| 440 | (i, is_started and 'not ' or '')) |
| 441 | |
| 442 | |
| 443 | def _assert_agents_not_started(self, indexes): |
| 444 | self._assert_agents_started(indexes, False) |
| 445 | |
| 446 | |
| 447 | def test_throttle_total(self): |
| 448 | self._setup_some_agents(4) |
| 449 | self._run_a_few_cycles() |
| 450 | self._assert_agents_started([0, 1, 2]) |
| 451 | self._assert_agents_not_started([3]) |
| 452 | |
| 453 | |
| 454 | def test_throttle_per_cycle(self): |
| 455 | self._setup_some_agents(3) |
| 456 | self._dispatcher._handle_agents() |
| 457 | self._assert_agents_started([0, 1]) |
| 458 | self._assert_agents_not_started([2]) |
| 459 | |
| 460 | |
| 461 | def test_throttle_with_synchronous(self): |
| 462 | self._setup_some_agents(2) |
| 463 | self._agents[0].num_processes = 3 |
| 464 | self._run_a_few_cycles() |
| 465 | self._assert_agents_started([0]) |
| 466 | self._assert_agents_not_started([1]) |
| 467 | |
| 468 | |
| 469 | def test_large_agent_starvation(self): |
| 470 | """ |
| 471 | Ensure large agents don't get starved by lower-priority agents. |
| 472 | """ |
| 473 | self._setup_some_agents(3) |
| 474 | self._agents[1].num_processes = 3 |
| 475 | self._run_a_few_cycles() |
| 476 | self._assert_agents_started([0]) |
| 477 | self._assert_agents_not_started([1, 2]) |
| 478 | |
| 479 | self._agents[0].set_done(True) |
| 480 | self._run_a_few_cycles() |
| 481 | self._assert_agents_started([1]) |
| 482 | self._assert_agents_not_started([2]) |
| 483 | |
| 484 | |
| 485 | def test_zero_process_agent(self): |
| 486 | self._setup_some_agents(5) |
| 487 | self._agents[4].num_processes = 0 |
| 488 | self._run_a_few_cycles() |
| 489 | self._assert_agents_started([0, 1, 2, 4]) |
| 490 | self._assert_agents_not_started([3]) |
| 491 | |
| 492 | |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 493 | class FindAbortTest(BaseSchedulerTest): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 494 | """ |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 495 | Test the dispatcher abort functionality. |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 496 | """ |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 497 | def _check_agent(self, agent, entry_and_host_id, include_host_tasks): |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 498 | self.assert_(isinstance(agent, monitor_db.Agent)) |
| 499 | tasks = list(agent.queue.queue) |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 500 | self.assert_(len(tasks) > 0) |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 501 | |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 502 | abort = tasks[0] |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 503 | self.assert_(isinstance(abort, monitor_db.AbortTask)) |
| 504 | self.assertEquals(abort.queue_entry.id, entry_and_host_id) |
| 505 | |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 506 | if not include_host_tasks: |
| 507 | self.assertEquals(len(tasks), 1) |
| 508 | return |
| 509 | |
| 510 | self.assertEquals(len(tasks), 3) |
| 511 | cleanup, verify = tasks[1:] |
| 512 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 513 | self.assert_(isinstance(cleanup, monitor_db.CleanupTask)) |
| 514 | self.assertEquals(cleanup.host.id, entry_and_host_id) |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 515 | |
| 516 | self.assert_(isinstance(verify, monitor_db.VerifyTask)) |
| 517 | self.assertEquals(verify.host.id, entry_and_host_id) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 518 | |
| 519 | |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 520 | def _check_agents(self, agents, include_host_tasks): |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 521 | self.assertEquals(len(agents), 2) |
| 522 | for index, agent in enumerate(agents): |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 523 | self._check_agent(agent, index + 1, include_host_tasks) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 524 | |
| 525 | |
| 526 | def test_find_aborting_inactive(self): |
| 527 | self._create_job(hosts=[1, 2]) |
| 528 | self._update_hqe(set='status="Abort"') |
| 529 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 530 | self._dispatcher._find_aborting() |
| 531 | |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 532 | self._check_agents(self._dispatcher._agents, include_host_tasks=False) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 533 | self.god.check_playback() |
| 534 | |
| 535 | |
| 536 | def test_find_aborting_active(self): |
| 537 | self._create_job(hosts=[1, 2]) |
| 538 | self._update_hqe(set='status="Abort", active=1') |
| 539 | # have to make an Agent for the active HQEs |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 540 | agent = self.god.create_mock_class(monitor_db.Agent, 'old_agent') |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 541 | agent.queue_entry_ids = [1, 2] |
| 542 | self._dispatcher.add_agent(agent) |
| 543 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 544 | self._dispatcher._find_aborting() |
| 545 | |
showard | 9d9ffd5 | 2008-11-09 23:14:35 +0000 | [diff] [blame] | 546 | self._check_agents(self._dispatcher._agents, include_host_tasks=True) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 547 | self.god.check_playback() |
| 548 | |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 549 | # ensure agent gets aborted |
| 550 | abort1 = self._dispatcher._agents[0].queue.queue[0] |
| 551 | self.assertEquals(abort1.agents_to_abort, [agent]) |
| 552 | abort2 = self._dispatcher._agents[1].queue.queue[0] |
| 553 | self.assertEquals(abort2.agents_to_abort, []) |
| 554 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 555 | |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 556 | class JobTimeoutTest(BaseSchedulerTest): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 557 | def _test_synch_start_timeout_helper(self, expect_abort, |
| 558 | set_created_on=True, set_active=True, |
| 559 | set_acl=True): |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 560 | self._dispatcher.synch_job_start_timeout_minutes = 60 |
| 561 | job = self._create_job(hosts=[1, 2]) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 562 | if set_active: |
| 563 | hqe = job.hostqueueentry_set.filter(host__id=1)[0] |
| 564 | hqe.status = 'Pending' |
| 565 | hqe.active = 1 |
| 566 | hqe.save() |
| 567 | |
| 568 | everyone_acl = models.AclGroup.smart_get('Everyone') |
| 569 | host1 = models.Host.smart_get(1) |
| 570 | if set_acl: |
| 571 | everyone_acl.hosts.add(host1) |
| 572 | else: |
| 573 | everyone_acl.hosts.remove(host1) |
| 574 | |
| 575 | job.created_on = datetime.datetime.now() |
| 576 | if set_created_on: |
| 577 | job.created_on -= datetime.timedelta(minutes=100) |
| 578 | job.save() |
| 579 | |
| 580 | self._dispatcher._abort_jobs_past_synch_start_timeout() |
| 581 | |
| 582 | for hqe in job.hostqueueentry_set.all(): |
| 583 | if expect_abort: |
| 584 | self.assert_(hqe.status in ('Abort', 'Aborted'), hqe.status) |
| 585 | else: |
| 586 | self.assert_(hqe.status not in ('Abort', 'Aborted'), hqe.status) |
| 587 | |
| 588 | |
| 589 | def test_synch_start_timeout_helper(self): |
| 590 | # no abort if any of the condition aren't met |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 591 | self._test_synch_start_timeout_helper(False, set_created_on=False) |
| 592 | self._test_synch_start_timeout_helper(False, set_active=False) |
| 593 | self._test_synch_start_timeout_helper(False, set_acl=False) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 594 | # abort if all conditions are met |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 595 | self._test_synch_start_timeout_helper(True) |
showard | 9886397 | 2008-10-29 21:14:56 +0000 | [diff] [blame] | 596 | |
| 597 | |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 598 | class PidfileRunMonitorTest(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 599 | results_dir = '/test/path' |
| 600 | pidfile_path = os.path.join(results_dir, monitor_db.AUTOSERV_PID_FILE) |
| 601 | pid = 12345 |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 602 | num_tests_failed = 1 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 603 | args = ('nice -n 10 autoserv -P 123-myuser/myhost -p -n ' |
| 604 | '-r ' + results_dir + ' -b -u myuser -l my-job-name ' |
| 605 | '-m myhost /tmp/filejx43Zi -c') |
| 606 | bad_args = args.replace(results_dir, '/random/results/dir') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 607 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 608 | def setUp(self): |
| 609 | self.god = mock.mock_god() |
| 610 | self.god.stub_function(monitor_db, 'open') |
| 611 | self.god.stub_function(os.path, 'exists') |
| 612 | self.god.stub_function(monitor_db.email_manager, |
| 613 | 'enqueue_notify_email') |
| 614 | self.monitor = monitor_db.PidfileRunMonitor(self.results_dir) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 615 | |
| 616 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 617 | def tearDown(self): |
| 618 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 619 | |
| 620 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 621 | def set_not_yet_run(self): |
| 622 | os.path.exists.expect_call(self.pidfile_path).and_return(False) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 623 | |
| 624 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 625 | def setup_pidfile(self, pidfile_contents): |
| 626 | os.path.exists.expect_call(self.pidfile_path).and_return(True) |
| 627 | pidfile = StringIO.StringIO(pidfile_contents) |
| 628 | monitor_db.open.expect_call( |
| 629 | self.pidfile_path, 'r').and_return(pidfile) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 630 | |
| 631 | |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 632 | def set_empty_pidfile(self): |
| 633 | self.setup_pidfile('') |
| 634 | |
| 635 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 636 | def set_running(self): |
| 637 | self.setup_pidfile(str(self.pid) + '\n') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 638 | |
| 639 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 640 | def set_complete(self, error_code): |
| 641 | self.setup_pidfile(str(self.pid) + '\n' + |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 642 | str(error_code) + '\n' + |
| 643 | str(self.num_tests_failed)) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 644 | |
| 645 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 646 | def _test_read_pidfile_helper(self, expected_pid, expected_exit_status, |
| 647 | expected_num_tests_failed): |
| 648 | self.monitor._read_pidfile() |
| 649 | self.assertEquals(self.monitor._state.pid, expected_pid) |
| 650 | self.assertEquals(self.monitor._state.exit_status, expected_exit_status) |
| 651 | self.assertEquals(self.monitor._state.num_tests_failed, |
| 652 | expected_num_tests_failed) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 653 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 654 | |
| 655 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 656 | def _get_expected_tests_failed(self, expected_exit_status): |
| 657 | if expected_exit_status is None: |
| 658 | expected_tests_failed = None |
| 659 | else: |
| 660 | expected_tests_failed = self.num_tests_failed |
| 661 | return expected_tests_failed |
| 662 | |
| 663 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 664 | def test_read_pidfile(self): |
| 665 | self.set_not_yet_run() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 666 | self._test_read_pidfile_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 667 | |
showard | 3dd6b88 | 2008-10-27 19:21:39 +0000 | [diff] [blame] | 668 | self.set_empty_pidfile() |
| 669 | self._test_read_pidfile_helper(None, None, None) |
| 670 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 671 | self.set_running() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 672 | self._test_read_pidfile_helper(self.pid, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 673 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 674 | self.set_complete(123) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 675 | self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 676 | |
| 677 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 678 | def test_read_pidfile_error(self): |
| 679 | self.setup_pidfile('asdf') |
| 680 | self.assertRaises(monitor_db.PidfileException, |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 681 | self.monitor._read_pidfile) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 682 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 683 | |
| 684 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 685 | def setup_proc_cmdline(self, args): |
| 686 | proc_cmdline = args.replace(' ', '\x00') |
| 687 | proc_file = StringIO.StringIO(proc_cmdline) |
| 688 | monitor_db.open.expect_call( |
| 689 | '/proc/%d/cmdline' % self.pid, 'r').and_return(proc_file) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 690 | |
| 691 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 692 | def setup_find_autoservs(self, process_dict): |
| 693 | self.god.stub_class_method(monitor_db.Dispatcher, |
| 694 | 'find_autoservs') |
| 695 | monitor_db.Dispatcher.find_autoservs.expect_call().and_return( |
| 696 | process_dict) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 697 | |
| 698 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 699 | def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status, |
| 700 | expected_num_tests_failed): |
| 701 | self.monitor._get_pidfile_info() |
| 702 | self.assertEquals(self.monitor._state.pid, expected_pid) |
| 703 | self.assertEquals(self.monitor._state.exit_status, expected_exit_status) |
| 704 | self.assertEquals(self.monitor._state.num_tests_failed, |
| 705 | expected_num_tests_failed) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 706 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 707 | |
| 708 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 709 | def test_get_pidfile_info(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 710 | """ |
| 711 | normal cases for get_pidfile_info |
| 712 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 713 | # running |
| 714 | self.set_running() |
| 715 | self.setup_proc_cmdline(self.args) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 716 | self._test_get_pidfile_info_helper(self.pid, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 717 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 718 | # exited during check |
| 719 | self.set_running() |
| 720 | monitor_db.open.expect_call( |
| 721 | '/proc/%d/cmdline' % self.pid, 'r').and_raises(IOError) |
| 722 | self.set_complete(123) # pidfile gets read again |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 723 | self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 724 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 725 | # completed |
| 726 | self.set_complete(123) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 727 | self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 728 | |
| 729 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 730 | def test_get_pidfile_info_running_no_proc(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 731 | """ |
| 732 | pidfile shows process running, but no proc exists |
| 733 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 734 | # running but no proc |
| 735 | self.set_running() |
| 736 | monitor_db.open.expect_call( |
| 737 | '/proc/%d/cmdline' % self.pid, 'r').and_raises(IOError) |
| 738 | self.set_running() |
| 739 | monitor_db.email_manager.enqueue_notify_email.expect_call( |
| 740 | mock.is_string_comparator(), mock.is_string_comparator()) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 741 | self._test_get_pidfile_info_helper(self.pid, 1, 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 742 | self.assertTrue(self.monitor.lost_process) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 743 | |
| 744 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 745 | def test_get_pidfile_info_not_yet_run(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 746 | """ |
| 747 | pidfile hasn't been written yet |
| 748 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 749 | # process not running |
| 750 | self.set_not_yet_run() |
| 751 | self.setup_find_autoservs({}) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 752 | self._test_get_pidfile_info_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 753 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 754 | # process running |
| 755 | self.set_not_yet_run() |
| 756 | self.setup_find_autoservs({self.pid : self.args}) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 757 | self._test_get_pidfile_info_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 758 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 759 | # another process running under same pid |
| 760 | self.set_not_yet_run() |
| 761 | self.setup_find_autoservs({self.pid : self.bad_args}) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 762 | self._test_get_pidfile_info_helper(None, None, None) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 763 | |
| 764 | |
| 765 | class AgentTest(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 766 | def setUp(self): |
| 767 | self.god = mock.mock_god() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 768 | |
| 769 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 770 | def tearDown(self): |
| 771 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 772 | |
| 773 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 774 | def test_agent(self): |
| 775 | task1 = self.god.create_mock_class(monitor_db.AgentTask, |
| 776 | 'task1') |
| 777 | task2 = self.god.create_mock_class(monitor_db.AgentTask, |
| 778 | 'task2') |
| 779 | task3 = self.god.create_mock_class(monitor_db.AgentTask, |
| 780 | 'task3') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 781 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 782 | task1.start.expect_call() |
| 783 | task1.is_done.expect_call().and_return(False) |
| 784 | task1.poll.expect_call() |
| 785 | task1.is_done.expect_call().and_return(True) |
| 786 | task1.is_done.expect_call().and_return(True) |
| 787 | task1.success = True |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 788 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 789 | task2.start.expect_call() |
| 790 | task2.is_done.expect_call().and_return(True) |
| 791 | task2.is_done.expect_call().and_return(True) |
| 792 | task2.success = False |
| 793 | task2.failure_tasks = [task3] |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 794 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 795 | task3.start.expect_call() |
| 796 | task3.is_done.expect_call().and_return(True) |
| 797 | task3.is_done.expect_call().and_return(True) |
| 798 | task3.success = True |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 799 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 800 | agent = monitor_db.Agent([task1, task2]) |
| 801 | agent.dispatcher = object() |
| 802 | agent.start() |
| 803 | while not agent.is_done(): |
| 804 | agent.tick() |
| 805 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 806 | |
| 807 | |
| 808 | class AgentTasksTest(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 809 | TEMP_DIR = '/temp/dir' |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 810 | RESULTS_DIR = '/results/dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 811 | HOSTNAME = 'myhost' |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 812 | HOST_PROTECTION = host_protections.default |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 813 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 814 | def setUp(self): |
| 815 | self.god = mock.mock_god() |
| 816 | self.god.stub_with(tempfile, 'mkdtemp', |
| 817 | mock.mock_function('mkdtemp', self.TEMP_DIR)) |
| 818 | self.god.stub_class_method(monitor_db.RunMonitor, 'run') |
| 819 | self.god.stub_class_method(monitor_db.RunMonitor, 'exit_code') |
| 820 | self.host = self.god.create_mock_class(monitor_db.Host, 'host') |
| 821 | self.host.hostname = self.HOSTNAME |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 822 | self.host.protection = self.HOST_PROTECTION |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 823 | self.queue_entry = self.god.create_mock_class( |
| 824 | monitor_db.HostQueueEntry, 'queue_entry') |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 825 | self.job = self.god.create_mock_class(monitor_db.Job, 'job') |
| 826 | self.queue_entry.job = self.job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 827 | self.queue_entry.host = self.host |
| 828 | self.queue_entry.meta_host = None |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 829 | |
| 830 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 831 | def tearDown(self): |
| 832 | self.god.unstub_all() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 833 | |
| 834 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 835 | def run_task(self, task, success): |
| 836 | """ |
| 837 | Do essentially what an Agent would do, but protect againt |
| 838 | infinite looping from test errors. |
| 839 | """ |
| 840 | if not getattr(task, 'agent', None): |
| 841 | task.agent = object() |
| 842 | task.start() |
| 843 | count = 0 |
| 844 | while not task.is_done(): |
| 845 | count += 1 |
| 846 | if count > 10: |
| 847 | print 'Task failed to finish' |
| 848 | # in case the playback has clues to why it |
| 849 | # failed |
| 850 | self.god.check_playback() |
| 851 | self.fail() |
| 852 | task.poll() |
| 853 | self.assertEquals(task.success, success) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 854 | |
| 855 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 856 | def setup_run_monitor(self, exit_status): |
| 857 | monitor_db.RunMonitor.run.expect_call() |
| 858 | monitor_db.RunMonitor.exit_code.expect_call() |
| 859 | monitor_db.RunMonitor.exit_code.expect_call().and_return( |
| 860 | exit_status) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 861 | |
| 862 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 863 | def _test_repair_task_helper(self, success): |
| 864 | self.host.set_status.expect_call('Repairing') |
| 865 | if success: |
| 866 | self.setup_run_monitor(0) |
| 867 | self.host.set_status.expect_call('Ready') |
| 868 | else: |
| 869 | self.setup_run_monitor(1) |
| 870 | self.host.set_status.expect_call('Repair Failed') |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 871 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 872 | task = monitor_db.RepairTask(self.host) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 873 | self.assertEquals(task.failure_tasks, []) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 874 | self.run_task(task, success) |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 875 | |
| 876 | expected_protection = host_protections.Protection.get_string( |
| 877 | host_protections.default) |
mbligh | 3e0f7e0 | 2008-07-28 19:42:01 +0000 | [diff] [blame] | 878 | expected_protection = host_protections.Protection.get_attr_name( |
| 879 | expected_protection) |
| 880 | |
mbligh | c160352 | 2008-07-17 21:32:21 +0000 | [diff] [blame] | 881 | self.assertTrue(set(task.monitor.cmd) >= |
mbligh | f40cf53 | 2008-06-23 23:53:23 +0000 | [diff] [blame] | 882 | set(['autoserv', '-R', '-m', self.HOSTNAME, '-r', |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 883 | self.TEMP_DIR, '--host-protection', |
| 884 | expected_protection])) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 885 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 886 | |
| 887 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 888 | def test_repair_task(self): |
| 889 | self._test_repair_task_helper(True) |
| 890 | self._test_repair_task_helper(False) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 891 | |
| 892 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 893 | def test_repair_task_with_queue_entry(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 894 | self.host.set_status.expect_call('Repairing') |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 895 | self.queue_entry.requeue.expect_call() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 896 | self.setup_run_monitor(1) |
| 897 | self.host.set_status.expect_call('Repair Failed') |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 898 | self.queue_entry.handle_host_failure.expect_call() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 899 | |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 900 | task = monitor_db.RepairTask(self.host, self.queue_entry) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 901 | self.run_task(task, False) |
| 902 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 903 | |
| 904 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 905 | def setup_verify_expects(self, success, use_queue_entry): |
| 906 | if use_queue_entry: |
| 907 | self.queue_entry.set_status.expect_call('Verifying') |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 908 | self.queue_entry.clear_results_dir.expect_call() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 909 | self.host.set_status.expect_call('Verifying') |
| 910 | if success: |
| 911 | self.setup_run_monitor(0) |
| 912 | self.host.set_status.expect_call('Ready') |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 913 | if use_queue_entry: |
| 914 | self.queue_entry.on_pending.expect_call() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 915 | else: |
| 916 | self.setup_run_monitor(1) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 917 | |
| 918 | |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 919 | def _check_verify_failure_tasks(self, verify_task): |
| 920 | self.assertEquals(len(verify_task.failure_tasks), 1) |
| 921 | repair_task = verify_task.failure_tasks[0] |
| 922 | self.assert_(isinstance(repair_task, monitor_db.RepairTask)) |
| 923 | self.assertEquals(verify_task.host, repair_task.host) |
| 924 | if verify_task.queue_entry and not verify_task.queue_entry.meta_host: |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 925 | self.assertEquals(repair_task.queue_entry, |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 926 | verify_task.queue_entry) |
| 927 | else: |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 928 | self.assertEquals(repair_task.queue_entry, None) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 929 | |
| 930 | |
| 931 | def _test_verify_task_helper(self, success, use_queue_entry=False, |
| 932 | use_meta_host=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 933 | self.setup_verify_expects(success, use_queue_entry) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 934 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 935 | if use_queue_entry: |
| 936 | task = monitor_db.VerifyTask( |
| 937 | queue_entry=self.queue_entry) |
| 938 | else: |
| 939 | task = monitor_db.VerifyTask(host=self.host) |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 940 | self._check_verify_failure_tasks(task) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 941 | self.run_task(task, success) |
mbligh | c160352 | 2008-07-17 21:32:21 +0000 | [diff] [blame] | 942 | self.assertTrue(set(task.monitor.cmd) >= |
mbligh | f40cf53 | 2008-06-23 23:53:23 +0000 | [diff] [blame] | 943 | set(['autoserv', '-v', '-m', self.HOSTNAME, '-r', |
| 944 | self.TEMP_DIR])) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 945 | self.god.check_playback() |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 946 | |
| 947 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 948 | def test_verify_task_with_host(self): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 949 | self._test_verify_task_helper(True) |
| 950 | self._test_verify_task_helper(False) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 951 | |
| 952 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 953 | def test_verify_task_with_queue_entry(self): |
showard | 56193bb | 2008-08-13 20:07:41 +0000 | [diff] [blame] | 954 | self._test_verify_task_helper(True, use_queue_entry=True) |
| 955 | self._test_verify_task_helper(False, use_queue_entry=True) |
| 956 | |
| 957 | |
| 958 | def test_verify_task_with_metahost(self): |
| 959 | self._test_verify_task_helper(True, use_queue_entry=True, |
| 960 | use_meta_host=True) |
| 961 | self._test_verify_task_helper(False, use_queue_entry=True, |
| 962 | use_meta_host=True) |
jadmanski | 3d161b0 | 2008-06-06 15:43:36 +0000 | [diff] [blame] | 963 | |
| 964 | |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 965 | def test_abort_task(self): |
| 966 | queue_entry = self.god.create_mock_class(monitor_db.HostQueueEntry, |
| 967 | 'queue_entry') |
| 968 | queue_entry.host_id, queue_entry.job_id = 1, 2 |
| 969 | task = self.god.create_mock_class(monitor_db.AgentTask, 'task') |
| 970 | agent = self.god.create_mock_class(monitor_db.Agent, 'agent') |
| 971 | agent.active_task = task |
| 972 | |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 973 | task.abort.expect_call() |
| 974 | queue_entry.set_status.expect_call('Aborted') |
| 975 | |
| 976 | abort_task = monitor_db.AbortTask(queue_entry, [agent]) |
| 977 | self.run_task(abort_task, True) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 978 | self.god.check_playback() |
| 979 | |
| 980 | |
| 981 | def _setup_pre_parse_expects(self, is_synch, num_machines): |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 982 | self.queue_entry.results_dir.expect_call().and_return(self.RESULTS_DIR) |
| 983 | self.queue_entry.set_status.expect_call('Parsing') |
| 984 | |
| 985 | |
| 986 | def _setup_post_parse_expects(self, autoserv_success): |
| 987 | pidfile_monitor = monitor_db.PidfileRunMonitor.expect_new( |
| 988 | self.RESULTS_DIR) |
| 989 | if autoserv_success: |
| 990 | code, status = 0, 'Completed' |
| 991 | else: |
| 992 | code, status = 1, 'Failed' |
| 993 | pidfile_monitor.exit_code.expect_call().and_return(code) |
| 994 | self.queue_entry.set_status.expect_call(status) |
| 995 | |
| 996 | |
| 997 | def _test_final_reparse_task_helper(self, is_synch=False, num_machines=1, |
| 998 | autoserv_success=True): |
| 999 | tko_dir = '/tko/dir' |
| 1000 | monitor_db.AUTOTEST_TKO_DIR = tko_dir |
| 1001 | parse_path = os.path.join(tko_dir, 'parse') |
| 1002 | |
| 1003 | self._setup_pre_parse_expects(is_synch, num_machines) |
| 1004 | self.setup_run_monitor(0) |
| 1005 | self._setup_post_parse_expects(autoserv_success) |
| 1006 | |
| 1007 | task = monitor_db.FinalReparseTask([self.queue_entry]) |
| 1008 | self.run_task(task, True) |
| 1009 | |
| 1010 | self.god.check_playback() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1011 | cmd = [parse_path, '-l', '2', '-r', '-o', self.RESULTS_DIR] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 1012 | self.assertEquals(task.cmd, cmd) |
| 1013 | |
| 1014 | |
| 1015 | def test_final_reparse_task(self): |
| 1016 | self.god.stub_class(monitor_db, 'PidfileRunMonitor') |
| 1017 | self._test_final_reparse_task_helper() |
| 1018 | self._test_final_reparse_task_helper(num_machines=2) |
| 1019 | self._test_final_reparse_task_helper(is_synch=True) |
| 1020 | self._test_final_reparse_task_helper(autoserv_success=False) |
| 1021 | |
| 1022 | |
| 1023 | def test_final_reparse_throttling(self): |
| 1024 | self.god.stub_class(monitor_db, 'PidfileRunMonitor') |
| 1025 | self.god.stub_function(monitor_db.FinalReparseTask, |
| 1026 | '_can_run_new_parse') |
| 1027 | |
| 1028 | self._setup_pre_parse_expects(False, 1) |
| 1029 | monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return( |
| 1030 | False) |
| 1031 | monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return( |
| 1032 | True) |
| 1033 | self.setup_run_monitor(0) |
| 1034 | self._setup_post_parse_expects(True) |
| 1035 | |
| 1036 | task = monitor_db.FinalReparseTask([self.queue_entry]) |
| 1037 | self.run_task(task, True) |
| 1038 | self.god.check_playback() |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 1039 | |
| 1040 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1041 | def _test_cleanup_task_helper(self, success, use_queue_entry=False): |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1042 | if use_queue_entry: |
| 1043 | self.queue_entry.get_host.expect_call().and_return(self.host) |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1044 | self.host.set_status.expect_call('Cleaning') |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1045 | if success: |
| 1046 | self.setup_run_monitor(0) |
| 1047 | self.host.set_status.expect_call('Ready') |
| 1048 | self.host.update_field.expect_call('dirty', 0) |
| 1049 | else: |
| 1050 | self.setup_run_monitor(1) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1051 | |
| 1052 | if use_queue_entry: |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1053 | task = monitor_db.CleanupTask(queue_entry=self.queue_entry) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1054 | else: |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1055 | task = monitor_db.CleanupTask(host=self.host) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1056 | self.assertEquals(len(task.failure_tasks), 1) |
| 1057 | repair_task = task.failure_tasks[0] |
| 1058 | self.assert_(isinstance(repair_task, monitor_db.RepairTask)) |
| 1059 | if use_queue_entry: |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 1060 | self.assertEquals(repair_task.queue_entry, self.queue_entry) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1061 | |
| 1062 | self.run_task(task, success) |
| 1063 | |
| 1064 | self.god.check_playback() |
| 1065 | self.assert_(set(task.monitor.cmd) >= |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1066 | set(['autoserv', '--cleanup', '-m', self.HOSTNAME, |
| 1067 | '-r', self.TEMP_DIR])) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1068 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1069 | def test_cleanup_task(self): |
| 1070 | self._test_cleanup_task_helper(True) |
| 1071 | self._test_cleanup_task_helper(False) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1072 | |
| 1073 | |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1074 | def test_cleanup_task_with_queue_entry(self): |
| 1075 | self._test_cleanup_task_helper(False, True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1076 | |
| 1077 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1078 | class JobTest(BaseSchedulerTest): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1079 | def setUp(self): |
| 1080 | super(JobTest, self).setUp() |
| 1081 | self.god.stub_function(os.path, 'exists') |
| 1082 | self.god.stub_function(monitor_db, 'ensure_directory_exists') |
| 1083 | |
| 1084 | |
| 1085 | def _setup_directory_expects(self, execution_subdir): |
| 1086 | job_path = os.path.join('.', '1-my_user') |
| 1087 | results_dir = os.path.join(job_path, execution_subdir) |
| 1088 | monitor_db.ensure_directory_exists.expect_call(job_path) |
| 1089 | os.path.exists.expect_call(results_dir) |
| 1090 | monitor_db.ensure_directory_exists.expect_call(results_dir) |
| 1091 | |
| 1092 | |
showard | e77ac67 | 2008-11-14 22:42:33 +0000 | [diff] [blame] | 1093 | def _test_run_helper(self, expect_agent=True, |
| 1094 | expected_status=models.HostQueueEntry.Status.STARTING): |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1095 | job = monitor_db.Job.fetch('id = 1').next() |
| 1096 | queue_entry = monitor_db.HostQueueEntry.fetch('id = 1').next() |
| 1097 | agent = job.run(queue_entry) |
| 1098 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1099 | self.god.check_playback() |
showard | e77ac67 | 2008-11-14 22:42:33 +0000 | [diff] [blame] | 1100 | self.assertEquals(models.HostQueueEntry.smart_get(1).status, |
| 1101 | expected_status) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1102 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1103 | if not expect_agent: |
| 1104 | self.assertEquals(agent, None) |
| 1105 | return |
| 1106 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1107 | self.assert_(isinstance(agent, monitor_db.Agent)) |
| 1108 | tasks = list(agent.queue.queue) |
| 1109 | return tasks |
| 1110 | |
| 1111 | |
| 1112 | def test_run_asynchronous(self): |
| 1113 | self._create_job(hosts=[1, 2]) |
| 1114 | |
| 1115 | tasks = self._test_run_helper() |
| 1116 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1117 | self.assertEquals(len(tasks), 1) |
| 1118 | verify_task = tasks[0] |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1119 | |
| 1120 | self.assert_(isinstance(verify_task, monitor_db.VerifyTask)) |
| 1121 | self.assertEquals(verify_task.queue_entry.id, 1) |
| 1122 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1123 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1124 | def test_run_asynchronous_skip_verify(self): |
| 1125 | job = self._create_job(hosts=[1, 2]) |
| 1126 | job.run_verify = False |
| 1127 | job.save() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1128 | self._setup_directory_expects('host1') |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1129 | |
| 1130 | tasks = self._test_run_helper() |
| 1131 | |
| 1132 | self.assertEquals(len(tasks), 1) |
| 1133 | queue_task = tasks[0] |
| 1134 | |
| 1135 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
| 1136 | self.assertEquals(queue_task.job.id, 1) |
| 1137 | |
| 1138 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1139 | def test_run_synchronous_verify(self): |
| 1140 | self._create_job(hosts=[1, 2], synchronous=True) |
| 1141 | |
| 1142 | tasks = self._test_run_helper() |
| 1143 | self.assertEquals(len(tasks), 1) |
| 1144 | verify_task = tasks[0] |
| 1145 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1146 | self.assert_(isinstance(verify_task, monitor_db.VerifyTask)) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1147 | self.assertEquals(verify_task.queue_entry.id, 1) |
| 1148 | |
| 1149 | |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1150 | def test_run_synchronous_skip_verify(self): |
| 1151 | job = self._create_job(hosts=[1, 2], synchronous=True) |
| 1152 | job.run_verify = False |
| 1153 | job.save() |
| 1154 | |
showard | e77ac67 | 2008-11-14 22:42:33 +0000 | [diff] [blame] | 1155 | self._test_run_helper( |
| 1156 | expect_agent=False, |
| 1157 | expected_status=models.HostQueueEntry.Status.PENDING) |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1158 | |
| 1159 | queue_entry = models.HostQueueEntry.smart_get(1) |
| 1160 | self.assertEquals(queue_entry.status, 'Pending') |
| 1161 | |
| 1162 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1163 | def test_run_synchronous_ready(self): |
| 1164 | self._create_job(hosts=[1, 2], synchronous=True) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1165 | self._update_hqe("status='Pending', execution_subdir='") |
| 1166 | self._setup_directory_expects('group0') |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 1167 | |
| 1168 | tasks = self._test_run_helper() |
| 1169 | self.assertEquals(len(tasks), 1) |
| 1170 | queue_task = tasks[0] |
| 1171 | |
| 1172 | self.assert_(isinstance(queue_task, monitor_db.QueueTask)) |
| 1173 | self.assertEquals(queue_task.job.id, 1) |
| 1174 | hqe_ids = [hqe.id for hqe in queue_task.queue_entries] |
| 1175 | self.assertEquals(hqe_ids, [1, 2]) |
| 1176 | |
| 1177 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1178 | def test_reboot_before_always(self): |
| 1179 | job = self._create_job(hosts=[1]) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 1180 | job.reboot_before = models.RebootBefore.ALWAYS |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1181 | job.save() |
| 1182 | |
| 1183 | tasks = self._test_run_helper() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1184 | self.assertEquals(len(tasks), 2) |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1185 | cleanup_task = tasks[0] |
| 1186 | self.assert_(isinstance(cleanup_task, monitor_db.CleanupTask)) |
| 1187 | self.assertEquals(cleanup_task.host.id, 1) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1188 | |
| 1189 | |
| 1190 | def _test_reboot_before_if_dirty_helper(self, expect_reboot): |
| 1191 | job = self._create_job(hosts=[1]) |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 1192 | job.reboot_before = models.RebootBefore.IF_DIRTY |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1193 | job.save() |
| 1194 | |
| 1195 | tasks = self._test_run_helper() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1196 | self.assertEquals(len(tasks), expect_reboot and 2 or 1) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1197 | if expect_reboot: |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1198 | cleanup_task = tasks[0] |
| 1199 | self.assert_(isinstance(cleanup_task, monitor_db.CleanupTask)) |
| 1200 | self.assertEquals(cleanup_task.host.id, 1) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1201 | |
| 1202 | def test_reboot_before_if_dirty(self): |
| 1203 | models.Host.smart_get(1).update_object(dirty=True) |
| 1204 | self._test_reboot_before_if_dirty_helper(True) |
| 1205 | |
| 1206 | |
| 1207 | def test_reboot_before_not_dirty(self): |
| 1208 | models.Host.smart_get(1).update_object(dirty=False) |
| 1209 | self._test_reboot_before_if_dirty_helper(False) |
| 1210 | |
| 1211 | |
| 1212 | |
showard | ce38e0c | 2008-05-29 19:36:16 +0000 | [diff] [blame] | 1213 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1214 | unittest.main() |