Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tools/telemetry/telemetry/util/cloud_storage_unittest.py

Issue 838253005: Refactor serving_dirs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed import. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
5 import unittest 6 import unittest
6 7
7 from telemetry import decorators 8 from telemetry import decorators
8 9
9 from telemetry.unittest_util import system_stub 10 from telemetry.unittest_util import system_stub
10 from telemetry.util import cloud_storage 11 from telemetry.util import cloud_storage
11 12
12 13
13 def _FakeFindGsutil(): 14 def _FakeFindGsutil():
14 return 'fake gsutil path' 15 return 'fake gsutil path'
15 16
17 def _FakeReadHash(_):
18 return 'hashthis!'
19
20 def _FakeCalulateHashMatchesRead(_):
21 return 'hashthis!'
22
23 def _FakeCalulateHashNewHash(_):
24 return 'omgnewhash'
25
16 26
17 class CloudStorageUnitTest(unittest.TestCase): 27 class CloudStorageUnitTest(unittest.TestCase):
18 28
19 def _FakeRunCommand(self, cmd): 29 def _FakeRunCommand(self, cmd):
20 pass 30 pass
21 31
22 def testValidCloudUrl(self): 32 def _FakeGet(self, bucket, remote_path, local_path):
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):
23 orig_run_command = cloud_storage._RunCommand 72 orig_run_command = cloud_storage._RunCommand
24 try: 73 try:
25 cloud_storage._RunCommand = self._FakeRunCommand 74 cloud_storage._RunCommand = self._FakeRunCommand
26 remote_path = 'test-remote-path.html' 75 remote_path = 'test-remote-path.html'
27 local_path = 'test-local-path.html' 76 local_path = 'test-local-path.html'
28 cloud_url = cloud_storage.Insert(cloud_storage.PUBLIC_BUCKET, 77 cloud_url = cloud_storage.Insert(cloud_storage.PUBLIC_BUCKET,
29 remote_path, local_path) 78 remote_path, local_path)
30 self.assertEqual('https://console.developers.google.com/m/cloudstorage' 79 self.assertEqual('https://console.developers.google.com/m/cloudstorage'
31 '/b/chromium-telemetry/o/test-remote-path.html', 80 '/b/chromium-telemetry/o/test-remote-path.html',
32 cloud_url) 81 cloud_url)
33 finally: 82 finally:
34 cloud_storage._RunCommand = orig_run_command 83 cloud_storage._RunCommand = orig_run_command
35 84
36 def testExistsReturnsFalse(self): 85 def testExistsReturnsFalse(self):
37 stubs = system_stub.Override(cloud_storage, ['subprocess']) 86 stubs = system_stub.Override(cloud_storage, ['subprocess'])
38 orig_find_gs_util = cloud_storage.FindGsutil 87 orig_find_gs_util = cloud_storage.FindGsutil
39 try: 88 try:
40 stubs.subprocess.Popen.communicate_result = ( 89 stubs.subprocess.Popen.communicate_result = (
41 '', 90 '',
42 'CommandException: One or more URLs matched no objects.\n') 91 'CommandException: One or more URLs matched no objects.\n')
43 stubs.subprocess.Popen.returncode_result = 1 92 stubs.subprocess.Popen.returncode_result = 1
44 cloud_storage.FindGsutil = _FakeFindGsutil 93 cloud_storage.FindGsutil = _FakeFindGsutil
45 self.assertFalse(cloud_storage.Exists('fake bucket', 94 self.assertFalse(cloud_storage.Exists('fake bucket',
46 'fake remote path')) 95 'fake remote path'))
47 finally: 96 finally:
48 stubs.Restore() 97 stubs.Restore()
49 cloud_storage.FindGsutil = orig_find_gs_util 98 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()
OLDNEW
« no previous file with comments | « tools/telemetry/telemetry/util/cloud_storage.py ('k') | tools/telemetry/telemetry/util/find_dependencies.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698