blob: 8849bcaf2537df5f138b25620dcaf4e07bc0ee3e [file] [log] [blame]
showardf828c772010-01-25 21:49:42 +00001#!/usr/bin/python
2
3import common
jamesrencd7a81a2010-04-21 20:39:08 +00004import unittest
Eric Li8a12e802011-02-17 14:24:13 -08005
6# This has to be done very early.
7from autotest_lib.client.common_lib import global_config
8global_config.global_config.override_config_value(
9 'HOSTS', 'default_protection',
10 'NO_PROTECTION')
11from autotest_lib.client.common_lib import host_protections
12
showardf828c772010-01-25 21:49:42 +000013from autotest_lib.frontend import setup_django_environment
14from autotest_lib.frontend import setup_test_environment
15from django.test import client
jamesrencd7a81a2010-04-21 20:39:08 +000016from autotest_lib.frontend.shared import resource_test_utils
17from autotest_lib.frontend.afe import control_file, models, model_attributes
showardf828c772010-01-25 21:49:42 +000018
jamesrencd7a81a2010-04-21 20:39:08 +000019class AfeResourceTestCase(resource_test_utils.ResourceTestCase):
showardf46ad4c2010-02-03 20:28:59 +000020 URI_PREFIX = 'http://testserver/afe/server/resources'
showardf828c772010-01-25 21:49:42 +000021
22 CONTROL_FILE_CONTENTS = 'my control file contents'
23
24 def setUp(self):
jamesrencd7a81a2010-04-21 20:39:08 +000025 super(AfeResourceTestCase, self).setUp()
showardf828c772010-01-25 21:49:42 +000026 self._add_additional_data()
showardf828c772010-01-25 21:49:42 +000027
28
29 def _add_additional_data(self):
30 models.Test.objects.create(name='mytest',
jamesrendd855242010-03-02 22:23:44 +000031 test_type=model_attributes.TestTypes.SERVER,
showardf828c772010-01-25 21:49:42 +000032 path='/path/to/mytest')
33
34
jamesrencd7a81a2010-04-21 20:39:08 +000035class FilteringPagingTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000036 # we'll arbitarily choose to use hosts for this
37
38 def setUp(self):
39 super(FilteringPagingTest, self).setUp()
40
41 self.labels[0].host_set = [self.hosts[0], self.hosts[1]]
42 for host in self.hosts[:3]:
43 host.locked = True
44 host.save()
45
46 def test_simple_filtering(self):
47 response = self.request('get', 'hosts?locked=true&has_label=label1')
jamesren3981f442010-02-16 19:27:59 +000048 self.check_collection(response, 'hostname', ['host1', 'host2'])
showardf828c772010-01-25 21:49:42 +000049
50
jamesrencd7a81a2010-04-21 20:39:08 +000051 def test_in_filtering(self):
52 response = self.request('get', 'hosts?hostname:in=host1,host2')
53 self.check_collection(response, 'hostname', ['host1', 'host2'])
54
55
showardf828c772010-01-25 21:49:42 +000056 def test_paging(self):
57 response = self.request('get', 'hosts?start_index=1&items_per_page=2')
jamesren3981f442010-02-16 19:27:59 +000058 self.check_collection(response, 'hostname', ['host2', 'host3'])
showardf828c772010-01-25 21:49:42 +000059 self.assertEquals(response['total_results'], 9)
60 self.assertEquals(response['items_per_page'], 2)
61 self.assertEquals(response['start_index'], 1)
62
63
jamesrencd7a81a2010-04-21 20:39:08 +000064 def test_full_representations(self):
65 response = self.request(
66 'get', 'hosts?hostname=host1&full_representations=true')
67 self.check_collection(response, 'hostname', ['host1'])
68 host = response['members'][0]
69 # invalid only included in full representation
70 self.assertEquals(host['invalid'], False)
71
72
73class MiscellaneousTest(AfeResourceTestCase):
jamesren3981f442010-02-16 19:27:59 +000074 def test_trailing_slash(self):
75 response = self.request('get', 'hosts/host1/')
76 self.assertEquals(response['hostname'], 'host1')
77
78
jamesrencd7a81a2010-04-21 20:39:08 +000079class AtomicGroupClassTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000080 def test_collection(self):
81 response = self.request('get', 'atomic_group_classes')
jamesren3981f442010-02-16 19:27:59 +000082 self.check_collection(response, 'name', ['atomic1', 'atomic2'],
83 length=2)
showardf828c772010-01-25 21:49:42 +000084
85
86 def test_entry(self):
87 response = self.request('get', 'atomic_group_classes/atomic1')
88 self.assertEquals(response['name'], 'atomic1')
89 self.assertEquals(response['max_number_of_machines'], 2)
90
91
92 def test_labels(self):
jamesren3981f442010-02-16 19:27:59 +000093 self.check_relationship('atomic_group_classes/atomic1', 'labels',
94 'label', 'name', ['label4', 'label5'])
showardf828c772010-01-25 21:49:42 +000095
96
jamesrencd7a81a2010-04-21 20:39:08 +000097class LabelTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000098 def test_collection(self):
99 response = self.request('get', 'labels')
jamesren3981f442010-02-16 19:27:59 +0000100 self.check_collection(response, 'name', ['label1', 'label2'], length=9,
101 check_number=2)
showardf828c772010-01-25 21:49:42 +0000102 label1 = self.sorted_by(response['members'], 'name')[0]
103 self.assertEquals(label1['is_platform'], False)
104
105
106 def test_entry(self):
107 response = self.request('get', 'labels/label1')
108 self.assertEquals(response['name'], 'label1')
109 self.assertEquals(response['is_platform'], False)
110 self.assertEquals(response['atomic_group_class'], None)
111
112
113 def test_hosts(self):
jamesren3981f442010-02-16 19:27:59 +0000114 self.check_relationship('labels/label1', 'hosts', 'host', 'hostname',
115 ['host1'])
showardf828c772010-01-25 21:49:42 +0000116
117
jamesrencd7a81a2010-04-21 20:39:08 +0000118class UserTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000119 def test_collection(self):
120 response = self.request('get', 'users')
jamesren3981f442010-02-16 19:27:59 +0000121 self.check_collection(response, 'username',
showardf828c772010-01-25 21:49:42 +0000122 ['autotest_system', 'debug_user'])
123
124
125 def test_entry(self):
126 response = self.request('get', 'users/debug_user')
127 self.assertEquals(response['username'], 'debug_user')
128
129 me_response = self.request('get', 'users/@me')
130 self.assertEquals(response, me_response)
131
132
133 def test_acls(self):
jamesren3981f442010-02-16 19:27:59 +0000134 self.check_relationship('users/debug_user', 'acls', 'acl', 'name',
135 ['Everyone', 'my_acl'])
showardf828c772010-01-25 21:49:42 +0000136
137
138 def test_accessible_hosts(self):
139 group = models.AclGroup.objects.create(name='mygroup')
140 models.User.objects.get(login='debug_user').aclgroup_set = [group]
141 self.hosts[0].aclgroup_set = [group]
142
jamesren3981f442010-02-16 19:27:59 +0000143 user = self.request('get', 'users/debug_user')
144 response = self.request('get', user['accessible_hosts']['href'])
145 self.check_collection(response, 'hostname', ['host1'])
showardf828c772010-01-25 21:49:42 +0000146
147
jamesrencd7a81a2010-04-21 20:39:08 +0000148class AclTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000149 def test_collection(self):
150 response = self.request('get', 'acls')
jamesren3981f442010-02-16 19:27:59 +0000151 self.check_collection(response, 'name', ['Everyone', 'my_acl'])
showardf828c772010-01-25 21:49:42 +0000152
153
154 def test_entry(self):
155 response = self.request('get', 'acls/my_acl')
156 self.assertEquals(response['name'], 'my_acl')
157
158
159 def test_users(self):
jamesren3981f442010-02-16 19:27:59 +0000160 self.check_relationship('acls/my_acl', 'users', 'user', 'username',
161 ['autotest_system', 'debug_user'])
showardf828c772010-01-25 21:49:42 +0000162
163
164 def test_hosts(self):
jamesren3981f442010-02-16 19:27:59 +0000165 self.check_relationship('acls/my_acl', 'hosts', 'host', 'hostname',
166 ['host1', 'host2'], length=9, check_number=2)
showardf828c772010-01-25 21:49:42 +0000167
168
jamesrencd7a81a2010-04-21 20:39:08 +0000169class HostTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000170 def test_collection(self):
171 response = self.request('get', 'hosts')
jamesren3981f442010-02-16 19:27:59 +0000172 self.check_collection(response, 'hostname', ['host1', 'host2'],
173 length=9, check_number=2)
showardf828c772010-01-25 21:49:42 +0000174 host1 = self.sorted_by(response['members'], 'hostname')[0]
175 self.assertEquals(host1['platform']['name'], 'myplatform')
176 self.assertEquals(host1['locked'], False)
177 self.assertEquals(host1['status'], 'Ready')
178
179
180 def test_entry(self):
181 response = self.request('get', 'hosts/host1')
182 self.assertEquals(response['protection_level'], 'No protection')
183
184
185 def test_labels(self):
jamesren3981f442010-02-16 19:27:59 +0000186 self.check_relationship('hosts/host1', 'labels', 'label', 'name',
187 ['label1', 'myplatform'])
showardf828c772010-01-25 21:49:42 +0000188
189
190 def test_acls(self):
jamesren3981f442010-02-16 19:27:59 +0000191 self.check_relationship('hosts/host1', 'acls', 'acl', 'name',
192 ['my_acl'])
showardf828c772010-01-25 21:49:42 +0000193
194
195 def test_queue_entries(self):
196 self._create_job(hosts=[1])
jamesren3981f442010-02-16 19:27:59 +0000197 host = self.request('get', 'hosts/host1')
198 entries = self.request('get', host['queue_entries']['href'])
199 self.check_collection(entries, ['job', 'id'], [1])
showardf828c772010-01-25 21:49:42 +0000200
201
202 def test_health_tasks(self):
203 models.SpecialTask.schedule_special_task(
204 host=self.hosts[0], task=models.SpecialTask.Task.VERIFY)
jamesren3981f442010-02-16 19:27:59 +0000205 host = self.request('get', 'hosts/host1')
206 tasks = self.request('get', host['health_tasks']['href'])
207 self.check_collection(tasks, 'task_type', ['Verify'])
showardf828c772010-01-25 21:49:42 +0000208
209
210 def test_put(self):
211 response = self.request('put', 'hosts/host1', data={'locked': True})
212 self.assertEquals(response['locked'], True)
213 response = self.request('get', 'hosts/host1')
214 self.assertEquals(response['locked'], True)
215 self.assertEquals(response['locked_by']['username'], 'debug_user')
216
217
218 def test_post(self):
219 data = {'hostname': 'newhost',
showardf828c772010-01-25 21:49:42 +0000220 'platform': {'href': self.URI_PREFIX + '/labels/myplatform'},
221 'protection_level': 'Do not verify'}
222 response = self.request('post', 'hosts', data=data)
223 self.assertEquals(response, self.URI_PREFIX + '/hosts/newhost')
224
225 host = models.Host.objects.get(hostname='newhost')
226 self.assertEquals(host.platform().name, 'myplatform')
227 self.assertEquals(host.protection, models.Host.Protection.DO_NOT_VERIFY)
jamesren3981f442010-02-16 19:27:59 +0000228
229
230 def _check_labels(self, host, expected_labels):
231 label_names = sorted(label.name for label in host.labels.all())
232 self.assertEquals(label_names, sorted(expected_labels))
showardf828c772010-01-25 21:49:42 +0000233
234
235 def test_add_label(self):
jamesren3981f442010-02-16 19:27:59 +0000236 labels_href = self.request('get', 'hosts/host1')['labels']['href']
237 data = {'label': self.URI_PREFIX + '/labels/label2'}
238 response = self.request('post', labels_href, data=data)
239 self._check_labels(self.hosts[0], ['label1', 'label2', 'myplatform'])
240
241
242 def test_remove_label(self):
243 labels_href = self.request('get', 'hosts/host1')['labels']['href']
244 labels_href += '&label=label1'
245 labelings = self.request('get', labels_href)['members']
246 self.assertEquals(len(labelings), 1)
247 self.request('delete', labelings[0]['href'])
248 self._check_labels(self.hosts[0], ['myplatform'])
showardf828c772010-01-25 21:49:42 +0000249
250
251 def test_delete(self):
252 self.request('delete', 'hosts/host1')
253 hosts = models.Host.valid_objects.filter(hostname='host1')
254 self.assertEquals(len(hosts), 0)
255
256
jamesrencd7a81a2010-04-21 20:39:08 +0000257class TestTest(AfeResourceTestCase): # yes, we're testing the "tests" resource
showardf828c772010-01-25 21:49:42 +0000258 def test_collection(self):
259 response = self.request('get', 'tests')
jamesren3981f442010-02-16 19:27:59 +0000260 self.check_collection(response, 'name', ['mytest'])
showardf828c772010-01-25 21:49:42 +0000261
262
263 def test_entry(self):
264 response = self.request('get', 'tests/mytest')
265 self.assertEquals(response['name'], 'mytest')
266 self.assertEquals(response['control_file_type'], 'Server')
267 self.assertEquals(response['control_file_path'], '/path/to/mytest')
268
269
270 def test_dependencies(self):
271 models.Test.objects.get(name='mytest').dependency_labels = [self.label3]
jamesren3981f442010-02-16 19:27:59 +0000272 self.check_relationship('tests/mytest', 'dependencies', 'label', 'name',
273 ['label3'])
showardf828c772010-01-25 21:49:42 +0000274
275
jamesrencd7a81a2010-04-21 20:39:08 +0000276class ExecutionInfoTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000277 def setUp(self):
278 super(ExecutionInfoTest, self).setUp()
279
280 def mock_read_control_file(test):
281 return self.CONTROL_FILE_CONTENTS
282 self.god.stub_with(control_file, 'read_control_file',
283 mock_read_control_file)
284 def test_get(self):
285 response = self.request('get', 'execution_info?tests=mytest')
286 info = response['execution_info']
287 self.assert_(self.CONTROL_FILE_CONTENTS in info['control_file'])
288 self.assertEquals(info['is_server'], True)
289 self.assertEquals(info['machines_per_execution'], 1)
290
291
jamesrencd7a81a2010-04-21 20:39:08 +0000292class QueueEntriesRequestTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000293 def test_get(self):
294 response = self.request(
295 'get',
296 'queue_entries_request?hosts=host1,host2&meta_hosts=label1')
297
298 # choose an arbitrary but consistent ordering to ease checking
299 def entry_href(entry):
300 if 'host' in entry:
301 return entry['host']['href']
302 return entry['meta_host']['href']
303 entries = sorted(response['queue_entries'], key=entry_href)
304
305 expected = [
306 {'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
307 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}},
308 {'meta_host': {'href': self.URI_PREFIX + '/labels/label1'}}]
309 self.assertEquals(entries, expected)
310
311
jamesrencd7a81a2010-04-21 20:39:08 +0000312class JobTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000313 def setUp(self):
314 super(JobTest, self).setUp()
315
316 for _ in xrange(2):
317 self._create_job(hosts=[1, 2])
318
319 job = models.Job.objects.get(id=1)
320 job.control_file = self.CONTROL_FILE_CONTENTS
321 job.save()
322
jamesrencd7a81a2010-04-21 20:39:08 +0000323 models.JobKeyval.objects.create(job=job, key='mykey', value='myvalue')
324
showardf828c772010-01-25 21:49:42 +0000325
326 def test_collection(self):
327 response = self.request('get', 'jobs')
jamesren3981f442010-02-16 19:27:59 +0000328 self.check_collection(response, 'id', [1, 2])
showardf828c772010-01-25 21:49:42 +0000329
330
Aviv Keshet1f23b692013-05-14 11:13:55 -0700331# def test_keyval_filtering(self):
332# response = self.request('get', 'jobs?has_keyval=mykey=myvalue')
333# self.check_collection(response, 'id', [1])
jamesrencd7a81a2010-04-21 20:39:08 +0000334
335
showardf828c772010-01-25 21:49:42 +0000336 def test_entry(self):
337 response = self.request('get', 'jobs/1')
338 self.assertEquals(response['id'], 1)
339 self.assertEquals(response['name'], 'test')
jamesrencd7a81a2010-04-21 20:39:08 +0000340 self.assertEquals(response['keyvals'], {'mykey': 'myvalue'})
showardf828c772010-01-25 21:49:42 +0000341 info = response['execution_info']
342 self.assertEquals(info['control_file'], self.CONTROL_FILE_CONTENTS)
343 self.assertEquals(info['is_server'], False)
344 self.assertEquals(info['cleanup_before_job'], 'Never')
345 self.assertEquals(info['cleanup_after_job'], 'Always')
346 self.assertEquals(info['machines_per_execution'], 1)
347 self.assertEquals(info['run_verify'], True)
348
349
350 def test_queue_entries(self):
jamesren3981f442010-02-16 19:27:59 +0000351 job = self.request('get', 'jobs/1')
352 entries = self.request('get', job['queue_entries']['href'])
353 self.check_collection(entries, ['host', 'hostname'], ['host1', 'host2'])
showardf828c772010-01-25 21:49:42 +0000354
355
jamesrene38a0a72010-04-19 18:05:31 +0000356 def _test_post_helper(self, owner):
showardf828c772010-01-25 21:49:42 +0000357 data = {'name': 'myjob',
358 'execution_info': {'control_file': self.CONTROL_FILE_CONTENTS,
359 'is_server': True},
jamesrene38a0a72010-04-19 18:05:31 +0000360 'owner': owner,
jamesren76fcf192010-04-21 20:39:50 +0000361 'drone_set': models.DroneSet.default_drone_set_name(),
showardf828c772010-01-25 21:49:42 +0000362 'queue_entries':
363 [{'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
364 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}}]}
365 response = self.request('post', 'jobs', data=data)
366 self.assertEquals(response, self.URI_PREFIX + '/jobs/3')
367 job = models.Job.objects.get(id=3)
368 self.assertEquals(job.name, 'myjob')
369 self.assertEquals(job.control_file, self.CONTROL_FILE_CONTENTS)
370 self.assertEquals(job.control_type, models.Job.ControlType.SERVER)
371 entries = job.hostqueueentry_set.order_by('host__hostname')
372 self.assertEquals(entries[0].host.hostname, 'host1')
373 self.assertEquals(entries[1].host.hostname, 'host2')
374
jamesrene38a0a72010-04-19 18:05:31 +0000375 owner_test = owner
376 if not owner_test:
377 owner_test = models.User.current_user().login
378 self.assertEquals(job.owner, owner_test)
379
380
381 def test_post_no_owner(self):
382 self._test_post_helper(None)
383
384
385 def test_post_with_owner(self):
386 self._test_post_helper('job_owner')
387
showardf828c772010-01-25 21:49:42 +0000388
jamesrencd7a81a2010-04-21 20:39:08 +0000389class DirectoryTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000390 def test_get(self):
391 response = self.request('get', '')
392 for key in ('atomic_group_classes', 'labels', 'users', 'acl_groups',
393 'hosts', 'tests', 'jobs', 'execution_info',
394 'queue_entries_request'):
395 self.assert_(key in response)
396
397
398if __name__ == '__main__':
399 unittest.main()