Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | #! /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-2016 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 | from django.core.urlresolvers import reverse |
| 23 | from django.utils import timezone |
| 24 | |
| 25 | from selenium_helpers import SeleniumTestCase |
| 26 | |
| 27 | from orm.models import Project, Release, BitbakeVersion, Build, LogMessage |
| 28 | from orm.models import Layer, Layer_Version, Recipe, CustomImageRecipe |
| 29 | |
| 30 | class TestBuildDashboardPage(SeleniumTestCase): |
| 31 | """ Tests for the build dashboard /build/X """ |
| 32 | |
| 33 | def setUp(self): |
| 34 | bbv = BitbakeVersion.objects.create(name='bbv1', giturl='/tmp/', |
| 35 | branch='master', dirpath="") |
| 36 | release = Release.objects.create(name='release1', |
| 37 | bitbake_version=bbv) |
| 38 | project = Project.objects.create_project(name='test project', |
| 39 | release=release) |
| 40 | |
| 41 | now = timezone.now() |
| 42 | |
| 43 | self.build1 = Build.objects.create(project=project, |
| 44 | started_on=now, |
| 45 | completed_on=now) |
| 46 | |
| 47 | self.build2 = Build.objects.create(project=project, |
| 48 | started_on=now, |
| 49 | completed_on=now) |
| 50 | |
| 51 | # exception |
| 52 | msg1 = 'an exception was thrown' |
| 53 | self.exception_message = LogMessage.objects.create( |
| 54 | build=self.build1, |
| 55 | level=LogMessage.EXCEPTION, |
| 56 | message=msg1 |
| 57 | ) |
| 58 | |
| 59 | # critical |
| 60 | msg2 = 'a critical error occurred' |
| 61 | self.critical_message = LogMessage.objects.create( |
| 62 | build=self.build1, |
| 63 | level=LogMessage.CRITICAL, |
| 64 | message=msg2 |
| 65 | ) |
| 66 | |
| 67 | # recipes related to the build, for testing the edit custom image/new |
| 68 | # custom image buttons |
| 69 | layer = Layer.objects.create(name='alayer') |
| 70 | layer_version = Layer_Version.objects.create( |
| 71 | layer=layer, build=self.build1 |
| 72 | ) |
| 73 | |
| 74 | # image recipes |
| 75 | self.image_recipe1 = Recipe.objects.create( |
| 76 | name='recipeA', |
| 77 | layer_version=layer_version, |
| 78 | file_path='/foo/recipeA.bb', |
| 79 | is_image=True |
| 80 | ) |
| 81 | self.image_recipe2 = Recipe.objects.create( |
| 82 | name='recipeB', |
| 83 | layer_version=layer_version, |
| 84 | file_path='/foo/recipeB.bb', |
| 85 | is_image=True |
| 86 | ) |
| 87 | |
| 88 | # custom image recipes for this project |
| 89 | self.custom_image_recipe1 = CustomImageRecipe.objects.create( |
| 90 | name='customRecipeY', |
| 91 | project=project, |
| 92 | layer_version=layer_version, |
| 93 | file_path='/foo/customRecipeY.bb', |
| 94 | base_recipe=self.image_recipe1, |
| 95 | is_image=True |
| 96 | ) |
| 97 | self.custom_image_recipe2 = CustomImageRecipe.objects.create( |
| 98 | name='customRecipeZ', |
| 99 | project=project, |
| 100 | layer_version=layer_version, |
| 101 | file_path='/foo/customRecipeZ.bb', |
| 102 | base_recipe=self.image_recipe2, |
| 103 | is_image=True |
| 104 | ) |
| 105 | |
| 106 | # custom image recipe for a different project (to test filtering |
| 107 | # of image recipes and custom image recipes is correct: this shouldn't |
| 108 | # show up in either query against self.build1) |
| 109 | self.custom_image_recipe3 = CustomImageRecipe.objects.create( |
| 110 | name='customRecipeOmega', |
| 111 | project=Project.objects.create(name='baz', release=release), |
| 112 | layer_version=Layer_Version.objects.create( |
| 113 | layer=layer, build=self.build2 |
| 114 | ), |
| 115 | file_path='/foo/customRecipeOmega.bb', |
| 116 | base_recipe=self.image_recipe2, |
| 117 | is_image=True |
| 118 | ) |
| 119 | |
| 120 | # another non-image recipe (to test filtering of image recipes and |
| 121 | # custom image recipes is correct: this shouldn't show up in either |
| 122 | # for any build) |
| 123 | self.non_image_recipe = Recipe.objects.create( |
| 124 | name='nonImageRecipe', |
| 125 | layer_version=layer_version, |
| 126 | file_path='/foo/nonImageRecipe.bb', |
| 127 | is_image=False |
| 128 | ) |
| 129 | |
| 130 | def _get_build_dashboard(self, build): |
| 131 | """ |
| 132 | Navigate to the build dashboard for build |
| 133 | """ |
| 134 | url = reverse('builddashboard', args=(build.id,)) |
| 135 | self.get(url) |
| 136 | |
| 137 | def _get_build_dashboard_errors(self, build): |
| 138 | """ |
| 139 | Get a list of HTML fragments representing the errors on the |
| 140 | dashboard for the Build object build |
| 141 | """ |
| 142 | self._get_build_dashboard(build) |
| 143 | return self.find_all('#errors div.alert-error') |
| 144 | |
| 145 | def _check_for_log_message(self, build, log_message): |
| 146 | """ |
| 147 | Check whether the LogMessage instance <log_message> is |
| 148 | represented as an HTML error in the dashboard page for the Build object |
| 149 | build |
| 150 | """ |
| 151 | errors = self._get_build_dashboard_errors(build) |
| 152 | self.assertEqual(len(errors), 2) |
| 153 | |
| 154 | expected_text = log_message.message |
| 155 | expected_id = str(log_message.id) |
| 156 | |
| 157 | found = False |
| 158 | for error in errors: |
| 159 | error_text = error.find_element_by_tag_name('pre').text |
| 160 | text_matches = (error_text == expected_text) |
| 161 | |
| 162 | error_id = error.get_attribute('data-error') |
| 163 | id_matches = (error_id == expected_id) |
| 164 | |
| 165 | if text_matches and id_matches: |
| 166 | found = True |
| 167 | break |
| 168 | |
| 169 | template_vars = (expected_text, error_text, |
| 170 | expected_id, error_id) |
| 171 | assertion_error_msg = 'exception not found as error: ' \ |
| 172 | 'expected text "%s" and got "%s"; ' \ |
| 173 | 'expected ID %s and got %s' % template_vars |
| 174 | self.assertTrue(found, assertion_error_msg) |
| 175 | |
| 176 | def _check_labels_in_modal(self, modal, expected): |
| 177 | """ |
| 178 | Check that the text values of the <label> elements inside |
| 179 | the WebElement modal match the list of text values in expected |
| 180 | """ |
| 181 | # labels containing the radio buttons we're testing for |
| 182 | labels = modal.find_elements_by_tag_name('label') |
| 183 | |
| 184 | # because the label content has the structure |
| 185 | # label text |
| 186 | # <input...> |
| 187 | # we have to regex on its innerHTML, as we can't just retrieve the |
| 188 | # "label text" on its own via the Selenium API |
| 189 | labels_text = sorted(map( |
| 190 | lambda label: label.get_attribute('innerHTML'), labels |
| 191 | )) |
| 192 | |
| 193 | expected = sorted(expected) |
| 194 | |
| 195 | self.assertEqual(len(labels_text), len(expected)) |
| 196 | |
| 197 | for idx, label_text in enumerate(labels_text): |
| 198 | self.assertRegexpMatches(label_text, expected[idx]) |
| 199 | |
| 200 | def test_exceptions_show_as_errors(self): |
| 201 | """ |
| 202 | LogMessages with level EXCEPTION should display in the errors |
| 203 | section of the page |
| 204 | """ |
| 205 | self._check_for_log_message(self.build1, self.exception_message) |
| 206 | |
| 207 | def test_criticals_show_as_errors(self): |
| 208 | """ |
| 209 | LogMessages with level CRITICAL should display in the errors |
| 210 | section of the page |
| 211 | """ |
| 212 | self._check_for_log_message(self.build1, self.critical_message) |
| 213 | |
| 214 | def test_edit_custom_image_button(self): |
| 215 | """ |
| 216 | A build which built two custom images should present a modal which lets |
| 217 | the user choose one of them to edit |
| 218 | """ |
| 219 | self._get_build_dashboard(self.build1) |
| 220 | modal = self.driver.find_element_by_id('edit-custom-image-modal') |
| 221 | |
| 222 | # recipes we expect to see in the edit custom image modal |
| 223 | expected_recipes = [ |
| 224 | self.custom_image_recipe1.name, |
| 225 | self.custom_image_recipe2.name |
| 226 | ] |
| 227 | |
| 228 | self._check_labels_in_modal(modal, expected_recipes) |
| 229 | |
| 230 | def test_new_custom_image_button(self): |
| 231 | """ |
| 232 | Check that a build with multiple images and custom images presents |
| 233 | all of them as options for creating a new custom image from |
| 234 | """ |
| 235 | self._get_build_dashboard(self.build1) |
| 236 | |
| 237 | # click the "new custom image" button, which populates the modal |
| 238 | selector = '[data-role="new-custom-image-trigger"] button' |
| 239 | self.click(selector) |
| 240 | |
| 241 | modal = self.driver.find_element_by_id('new-custom-image-modal') |
| 242 | |
| 243 | # recipes we expect to see in the new custom image modal |
| 244 | expected_recipes = [ |
| 245 | self.image_recipe1.name, |
| 246 | self.image_recipe2.name, |
| 247 | self.custom_image_recipe1.name, |
| 248 | self.custom_image_recipe2.name |
| 249 | ] |
| 250 | |
| 251 | self._check_labels_in_modal(modal, expected_recipes) |