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())
74 self
.assertEqual('android_main_rel/full-build-linux_1234567890abcdef.zip',
75 archive
.FilePath('1234567890abcdef'))
77 def test_FullBuildArchive_Linux_BuilderName(self
):
78 archive
= fetch_build
.FullBuildArchive()
79 archive
._platform
= 'linux'
80 self
.assertEqual('linux_full_bisect_builder', archive
.GetBuilderName())
82 def test_FullBuildArchive_Windows_BuildTime(self
):
83 archive
= fetch_build
.FullBuildArchive()
84 archive
._platform
= 'win'
85 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
87 def test_PerfBuildArchive_Linux(self
):
88 archive
= fetch_build
.PerfBuildArchive()
89 archive
._platform
= 'linux'
90 self
.assertEqual('chrome-perf', archive
.BucketName())
92 'Linux Builder/full-build-linux_1234567890abcdef.zip',
93 archive
.FilePath('1234567890abcdef'))
95 def test_PerfBuildArchive_Android(self
):
96 archive
= fetch_build
.PerfBuildArchive()
97 archive
._platform
= 'android'
98 self
.assertEqual('chrome-perf', archive
.BucketName())
100 'android_perf_rel/full-build-linux_123456.zip',
101 archive
.FilePath('123456'))
103 def test_PerfBuildArchive_64BitWindows(self
):
104 archive
= fetch_build
.PerfBuildArchive(target_arch
='x64')
105 archive
._platform
= 'win64'
106 self
.assertEqual('chrome-perf', archive
.BucketName())
108 'Win x64 Builder/full-build-win32_123456.zip',
109 archive
.FilePath('123456'))
111 def test_PerfBuildArchive_WithDepsPatchSha(self
):
112 archive
= fetch_build
.PerfBuildArchive()
113 archive
._platform
= 'linux'
115 'Linux Builder/full-build-linux_123456'
116 '_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.zip',
117 archive
.FilePath(123456, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'))
119 def test_PerfBuildArchive_64BitWindows_BuilderName(self
):
120 archive
= fetch_build
.PerfBuildArchive()
121 archive
._platform
= 'win64'
122 self
.assertEqual('win_x64_perf_bisect_builder', archive
.GetBuilderName())
124 def test_PerfBuildArchive_64BitWindows_BuildTime(self
):
125 archive
= fetch_build
.PerfBuildArchive()
126 archive
._platform
= 'win64'
127 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
129 def test_PerfBuildArchive_Windows_BuilderName(self
):
130 archive
= fetch_build
.PerfBuildArchive()
131 archive
._platform
= 'win'
132 self
.assertEqual('win_perf_bisect_builder', archive
.GetBuilderName())
134 def test_PerfBuildArchive_Windows_BuildTime(self
):
135 archive
= fetch_build
.PerfBuildArchive()
136 archive
._platform
= 'win'
137 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
139 def test_PerfBuildArchive_Linux_BuilderName(self
):
140 archive
= fetch_build
.PerfBuildArchive()
141 archive
._platform
= 'linux'
142 self
.assertEqual('linux_perf_bisect_builder', archive
.GetBuilderName())
144 def test_PerfBuildArchive_Linux_BuildTime(self
):
145 archive
= fetch_build
.PerfBuildArchive()
146 archive
._platform
= 'linux'
147 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
149 def test_PerfBuildArchive_Android_BuilderName(self
):
150 archive
= fetch_build
.PerfBuildArchive()
151 archive
._platform
= 'android'
152 self
.assertEqual('android_perf_bisect_builder', archive
.GetBuilderName())
154 def test_PerfBuildArchive_Android_BuildTime(self
):
155 archive
= fetch_build
.PerfBuildArchive()
156 archive
._platform
= 'android'
157 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
159 def test_PerfBuildArchive_Mac_BuilderName(self
):
160 archive
= fetch_build
.PerfBuildArchive()
161 archive
._platform
= 'mac'
162 self
.assertEqual('mac_perf_bisect_builder', archive
.GetBuilderName())
164 def test_PerfBuildArchive_mac_BuildTime(self
):
165 archive
= fetch_build
.PerfBuildArchive()
166 archive
._platform
= 'mac'
167 self
.assertEqual(14400, archive
.GetBuilderBuildTime())
170 class UnzipTest(unittest
.TestCase
):
173 # Mocks of the os and bisect_utils modules are used in the methods below.
174 os_patcher
= mock
.patch('fetch_build.os')
175 self
.mock_os
= os_patcher
.start()
176 self
.addCleanup(os_patcher
.stop
)
178 bisect_utils_patcher
= mock
.patch('fetch_build.bisect_utils')
179 self
.mock_bisect_utils
= bisect_utils_patcher
.start()
180 self
.addCleanup(bisect_utils_patcher
.stop
)
182 @mock.patch('fetch_build._MakeDirectory')
183 @mock.patch('fetch_build._UnzipUsingCommand')
184 def test_Unzip_Linux(self
, mock_UnzipUsingCommand
, mock_MakeDirectory
):
185 self
.mock_bisect_utils
.IsLinuxHost
.return_value
= True
186 self
.mock_bisect_utils
.IsMacHost
.return_value
= False
187 self
.mock_bisect_utils
.IsWindowsHost
.return_value
= False
188 fetch_build
.Unzip('x.zip', 'out_dir', verbose
=False)
189 mock_MakeDirectory
.assert_called_with('out_dir')
190 mock_UnzipUsingCommand
.assert_called_with(
191 ['unzip', '-o'], 'x.zip', 'out_dir')
193 @mock.patch('fetch_build._MakeDirectory')
194 @mock.patch('fetch_build._UnzipUsingZipFile')
195 def test_Unzip_Mac_LargeFile(
196 self
, mock_UnzipUsingZipFile
, mock_MakeDirectory
):
197 # The zipfile module is used to unzip on mac when the file is > 4GB.
198 self
.mock_bisect_utils
.IsLinuxHost
.return_value
= False
199 self
.mock_bisect_utils
.IsMacHost
.return_value
= True
200 self
.mock_bisect_utils
.IsWindowsHost
.return_value
= False
201 self
.mock_os
.path
.getsize
.return_value
= 2 ** 33 # 8GB
202 fetch_build
.Unzip('x.zip', 'out_dir', verbose
=False)
203 mock_MakeDirectory
.assert_called_with('out_dir')
204 mock_UnzipUsingZipFile
.assert_called_with('x.zip', 'out_dir', False)
206 def test_UnzipUsingCommand(self
):
207 # The _UnzipUsingCommand function should move to the output
208 # directory and run the command with the file's absolute path.
209 self
.mock_os
.path
.abspath
.return_value
= '/foo/some/path/x.zip'
210 self
.mock_os
.getcwd
.return_value
= 'curr_dir'
211 self
.mock_bisect_utils
.RunProcess
.return_value
= 0
212 fetch_build
._UnzipUsingCommand
(['unzip'], 'x.zip', 'out_dir')
213 self
.mock_os
.chdir
.assert_has_calls(
214 [mock
.call('out_dir'), mock
.call('curr_dir')])
215 self
.mock_bisect_utils
.RunProcess
.assert_called_with(
216 ['unzip', '/foo/some/path/x.zip'])
218 def test_MakeDirectory(self
):
219 # _MakeDirectory uses os.makedirs.
220 fetch_build
._MakeDirectory
('some/path')
221 self
.mock_os
.makedirs
.assert_called_with('some/path')
223 def test_MakeDirectory_RaisesError(self
):
224 self
.mock_os
.makedirs
.side_effect
= OSError()
225 self
.assertRaises(OSError, fetch_build
._MakeDirectory
, 'some/path')
227 def test_MakeDirectory_NoErrorIfDirectoryAlreadyExists(self
):
228 already_exists
= OSError()
229 already_exists
.errno
= errno
.EEXIST
230 self
.mock_os
.makedirs
.side_effect
= already_exists
231 fetch_build
._MakeDirectory
('some/path')
233 @mock.patch('fetch_build.shutil')
234 def test_RemoveDirectoryTree(self
, mock_shutil
):
235 # _RemoveDirectoryTree uses shutil.rmtree.
236 fetch_build
._RemoveDirectoryTree
('some/path')
237 mock_shutil
.rmtree
.assert_called_with('some/path')
240 if __name__
== '__main__':