blob: f1a682457c203d58cbe5b8325cb2789455c96cd7 [file] [log] [blame]
showard1a5a4082009-07-28 20:01:37 +00001#!/usr/bin/python
showardb6d16622009-05-26 19:35:29 +00002
3import datetime, unittest
4import common
5from autotest_lib.frontend import setup_django_environment
6from autotest_lib.frontend.afe import frontend_test_utils
7from django.db import connection
8from autotest_lib.frontend.afe import models, rpc_interface, frontend_test_utils
showardcafd16e2009-05-29 18:37:49 +00009from autotest_lib.frontend.afe import model_logic
showardb6d16622009-05-26 19:35:29 +000010
11
12_hqe_status = models.HostQueueEntry.Status
13
14
15class 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
showarda5288b42009-07-28 20:06:08 +000025 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
showardcafd16e2009-05-29 18:37:49 +000038 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
showarda5288b42009-07-28 20:06:08 +000052 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'])
63
64
65 def test_get_hosts_multiple_labels(self):
66 hosts = rpc_interface.get_hosts(
67 multiple_labels=['myplatform', 'label1'])
68 self._check_hostnames(hosts, ['host1'])
69
70
71 def test_get_hosts_exclude_only_if_needed(self):
72 self.hosts[0].labels.add(self.label3)
73
74 hosts = rpc_interface.get_hosts(hostname__in=['host1', 'host2'],
75 exclude_only_if_needed_labels=True)
76 self._check_hostnames(hosts, ['host2'])
77
78
showard87cc38f2009-08-20 23:37:04 +000079 def test_get_hosts_exclude_atomic_group_hosts(self):
80 hosts = rpc_interface.get_hosts(
81 exclude_atomic_group_hosts=True,
82 hostname__in=['host4', 'host5', 'host6'])
83 self._check_hostnames(hosts, ['host4'])
84
85
86 def test_get_hosts_exclude_both(self):
87 self.hosts[0].labels.add(self.label3)
88
89 hosts = rpc_interface.get_hosts(
90 hostname__in=['host1', 'host2', 'host5'],
91 exclude_only_if_needed_labels=True,
92 exclude_atomic_group_hosts=True)
93 self._check_hostnames(hosts, ['host2'])
94
95
showardb6d16622009-05-26 19:35:29 +000096 def test_get_jobs_summary(self):
showardc0ac3a72009-07-08 21:14:45 +000097 job = self._create_job(hosts=xrange(1, 4))
showardb6d16622009-05-26 19:35:29 +000098 entries = list(job.hostqueueentry_set.all())
99 entries[1].status = _hqe_status.FAILED
100 entries[1].save()
101 entries[2].status = _hqe_status.FAILED
102 entries[2].aborted = True
103 entries[2].save()
104
105 job_summaries = rpc_interface.get_jobs_summary(id=job.id)
106 self.assertEquals(len(job_summaries), 1)
107 summary = job_summaries[0]
108 self.assertEquals(summary['status_counts'], {'Queued': 1,
109 'Failed': 2})
110
111
showard6c65d252009-10-01 18:45:22 +0000112 def test_get_jobs_filters(self):
113 HqeStatus = models.HostQueueEntry.Status
114 def create_two_host_job():
115 return self._create_job(hosts=[1, 2])
116 def set_hqe_statuses(job, first_status, second_status):
117 entries = job.hostqueueentry_set.all()
118 entries[0].update_object(status=first_status)
119 entries[1].update_object(status=second_status)
120
121 queued = create_two_host_job()
122
123 queued_and_running = create_two_host_job()
124 set_hqe_statuses(queued_and_running, HqeStatus.QUEUED,
125 HqeStatus.RUNNING)
126
127 running_and_complete = create_two_host_job()
128 set_hqe_statuses(running_and_complete, HqeStatus.RUNNING,
129 HqeStatus.COMPLETED)
130
131 complete = create_two_host_job()
132 set_hqe_statuses(complete, HqeStatus.COMPLETED, HqeStatus.COMPLETED)
133
134 started_but_inactive = create_two_host_job()
135 set_hqe_statuses(started_but_inactive, HqeStatus.QUEUED,
136 HqeStatus.COMPLETED)
137
138 parsing = create_two_host_job()
139 set_hqe_statuses(parsing, HqeStatus.PARSING, HqeStatus.PARSING)
140
141 def check_job_ids(actual_job_dicts, expected_jobs):
142 self.assertEquals(
143 set(job_dict['id'] for job_dict in actual_job_dicts),
144 set(job.id for job in expected_jobs))
145
146 check_job_ids(rpc_interface.get_jobs(not_yet_run=True), [queued])
147 check_job_ids(rpc_interface.get_jobs(running=True),
148 [queued_and_running, running_and_complete,
149 started_but_inactive, parsing])
150 check_job_ids(rpc_interface.get_jobs(finished=True), [complete])
151
152
showarda5288b42009-07-28 20:06:08 +0000153 def _create_job_helper(self, **kwargs):
154 return rpc_interface.create_job('test', 'Medium', 'control file',
155 'Server', **kwargs)
156
157
showard2924b0a2009-06-18 23:16:15 +0000158 def test_one_time_hosts(self):
showarda5288b42009-07-28 20:06:08 +0000159 job = self._create_job_helper(one_time_hosts=['testhost'])
showard2924b0a2009-06-18 23:16:15 +0000160 host = models.Host.objects.get(hostname='testhost')
161 self.assertEquals(host.invalid, True)
162 self.assertEquals(host.labels.count(), 0)
163 self.assertEquals(host.aclgroup_set.count(), 0)
164
165
showard1a5a4082009-07-28 20:01:37 +0000166 def _setup_special_tasks(self):
showardc0ac3a72009-07-08 21:14:45 +0000167 host = self.hosts[0]
168
169 job1 = self._create_job(hosts=[1])
170 job2 = self._create_job(hosts=[1])
171
172 entry1 = job1.hostqueueentry_set.all()[0]
173 entry1.update_object(started_on=datetime.datetime(2009, 1, 2),
174 execution_subdir='1-myuser/host1')
175 entry2 = job2.hostqueueentry_set.all()[0]
176 entry2.update_object(started_on=datetime.datetime(2009, 1, 3),
177 execution_subdir='2-myuser/host1')
178
showard1a5a4082009-07-28 20:01:37 +0000179 self.task1 = models.SpecialTask.objects.create(
showardc0ac3a72009-07-08 21:14:45 +0000180 host=host, task=models.SpecialTask.Task.VERIFY,
181 time_started=datetime.datetime(2009, 1, 1), # ran before job 1
182 is_complete=True)
showard1a5a4082009-07-28 20:01:37 +0000183 self.task2 = models.SpecialTask.objects.create(
showardc0ac3a72009-07-08 21:14:45 +0000184 host=host, task=models.SpecialTask.Task.VERIFY,
185 queue_entry=entry2, # ran with job 2
186 is_active=True)
showard1a5a4082009-07-28 20:01:37 +0000187 self.task3 = models.SpecialTask.objects.create(
showardc0ac3a72009-07-08 21:14:45 +0000188 host=host, task=models.SpecialTask.Task.VERIFY) # not yet run
189
showard1a5a4082009-07-28 20:01:37 +0000190
191 def test_get_special_tasks(self):
192 self._setup_special_tasks()
193 tasks = rpc_interface.get_special_tasks(host__hostname='host1',
194 queue_entry__isnull=True)
195 self.assertEquals(len(tasks), 2)
196 self.assertEquals(tasks[0]['task'], models.SpecialTask.Task.VERIFY)
197 self.assertEquals(tasks[0]['is_active'], False)
198 self.assertEquals(tasks[0]['is_complete'], True)
199
200
201 def test_get_latest_special_task(self):
202 # a particular usage of get_special_tasks()
203 self._setup_special_tasks()
204 self.task2.time_started = datetime.datetime(2009, 1, 2)
205 self.task2.save()
206
207 tasks = rpc_interface.get_special_tasks(
208 host__hostname='host1', task=models.SpecialTask.Task.VERIFY,
209 time_started__isnull=False, sort_by=['-time_started'],
210 query_limit=1)
211 self.assertEquals(len(tasks), 1)
212 self.assertEquals(tasks[0]['id'], 2)
213
214
215 def _common_entry_check(self, entry_dict):
216 self.assertEquals(entry_dict['host']['hostname'], 'host1')
217 self.assertEquals(entry_dict['job']['id'], 2)
218
219
220 def test_get_host_queue_entries_and_special_tasks(self):
221 self._setup_special_tasks()
222
showardc0ac3a72009-07-08 21:14:45 +0000223 entries_and_tasks = (
224 rpc_interface.get_host_queue_entries_and_special_tasks('host1'))
225
226 paths = [entry['execution_path'] for entry in entries_and_tasks]
227 self.assertEquals(paths, ['hosts/host1/3-verify',
228 '2-myuser/host1',
229 'hosts/host1/2-verify',
230 '1-myuser/host1',
231 'hosts/host1/1-verify'])
232
233 verify2 = entries_and_tasks[2]
234 self._common_entry_check(verify2)
235 self.assertEquals(verify2['type'], 'Verify')
236 self.assertEquals(verify2['status'], 'Running')
237 self.assertEquals(verify2['execution_path'], 'hosts/host1/2-verify')
238
239 entry2 = entries_and_tasks[1]
240 self._common_entry_check(entry2)
241 self.assertEquals(entry2['type'], 'Job')
242 self.assertEquals(entry2['status'], 'Queued')
243 self.assertEquals(entry2['started_on'], '2009-01-03 00:00:00')
244
245
showarda5288b42009-07-28 20:06:08 +0000246 def _create_job_helper(self, **kwargs):
247 return rpc_interface.create_job('test', 'Medium', 'control file',
248 'Server', **kwargs)
249
250
showard8aa84fc2009-09-16 17:17:55 +0000251 def test_view_invalid_host(self):
252 # RPCs used by View Host page should work for invalid hosts
253 self._create_job_helper(hosts=[1])
254 self.hosts[0].delete()
255
256 self.assertEquals(1, rpc_interface.get_num_hosts(hostname='host1',
257 valid_only=False))
258 data = rpc_interface.get_hosts(hostname='host1', valid_only=False)
259 self.assertEquals(1, len(data))
260
261 self.assertEquals(1, rpc_interface.get_num_host_queue_entries(
262 host__hostname='host1'))
263 data = rpc_interface.get_host_queue_entries(host__hostname='host1')
264 self.assertEquals(1, len(data))
265
266 count = rpc_interface.get_num_host_queue_entries_and_special_tasks(
267 hostname='host1')
268 self.assertEquals(1, count)
269 data = rpc_interface.get_host_queue_entries_and_special_tasks(
270 hostname='host1')
271 self.assertEquals(1, len(data))
272
273
showardb6d16622009-05-26 19:35:29 +0000274if __name__ == '__main__':
275 unittest.main()