blob: 1463077e913b9d14e90989f8fcffc08eed30749e [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#! /usr/bin/env python
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# BitBake Toaster Implementation
6#
7# Copyright (C) 2013-2015 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22"""Test cases for Toaster GUI and ReST."""
23
24from django.test import TestCase
25from django.test.client import RequestFactory
26from django.core.urlresolvers import reverse
27from django.db.models import Q
28
29from orm.models import Project, Package
30from orm.models import Layer_Version, Recipe
31from orm.models import CustomImageRecipe
32from orm.models import CustomImagePackage
33
34import inspect
35import toastergui
36
37from toastergui.tables import SoftwareRecipesTable
38import json
39from bs4 import BeautifulSoup
40import string
41
42PROJECT_NAME = "test project"
43PROJECT_NAME2 = "test project 2"
44CLI_BUILDS_PROJECT_NAME = 'Command line builds'
45
46
47class ViewTests(TestCase):
48 """Tests to verify view APIs."""
49
50 fixtures = ['toastergui-unittest-data']
51
52 def setUp(self):
53
54 self.project = Project.objects.first()
55 self.recipe1 = Recipe.objects.get(pk=2)
56 self.customr = CustomImageRecipe.objects.first()
57 self.cust_package = CustomImagePackage.objects.first()
58 self.package = Package.objects.first()
59 self.lver = Layer_Version.objects.first()
60
61 def test_get_base_call_returns_html(self):
62 """Basic test for all-projects view"""
63 response = self.client.get(reverse('all-projects'), follow=True)
64 self.assertEqual(response.status_code, 200)
65 self.assertTrue(response['Content-Type'].startswith('text/html'))
66 self.assertTemplateUsed(response, "projects-toastertable.html")
67
68 def test_get_json_call_returns_json(self):
69 """Test for all projects output in json format"""
70 url = reverse('all-projects')
71 response = self.client.get(url, {"format": "json"}, follow=True)
72 self.assertEqual(response.status_code, 200)
73 self.assertTrue(response['Content-Type'].startswith(
74 'application/json'))
75
76 data = json.loads(response.content.decode('utf-8'))
77
78 self.assertTrue("error" in data)
79 self.assertEqual(data["error"], "ok")
80 self.assertTrue("rows" in data)
81
82 name_found = False
83 for row in data["rows"]:
84 name_found = row['name'].find(self.project.name)
85
86 self.assertTrue(name_found,
87 "project name not found in projects table")
88
89 def test_typeaheads(self):
90 """Test typeahead ReST API"""
91 layers_url = reverse('xhr_layerstypeahead', args=(self.project.id,))
92 prj_url = reverse('xhr_projectstypeahead')
93
94 urls = [layers_url,
95 prj_url,
96 reverse('xhr_recipestypeahead', args=(self.project.id,)),
97 reverse('xhr_machinestypeahead', args=(self.project.id,))]
98
99 def basic_reponse_check(response, url):
100 """Check data structure of http response."""
101 self.assertEqual(response.status_code, 200)
102 self.assertTrue(response['Content-Type'].startswith(
103 'application/json'))
104
105 data = json.loads(response.content.decode('utf-8'))
106
107 self.assertTrue("error" in data)
108 self.assertEqual(data["error"], "ok")
109 self.assertTrue("results" in data)
110
111 # We got a result so now check the fields
112 if len(data['results']) > 0:
113 result = data['results'][0]
114
115 self.assertTrue(len(result['name']) > 0)
116 self.assertTrue("detail" in result)
117 self.assertTrue(result['id'] > 0)
118
119 # Special check for the layers typeahead's extra fields
120 if url == layers_url:
121 self.assertTrue(len(result['layerdetailurl']) > 0)
122 self.assertTrue(len(result['vcs_url']) > 0)
123 self.assertTrue(len(result['vcs_reference']) > 0)
124 # Special check for project typeahead extra fields
125 elif url == prj_url:
126 self.assertTrue(len(result['projectPageUrl']) > 0)
127
128 return True
129
130 return False
131
132 for url in urls:
133 results = False
134
135 for typeing in list(string.ascii_letters):
136 response = self.client.get(url, {'search': typeing})
137 results = basic_reponse_check(response, url)
138 if results:
139 break
140
141 # After "typeing" the alpabet we should have result true
142 # from each of the urls
143 self.assertTrue(results)
144
145 def test_xhr_add_layer(self):
146 """Test xhr_add API"""
147 # Test for importing an already existing layer
148 api_url = reverse('xhr_layer', args=(self.project.id,))
149
150 layer_data = {'vcs_url': "git://git.example.com/test",
151 'name': "base-layer",
152 'git_ref': "c12b9596afd236116b25ce26dbe0d793de9dc7ce",
153 'project_id': self.project.id,
154 'local_source_dir': "",
155 'add_to_project': True,
156 'dir_path': "/path/in/repository"}
157
158 layer_data_json = json.dumps(layer_data)
159
160 response = self.client.put(api_url, layer_data_json)
161 data = json.loads(response.content.decode('utf-8'))
162 self.assertEqual(response.status_code, 200)
163 self.assertEqual(data["error"], "ok")
164
165 self.assertTrue(
166 layer_data['name'] in
167 self.project.get_all_compatible_layer_versions().values_list(
168 'layer__name',
169 flat=True),
170 "Could not find imported layer in project's all layers list"
171 )
172
173 # Empty data passed
174 response = self.client.put(api_url, "{}")
175 data = json.loads(response.content.decode('utf-8'))
176 self.assertNotEqual(data["error"], "ok")
177
178 def test_custom_ok(self):
179 """Test successful return from ReST API xhr_customrecipe"""
180 url = reverse('xhr_customrecipe')
181 params = {'name': 'custom', 'project': self.project.id,
182 'base': self.recipe1.id}
183 response = self.client.post(url, params)
184 self.assertEqual(response.status_code, 200)
185 data = json.loads(response.content.decode('utf-8'))
186 self.assertEqual(data['error'], 'ok')
187 self.assertTrue('url' in data)
188 # get recipe from the database
189 recipe = CustomImageRecipe.objects.get(project=self.project,
190 name=params['name'])
191 args = (self.project.id, recipe.id,)
192 self.assertEqual(reverse('customrecipe', args=args), data['url'])
193
194 def test_custom_incomplete_params(self):
195 """Test not passing all required parameters to xhr_customrecipe"""
196 url = reverse('xhr_customrecipe')
197 for params in [{}, {'name': 'custom'},
198 {'name': 'custom', 'project': self.project.id}]:
199 response = self.client.post(url, params)
200 self.assertEqual(response.status_code, 200)
201 data = json.loads(response.content.decode('utf-8'))
202 self.assertNotEqual(data["error"], "ok")
203
204 def test_xhr_custom_wrong_project(self):
205 """Test passing wrong project id to xhr_customrecipe"""
206 url = reverse('xhr_customrecipe')
207 params = {'name': 'custom', 'project': 0, "base": self.recipe1.id}
208 response = self.client.post(url, params)
209 self.assertEqual(response.status_code, 200)
210 data = json.loads(response.content.decode('utf-8'))
211 self.assertNotEqual(data["error"], "ok")
212
213 def test_xhr_custom_wrong_base(self):
214 """Test passing wrong base recipe id to xhr_customrecipe"""
215 url = reverse('xhr_customrecipe')
216 params = {'name': 'custom', 'project': self.project.id, "base": 0}
217 response = self.client.post(url, params)
218 self.assertEqual(response.status_code, 200)
219 data = json.loads(response.content.decode('utf-8'))
220 self.assertNotEqual(data["error"], "ok")
221
222 def test_xhr_custom_details(self):
223 """Test getting custom recipe details"""
224 url = reverse('xhr_customrecipe_id', args=(self.customr.id,))
225 response = self.client.get(url)
226 self.assertEqual(response.status_code, 200)
227 expected = {"error": "ok",
228 "info": {'id': self.customr.id,
229 'name': self.customr.name,
230 'base_recipe_id': self.recipe1.id,
231 'project_id': self.project.id}}
232 self.assertEqual(json.loads(response.content.decode('utf-8')),
233 expected)
234
235 def test_xhr_custom_del(self):
236 """Test deleting custom recipe"""
237 name = "to be deleted"
238 recipe = CustomImageRecipe.objects.create(
239 name=name, project=self.project,
240 base_recipe=self.recipe1,
241 file_path="/tmp/testing",
242 layer_version=self.customr.layer_version)
243 url = reverse('xhr_customrecipe_id', args=(recipe.id,))
244 response = self.client.delete(url)
245 self.assertEqual(response.status_code, 200)
246
247 gotoUrl = reverse('projectcustomimages', args=(self.project.pk,))
248
249 self.assertEqual(json.loads(response.content.decode('utf-8')),
250 {"error": "ok",
251 "gotoUrl": gotoUrl})
252
253 # try to delete not-existent recipe
254 url = reverse('xhr_customrecipe_id', args=(recipe.id,))
255 response = self.client.delete(url)
256 self.assertEqual(response.status_code, 200)
257 self.assertNotEqual(json.loads(
258 response.content.decode('utf-8'))["error"], "ok")
259
260 def test_xhr_custom_packages(self):
261 """Test adding and deleting package to a custom recipe"""
262 # add self.package to recipe
263 response = self.client.put(reverse('xhr_customrecipe_packages',
264 args=(self.customr.id,
265 self.cust_package.id)))
266
267 self.assertEqual(response.status_code, 200)
268 self.assertEqual(json.loads(response.content.decode('utf-8')),
269 {"error": "ok"})
270 self.assertEqual(self.customr.appends_set.first().name,
271 self.cust_package.name)
272 # delete it
273 to_delete = self.customr.appends_set.first().pk
274 del_url = reverse('xhr_customrecipe_packages',
275 args=(self.customr.id, to_delete))
276
277 response = self.client.delete(del_url)
278 self.assertEqual(response.status_code, 200)
279 self.assertEqual(json.loads(response.content.decode('utf-8')),
280 {"error": "ok"})
281 all_packages = self.customr.get_all_packages().values_list('pk',
282 flat=True)
283
284 self.assertFalse(to_delete in all_packages)
285 # delete invalid package to test error condition
286 del_url = reverse('xhr_customrecipe_packages',
287 args=(self.customr.id,
288 99999))
289
290 response = self.client.delete(del_url)
291 self.assertEqual(response.status_code, 200)
292 self.assertNotEqual(json.loads(
293 response.content.decode('utf-8'))["error"], "ok")
294
295 def test_xhr_custom_packages_err(self):
296 """Test error conditions of xhr_customrecipe_packages"""
297 # test calls with wrong recipe id and wrong package id
298 for args in [(0, self.package.id), (self.customr.id, 0)]:
299 url = reverse('xhr_customrecipe_packages', args=args)
300 # test put and delete methods
301 for method in (self.client.put, self.client.delete):
302 response = method(url)
303 self.assertEqual(response.status_code, 200)
304 self.assertNotEqual(json.loads(
305 response.content.decode('utf-8')),
306 {"error": "ok"})
307
308 def test_download_custom_recipe(self):
309 """Download the recipe file generated for the custom image"""
310
311 # Create a dummy recipe file for the custom image generation to read
312 open("/tmp/a_recipe.bb", 'a').close()
313 response = self.client.get(reverse('customrecipedownload',
314 args=(self.project.id,
315 self.customr.id)))
316
317 self.assertEqual(response.status_code, 200)
318
319 def test_software_recipes_table(self):
320 """Test structure returned for Software RecipesTable"""
321 table = SoftwareRecipesTable()
322 request = RequestFactory().get('/foo/', {'format': 'json'})
323 response = table.get(request, pid=self.project.id)
324 data = json.loads(response.content.decode('utf-8'))
325
326 recipes = Recipe.objects.filter(Q(is_image=False))
327 self.assertTrue(len(recipes) > 1,
328 "Need more than one software recipe to test "
329 "SoftwareRecipesTable")
330
331 recipe1 = recipes[0]
332 recipe2 = recipes[1]
333
334 rows = data['rows']
335 row1 = next(x for x in rows if x['name'] == recipe1.name)
336 row2 = next(x for x in rows if x['name'] == recipe2.name)
337
338 self.assertEqual(response.status_code, 200, 'should be 200 OK status')
339
340 # check other columns have been populated correctly
341 self.assertTrue(recipe1.name in row1['name'])
342 self.assertTrue(recipe1.version in row1['version'])
343 self.assertTrue(recipe1.description in
344 row1['get_description_or_summary'])
345
346 self.assertTrue(recipe1.layer_version.layer.name in
347 row1['layer_version__layer__name'])
348
349 self.assertTrue(recipe2.name in row2['name'])
350 self.assertTrue(recipe2.version in row2['version'])
351 self.assertTrue(recipe2.description in
352 row2['get_description_or_summary'])
353
354 self.assertTrue(recipe2.layer_version.layer.name in
355 row2['layer_version__layer__name'])
356
357 def test_toaster_tables(self):
358 """Test all ToasterTables instances"""
359
360 def get_data(table, options={}):
361 """Send a request and parse the json response"""
362 options['format'] = "json"
363 options['nocache'] = "true"
364 request = RequestFactory().get('/', options)
365
366 # This is the image recipe needed for a package list for
367 # PackagesTable do this here to throw a non exist exception
368 image_recipe = Recipe.objects.get(pk=4)
369
370 # Add any kwargs that are needed by any of the possible tables
371 args = {'pid': self.project.id,
372 'layerid': self.lver.pk,
373 'recipeid': self.recipe1.pk,
374 'recipe_id': image_recipe.pk,
375 'custrecipeid': self.customr.pk,
376 'build_id': 1,
377 'target_id': 1}
378
379 response = table.get(request, **args)
380 return json.loads(response.content.decode('utf-8'))
381
382 def get_text_from_td(td):
383 """If we have html in the td then extract the text portion"""
384 # just so we don't waste time parsing non html
385 if "<" not in td:
386 ret = td
387 else:
388 ret = BeautifulSoup(td, "html.parser").text
389
390 if len(ret):
391 return "0"
392 else:
393 return ret
394
395 # Get a list of classes in tables module
396 tables = inspect.getmembers(toastergui.tables, inspect.isclass)
397 tables.extend(inspect.getmembers(toastergui.buildtables,
398 inspect.isclass))
399
400 for name, table_cls in tables:
401 # Filter out the non ToasterTables from the tables module
402 if not issubclass(table_cls, toastergui.widgets.ToasterTable) or \
403 table_cls == toastergui.widgets.ToasterTable or \
404 'Mixin' in name:
405 continue
406
407 # Get the table data without any options, this also does the
408 # initialisation of the table i.e. setup_columns,
409 # setup_filters and setup_queryset that we can use later
410 table = table_cls()
411 all_data = get_data(table)
412
413 self.assertTrue(len(all_data['rows']) > 1,
414 "Cannot test on a %s table with < 1 row" % name)
415
416 if table.default_orderby:
417 row_one = get_text_from_td(
418 all_data['rows'][0][table.default_orderby.strip("-")])
419 row_two = get_text_from_td(
420 all_data['rows'][1][table.default_orderby.strip("-")])
421
422 if '-' in table.default_orderby:
423 self.assertTrue(row_one >= row_two,
424 "Default ordering not working on %s"
425 " '%s' should be >= '%s'" %
426 (name, row_one, row_two))
427 else:
428 self.assertTrue(row_one <= row_two,
429 "Default ordering not working on %s"
430 " '%s' should be <= '%s'" %
431 (name, row_one, row_two))
432
433 # Test the column ordering and filtering functionality
434 for column in table.columns:
435 if column['orderable']:
436 # If a column is orderable test it in both order
437 # directions ordering on the columns field_name
438 ascending = get_data(table_cls(),
439 {"orderby": column['field_name']})
440
441 row_one = get_text_from_td(
442 ascending['rows'][0][column['field_name']])
443 row_two = get_text_from_td(
444 ascending['rows'][1][column['field_name']])
445
446 self.assertTrue(row_one <= row_two,
447 "Ascending sort applied but row 0: \"%s\""
448 " is less than row 1: \"%s\" "
449 "%s %s " %
450 (row_one, row_two,
451 column['field_name'], name))
452
453 descending = get_data(table_cls(),
454 {"orderby":
455 '-'+column['field_name']})
456
457 row_one = get_text_from_td(
458 descending['rows'][0][column['field_name']])
459 row_two = get_text_from_td(
460 descending['rows'][1][column['field_name']])
461
462 self.assertTrue(row_one >= row_two,
463 "Descending sort applied but row 0: %s"
464 "is greater than row 1: %s"
465 "field %s table %s" %
466 (row_one,
467 row_two,
468 column['field_name'], name))
469
470 # If the two start rows are the same we haven't actually
471 # changed the order
472 self.assertNotEqual(ascending['rows'][0],
473 descending['rows'][0],
474 "An orderby %s has not changed the "
475 "order of the data in table %s" %
476 (column['field_name'], name))
477
478 if column['filter_name']:
479 # If a filter is available for the column get the filter
480 # info. This contains what filter actions are defined.
481 filter_info = get_data(table_cls(),
482 {"cmd": "filterinfo",
483 "name": column['filter_name']})
484 self.assertTrue(len(filter_info['filter_actions']) > 0,
485 "Filter %s was defined but no actions "
486 "added to it" % column['filter_name'])
487
488 for filter_action in filter_info['filter_actions']:
489 # filter string to pass as the option
490 # This is the name of the filter:action
491 # e.g. project_filter:not_in_project
492 filter_string = "%s:%s" % (
493 column['filter_name'],
494 filter_action['action_name'])
495 # Now get the data with the filter applied
496 filtered_data = get_data(table_cls(),
497 {"filter": filter_string})
498
499 # date range filter actions can't specify the
500 # number of results they return, so their count is 0
501 if filter_action['count'] is not None:
502 self.assertEqual(
503 len(filtered_data['rows']),
504 int(filter_action['count']),
505 "We added a table filter for %s but "
506 "the number of rows returned was not "
507 "what the filter info said there "
508 "would be" % name)
509
510 # Test search functionality on the table
511 something_found = False
512 for search in list(string.ascii_letters):
513 search_data = get_data(table_cls(), {'search': search})
514
515 if len(search_data['rows']) > 0:
516 something_found = True
517 break
518
519 self.assertTrue(something_found,
520 "We went through the whole alphabet and nothing"
521 " was found for the search of table %s" % name)
522
523 # Test the limit functionality on the table
524 limited_data = get_data(table_cls(), {'limit': "1"})
525 self.assertEqual(len(limited_data['rows']),
526 1,
527 "Limit 1 set on table %s but not 1 row returned"
528 % name)
529
530 # Test the pagination functionality on the table
531 page_one_data = get_data(table_cls(), {'limit': "1",
532 "page": "1"})['rows'][0]
533
534 page_two_data = get_data(table_cls(), {'limit': "1",
535 "page": "2"})['rows'][0]
536
537 self.assertNotEqual(page_one_data,
538 page_two_data,
539 "Changed page on table %s but first row is"
540 " the same as the previous page" % name)