1 # Copyright 2013 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 collections 6 import os 7 import sys 8 import unittest 9 10 PERF_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 11 sys.path.insert(0, os.path.join(os.path.dirname(PERF_ROOT), 'telemetry')) 12 from telemetry.unittest import system_stub 13 14 sys.path.insert(0, PERF_ROOT) 15 from page_sets import PRESUBMIT 16 17 18 class AffectedFileStub(object): 19 def __init__(self, absolute_local_path): 20 self._absolute_local_path = absolute_local_path 21 22 def AbsoluteLocalPath(self): 23 return self._absolute_local_path 24 25 26 class InputAPIStub(object): 27 def __init__(self, paths, deleted_paths=None): 28 self._paths = paths 29 if deleted_paths: 30 self._deleted_paths = deleted_paths 31 else: 32 self._deleted_paths = [] 33 34 def AffectedFiles(self, include_deletes=True): 35 affected_files = [AffectedFileStub(path) for path in self._paths] 36 if include_deletes: 37 affected_files += [AffectedFileStub(path) for path in self._deleted_paths] 38 return affected_files 39 40 def AbsoluteLocalPaths(self): 41 return [af.AbsoluteLocalPath() for af in self.AffectedFiles()] 42 43 def PresubmitLocalPath(self): 44 return PRESUBMIT.__file__ 45 46 47 class OutputAPIStub(object): 48 class PresubmitError(Exception): 49 pass 50 51 class PresubmitNotifyResult(Exception): 52 pass 53 54 55 PRESUBMIT.LoadSupport(InputAPIStub([])) # do this to support monkey patching 56 57 class PresubmitTest(unittest.TestCase): 58 def setUp(self): 59 success_file_hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' 60 61 self._stubs = system_stub.Override( 62 PRESUBMIT, ['cloud_storage', 'open', 'os']) 63 # Files in Cloud Storage. 64 self._stubs.cloud_storage.remote_paths = [ 65 'skip'.zfill(40), 66 ] 67 # Local data files and their hashes. 68 self._stubs.cloud_storage.local_file_hashes = { 69 '/path/to/skip.wpr': 'skip'.zfill(40), 70 '/path/to/success.wpr': success_file_hash, 71 '/path/to/wrong_hash.wpr': success_file_hash, 72 } 73 # Local data files. 74 self._stubs.os.path.files = ( 75 self._stubs.cloud_storage.local_file_hashes.keys()) 76 # Local hash files and their contents. 77 self._stubs.open.files = { 78 '/path/to/invalid_hash.wpr.sha1': 'invalid_hash', 79 '/path/to/missing.wpr.sha1': 'missing'.zfill(40), 80 '/path/to/success.wpr.sha1': success_file_hash, 81 '/path/to/skip.wpr.sha1': 'skip'.zfill(40), 82 '/path/to/wrong_hash.wpr.sha1': 'wronghash'.zfill(40), 83 } 84 85 def tearDown(self): 86 self._stubs.Restore() 87 88 def assertResultCount(self, results, expected_errors, expected_notifications): 89 counts = collections.defaultdict(int) 90 for result in results: 91 counts[type(result)] += 1 92 actual_errors = counts[OutputAPIStub.PresubmitError] 93 actual_notifications = counts[OutputAPIStub.PresubmitNotifyResult] 94 self.assertEqual(expected_errors, actual_errors, 95 msg='Expected %d errors, but got %d. Results: %s' % 96 (expected_errors, actual_errors, results)) 97 self.assertEqual(expected_notifications, actual_notifications, 98 msg='Expected %d notifications, but got %d. Results: %s' % 99 (expected_notifications, actual_notifications, results)) 100 101 def _CheckUpload(self, paths, deleted_paths=None): 102 input_api = InputAPIStub(paths, deleted_paths) 103 return PRESUBMIT.CheckChangeOnUpload(input_api, OutputAPIStub()) 104 105 def testIgnoreDeleted(self): 106 results = self._CheckUpload([], ['/path/to/deleted.wpr.sha1']) 107 self.assertResultCount(results, 0, 0) 108 109 def testIgnoreNonHashes(self): 110 results = self._CheckUpload(['/path/to/irrelevant.py']) 111 self.assertResultCount(results, 0, 0) 112 113 def testInvalidHash(self): 114 results = self._CheckUpload(['/path/to/invalid_hash.wpr.sha1']) 115 self.assertResultCount(results, 1, 0) 116 self.assertTrue('valid SHA-1 hash' in str(results[0]), msg=results[0]) 117 118 def testMissingFile(self): 119 results = self._CheckUpload(['/path/to/missing.wpr.sha1']) 120 self.assertResultCount(results, 1, 0) 121 self.assertTrue('not found' in str(results[0]), msg=results[0]) 122 123 def testSkip(self): 124 results = self._CheckUpload(['/path/to/skip.wpr.sha1']) 125 self.assertResultCount(results, 0, 1) 126 self.assertTrue('skipping' in str(results[0]), msg=results[0]) 127 128 def testSuccess(self): 129 results = self._CheckUpload(['/path/to/success.wpr.sha1']) 130 self.assertResultCount(results, 0, 1) 131 self.assertTrue('Uploaded' in str(results[0]), msg=results[0]) 132 133 def testWrongHash(self): 134 results = self._CheckUpload(['/path/to/wrong_hash.wpr.sha1']) 135 self.assertTrue('does not match' in str(results[0]), msg=results[0]) 136 137 138 if __name__ == '__main__': 139 unittest.main() 140