showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 2 | |
| 3 | import datetime, unittest |
| 4 | import common |
| 5 | from autotest_lib.frontend import setup_django_environment |
| 6 | from autotest_lib.frontend.afe import frontend_test_utils |
| 7 | from django.db import connection |
| 8 | from autotest_lib.frontend.afe import models, rpc_interface, frontend_test_utils |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 9 | from autotest_lib.frontend.afe import model_logic |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | _hqe_status = models.HostQueueEntry.Status |
| 13 | |
| 14 | |
| 15 | class RpcInterfaceTest(unittest.TestCase, |
| 16 | frontend_test_utils.FrontendTestMixin): |
| 17 | def setUp(self): |
| 18 | self._frontend_common_setup() |
| 19 | |
| 20 | |
| 21 | def tearDown(self): |
| 22 | self._frontend_common_teardown() |
| 23 | |
| 24 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 25 | def test_validation(self): |
| 26 | # non-number for a numeric field |
| 27 | self.assertRaises(model_logic.ValidationError, |
| 28 | rpc_interface.add_atomic_group, name='foo', |
| 29 | max_number_of_machines='bar') |
| 30 | # omit a required field |
| 31 | self.assertRaises(model_logic.ValidationError, rpc_interface.add_label, |
| 32 | name=None) |
| 33 | # violate uniqueness constraint |
| 34 | self.assertRaises(model_logic.ValidationError, rpc_interface.add_host, |
| 35 | hostname='host1') |
| 36 | |
| 37 | |
showard | cafd16e | 2009-05-29 18:37:49 +0000 | [diff] [blame] | 38 | def test_multiple_platforms(self): |
| 39 | platform2 = models.Label.objects.create(name='platform2', platform=True) |
| 40 | self.assertRaises(model_logic.ValidationError, |
| 41 | rpc_interface. label_add_hosts, 'platform2', |
| 42 | ['host1', 'host2']) |
| 43 | self.assertRaises(model_logic.ValidationError, |
| 44 | rpc_interface.host_add_labels, 'host1', ['platform2']) |
| 45 | # make sure the platform didn't get added |
| 46 | platforms = rpc_interface.get_labels( |
| 47 | host__hostname__in=['host1', 'host2'], platform=True) |
| 48 | self.assertEquals(len(platforms), 1) |
| 49 | self.assertEquals(platforms[0]['name'], 'myplatform') |
| 50 | |
| 51 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 52 | def _check_hostnames(self, hosts, expected_hostnames): |
| 53 | self.assertEquals(set(host['hostname'] for host in hosts), |
| 54 | set(expected_hostnames)) |
| 55 | |
| 56 | |
| 57 | def test_get_hosts(self): |
| 58 | hosts = rpc_interface.get_hosts() |
| 59 | self._check_hostnames(hosts, [host.hostname for host in self.hosts]) |
| 60 | |
| 61 | hosts = rpc_interface.get_hosts(hostname='host1') |
| 62 | self._check_hostnames(hosts, ['host1']) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 63 | host = hosts[0] |
| 64 | self.assertEquals(sorted(host['labels']), ['label1', 'myplatform']) |
| 65 | self.assertEquals(host['platform'], 'myplatform') |
| 66 | self.assertEquals(host['atomic_group'], None) |
| 67 | self.assertEquals(host['acls'], ['my_acl']) |
| 68 | self.assertEquals(host['attributes'], {}) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def test_get_hosts_multiple_labels(self): |
| 72 | hosts = rpc_interface.get_hosts( |
| 73 | multiple_labels=['myplatform', 'label1']) |
| 74 | self._check_hostnames(hosts, ['host1']) |
| 75 | |
| 76 | |
| 77 | def test_get_hosts_exclude_only_if_needed(self): |
| 78 | self.hosts[0].labels.add(self.label3) |
| 79 | |
| 80 | hosts = rpc_interface.get_hosts(hostname__in=['host1', 'host2'], |
| 81 | exclude_only_if_needed_labels=True) |
| 82 | self._check_hostnames(hosts, ['host2']) |
| 83 | |
| 84 | |
showard | 87cc38f | 2009-08-20 23:37:04 +0000 | [diff] [blame] | 85 | def test_get_hosts_exclude_atomic_group_hosts(self): |
| 86 | hosts = rpc_interface.get_hosts( |
| 87 | exclude_atomic_group_hosts=True, |
| 88 | hostname__in=['host4', 'host5', 'host6']) |
| 89 | self._check_hostnames(hosts, ['host4']) |
| 90 | |
| 91 | |
| 92 | def test_get_hosts_exclude_both(self): |
| 93 | self.hosts[0].labels.add(self.label3) |
| 94 | |
| 95 | hosts = rpc_interface.get_hosts( |
| 96 | hostname__in=['host1', 'host2', 'host5'], |
| 97 | exclude_only_if_needed_labels=True, |
| 98 | exclude_atomic_group_hosts=True) |
| 99 | self._check_hostnames(hosts, ['host2']) |
| 100 | |
| 101 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 102 | def test_job_keyvals(self): |
| 103 | keyval_dict = {'mykey': 'myvalue'} |
| 104 | job_id = rpc_interface.create_job(name='test', priority='Medium', |
| 105 | control_file='foo', |
| 106 | control_type='Client', |
| 107 | hosts=['host1'], |
| 108 | keyvals=keyval_dict) |
| 109 | jobs = rpc_interface.get_jobs(id=job_id) |
| 110 | self.assertEquals(len(jobs), 1) |
| 111 | self.assertEquals(jobs[0]['keyvals'], keyval_dict) |
| 112 | |
| 113 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 114 | def test_get_jobs_summary(self): |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 115 | job = self._create_job(hosts=xrange(1, 4)) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 116 | entries = list(job.hostqueueentry_set.all()) |
| 117 | entries[1].status = _hqe_status.FAILED |
| 118 | entries[1].save() |
| 119 | entries[2].status = _hqe_status.FAILED |
| 120 | entries[2].aborted = True |
| 121 | entries[2].save() |
| 122 | |
| 123 | job_summaries = rpc_interface.get_jobs_summary(id=job.id) |
| 124 | self.assertEquals(len(job_summaries), 1) |
| 125 | summary = job_summaries[0] |
| 126 | self.assertEquals(summary['status_counts'], {'Queued': 1, |
| 127 | 'Failed': 2}) |
| 128 | |
| 129 | |
showard | 6c65d25 | 2009-10-01 18:45:22 +0000 | [diff] [blame] | 130 | def test_get_jobs_filters(self): |
| 131 | HqeStatus = models.HostQueueEntry.Status |
| 132 | def create_two_host_job(): |
| 133 | return self._create_job(hosts=[1, 2]) |
| 134 | def set_hqe_statuses(job, first_status, second_status): |
| 135 | entries = job.hostqueueentry_set.all() |
| 136 | entries[0].update_object(status=first_status) |
| 137 | entries[1].update_object(status=second_status) |
| 138 | |
| 139 | queued = create_two_host_job() |
| 140 | |
| 141 | queued_and_running = create_two_host_job() |
| 142 | set_hqe_statuses(queued_and_running, HqeStatus.QUEUED, |
| 143 | HqeStatus.RUNNING) |
| 144 | |
| 145 | running_and_complete = create_two_host_job() |
| 146 | set_hqe_statuses(running_and_complete, HqeStatus.RUNNING, |
| 147 | HqeStatus.COMPLETED) |
| 148 | |
| 149 | complete = create_two_host_job() |
| 150 | set_hqe_statuses(complete, HqeStatus.COMPLETED, HqeStatus.COMPLETED) |
| 151 | |
| 152 | started_but_inactive = create_two_host_job() |
| 153 | set_hqe_statuses(started_but_inactive, HqeStatus.QUEUED, |
| 154 | HqeStatus.COMPLETED) |
| 155 | |
| 156 | parsing = create_two_host_job() |
| 157 | set_hqe_statuses(parsing, HqeStatus.PARSING, HqeStatus.PARSING) |
| 158 | |
| 159 | def check_job_ids(actual_job_dicts, expected_jobs): |
| 160 | self.assertEquals( |
| 161 | set(job_dict['id'] for job_dict in actual_job_dicts), |
| 162 | set(job.id for job in expected_jobs)) |
| 163 | |
| 164 | check_job_ids(rpc_interface.get_jobs(not_yet_run=True), [queued]) |
| 165 | check_job_ids(rpc_interface.get_jobs(running=True), |
| 166 | [queued_and_running, running_and_complete, |
| 167 | started_but_inactive, parsing]) |
| 168 | check_job_ids(rpc_interface.get_jobs(finished=True), [complete]) |
| 169 | |
| 170 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 171 | def _create_job_helper(self, **kwargs): |
| 172 | return rpc_interface.create_job('test', 'Medium', 'control file', |
| 173 | 'Server', **kwargs) |
| 174 | |
| 175 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 176 | def test_one_time_hosts(self): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 177 | job = self._create_job_helper(one_time_hosts=['testhost']) |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 178 | host = models.Host.objects.get(hostname='testhost') |
| 179 | self.assertEquals(host.invalid, True) |
| 180 | self.assertEquals(host.labels.count(), 0) |
| 181 | self.assertEquals(host.aclgroup_set.count(), 0) |
| 182 | |
| 183 | |
showard | 09d80f9 | 2009-11-19 01:01:19 +0000 | [diff] [blame] | 184 | def test_create_job_duplicate_hosts(self): |
| 185 | self.assertRaises(model_logic.ValidationError, self._create_job_helper, |
| 186 | hosts=[1, 1]) |
| 187 | |
| 188 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 189 | def test_create_hostless_job(self): |
| 190 | job_id = self._create_job_helper(hostless=True) |
| 191 | job = models.Job.objects.get(pk=job_id) |
| 192 | queue_entries = job.hostqueueentry_set.all() |
| 193 | self.assertEquals(len(queue_entries), 1) |
| 194 | self.assertEquals(queue_entries[0].host, None) |
| 195 | self.assertEquals(queue_entries[0].meta_host, None) |
| 196 | self.assertEquals(queue_entries[0].atomic_group, None) |
| 197 | |
| 198 | |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 199 | def _setup_special_tasks(self): |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 200 | host = self.hosts[0] |
| 201 | |
| 202 | job1 = self._create_job(hosts=[1]) |
| 203 | job2 = self._create_job(hosts=[1]) |
| 204 | |
| 205 | entry1 = job1.hostqueueentry_set.all()[0] |
| 206 | entry1.update_object(started_on=datetime.datetime(2009, 1, 2), |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 207 | execution_subdir='host1') |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 208 | entry2 = job2.hostqueueentry_set.all()[0] |
| 209 | entry2.update_object(started_on=datetime.datetime(2009, 1, 3), |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 210 | execution_subdir='host1') |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 211 | |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 212 | self.task1 = models.SpecialTask.objects.create( |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 213 | host=host, task=models.SpecialTask.Task.VERIFY, |
| 214 | time_started=datetime.datetime(2009, 1, 1), # ran before job 1 |
| 215 | is_complete=True) |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 216 | self.task2 = models.SpecialTask.objects.create( |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 217 | host=host, task=models.SpecialTask.Task.VERIFY, |
| 218 | queue_entry=entry2, # ran with job 2 |
| 219 | is_active=True) |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 220 | self.task3 = models.SpecialTask.objects.create( |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 221 | host=host, task=models.SpecialTask.Task.VERIFY) # not yet run |
| 222 | |
showard | 1a5a408 | 2009-07-28 20:01:37 +0000 | [diff] [blame] | 223 | |
| 224 | def test_get_special_tasks(self): |
| 225 | self._setup_special_tasks() |
| 226 | tasks = rpc_interface.get_special_tasks(host__hostname='host1', |
| 227 | queue_entry__isnull=True) |
| 228 | self.assertEquals(len(tasks), 2) |
| 229 | self.assertEquals(tasks[0]['task'], models.SpecialTask.Task.VERIFY) |
| 230 | self.assertEquals(tasks[0]['is_active'], False) |
| 231 | self.assertEquals(tasks[0]['is_complete'], True) |
| 232 | |
| 233 | |
| 234 | def test_get_latest_special_task(self): |
| 235 | # a particular usage of get_special_tasks() |
| 236 | self._setup_special_tasks() |
| 237 | self.task2.time_started = datetime.datetime(2009, 1, 2) |
| 238 | self.task2.save() |
| 239 | |
| 240 | tasks = rpc_interface.get_special_tasks( |
| 241 | host__hostname='host1', task=models.SpecialTask.Task.VERIFY, |
| 242 | time_started__isnull=False, sort_by=['-time_started'], |
| 243 | query_limit=1) |
| 244 | self.assertEquals(len(tasks), 1) |
| 245 | self.assertEquals(tasks[0]['id'], 2) |
| 246 | |
| 247 | |
| 248 | def _common_entry_check(self, entry_dict): |
| 249 | self.assertEquals(entry_dict['host']['hostname'], 'host1') |
| 250 | self.assertEquals(entry_dict['job']['id'], 2) |
| 251 | |
| 252 | |
| 253 | def test_get_host_queue_entries_and_special_tasks(self): |
| 254 | self._setup_special_tasks() |
| 255 | |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 256 | entries_and_tasks = ( |
| 257 | rpc_interface.get_host_queue_entries_and_special_tasks('host1')) |
| 258 | |
| 259 | paths = [entry['execution_path'] for entry in entries_and_tasks] |
| 260 | self.assertEquals(paths, ['hosts/host1/3-verify', |
showard | fd8b89f | 2010-01-20 19:06:30 +0000 | [diff] [blame] | 261 | '2-autotest_system/host1', |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 262 | 'hosts/host1/2-verify', |
showard | fd8b89f | 2010-01-20 19:06:30 +0000 | [diff] [blame] | 263 | '1-autotest_system/host1', |
showard | c0ac3a7 | 2009-07-08 21:14:45 +0000 | [diff] [blame] | 264 | 'hosts/host1/1-verify']) |
| 265 | |
| 266 | verify2 = entries_and_tasks[2] |
| 267 | self._common_entry_check(verify2) |
| 268 | self.assertEquals(verify2['type'], 'Verify') |
| 269 | self.assertEquals(verify2['status'], 'Running') |
| 270 | self.assertEquals(verify2['execution_path'], 'hosts/host1/2-verify') |
| 271 | |
| 272 | entry2 = entries_and_tasks[1] |
| 273 | self._common_entry_check(entry2) |
| 274 | self.assertEquals(entry2['type'], 'Job') |
| 275 | self.assertEquals(entry2['status'], 'Queued') |
| 276 | self.assertEquals(entry2['started_on'], '2009-01-03 00:00:00') |
| 277 | |
| 278 | |
showard | 8aa84fc | 2009-09-16 17:17:55 +0000 | [diff] [blame] | 279 | def test_view_invalid_host(self): |
| 280 | # RPCs used by View Host page should work for invalid hosts |
| 281 | self._create_job_helper(hosts=[1]) |
| 282 | self.hosts[0].delete() |
| 283 | |
| 284 | self.assertEquals(1, rpc_interface.get_num_hosts(hostname='host1', |
| 285 | valid_only=False)) |
| 286 | data = rpc_interface.get_hosts(hostname='host1', valid_only=False) |
| 287 | self.assertEquals(1, len(data)) |
| 288 | |
| 289 | self.assertEquals(1, rpc_interface.get_num_host_queue_entries( |
| 290 | host__hostname='host1')) |
| 291 | data = rpc_interface.get_host_queue_entries(host__hostname='host1') |
| 292 | self.assertEquals(1, len(data)) |
| 293 | |
| 294 | count = rpc_interface.get_num_host_queue_entries_and_special_tasks( |
| 295 | hostname='host1') |
| 296 | self.assertEquals(1, count) |
| 297 | data = rpc_interface.get_host_queue_entries_and_special_tasks( |
| 298 | hostname='host1') |
| 299 | self.assertEquals(1, len(data)) |
| 300 | |
| 301 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 302 | def test_reverify_hosts(self): |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 303 | hostname_list = rpc_interface.reverify_hosts(id__in=[1, 2]) |
| 304 | self.assertEquals(hostname_list, ['host1', 'host2']) |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 305 | tasks = rpc_interface.get_special_tasks() |
| 306 | self.assertEquals(len(tasks), 2) |
| 307 | self.assertEquals(set(task['host']['id'] for task in tasks), |
| 308 | set([1, 2])) |
| 309 | |
| 310 | task = tasks[0] |
| 311 | self.assertEquals(task['task'], models.SpecialTask.Task.VERIFY) |
showard | fd8b89f | 2010-01-20 19:06:30 +0000 | [diff] [blame] | 312 | self.assertEquals(task['requested_by'], 'autotest_system') |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 313 | |
| 314 | |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 315 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 316 | if __name__ == '__main__': |
| 317 | unittest.main() |