blob: 9120335cf18ef5c3bee5d60d164276dabdf3c5b9 [file] [log] [blame]
showardf828c772010-01-25 21:49:42 +00001#!/usr/bin/python
2
3import common
jamesrencd7a81a2010-04-21 20:39:08 +00004import unittest
showardf828c772010-01-25 21:49:42 +00005from autotest_lib.frontend import setup_django_environment
6from autotest_lib.frontend import setup_test_environment
7from django.test import client
jamesrencd7a81a2010-04-21 20:39:08 +00008from autotest_lib.frontend.shared import resource_test_utils
9from autotest_lib.frontend.afe import control_file, models, model_attributes
showardf828c772010-01-25 21:49:42 +000010
jamesrencd7a81a2010-04-21 20:39:08 +000011class AfeResourceTestCase(resource_test_utils.ResourceTestCase):
showardf46ad4c2010-02-03 20:28:59 +000012 URI_PREFIX = 'http://testserver/afe/server/resources'
showardf828c772010-01-25 21:49:42 +000013
14 CONTROL_FILE_CONTENTS = 'my control file contents'
15
16 def setUp(self):
jamesrencd7a81a2010-04-21 20:39:08 +000017 super(AfeResourceTestCase, self).setUp()
showardf828c772010-01-25 21:49:42 +000018 self._add_additional_data()
showardf828c772010-01-25 21:49:42 +000019
20
21 def _add_additional_data(self):
22 models.Test.objects.create(name='mytest',
jamesrendd855242010-03-02 22:23:44 +000023 test_type=model_attributes.TestTypes.SERVER,
showardf828c772010-01-25 21:49:42 +000024 path='/path/to/mytest')
25
26
jamesrencd7a81a2010-04-21 20:39:08 +000027class FilteringPagingTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000028 # we'll arbitarily choose to use hosts for this
29
30 def setUp(self):
31 super(FilteringPagingTest, self).setUp()
32
33 self.labels[0].host_set = [self.hosts[0], self.hosts[1]]
34 for host in self.hosts[:3]:
35 host.locked = True
36 host.save()
37
38 def test_simple_filtering(self):
39 response = self.request('get', 'hosts?locked=true&has_label=label1')
jamesren3981f442010-02-16 19:27:59 +000040 self.check_collection(response, 'hostname', ['host1', 'host2'])
showardf828c772010-01-25 21:49:42 +000041
42
jamesrencd7a81a2010-04-21 20:39:08 +000043 def test_in_filtering(self):
44 response = self.request('get', 'hosts?hostname:in=host1,host2')
45 self.check_collection(response, 'hostname', ['host1', 'host2'])
46
47
showardf828c772010-01-25 21:49:42 +000048 def test_paging(self):
49 response = self.request('get', 'hosts?start_index=1&items_per_page=2')
jamesren3981f442010-02-16 19:27:59 +000050 self.check_collection(response, 'hostname', ['host2', 'host3'])
showardf828c772010-01-25 21:49:42 +000051 self.assertEquals(response['total_results'], 9)
52 self.assertEquals(response['items_per_page'], 2)
53 self.assertEquals(response['start_index'], 1)
54
55
jamesrencd7a81a2010-04-21 20:39:08 +000056 def test_full_representations(self):
57 response = self.request(
58 'get', 'hosts?hostname=host1&full_representations=true')
59 self.check_collection(response, 'hostname', ['host1'])
60 host = response['members'][0]
61 # invalid only included in full representation
62 self.assertEquals(host['invalid'], False)
63
64
65class MiscellaneousTest(AfeResourceTestCase):
jamesren3981f442010-02-16 19:27:59 +000066 def test_trailing_slash(self):
67 response = self.request('get', 'hosts/host1/')
68 self.assertEquals(response['hostname'], 'host1')
69
70
jamesrencd7a81a2010-04-21 20:39:08 +000071class AtomicGroupClassTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000072 def test_collection(self):
73 response = self.request('get', 'atomic_group_classes')
jamesren3981f442010-02-16 19:27:59 +000074 self.check_collection(response, 'name', ['atomic1', 'atomic2'],
75 length=2)
showardf828c772010-01-25 21:49:42 +000076
77
78 def test_entry(self):
79 response = self.request('get', 'atomic_group_classes/atomic1')
80 self.assertEquals(response['name'], 'atomic1')
81 self.assertEquals(response['max_number_of_machines'], 2)
82
83
84 def test_labels(self):
jamesren3981f442010-02-16 19:27:59 +000085 self.check_relationship('atomic_group_classes/atomic1', 'labels',
86 'label', 'name', ['label4', 'label5'])
showardf828c772010-01-25 21:49:42 +000087
88
jamesrencd7a81a2010-04-21 20:39:08 +000089class LabelTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000090 def test_collection(self):
91 response = self.request('get', 'labels')
jamesren3981f442010-02-16 19:27:59 +000092 self.check_collection(response, 'name', ['label1', 'label2'], length=9,
93 check_number=2)
showardf828c772010-01-25 21:49:42 +000094 label1 = self.sorted_by(response['members'], 'name')[0]
95 self.assertEquals(label1['is_platform'], False)
96
97
98 def test_entry(self):
99 response = self.request('get', 'labels/label1')
100 self.assertEquals(response['name'], 'label1')
101 self.assertEquals(response['is_platform'], False)
102 self.assertEquals(response['atomic_group_class'], None)
103
104
105 def test_hosts(self):
jamesren3981f442010-02-16 19:27:59 +0000106 self.check_relationship('labels/label1', 'hosts', 'host', 'hostname',
107 ['host1'])
showardf828c772010-01-25 21:49:42 +0000108
109
jamesrencd7a81a2010-04-21 20:39:08 +0000110class UserTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000111 def test_collection(self):
112 response = self.request('get', 'users')
jamesren3981f442010-02-16 19:27:59 +0000113 self.check_collection(response, 'username',
showardf828c772010-01-25 21:49:42 +0000114 ['autotest_system', 'debug_user'])
115
116
117 def test_entry(self):
118 response = self.request('get', 'users/debug_user')
119 self.assertEquals(response['username'], 'debug_user')
120
121 me_response = self.request('get', 'users/@me')
122 self.assertEquals(response, me_response)
123
124
125 def test_acls(self):
jamesren3981f442010-02-16 19:27:59 +0000126 self.check_relationship('users/debug_user', 'acls', 'acl', 'name',
127 ['Everyone', 'my_acl'])
showardf828c772010-01-25 21:49:42 +0000128
129
130 def test_accessible_hosts(self):
131 group = models.AclGroup.objects.create(name='mygroup')
132 models.User.objects.get(login='debug_user').aclgroup_set = [group]
133 self.hosts[0].aclgroup_set = [group]
134
jamesren3981f442010-02-16 19:27:59 +0000135 user = self.request('get', 'users/debug_user')
136 response = self.request('get', user['accessible_hosts']['href'])
137 self.check_collection(response, 'hostname', ['host1'])
showardf828c772010-01-25 21:49:42 +0000138
139
jamesrencd7a81a2010-04-21 20:39:08 +0000140class AclTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000141 def test_collection(self):
142 response = self.request('get', 'acls')
jamesren3981f442010-02-16 19:27:59 +0000143 self.check_collection(response, 'name', ['Everyone', 'my_acl'])
showardf828c772010-01-25 21:49:42 +0000144
145
146 def test_entry(self):
147 response = self.request('get', 'acls/my_acl')
148 self.assertEquals(response['name'], 'my_acl')
149
150
151 def test_users(self):
jamesren3981f442010-02-16 19:27:59 +0000152 self.check_relationship('acls/my_acl', 'users', 'user', 'username',
153 ['autotest_system', 'debug_user'])
showardf828c772010-01-25 21:49:42 +0000154
155
156 def test_hosts(self):
jamesren3981f442010-02-16 19:27:59 +0000157 self.check_relationship('acls/my_acl', 'hosts', 'host', 'hostname',
158 ['host1', 'host2'], length=9, check_number=2)
showardf828c772010-01-25 21:49:42 +0000159
160
jamesrencd7a81a2010-04-21 20:39:08 +0000161class HostTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000162 def test_collection(self):
163 response = self.request('get', 'hosts')
jamesren3981f442010-02-16 19:27:59 +0000164 self.check_collection(response, 'hostname', ['host1', 'host2'],
165 length=9, check_number=2)
showardf828c772010-01-25 21:49:42 +0000166 host1 = self.sorted_by(response['members'], 'hostname')[0]
167 self.assertEquals(host1['platform']['name'], 'myplatform')
168 self.assertEquals(host1['locked'], False)
169 self.assertEquals(host1['status'], 'Ready')
170
171
172 def test_entry(self):
173 response = self.request('get', 'hosts/host1')
174 self.assertEquals(response['protection_level'], 'No protection')
175
176
177 def test_labels(self):
jamesren3981f442010-02-16 19:27:59 +0000178 self.check_relationship('hosts/host1', 'labels', 'label', 'name',
179 ['label1', 'myplatform'])
showardf828c772010-01-25 21:49:42 +0000180
181
182 def test_acls(self):
jamesren3981f442010-02-16 19:27:59 +0000183 self.check_relationship('hosts/host1', 'acls', 'acl', 'name',
184 ['my_acl'])
showardf828c772010-01-25 21:49:42 +0000185
186
187 def test_queue_entries(self):
188 self._create_job(hosts=[1])
jamesren3981f442010-02-16 19:27:59 +0000189 host = self.request('get', 'hosts/host1')
190 entries = self.request('get', host['queue_entries']['href'])
191 self.check_collection(entries, ['job', 'id'], [1])
showardf828c772010-01-25 21:49:42 +0000192
193
194 def test_health_tasks(self):
195 models.SpecialTask.schedule_special_task(
196 host=self.hosts[0], task=models.SpecialTask.Task.VERIFY)
jamesren3981f442010-02-16 19:27:59 +0000197 host = self.request('get', 'hosts/host1')
198 tasks = self.request('get', host['health_tasks']['href'])
199 self.check_collection(tasks, 'task_type', ['Verify'])
showardf828c772010-01-25 21:49:42 +0000200
201
202 def test_put(self):
203 response = self.request('put', 'hosts/host1', data={'locked': True})
204 self.assertEquals(response['locked'], True)
205 response = self.request('get', 'hosts/host1')
206 self.assertEquals(response['locked'], True)
207 self.assertEquals(response['locked_by']['username'], 'debug_user')
208
209
210 def test_post(self):
211 data = {'hostname': 'newhost',
showardf828c772010-01-25 21:49:42 +0000212 'platform': {'href': self.URI_PREFIX + '/labels/myplatform'},
213 'protection_level': 'Do not verify'}
214 response = self.request('post', 'hosts', data=data)
215 self.assertEquals(response, self.URI_PREFIX + '/hosts/newhost')
216
217 host = models.Host.objects.get(hostname='newhost')
218 self.assertEquals(host.platform().name, 'myplatform')
219 self.assertEquals(host.protection, models.Host.Protection.DO_NOT_VERIFY)
jamesren3981f442010-02-16 19:27:59 +0000220
221
222 def _check_labels(self, host, expected_labels):
223 label_names = sorted(label.name for label in host.labels.all())
224 self.assertEquals(label_names, sorted(expected_labels))
showardf828c772010-01-25 21:49:42 +0000225
226
227 def test_add_label(self):
jamesren3981f442010-02-16 19:27:59 +0000228 labels_href = self.request('get', 'hosts/host1')['labels']['href']
229 data = {'label': self.URI_PREFIX + '/labels/label2'}
230 response = self.request('post', labels_href, data=data)
231 self._check_labels(self.hosts[0], ['label1', 'label2', 'myplatform'])
232
233
234 def test_remove_label(self):
235 labels_href = self.request('get', 'hosts/host1')['labels']['href']
236 labels_href += '&label=label1'
237 labelings = self.request('get', labels_href)['members']
238 self.assertEquals(len(labelings), 1)
239 self.request('delete', labelings[0]['href'])
240 self._check_labels(self.hosts[0], ['myplatform'])
showardf828c772010-01-25 21:49:42 +0000241
242
243 def test_delete(self):
244 self.request('delete', 'hosts/host1')
245 hosts = models.Host.valid_objects.filter(hostname='host1')
246 self.assertEquals(len(hosts), 0)
247
248
jamesrencd7a81a2010-04-21 20:39:08 +0000249class TestTest(AfeResourceTestCase): # yes, we're testing the "tests" resource
showardf828c772010-01-25 21:49:42 +0000250 def test_collection(self):
251 response = self.request('get', 'tests')
jamesren3981f442010-02-16 19:27:59 +0000252 self.check_collection(response, 'name', ['mytest'])
showardf828c772010-01-25 21:49:42 +0000253
254
255 def test_entry(self):
256 response = self.request('get', 'tests/mytest')
257 self.assertEquals(response['name'], 'mytest')
258 self.assertEquals(response['control_file_type'], 'Server')
259 self.assertEquals(response['control_file_path'], '/path/to/mytest')
260
261
262 def test_dependencies(self):
263 models.Test.objects.get(name='mytest').dependency_labels = [self.label3]
jamesren3981f442010-02-16 19:27:59 +0000264 self.check_relationship('tests/mytest', 'dependencies', 'label', 'name',
265 ['label3'])
showardf828c772010-01-25 21:49:42 +0000266
267
jamesrencd7a81a2010-04-21 20:39:08 +0000268class ExecutionInfoTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000269 def setUp(self):
270 super(ExecutionInfoTest, self).setUp()
271
272 def mock_read_control_file(test):
273 return self.CONTROL_FILE_CONTENTS
274 self.god.stub_with(control_file, 'read_control_file',
275 mock_read_control_file)
276 def test_get(self):
277 response = self.request('get', 'execution_info?tests=mytest')
278 info = response['execution_info']
279 self.assert_(self.CONTROL_FILE_CONTENTS in info['control_file'])
280 self.assertEquals(info['is_server'], True)
281 self.assertEquals(info['machines_per_execution'], 1)
282
283
jamesrencd7a81a2010-04-21 20:39:08 +0000284class QueueEntriesRequestTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000285 def test_get(self):
286 response = self.request(
287 'get',
288 'queue_entries_request?hosts=host1,host2&meta_hosts=label1')
289
290 # choose an arbitrary but consistent ordering to ease checking
291 def entry_href(entry):
292 if 'host' in entry:
293 return entry['host']['href']
294 return entry['meta_host']['href']
295 entries = sorted(response['queue_entries'], key=entry_href)
296
297 expected = [
298 {'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
299 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}},
300 {'meta_host': {'href': self.URI_PREFIX + '/labels/label1'}}]
301 self.assertEquals(entries, expected)
302
303
jamesrencd7a81a2010-04-21 20:39:08 +0000304class JobTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000305 def setUp(self):
306 super(JobTest, self).setUp()
307
308 for _ in xrange(2):
309 self._create_job(hosts=[1, 2])
310
311 job = models.Job.objects.get(id=1)
312 job.control_file = self.CONTROL_FILE_CONTENTS
313 job.save()
314
jamesrencd7a81a2010-04-21 20:39:08 +0000315 models.JobKeyval.objects.create(job=job, key='mykey', value='myvalue')
316
showardf828c772010-01-25 21:49:42 +0000317
318 def test_collection(self):
319 response = self.request('get', 'jobs')
jamesren3981f442010-02-16 19:27:59 +0000320 self.check_collection(response, 'id', [1, 2])
showardf828c772010-01-25 21:49:42 +0000321
322
jamesrencd7a81a2010-04-21 20:39:08 +0000323 def test_keyval_filtering(self):
324 response = self.request('get', 'jobs?has_keyval=mykey=myvalue')
325 self.check_collection(response, 'id', [1])
326
327
showardf828c772010-01-25 21:49:42 +0000328 def test_entry(self):
329 response = self.request('get', 'jobs/1')
330 self.assertEquals(response['id'], 1)
331 self.assertEquals(response['name'], 'test')
jamesrencd7a81a2010-04-21 20:39:08 +0000332 self.assertEquals(response['keyvals'], {'mykey': 'myvalue'})
showardf828c772010-01-25 21:49:42 +0000333 info = response['execution_info']
334 self.assertEquals(info['control_file'], self.CONTROL_FILE_CONTENTS)
335 self.assertEquals(info['is_server'], False)
336 self.assertEquals(info['cleanup_before_job'], 'Never')
337 self.assertEquals(info['cleanup_after_job'], 'Always')
338 self.assertEquals(info['machines_per_execution'], 1)
339 self.assertEquals(info['run_verify'], True)
340
341
342 def test_queue_entries(self):
jamesren3981f442010-02-16 19:27:59 +0000343 job = self.request('get', 'jobs/1')
344 entries = self.request('get', job['queue_entries']['href'])
345 self.check_collection(entries, ['host', 'hostname'], ['host1', 'host2'])
showardf828c772010-01-25 21:49:42 +0000346
347
jamesrene38a0a72010-04-19 18:05:31 +0000348 def _test_post_helper(self, owner):
showardf828c772010-01-25 21:49:42 +0000349 data = {'name': 'myjob',
350 'execution_info': {'control_file': self.CONTROL_FILE_CONTENTS,
351 'is_server': True},
jamesrene38a0a72010-04-19 18:05:31 +0000352 'owner': owner,
jamesren76fcf192010-04-21 20:39:50 +0000353 'drone_set': models.DroneSet.default_drone_set_name(),
showardf828c772010-01-25 21:49:42 +0000354 'queue_entries':
355 [{'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
356 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}}]}
357 response = self.request('post', 'jobs', data=data)
358 self.assertEquals(response, self.URI_PREFIX + '/jobs/3')
359 job = models.Job.objects.get(id=3)
360 self.assertEquals(job.name, 'myjob')
361 self.assertEquals(job.control_file, self.CONTROL_FILE_CONTENTS)
362 self.assertEquals(job.control_type, models.Job.ControlType.SERVER)
363 entries = job.hostqueueentry_set.order_by('host__hostname')
364 self.assertEquals(entries[0].host.hostname, 'host1')
365 self.assertEquals(entries[1].host.hostname, 'host2')
366
jamesrene38a0a72010-04-19 18:05:31 +0000367 owner_test = owner
368 if not owner_test:
369 owner_test = models.User.current_user().login
370 self.assertEquals(job.owner, owner_test)
371
372
373 def test_post_no_owner(self):
374 self._test_post_helper(None)
375
376
377 def test_post_with_owner(self):
378 self._test_post_helper('job_owner')
379
showardf828c772010-01-25 21:49:42 +0000380
jamesrencd7a81a2010-04-21 20:39:08 +0000381class DirectoryTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000382 def test_get(self):
383 response = self.request('get', '')
384 for key in ('atomic_group_classes', 'labels', 'users', 'acl_groups',
385 'hosts', 'tests', 'jobs', 'execution_info',
386 'queue_entries_request'):
387 self.assert_(key in response)
388
389
390if __name__ == '__main__':
391 unittest.main()