| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 os | |
| 6 import unittest | 5 import unittest |
| 7 | 6 |
| 8 from telemetry import decorators | 7 from telemetry import decorators |
| 9 | 8 |
| 10 from telemetry.unittest_util import system_stub | 9 from telemetry.unittest_util import system_stub |
| 11 from telemetry.util import cloud_storage | 10 from telemetry.util import cloud_storage |
| 12 | 11 |
| 13 | 12 |
| 14 def _FakeFindGsutil(): | 13 def _FakeFindGsutil(): |
| 15 return 'fake gsutil path' | 14 return 'fake gsutil path' |
| 16 | 15 |
| 17 def _FakeReadHash(_): | |
| 18 return 'hashthis!' | |
| 19 | |
| 20 def _FakeCalulateHashMatchesRead(_): | |
| 21 return 'hashthis!' | |
| 22 | |
| 23 def _FakeCalulateHashNewHash(_): | |
| 24 return 'omgnewhash' | |
| 25 | |
| 26 | 16 |
| 27 class CloudStorageUnitTest(unittest.TestCase): | 17 class CloudStorageUnitTest(unittest.TestCase): |
| 28 | 18 |
| 29 def _FakeRunCommand(self, cmd): | 19 def _FakeRunCommand(self, cmd): |
| 30 pass | 20 pass |
| 31 | 21 |
| 32 def _FakeGet(self, bucket, remote_path, local_path): | 22 def testValidCloudUrl(self): |
| 33 pass | |
| 34 | |
| 35 def _assertRunCommandRaisesError(self, communicate_strs, error): | |
| 36 stubs = system_stub.Override(cloud_storage, ['open', 'subprocess']) | |
| 37 orig_find_gs_util = cloud_storage.FindGsutil | |
| 38 cloud_storage.FindGsutil = _FakeFindGsutil | |
| 39 stubs.open.files = {'fake gsutil path':''} | |
| 40 stubs.subprocess.Popen.returncode_result = 1 | |
| 41 try: | |
| 42 for string in communicate_strs: | |
| 43 stubs.subprocess.Popen.communicate_result = ('', string) | |
| 44 self.assertRaises(error, cloud_storage._RunCommand, []) | |
| 45 finally: | |
| 46 stubs.Restore() | |
| 47 cloud_storage.FindGsutil = orig_find_gs_util | |
| 48 | |
| 49 def testRunCommandCredentialsError(self): | |
| 50 strs = ['You are attempting to access protected data with no configured', | |
| 51 'Failure: No handler was ready to authenticate.'] | |
| 52 self._assertRunCommandRaisesError(strs, cloud_storage.CredentialsError) | |
| 53 | |
| 54 def testRunCommandPermissionError(self): | |
| 55 strs = ['status=403', 'status 403', '403 Forbidden'] | |
| 56 self._assertRunCommandRaisesError(strs, cloud_storage.PermissionError) | |
| 57 | |
| 58 def testRunCommandNotFoundError(self): | |
| 59 strs = ['InvalidUriError', 'No such object', 'No URLs matched', | |
| 60 'One or more URLs matched no', 'InvalidUriError'] | |
| 61 self._assertRunCommandRaisesError(strs, cloud_storage.NotFoundError) | |
| 62 | |
| 63 def testRunCommandServerError(self): | |
| 64 strs = ['500 Internal Server Error'] | |
| 65 self._assertRunCommandRaisesError(strs, cloud_storage.ServerError) | |
| 66 | |
| 67 def testRunCommandGenericError(self): | |
| 68 strs = ['Random string'] | |
| 69 self._assertRunCommandRaisesError(strs, cloud_storage.CloudStorageError) | |
| 70 | |
| 71 def testInsertCreatesValidCloudUrl(self): | |
| 72 orig_run_command = cloud_storage._RunCommand | 23 orig_run_command = cloud_storage._RunCommand |
| 73 try: | 24 try: |
| 74 cloud_storage._RunCommand = self._FakeRunCommand | 25 cloud_storage._RunCommand = self._FakeRunCommand |
| 75 remote_path = 'test-remote-path.html' | 26 remote_path = 'test-remote-path.html' |
| 76 local_path = 'test-local-path.html' | 27 local_path = 'test-local-path.html' |
| 77 cloud_url = cloud_storage.Insert(cloud_storage.PUBLIC_BUCKET, | 28 cloud_url = cloud_storage.Insert(cloud_storage.PUBLIC_BUCKET, |
| 78 remote_path, local_path) | 29 remote_path, local_path) |
| 79 self.assertEqual('https://console.developers.google.com/m/cloudstorage' | 30 self.assertEqual('https://console.developers.google.com/m/cloudstorage' |
| 80 '/b/chromium-telemetry/o/test-remote-path.html', | 31 '/b/chromium-telemetry/o/test-remote-path.html', |
| 81 cloud_url) | 32 cloud_url) |
| 82 finally: | 33 finally: |
| 83 cloud_storage._RunCommand = orig_run_command | 34 cloud_storage._RunCommand = orig_run_command |
| 84 | 35 |
| 85 def testExistsReturnsFalse(self): | 36 def testExistsReturnsFalse(self): |
| 86 stubs = system_stub.Override(cloud_storage, ['subprocess']) | 37 stubs = system_stub.Override(cloud_storage, ['subprocess']) |
| 87 orig_find_gs_util = cloud_storage.FindGsutil | 38 orig_find_gs_util = cloud_storage.FindGsutil |
| 88 try: | 39 try: |
| 89 stubs.subprocess.Popen.communicate_result = ( | 40 stubs.subprocess.Popen.communicate_result = ( |
| 90 '', | 41 '', |
| 91 'CommandException: One or more URLs matched no objects.\n') | 42 'CommandException: One or more URLs matched no objects.\n') |
| 92 stubs.subprocess.Popen.returncode_result = 1 | 43 stubs.subprocess.Popen.returncode_result = 1 |
| 93 cloud_storage.FindGsutil = _FakeFindGsutil | 44 cloud_storage.FindGsutil = _FakeFindGsutil |
| 94 self.assertFalse(cloud_storage.Exists('fake bucket', | 45 self.assertFalse(cloud_storage.Exists('fake bucket', |
| 95 'fake remote path')) | 46 'fake remote path')) |
| 96 finally: | 47 finally: |
| 97 stubs.Restore() | 48 stubs.Restore() |
| 98 cloud_storage.FindGsutil = orig_find_gs_util | 49 cloud_storage.FindGsutil = orig_find_gs_util |
| 99 | |
| 100 def testGetIfChanged(self): | |
| 101 stubs = system_stub.Override(cloud_storage, ['os', 'open']) | |
| 102 stubs.open.files[_FakeFindGsutil()] = '' | |
| 103 orig_get = cloud_storage.Get | |
| 104 orig_read_hash = cloud_storage.ReadHash | |
| 105 orig_calculate_hash = cloud_storage.CalculateHash | |
| 106 cloud_storage.ReadHash = _FakeReadHash | |
| 107 cloud_storage.CalculateHash = _FakeCalulateHashMatchesRead | |
| 108 file_path = 'test-file-path.wpr' | |
| 109 hash_path = file_path + '.sha1' | |
| 110 try: | |
| 111 cloud_storage.Get = self._FakeGet | |
| 112 # hash_path doesn't exist. | |
| 113 self.assertFalse(cloud_storage.GetIfChanged(file_path, | |
| 114 cloud_storage.PUBLIC_BUCKET)) | |
| 115 # hash_path exists, but file_path doesn't. | |
| 116 stubs.os.path.files.append(hash_path) | |
| 117 self.assertTrue(cloud_storage.GetIfChanged(file_path, | |
| 118 cloud_storage.PUBLIC_BUCKET)) | |
| 119 # hash_path and file_path exist, and have same hash. | |
| 120 stubs.os.path.files.append(file_path) | |
| 121 self.assertFalse(cloud_storage.GetIfChanged(file_path, | |
| 122 cloud_storage.PUBLIC_BUCKET)) | |
| 123 # hash_path and file_path exist, and have different hashes. | |
| 124 cloud_storage.CalculateHash = _FakeCalulateHashNewHash | |
| 125 self.assertTrue(cloud_storage.GetIfChanged(file_path, | |
| 126 cloud_storage.PUBLIC_BUCKET)) | |
| 127 finally: | |
| 128 stubs.Restore() | |
| 129 cloud_storage.Get = orig_get | |
| 130 cloud_storage.CalculateHash = orig_calculate_hash | |
| 131 cloud_storage.ReadHash = orig_read_hash | |
| 132 | |
| 133 def testGetFilesInDirectoryIfChanged(self): | |
| 134 stubs = system_stub.Override(cloud_storage, ['os']) | |
| 135 stubs.os._directory = {'dir1':['1file1.sha1', '1file2.txt', '1file3.sha1'], | |
| 136 'dir2':['2file.txt'], 'dir3':['3file1.sha1']} | |
| 137 stubs.os.path.dirs = ['real_dir_path'] | |
| 138 def IncrementFilesUpdated(*_): | |
| 139 IncrementFilesUpdated.files_updated +=1 | |
| 140 IncrementFilesUpdated.files_updated = 0 | |
| 141 orig_get_if_changed = cloud_storage.GetIfChanged | |
| 142 cloud_storage.GetIfChanged = IncrementFilesUpdated | |
| 143 try: | |
| 144 self.assertRaises(ValueError, cloud_storage.GetFilesInDirectoryIfChanged, | |
| 145 os.path.abspath(os.sep), cloud_storage.PUBLIC_BUCKET) | |
| 146 self.assertEqual(0, IncrementFilesUpdated.files_updated) | |
| 147 self.assertRaises(ValueError, cloud_storage.GetFilesInDirectoryIfChanged, | |
| 148 'fake_dir_path', cloud_storage.PUBLIC_BUCKET) | |
| 149 self.assertEqual(0, IncrementFilesUpdated.files_updated) | |
| 150 cloud_storage.GetFilesInDirectoryIfChanged('real_dir_path', | |
| 151 cloud_storage.PUBLIC_BUCKET) | |
| 152 self.assertEqual(3, IncrementFilesUpdated.files_updated) | |
| 153 finally: | |
| 154 cloud_storage.GetIfChanged = orig_get_if_changed | |
| 155 stubs.Restore() | |
| OLD | NEW |