| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import copy | 5 import copy |
| 6 import logging | 6 import logging |
| 7 | 7 |
| 8 from google.appengine.api import app_identity | 8 from google.appengine.api import app_identity |
| 9 | 9 |
| 10 from crash.findit import Findit | |
| 11 from crash.type_enums import CrashClient | 10 from crash.type_enums import CrashClient |
| 12 from crash.test.predator_testcase import PredatorTestCase | 11 from crash.test.predator_testcase import PredatorTestCase |
| 13 from model.crash.crash_config import CrashConfig | 12 from model import analysis_status |
| 14 from model.crash.fracas_crash_analysis import FracasCrashAnalysis | 13 from model.crash.cracas_crash_analysis import CracasCrashAnalysis |
| 15 | 14 from model.crash.crash_analysis import CrashAnalysis |
| 16 MOCK_GET_REPOSITORY = lambda _: None # pragma: no cover | |
| 17 | |
| 18 | |
| 19 class UnsupportedClient(Findit): # pylint: disable=W0223 | |
| 20 # TODO(http://crbug.com/659346): this isn't being called for some reason. | |
| 21 @property | |
| 22 def client_id(self): # pragma: no cover | |
| 23 return self._client_id | |
| 24 | |
| 25 @property | |
| 26 def client_config(self): # pragma: no cover | |
| 27 """Don't return None, so that PlatformRename doesn't crash.""" | |
| 28 return {} | |
| 29 | |
| 30 def __init__(self, client_id=None, config=None): | |
| 31 if client_id is None: | |
| 32 client_id = 'unsupported_client' | |
| 33 self._client_id = client_id | |
| 34 super(UnsupportedClient, self).__init__(MOCK_GET_REPOSITORY, config) | |
| 35 | |
| 36 | |
| 37 class MockFindit(Findit): # pylint: disable = W | |
| 38 """Overwrite abstract method of Findit for testing.""" | |
| 39 | |
| 40 def __init__(self, config=None): | |
| 41 super(MockFindit, self).__init__(MOCK_GET_REPOSITORY, config) | |
| 42 | |
| 43 @classmethod | |
| 44 def _ClientID(cls): | |
| 45 return CrashClient.FRACAS | |
| 46 | |
| 47 def ProcessResultForPublishing(self, result, key): | |
| 48 return result | |
| 49 | 15 |
| 50 | 16 |
| 51 class FinditTest(PredatorTestCase): | 17 class FinditTest(PredatorTestCase): |
| 52 | 18 |
| 53 def setUp(self): | 19 def setUp(self): |
| 54 super(FinditTest, self).setUp() | 20 super(FinditTest, self).setUp() |
| 55 self.findit = MockFindit(CrashConfig.Get()) | 21 self.findit = self.GetMockFindit(client_id=CrashClient.FRACAS) |
| 56 | 22 |
| 57 def testPlatformRename(self): | 23 def testPlatformRename(self): |
| 58 self.assertEqual( | 24 self.assertEqual(self.findit.RenamePlatform('linux'), 'unix') |
| 59 self.findit.RenamePlatform('linux'), 'unix') | |
| 60 | 25 |
| 61 def testCheckPolicyUnsupportedClient(self): | 26 def testCheckPolicyNoneClientConfig(self): |
| 62 self.assertIsNone(UnsupportedClient(config=CrashConfig.Get()).CheckPolicy( | 27 unsupported_client = self.GetMockFindit(client_id='unconfiged') |
| 63 self.GetDummyCrashData( | 28 crash_data = unsupported_client.GetCrashData( |
| 29 self.GetDummyChromeCrashData( |
| 64 platform = 'canary', | 30 platform = 'canary', |
| 65 signature = 'sig', | 31 signature = 'sig', |
| 66 ))) | 32 )) |
| 33 self.assertTrue(unsupported_client._CheckPolicy(crash_data)) |
| 67 | 34 |
| 68 def testCreateAnalysisForUnsupportedClientId(self): | 35 def testCheckPolicyNoBlackList(self): |
| 69 unsupported_client = UnsupportedClient('unsupported_id', | 36 """Tests ``_CheckPolicy`` method with no black list configured.""" |
| 70 config=CrashConfig.Get()) | 37 crash_data = self.findit.GetCrashData(self.GetDummyChromeCrashData()) |
| 71 self.assertIsNone(unsupported_client.CreateAnalysis({'signature': 'sig'})) | 38 self.assertTrue(self.findit._CheckPolicy(crash_data)) |
| 72 | 39 |
| 73 def testGetAnalysisForUnsuportedClient(self): | 40 def testCheckPolicyWithBlackList(self): |
| 74 crash_identifiers = {'signature': 'sig'} | 41 """Tests ``_CheckPolicy`` return false if signature is blacklisted.""" |
| 75 # TODO(wrengr): it'd be less fragile to call FinditForFracas.CreateAnalysis | 42 crash_data = self.findit.GetCrashData(self.GetDummyChromeCrashData( |
| 76 # instead. But then we'd need to make UnsupportedClient inherit that | 43 client_id=CrashClient.FRACAS, |
| 77 # implementation, rather than inheriting the one from the Findit | 44 signature='Blacklist marker signature')) |
| 78 # base class. | 45 self.assertFalse(self.findit._CheckPolicy(crash_data)) |
| 79 analysis = FracasCrashAnalysis.Create(crash_identifiers) | 46 |
| 80 analysis.put() | 47 def testDoesNotNeedNewAnalysisIfCheckPolicyFailed(self): |
| 81 unsupported_client = UnsupportedClient('Unsupported_client', | 48 """Tests that ``NeedsNewAnalysis`` returns False if policy check failed.""" |
| 82 config=CrashConfig.Get()) | 49 raw_crash_data = self.GetDummyChromeCrashData( |
| 83 self.assertIsNone( | 50 client_id=CrashClient.FRACAS, |
| 84 unsupported_client.GetAnalysis(crash_identifiers), | 51 signature='Blacklist marker signature') |
| 85 'Unsupported client unexpectedly got analysis %s via identifiers %s' | 52 crash_data = self.findit.GetCrashData(raw_crash_data) |
| 86 % (analysis, crash_identifiers)) | 53 self.assertFalse(self.findit.NeedsNewAnalysis(crash_data)) |
| 54 |
| 55 def testDoesNotNeedNewAnalysisIfCrashAnalyzedBefore(self): |
| 56 """Tests that ``NeedsNewAnalysis`` returns False if crash analyzed.""" |
| 57 crash_data = self.findit.GetCrashData(self.GetDummyChromeCrashData()) |
| 58 crash_analysis_model = self.findit.CreateAnalysis(crash_data.identifiers) |
| 59 crash_analysis_model.regression_range = crash_data.regression_range |
| 60 crash_analysis_model.put() |
| 61 self.assertFalse(self.findit.NeedsNewAnalysis(crash_data)) |
| 62 |
| 63 def testNeedsNewAnalysisIfNoAnalysisYet(self): |
| 64 """Tests that ``NeedsNewAnalysis`` returns True if crash not analyzed.""" |
| 65 crash_data = self.findit.GetCrashData(self.GetDummyChromeCrashData()) |
| 66 self.mock(CrashAnalysis, 'Initialize', lambda *_: None) |
| 67 self.assertTrue(self.findit.NeedsNewAnalysis(crash_data)) |
| 68 |
| 69 def testNeedsNewAnalysisIfLastOneFailed(self): |
| 70 """Tests that ``NeedsNewAnalysis`` returns True if last analysis failed.""" |
| 71 crash_data = self.findit.GetCrashData(self.GetDummyChromeCrashData()) |
| 72 crash_analysis_model = self.findit.CreateAnalysis(crash_data.identifiers) |
| 73 crash_analysis_model.status = analysis_status.ERROR |
| 74 crash_analysis_model.put() |
| 75 self.mock(CrashAnalysis, 'Initialize', lambda *_: None) |
| 76 self.assertTrue(self.findit.NeedsNewAnalysis(crash_data)) |
| 87 | 77 |
| 88 def testGetPublishableResultFoundTrue(self): | 78 def testGetPublishableResultFoundTrue(self): |
| 89 analysis_result = { | 79 analysis_result = { |
| 90 'found': True, | 80 'found': True, |
| 91 'suspected_cls': [ | 81 'suspected_cls': [ |
| 92 {'confidence': 0.21434, | 82 {'confidence': 0.21434, |
| 93 'reasons': ['reason1', 'reason2'], | 83 'reasons': ['reason1', 'reason2'], |
| 94 'other': 'data'} | 84 'other': 'data'} |
| 95 ], | 85 ], |
| 96 'other_data': 'data', | 86 'other_data': 'data', |
| 97 } | 87 } |
| 98 | 88 |
| 99 processed_analysis_result = copy.deepcopy(analysis_result) | 89 processed_analysis_result = copy.deepcopy(analysis_result) |
| 100 for cl in processed_analysis_result['suspected_cls']: | 90 for cl in processed_analysis_result['suspected_cls']: |
| 101 cl['confidence'] = round(cl['confidence'], 2) | 91 cl['confidence'] = round(cl['confidence'], 2) |
| 102 cl.pop('reasons', None) | 92 cl.pop('reasons', None) |
| 103 | 93 |
| 104 crash_identifiers = {'signature': 'sig'} | 94 crash_identifiers = {'signature': 'sig'} |
| 105 expected_processed_result = { | 95 expected_processed_result = { |
| 106 'crash_identifiers': crash_identifiers, | 96 'crash_identifiers': crash_identifiers, |
| 107 'client_id': self.findit.client_id, | 97 'client_id': self.findit.client_id, |
| 108 'result': processed_analysis_result, | 98 'result': processed_analysis_result, |
| 109 } | 99 } |
| 110 | 100 |
| 111 analysis = FracasCrashAnalysis.Create(crash_identifiers) | 101 analysis = CracasCrashAnalysis.Create(crash_identifiers) |
| 112 analysis.result = analysis_result | 102 analysis.result = analysis_result |
| 113 | 103 |
| 114 self.assertDictEqual(self.findit.GetPublishableResult(crash_identifiers, | 104 self.assertDictEqual(self.findit.GetPublishableResult(crash_identifiers, |
| 115 analysis), | 105 analysis), |
| 116 expected_processed_result) | 106 expected_processed_result) |
| 117 | 107 |
| 118 def testGetPublishableResultFoundFalse(self): | 108 def testGetPublishableResultFoundFalse(self): |
| 119 analysis_result = { | 109 analysis_result = { |
| 120 'found': False, | 110 'found': False, |
| 121 } | 111 } |
| 122 crash_identifiers = {'signature': 'sig'} | 112 crash_identifiers = {'signature': 'sig'} |
| 123 expected_processed_result = { | 113 expected_processed_result = { |
| 124 'crash_identifiers': crash_identifiers, | 114 'crash_identifiers': crash_identifiers, |
| 125 'client_id': self.findit.client_id, | 115 'client_id': self.findit.client_id, |
| 126 'result': copy.deepcopy(analysis_result), | 116 'result': copy.deepcopy(analysis_result), |
| 127 } | 117 } |
| 128 | 118 |
| 129 analysis = FracasCrashAnalysis.Create(crash_identifiers) | 119 analysis = CracasCrashAnalysis.Create(crash_identifiers) |
| 130 analysis.result = analysis_result | 120 analysis.result = analysis_result |
| 131 | 121 |
| 132 self.assertDictEqual(self.findit.GetPublishableResult(crash_identifiers, | 122 self.assertDictEqual(self.findit.GetPublishableResult(crash_identifiers, |
| 133 analysis), | 123 analysis), |
| 134 expected_processed_result) | 124 expected_processed_result) |
| OLD | NEW |