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
3 # found in the LICENSE file.
5 """Unit tests for the fetch_builds module."""
10 # The third-party mock module is expected to be available in PYTHONPATH.
16 # The tests below test private functions (W0212).
17 # Some methods don't reference self because they use the mock module (R0201).
18 # pylint: disable=R0201,W0212
19 class FetchBuildTest(unittest
.TestCase
):
22 # Mocks of the os and bisect_utils modules are used in the methods below.
23 cloud_storage_patcher
= mock
.patch('fetch_build.cloud_storage')
24 self
.mock_cloud_storage
= cloud_storage_patcher
.start()
25 self
.addCleanup(cloud_storage_patcher
.stop
)
27 @mock.patch('fetch_build.os.path.exists')
28 def test_FetchFromCloudStorage_FileFound(self
, mock_os_path_exists
):
29 self
.mock_cloud_storage
.Exists
.return_value
= True
30 mock_os_path_exists
.return_value
= True
31 local_path
= fetch_build
.FetchFromCloudStorage(
32 'my_bucket', 'remote/foo.zip', 'local')
33 self
.assertEqual('local/foo.zip', local_path
)
34 self
.mock_cloud_storage
.Get
.assert_called_with(
35 'my_bucket', 'remote/foo.zip', 'local/foo.zip')
37 def test_FetchFromCloudStorage_FileNotFound(self
):
38 self
.mock_cloud_storage
.Exists
.return_value
= False
39 local_path
= fetch_build
.FetchFromCloudStorage(
40 'my_bucket', 'remote/foo.zip', 'local')
41 self
.assertIsNone(local_path
)
42 self
.assertFalse(self
.mock_cloud_storage
.Get
.called
)
45 class BuildArchiveTest(unittest
.TestCase
):
47 def test_CreatePerfBuildArchive(self
):
48 archive
= fetch_build
.BuildArchive
.Create(fetch_build
.PERF_BUILDER
)
49 self
.assertEqual('chrome-perf', archive
.BucketName())
50 self
.assertTrue(isinstance(archive
, fetch_build
.PerfBuildArchive
))
52 def test_CreateFullBuildArchive(self
):
53 archive
= fetch_build
.BuildArchive
.Create(fetch_build
.FULL_BUILDER
)
54 archive
._platform
= 'linux'
55 self
.assertEqual('chromium-linux-archive', archive
.BucketName())
56 self
.assertTrue(isinstance(archive
, fetch_build
.FullBuildArchive
))
58 def test_BuildArchive_NonExistentType(self
):
60 NotImplementedError, fetch_build
.BuildArchive
.Create
, 'other')
62 def test_FullBuildArchive_Linux(self
):
63 archive
= fetch_build
.FullBuildArchive()
64 archive
._platform
= 'linux'
65 self
.assertEqual('chromium-linux-archive', archive
.BucketName())
67 'chromium.linux/Linux Builder/full-build-linux_1234567890abcdef.zip',
68 archive
.FilePath('1234567890abcdef'))
70 def test_FullBuildArchive_Android(self
):
71 archive
= fetch_build
.FullBuildArchive()
72 archive
._platform
= 'android'
73 self
.assertEqual('chromium-android', archive
.BucketName())
75 'android_main_rel/full-build-linux_1234567890abcdef.zip',
76 archive
.FilePath('1234567890abcdef'))
78 def test_FullBuildArchive_Linux_BuilderName(self
):
79 archive
= fetch_build
.FullBuildArchive()
80 archive
._platform
= 'linux'
81 self
.assertEqual('linux_full_bisect_builder', archive
.GetBuilderName())
83 def test_FullBuildArchive_Windows_BuildTime(self
):
84 archive
= fetch_build
.FullBuildArchive()
85 archive
._platform
= 'win'
86 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
88 def test_PerfBuildArchive_Linux(self
):
89 archive
= fetch_build
.PerfBuildArchive()
90 archive
._platform
= 'linux'
91 self
.assertEqual('chrome-perf', archive
.BucketName())
93 'Linux Builder/full-build-linux_1234567890abcdef.zip',
94 archive
.FilePath('1234567890abcdef'))
96 def test_PerfBuildArchive_Android(self
):
97 archive
= fetch_build
.PerfBuildArchive()
98 archive
._platform
= 'android'
99 self
.assertEqual('chrome-perf', archive
.BucketName())
101 'android_perf_rel/full-build-linux_123456.zip',
102 archive
.FilePath('123456'))
104 def test_PerfBuildArchive_64BitWindows(self
):
105 archive
= fetch_build
.PerfBuildArchive(target_arch
='x64')
106 archive
._platform
= 'win64'
107 self
.assertEqual('chrome-perf', archive
.BucketName())
109 'Win x64 Builder/full-build-win32_123456.zip',
110 archive
.FilePath('123456'))
112 def test_PerfBuildArchive_WithDepsPatchSha(self
):
113 archive
= fetch_build
.PerfBuildArchive()
114 archive
._platform
= 'linux'
116 'Linux Builder/full-build-linux_123456'
117 '_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.zip',
118 archive
.FilePath(123456, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'))
120 def test_PerfBuildArchive_64BitWindows_BuilderName(self
):
121 archive
= fetch_build
.PerfBuildArchive()
122 archive
._platform
= 'win64'
123 self
.assertEqual('win_x64_perf_bisect_builder', archive
.GetBuilderName())
125 def test_PerfBuildArchive_64BitWindows_BuildTime(self
):
126 archive
= fetch_build
.PerfBuildArchive()
127 archive
._platform
= 'win64'
128 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
130 def test_PerfBuildArchive_Windows_BuilderName(self
):
131 archive
= fetch_build
.PerfBuildArchive()
132 archive
._platform
= 'win'
133 self
.assertEqual('win_perf_bisect_builder', archive
.GetBuilderName())
135 def test_PerfBuildArchive_Windows_BuildTime(self
):
136 archive
= fetch_build
.PerfBuildArchive()
137 archive
._platform
= 'win'
138 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
140 def test_PerfBuildArchive_Linux_BuilderName(self
):
141 archive
= fetch_build
.PerfBuildArchive()
142 archive
._platform
= 'linux'
143 self
.assertEqual('linux_perf_bisect_builder', archive
.GetBuilderName())
145 def test_PerfBuildArchive_Linux_BuildTime(self
):
146 archive
= fetch_build
.PerfBuildArchive()
147 archive
._platform
= 'linux'
148 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
150 def test_PerfBuildArchive_Android_BuilderName(self
):
151 archive
= fetch_build
.PerfBuildArchive()
152 archive
._platform
= 'android'
153 self
.assertEqual('android_perf_bisect_builder', archive
.GetBuilderName())
155 def test_PerfBuildArchive_Android_BuildTime(self
):
156 archive
= fetch_build
.PerfBuildArchive()
157 archive
._platform
= 'android'
158 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
160 def test_PerfBuildArchive_Mac_BuilderName(self
):
161 archive
= fetch_build
.PerfBuildArchive()
162 archive
._platform
= 'mac'
163 self
.assertEqual('mac_perf_bisect_builder', archive
.GetBuilderName())
165 def test_PerfBuildArchive_mac_BuildTime(self
):
166 archive
= fetch_build
.PerfBuildArchive()
167 archive
._platform
= 'mac'
168 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
170 def test_GetBuildBotUrl_Perf(self
):
172 fetch_build
.PERF_TRY_SERVER_URL
,
173 fetch_build
.GetBuildBotUrl(fetch_build
.PERF_BUILDER
))
175 def test_GetBuildBotUrl_full(self
):
177 fetch_build
.LINUX_TRY_SERVER_URL
,
178 fetch_build
.GetBuildBotUrl(fetch_build
.FULL_BUILDER
))
181 class UnzipTest(unittest
.TestCase
):
184 # Mocks of the os and bisect_utils modules are used in the methods below.
185 os_patcher
= mock
.patch('fetch_build.os')
186 self
.mock_os
= os_patcher
.start()
187 self
.addCleanup(os_patcher
.stop
)
189 bisect_utils_patcher
= mock
.patch('fetch_build.bisect_utils')
190 self
.mock_bisect_utils
= bisect_utils_patcher
.start()
191 self
.addCleanup(bisect_utils_patcher
.stop
)
193 @mock.patch('fetch_build._MakeDirectory')
194 @mock.patch('fetch_build._UnzipUsingCommand')
195 def test_Unzip_Linux(self
, mock_UnzipUsingCommand
, mock_MakeDirectory
):
196 self
.mock_bisect_utils
.IsLinuxHost
.return_value
= True
197 self
.mock_bisect_utils
.IsMacHost
.return_value
= False
198 self
.mock_bisect_utils
.IsWindowsHost
.return_value
= False
199 fetch_build
.Unzip('x.zip', 'out_dir', verbose
=False)
200 mock_MakeDirectory
.assert_called_with('out_dir')
201 mock_UnzipUsingCommand
.assert_called_with(
202 ['unzip', '-o'], 'x.zip', 'out_dir')
204 @mock.patch('fetch_build._MakeDirectory')
205 @mock.patch('fetch_build._UnzipUsingZipFile')
206 def test_Unzip_Mac_LargeFile(
207 self
, mock_UnzipUsingZipFile
, mock_MakeDirectory
):
208 # The zipfile module is used to unzip on mac when the file is > 4GB.
209 self
.mock_bisect_utils
.IsLinuxHost
.return_value
= False
210 self
.mock_bisect_utils
.IsMacHost
.return_value
= True
211 self
.mock_bisect_utils
.IsWindowsHost
.return_value
= False
212 self
.mock_os
.path
.getsize
.return_value
= 2 ** 33 # 8GB
213 fetch_build
.Unzip('x.zip', 'out_dir', verbose
=False)
214 mock_MakeDirectory
.assert_called_with('out_dir')
215 mock_UnzipUsingZipFile
.assert_called_with('x.zip', 'out_dir', False)
217 def test_UnzipUsingCommand(self
):
218 # The _UnzipUsingCommand function should move to the output
219 # directory and run the command with the file's absolute path.
220 self
.mock_os
.path
.abspath
.return_value
= '/foo/some/path/x.zip'
221 self
.mock_os
.getcwd
.return_value
= 'curr_dir'
222 self
.mock_bisect_utils
.RunProcess
.return_value
= 0
223 fetch_build
._UnzipUsingCommand
(['unzip'], 'x.zip', 'out_dir')
224 self
.mock_os
.chdir
.assert_has_calls(
225 [mock
.call('out_dir'), mock
.call('curr_dir')])
226 self
.mock_bisect_utils
.RunProcess
.assert_called_with(
227 ['unzip', '/foo/some/path/x.zip'])
229 def test_MakeDirectory(self
):
230 # _MakeDirectory uses os.makedirs.
231 fetch_build
._MakeDirectory
('some/path')
232 self
.mock_os
.makedirs
.assert_called_with('some/path')
234 def test_MakeDirectory_RaisesError(self
):
235 self
.mock_os
.makedirs
.side_effect
= OSError()
236 self
.assertRaises(OSError, fetch_build
._MakeDirectory
, 'some/path')
238 def test_MakeDirectory_NoErrorIfDirectoryAlreadyExists(self
):
239 already_exists
= OSError()
240 already_exists
.errno
= errno
.EEXIST
241 self
.mock_os
.makedirs
.side_effect
= already_exists
242 fetch_build
._MakeDirectory
('some/path')
244 @mock.patch('fetch_build.shutil')
245 def test_RemoveDirectoryTree(self
, mock_shutil
):
246 # _RemoveDirectoryTree uses shutil.rmtree.
247 fetch_build
._RemoveDirectoryTree
('some/path')
248 mock_shutil
.rmtree
.assert_called_with('some/path')
251 if __name__
== '__main__':