blob: 1f679ee8b3c2a4521b632b71e7c2d0119911f6b9 [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
Aviv Keshet3dd8beb2013-05-13 17:36:04 -070018from autotest_lib.client.common_lib import control_data
showardf828c772010-01-25 21:49:42 +000019
jamesrencd7a81a2010-04-21 20:39:08 +000020class AfeResourceTestCase(resource_test_utils.ResourceTestCase):
showardf46ad4c2010-02-03 20:28:59 +000021 URI_PREFIX = 'http://testserver/afe/server/resources'
showardf828c772010-01-25 21:49:42 +000022
23 CONTROL_FILE_CONTENTS = 'my control file contents'
24
25 def setUp(self):
jamesrencd7a81a2010-04-21 20:39:08 +000026 super(AfeResourceTestCase, self).setUp()
showardf828c772010-01-25 21:49:42 +000027 self._add_additional_data()
showardf828c772010-01-25 21:49:42 +000028
29
30 def _add_additional_data(self):
31 models.Test.objects.create(name='mytest',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -070032 test_type=control_data.CONTROL_TYPE.SERVER,
showardf828c772010-01-25 21:49:42 +000033 path='/path/to/mytest')
34
35
jamesrencd7a81a2010-04-21 20:39:08 +000036class FilteringPagingTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000037 # we'll arbitarily choose to use hosts for this
38
39 def setUp(self):
40 super(FilteringPagingTest, self).setUp()
41
42 self.labels[0].host_set = [self.hosts[0], self.hosts[1]]
43 for host in self.hosts[:3]:
44 host.locked = True
45 host.save()
46
47 def test_simple_filtering(self):
48 response = self.request('get', 'hosts?locked=true&has_label=label1')
jamesren3981f442010-02-16 19:27:59 +000049 self.check_collection(response, 'hostname', ['host1', 'host2'])
showardf828c772010-01-25 21:49:42 +000050
51
jamesrencd7a81a2010-04-21 20:39:08 +000052 def test_in_filtering(self):
53 response = self.request('get', 'hosts?hostname:in=host1,host2')
54 self.check_collection(response, 'hostname', ['host1', 'host2'])
55
56
showardf828c772010-01-25 21:49:42 +000057 def test_paging(self):
58 response = self.request('get', 'hosts?start_index=1&items_per_page=2')
jamesren3981f442010-02-16 19:27:59 +000059 self.check_collection(response, 'hostname', ['host2', 'host3'])
showardf828c772010-01-25 21:49:42 +000060 self.assertEquals(response['total_results'], 9)
61 self.assertEquals(response['items_per_page'], 2)
62 self.assertEquals(response['start_index'], 1)
63
64
jamesrencd7a81a2010-04-21 20:39:08 +000065 def test_full_representations(self):
66 response = self.request(
67 'get', 'hosts?hostname=host1&full_representations=true')
68 self.check_collection(response, 'hostname', ['host1'])
69 host = response['members'][0]
70 # invalid only included in full representation
71 self.assertEquals(host['invalid'], False)
72
73
74class MiscellaneousTest(AfeResourceTestCase):
jamesren3981f442010-02-16 19:27:59 +000075 def test_trailing_slash(self):
76 response = self.request('get', 'hosts/host1/')
77 self.assertEquals(response['hostname'], 'host1')
78
79
jamesrencd7a81a2010-04-21 20:39:08 +000080class AtomicGroupClassTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000081 def test_collection(self):
82 response = self.request('get', 'atomic_group_classes')
jamesren3981f442010-02-16 19:27:59 +000083 self.check_collection(response, 'name', ['atomic1', 'atomic2'],
84 length=2)
showardf828c772010-01-25 21:49:42 +000085
86
87 def test_entry(self):
88 response = self.request('get', 'atomic_group_classes/atomic1')
89 self.assertEquals(response['name'], 'atomic1')
90 self.assertEquals(response['max_number_of_machines'], 2)
91
92
93 def test_labels(self):
jamesren3981f442010-02-16 19:27:59 +000094 self.check_relationship('atomic_group_classes/atomic1', 'labels',
95 'label', 'name', ['label4', 'label5'])
showardf828c772010-01-25 21:49:42 +000096
97
jamesrencd7a81a2010-04-21 20:39:08 +000098class LabelTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +000099 def test_collection(self):
100 response = self.request('get', 'labels')
jamesren3981f442010-02-16 19:27:59 +0000101 self.check_collection(response, 'name', ['label1', 'label2'], length=9,
102 check_number=2)
showardf828c772010-01-25 21:49:42 +0000103 label1 = self.sorted_by(response['members'], 'name')[0]
104 self.assertEquals(label1['is_platform'], False)
105
106
107 def test_entry(self):
108 response = self.request('get', 'labels/label1')
109 self.assertEquals(response['name'], 'label1')
110 self.assertEquals(response['is_platform'], False)
111 self.assertEquals(response['atomic_group_class'], None)
112
113
114 def test_hosts(self):
jamesren3981f442010-02-16 19:27:59 +0000115 self.check_relationship('labels/label1', 'hosts', 'host', 'hostname',
116 ['host1'])
showardf828c772010-01-25 21:49:42 +0000117
118
jamesrencd7a81a2010-04-21 20:39:08 +0000119class UserTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000120 def test_collection(self):
121 response = self.request('get', 'users')
jamesren3981f442010-02-16 19:27:59 +0000122 self.check_collection(response, 'username',
showardf828c772010-01-25 21:49:42 +0000123 ['autotest_system', 'debug_user'])
124
125
126 def test_entry(self):
127 response = self.request('get', 'users/debug_user')
128 self.assertEquals(response['username'], 'debug_user')
129
130 me_response = self.request('get', 'users/@me')
131 self.assertEquals(response, me_response)
132
133
134 def test_acls(self):
jamesren3981f442010-02-16 19:27:59 +0000135 self.check_relationship('users/debug_user', 'acls', 'acl', 'name',
136 ['Everyone', 'my_acl'])
showardf828c772010-01-25 21:49:42 +0000137
138
139 def test_accessible_hosts(self):
140 group = models.AclGroup.objects.create(name='mygroup')
141 models.User.objects.get(login='debug_user').aclgroup_set = [group]
142 self.hosts[0].aclgroup_set = [group]
143
jamesren3981f442010-02-16 19:27:59 +0000144 user = self.request('get', 'users/debug_user')
145 response = self.request('get', user['accessible_hosts']['href'])
146 self.check_collection(response, 'hostname', ['host1'])
showardf828c772010-01-25 21:49:42 +0000147
148
jamesrencd7a81a2010-04-21 20:39:08 +0000149class AclTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000150 def test_collection(self):
151 response = self.request('get', 'acls')
jamesren3981f442010-02-16 19:27:59 +0000152 self.check_collection(response, 'name', ['Everyone', 'my_acl'])
showardf828c772010-01-25 21:49:42 +0000153
154
155 def test_entry(self):
156 response = self.request('get', 'acls/my_acl')
157 self.assertEquals(response['name'], 'my_acl')
158
159
160 def test_users(self):
jamesren3981f442010-02-16 19:27:59 +0000161 self.check_relationship('acls/my_acl', 'users', 'user', 'username',
162 ['autotest_system', 'debug_user'])
showardf828c772010-01-25 21:49:42 +0000163
164
165 def test_hosts(self):
jamesren3981f442010-02-16 19:27:59 +0000166 self.check_relationship('acls/my_acl', 'hosts', 'host', 'hostname',
167 ['host1', 'host2'], length=9, check_number=2)
showardf828c772010-01-25 21:49:42 +0000168
169
jamesrencd7a81a2010-04-21 20:39:08 +0000170class HostTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000171 def test_collection(self):
172 response = self.request('get', 'hosts')
jamesren3981f442010-02-16 19:27:59 +0000173 self.check_collection(response, 'hostname', ['host1', 'host2'],
174 length=9, check_number=2)
showardf828c772010-01-25 21:49:42 +0000175 host1 = self.sorted_by(response['members'], 'hostname')[0]
176 self.assertEquals(host1['platform']['name'], 'myplatform')
177 self.assertEquals(host1['locked'], False)
178 self.assertEquals(host1['status'], 'Ready')
179
180
181 def test_entry(self):
182 response = self.request('get', 'hosts/host1')
183 self.assertEquals(response['protection_level'], 'No protection')
184
185
186 def test_labels(self):
jamesren3981f442010-02-16 19:27:59 +0000187 self.check_relationship('hosts/host1', 'labels', 'label', 'name',
188 ['label1', 'myplatform'])
showardf828c772010-01-25 21:49:42 +0000189
190
191 def test_acls(self):
jamesren3981f442010-02-16 19:27:59 +0000192 self.check_relationship('hosts/host1', 'acls', 'acl', 'name',
193 ['my_acl'])
showardf828c772010-01-25 21:49:42 +0000194
195
196 def test_queue_entries(self):
197 self._create_job(hosts=[1])
jamesren3981f442010-02-16 19:27:59 +0000198 host = self.request('get', 'hosts/host1')
199 entries = self.request('get', host['queue_entries']['href'])
200 self.check_collection(entries, ['job', 'id'], [1])
showardf828c772010-01-25 21:49:42 +0000201
202
203 def test_health_tasks(self):
204 models.SpecialTask.schedule_special_task(
205 host=self.hosts[0], task=models.SpecialTask.Task.VERIFY)
jamesren3981f442010-02-16 19:27:59 +0000206 host = self.request('get', 'hosts/host1')
207 tasks = self.request('get', host['health_tasks']['href'])
208 self.check_collection(tasks, 'task_type', ['Verify'])
showardf828c772010-01-25 21:49:42 +0000209
210
211 def test_put(self):
212 response = self.request('put', 'hosts/host1', data={'locked': True})
213 self.assertEquals(response['locked'], True)
214 response = self.request('get', 'hosts/host1')
215 self.assertEquals(response['locked'], True)
216 self.assertEquals(response['locked_by']['username'], 'debug_user')
217
218
219 def test_post(self):
220 data = {'hostname': 'newhost',
showardf828c772010-01-25 21:49:42 +0000221 'platform': {'href': self.URI_PREFIX + '/labels/myplatform'},
222 'protection_level': 'Do not verify'}
223 response = self.request('post', 'hosts', data=data)
224 self.assertEquals(response, self.URI_PREFIX + '/hosts/newhost')
225
226 host = models.Host.objects.get(hostname='newhost')
227 self.assertEquals(host.platform().name, 'myplatform')
228 self.assertEquals(host.protection, models.Host.Protection.DO_NOT_VERIFY)
jamesren3981f442010-02-16 19:27:59 +0000229
230
231 def _check_labels(self, host, expected_labels):
232 label_names = sorted(label.name for label in host.labels.all())
233 self.assertEquals(label_names, sorted(expected_labels))
showardf828c772010-01-25 21:49:42 +0000234
235
236 def test_add_label(self):
jamesren3981f442010-02-16 19:27:59 +0000237 labels_href = self.request('get', 'hosts/host1')['labels']['href']
238 data = {'label': self.URI_PREFIX + '/labels/label2'}
239 response = self.request('post', labels_href, data=data)
240 self._check_labels(self.hosts[0], ['label1', 'label2', 'myplatform'])
241
242
243 def test_remove_label(self):
244 labels_href = self.request('get', 'hosts/host1')['labels']['href']
245 labels_href += '&label=label1'
246 labelings = self.request('get', labels_href)['members']
247 self.assertEquals(len(labelings), 1)
248 self.request('delete', labelings[0]['href'])
249 self._check_labels(self.hosts[0], ['myplatform'])
showardf828c772010-01-25 21:49:42 +0000250
251
252 def test_delete(self):
253 self.request('delete', 'hosts/host1')
254 hosts = models.Host.valid_objects.filter(hostname='host1')
255 self.assertEquals(len(hosts), 0)
256
257
jamesrencd7a81a2010-04-21 20:39:08 +0000258class TestTest(AfeResourceTestCase): # yes, we're testing the "tests" resource
showardf828c772010-01-25 21:49:42 +0000259 def test_collection(self):
260 response = self.request('get', 'tests')
jamesren3981f442010-02-16 19:27:59 +0000261 self.check_collection(response, 'name', ['mytest'])
showardf828c772010-01-25 21:49:42 +0000262
263
264 def test_entry(self):
265 response = self.request('get', 'tests/mytest')
266 self.assertEquals(response['name'], 'mytest')
267 self.assertEquals(response['control_file_type'], 'Server')
268 self.assertEquals(response['control_file_path'], '/path/to/mytest')
269
270
271 def test_dependencies(self):
272 models.Test.objects.get(name='mytest').dependency_labels = [self.label3]
jamesren3981f442010-02-16 19:27:59 +0000273 self.check_relationship('tests/mytest', 'dependencies', 'label', 'name',
274 ['label3'])
showardf828c772010-01-25 21:49:42 +0000275
276
jamesrencd7a81a2010-04-21 20:39:08 +0000277class ExecutionInfoTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000278 def setUp(self):
279 super(ExecutionInfoTest, self).setUp()
280
281 def mock_read_control_file(test):
282 return self.CONTROL_FILE_CONTENTS
283 self.god.stub_with(control_file, 'read_control_file',
284 mock_read_control_file)
285 def test_get(self):
286 response = self.request('get', 'execution_info?tests=mytest')
287 info = response['execution_info']
288 self.assert_(self.CONTROL_FILE_CONTENTS in info['control_file'])
289 self.assertEquals(info['is_server'], True)
290 self.assertEquals(info['machines_per_execution'], 1)
291
292
jamesrencd7a81a2010-04-21 20:39:08 +0000293class QueueEntriesRequestTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000294 def test_get(self):
295 response = self.request(
296 'get',
297 'queue_entries_request?hosts=host1,host2&meta_hosts=label1')
298
299 # choose an arbitrary but consistent ordering to ease checking
300 def entry_href(entry):
301 if 'host' in entry:
302 return entry['host']['href']
303 return entry['meta_host']['href']
304 entries = sorted(response['queue_entries'], key=entry_href)
305
306 expected = [
307 {'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
308 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}},
309 {'meta_host': {'href': self.URI_PREFIX + '/labels/label1'}}]
310 self.assertEquals(entries, expected)
311
312
jamesrencd7a81a2010-04-21 20:39:08 +0000313class JobTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000314 def setUp(self):
315 super(JobTest, self).setUp()
316
317 for _ in xrange(2):
318 self._create_job(hosts=[1, 2])
319
320 job = models.Job.objects.get(id=1)
321 job.control_file = self.CONTROL_FILE_CONTENTS
322 job.save()
323
jamesrencd7a81a2010-04-21 20:39:08 +0000324 models.JobKeyval.objects.create(job=job, key='mykey', value='myvalue')
325
showardf828c772010-01-25 21:49:42 +0000326
327 def test_collection(self):
328 response = self.request('get', 'jobs')
jamesren3981f442010-02-16 19:27:59 +0000329 self.check_collection(response, 'id', [1, 2])
showardf828c772010-01-25 21:49:42 +0000330
331
Aviv Keshet1f23b692013-05-14 11:13:55 -0700332# def test_keyval_filtering(self):
333# response = self.request('get', 'jobs?has_keyval=mykey=myvalue')
334# self.check_collection(response, 'id', [1])
jamesrencd7a81a2010-04-21 20:39:08 +0000335
336
showardf828c772010-01-25 21:49:42 +0000337 def test_entry(self):
338 response = self.request('get', 'jobs/1')
339 self.assertEquals(response['id'], 1)
340 self.assertEquals(response['name'], 'test')
jamesrencd7a81a2010-04-21 20:39:08 +0000341 self.assertEquals(response['keyvals'], {'mykey': 'myvalue'})
showardf828c772010-01-25 21:49:42 +0000342 info = response['execution_info']
343 self.assertEquals(info['control_file'], self.CONTROL_FILE_CONTENTS)
344 self.assertEquals(info['is_server'], False)
345 self.assertEquals(info['cleanup_before_job'], 'Never')
346 self.assertEquals(info['cleanup_after_job'], 'Always')
347 self.assertEquals(info['machines_per_execution'], 1)
348 self.assertEquals(info['run_verify'], True)
349
350
351 def test_queue_entries(self):
jamesren3981f442010-02-16 19:27:59 +0000352 job = self.request('get', 'jobs/1')
353 entries = self.request('get', job['queue_entries']['href'])
354 self.check_collection(entries, ['host', 'hostname'], ['host1', 'host2'])
showardf828c772010-01-25 21:49:42 +0000355
356
jamesrene38a0a72010-04-19 18:05:31 +0000357 def _test_post_helper(self, owner):
showardf828c772010-01-25 21:49:42 +0000358 data = {'name': 'myjob',
359 'execution_info': {'control_file': self.CONTROL_FILE_CONTENTS,
360 'is_server': True},
jamesrene38a0a72010-04-19 18:05:31 +0000361 'owner': owner,
jamesren76fcf192010-04-21 20:39:50 +0000362 'drone_set': models.DroneSet.default_drone_set_name(),
showardf828c772010-01-25 21:49:42 +0000363 'queue_entries':
364 [{'host': {'href': self.URI_PREFIX + '/hosts/host1'}},
365 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}}]}
366 response = self.request('post', 'jobs', data=data)
367 self.assertEquals(response, self.URI_PREFIX + '/jobs/3')
368 job = models.Job.objects.get(id=3)
369 self.assertEquals(job.name, 'myjob')
370 self.assertEquals(job.control_file, self.CONTROL_FILE_CONTENTS)
Aviv Keshet3dd8beb2013-05-13 17:36:04 -0700371 self.assertEquals(job.control_type, control_data.CONTROL_TYPE.SERVER)
showardf828c772010-01-25 21:49:42 +0000372 entries = job.hostqueueentry_set.order_by('host__hostname')
373 self.assertEquals(entries[0].host.hostname, 'host1')
374 self.assertEquals(entries[1].host.hostname, 'host2')
375
jamesrene38a0a72010-04-19 18:05:31 +0000376 owner_test = owner
377 if not owner_test:
378 owner_test = models.User.current_user().login
379 self.assertEquals(job.owner, owner_test)
380
381
382 def test_post_no_owner(self):
383 self._test_post_helper(None)
384
385
386 def test_post_with_owner(self):
387 self._test_post_helper('job_owner')
388
showardf828c772010-01-25 21:49:42 +0000389
jamesrencd7a81a2010-04-21 20:39:08 +0000390class DirectoryTest(AfeResourceTestCase):
showardf828c772010-01-25 21:49:42 +0000391 def test_get(self):
392 response = self.request('get', '')
393 for key in ('atomic_group_classes', 'labels', 'users', 'acl_groups',
394 'hosts', 'tests', 'jobs', 'execution_info',
395 'queue_entries_request'):
396 self.assert_(key in response)
397
398
399if __name__ == '__main__':
400 unittest.main()