| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import copy |
| 6 import json |
| 7 import re |
| 8 import webapp2 |
| 9 import webtest |
| 10 |
| 11 from google.appengine.api import users |
| 12 |
| 13 from gae_libs.testcase import TestCase |
| 14 from handlers.crash import crash_config |
| 15 from handlers.crash.crash_config import CrashConfig as CrashConfigHandler |
| 16 from model.crash.crash_config import CrashConfig as CrashConfigModel |
| 17 |
| 18 _MOCK_FRACAS_CONFIG = { |
| 19 'analysis_result_pubsub_topic': 'projects/project-name/topics/name', |
| 20 'supported_platform_list_by_channel': { |
| 21 'canary': ['win', 'mac', 'linux'], |
| 22 'supported_channel': ['supported_platform'], |
| 23 }, |
| 24 'platform_rename': {'linux': 'unix'}, |
| 25 'signature_blacklist_markers': ['Blacklist marker'], |
| 26 'top_n': 7 |
| 27 } |
| 28 |
| 29 _MOCK_COMPONENT_CONFIG = { |
| 30 'path_function_component': [ |
| 31 [ |
| 32 'src/comp1.*', |
| 33 '', |
| 34 'Comp1>Dummy' |
| 35 ], |
| 36 [ |
| 37 'src/comp2.*', |
| 38 'func2.*', |
| 39 'Comp2>Dummy' |
| 40 ], |
| 41 ], |
| 42 'top_n': 4 |
| 43 } |
| 44 |
| 45 _MOCK_PROJECT_CONFIG = { |
| 46 'project_path_function_hosts': [ |
| 47 ['android_os', ['googleplex-android/'], ['android.'], None], |
| 48 ['chromium', None, ['org.chromium'], ['src/', 'src/d/dep1', 'src/dep2']] |
| 49 ], |
| 50 'non_chromium_project_rank_priority': { |
| 51 'android_os': '-1', |
| 52 'others': '-2', |
| 53 }, |
| 54 'top_n': 4 |
| 55 } |
| 56 |
| 57 _MOCK_CONFIG = { |
| 58 'fracas': _MOCK_FRACAS_CONFIG, |
| 59 'cracas': _MOCK_FRACAS_CONFIG, |
| 60 'component_classifier': _MOCK_COMPONENT_CONFIG, |
| 61 'project_classifier': _MOCK_PROJECT_CONFIG |
| 62 } |
| 63 |
| 64 |
| 65 class CrashConfigTest(TestCase): |
| 66 """Tests utility functions and ``CrashConfig`` handler.""" |
| 67 app_module = webapp2.WSGIApplication([ |
| 68 ('/crash/config', CrashConfigHandler), |
| 69 ], debug=True) |
| 70 |
| 71 def testSortConfig(self): |
| 72 """Tests ``_SortConfig`` function.""" |
| 73 config = copy.deepcopy(_MOCK_CONFIG) |
| 74 crash_config._SortConfig(config) |
| 75 expected_config = copy.deepcopy(_MOCK_CONFIG) |
| 76 expected_config['project_classifier'][ |
| 77 'project_path_function_hosts'][1][3] = ['src/d/dep1/', 'src/dep2/', |
| 78 'src/'] |
| 79 self.assertDictEqual(expected_config, config) |
| 80 |
| 81 |
| 82 def testValidateComponentClassifierConfig(self): |
| 83 """Tests ``_ValidateComponentClassifierConfig`` function.""" |
| 84 # Return False if config is not a dict |
| 85 self.assertFalse(crash_config._ValidateComponentClassifierConfig(None)) |
| 86 |
| 87 # Return False if config dict has not ``path_function_component``. |
| 88 self.assertFalse(crash_config._ValidateComponentClassifierConfig({})) |
| 89 |
| 90 # Return False if config ``path_function_component`` is not list. |
| 91 config = {'path_function_component': {}} |
| 92 self.assertFalse(crash_config._ValidateComponentClassifierConfig(config)) |
| 93 |
| 94 # Return False if config ``top_n`` is not int. |
| 95 config = {'path_function_component': |
| 96 _MOCK_COMPONENT_CONFIG['path_function_component'], |
| 97 'top_n': []} |
| 98 self.assertFalse(crash_config._ValidateComponentClassifierConfig(config)) |
| 99 |
| 100 self.assertTrue(crash_config._ValidateComponentClassifierConfig( |
| 101 _MOCK_COMPONENT_CONFIG)) |
| 102 |
| 103 def testValidateProjectClassifierConfig(self): |
| 104 """Tests ``_ValidateProjectClassifierConfig`` function.""" |
| 105 # Return False if config is not a dict |
| 106 self.assertFalse(crash_config._ValidateProjectClassifierConfig(None)) |
| 107 |
| 108 # Return False if config dict has not ``project_path_function_hosts``. |
| 109 self.assertFalse(crash_config._ValidateProjectClassifierConfig({})) |
| 110 |
| 111 # Return False if config ``project_path_function_hosts`` is not list. |
| 112 config = {'project_path_function_hosts': {}} |
| 113 self.assertFalse(crash_config._ValidateProjectClassifierConfig(config)) |
| 114 |
| 115 # Return False if config ``non_chromium_project_rank_priority`` is not dict. |
| 116 config = {'project_path_function_hosts': |
| 117 _MOCK_PROJECT_CONFIG['project_path_function_hosts'], |
| 118 'non_chromium_project_rank_priority': []} |
| 119 self.assertFalse(crash_config._ValidateProjectClassifierConfig(config)) |
| 120 |
| 121 # Return False if config ``top_n`` is not int. |
| 122 config = {'project_path_function_hosts': |
| 123 _MOCK_PROJECT_CONFIG['project_path_function_hosts'], |
| 124 'non_chromium_project_rank_priority': |
| 125 _MOCK_PROJECT_CONFIG['non_chromium_project_rank_priority'], |
| 126 'top_n': []} |
| 127 self.assertFalse(crash_config._ValidateProjectClassifierConfig(config)) |
| 128 |
| 129 self.assertTrue(crash_config._ValidateProjectClassifierConfig( |
| 130 _MOCK_PROJECT_CONFIG)) |
| 131 |
| 132 def testConfigurationDictIsValid(self): |
| 133 """Tests ``_ConfigurationDictIsValid`` function.""" |
| 134 # Return False if config is not a dict |
| 135 self.assertFalse(crash_config._ConfigurationDictIsValid(None)) |
| 136 |
| 137 # Return False if there is one configuration failed validation. |
| 138 config = {'component_classifier': None} |
| 139 self.assertFalse(crash_config._ConfigurationDictIsValid(config)) |
| 140 |
| 141 self.assertTrue(crash_config._ConfigurationDictIsValid(_MOCK_CONFIG)) |
| 142 |
| 143 def testHandleGet(self): |
| 144 """Tests ``HandleGet`` method of ``CrashConfig`` handler.""" |
| 145 self.mock_current_user(user_email='test@chromium.org', is_admin=True) |
| 146 |
| 147 CrashConfigModel.Get().Update(users.GetCurrentUser(), True, **_MOCK_CONFIG) |
| 148 |
| 149 response = self.test_app.get('/crash/config', params={'format': 'json'}) |
| 150 self.assertEquals(response.status_int, 200) |
| 151 |
| 152 self.assertDictEqual(_MOCK_CONFIG, response.json_body) |
| 153 |
| 154 def testHandlePost(self): |
| 155 """Tests ``HandlePost`` method of ``CrashConfig`` handler.""" |
| 156 self.mock_current_user(user_email='test@chromium.org', is_admin=True) |
| 157 |
| 158 response = self.test_app.post('/crash/config', |
| 159 params={'data': json.dumps(_MOCK_CONFIG), |
| 160 'format': 'json'}) |
| 161 |
| 162 expected_config = copy.deepcopy(_MOCK_CONFIG) |
| 163 expected_config['project_classifier'][ |
| 164 'project_path_function_hosts'][1][3] = ['src/d/dep1/', 'src/dep2/', |
| 165 'src/'] |
| 166 |
| 167 self.assertDictEqual(expected_config, response.json_body) |
| 168 |
| 169 def testHandlePostMalFormattedData(self): |
| 170 """Tests ``HandlePost`` for mal-formatted data.""" |
| 171 self.mock_current_user(user_email='test@chromium.org', is_admin=True) |
| 172 |
| 173 self.assertRaisesRegexp( |
| 174 webtest.app.AppError, |
| 175 re.compile('New configuration settings are not properly formatted.', |
| 176 re.MULTILINE | re.DOTALL), |
| 177 self.test_app.post, '/crash/config', params={'data': json.dumps({}), |
| 178 'format': 'json'}) |
| OLD | NEW |