Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 """Unit tests for the fetch_builds module.""" | |
| 6 | |
| 7 import errno | |
| 8 import os | |
| 9 import sys | |
| 10 import unittest | |
| 11 | |
| 12 import fetch_build | |
| 13 import mock_wrapper as mock | |
| 14 | |
| 15 | |
| 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): | |
| 20 | |
|
ghost stip (do not use)
2014/09/17 00:12:08
remove blank line
qyearsley
2014/09/18 22:58:59
Google style guide says: "One blank line ... betwe
| |
| 21 def setUp(self): | |
| 22 # Don't print to console when running tests. | |
| 23 sys.stdout = open(os.devnull, 'w') | |
|
ghost stip (do not use)
2014/09/17 00:12:08
I don't think this is needed -- we want those debu
qyearsley
2014/09/18 22:58:59
Done.
| |
| 24 | |
| 25 @mock.patch('fetch_build.os.path.exists') | |
|
ghost stip (do not use)
2014/09/17 00:12:08
I'm not sure if it makes it easier but I used patc
qyearsley
2014/09/18 22:58:59
Using patcher.start() and patcher.stop() looks lik
| |
| 26 @mock.patch('fetch_build.cloud_storage') | |
| 27 def test_FetchFromCloudStorage_FileFound( | |
| 28 self, mock_cloud_storage, mock_os_path_exists): | |
| 29 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 mock_cloud_storage.Get.assert_called_with( | |
| 35 'my_bucket', 'remote/foo.zip', 'local/foo.zip') | |
| 36 | |
| 37 @mock.patch('fetch_build.cloud_storage') | |
| 38 def test_FetchFromCloudStorage_FileNotFound( | |
| 39 self, mock_cloud_storage): | |
| 40 mock_cloud_storage.Exists.return_value = False | |
| 41 local_path = fetch_build.FetchFromCloudStorage( | |
| 42 'my_bucket', 'remote/foo.zip', 'local') | |
| 43 self.assertIsNone(local_path) | |
| 44 self.assertFalse(mock_cloud_storage.Get.called) | |
| 45 | |
| 46 | |
| 47 class BuildArchiveTest(unittest.TestCase): | |
| 48 | |
|
ghost stip (do not use)
2014/09/17 00:12:08
remove blank line
qyearsley
2014/09/18 22:58:59
Same as comment above.
| |
| 49 @mock.patch('fetch_build.bisect_utils') | |
| 50 def test_CreatePerfBuildArchive(self, mock_bisect_utils): | |
| 51 self._SetPlatformLinux(mock_bisect_utils) | |
| 52 instance = fetch_build.BuildArchive.Create(fetch_build.PERF_BUILDER) | |
| 53 self._SetPlatformLinux(mock_bisect_utils) | |
| 54 self.assertEqual('chrome-perf', instance.BucketName()) | |
| 55 self.assertTrue(isinstance(instance, fetch_build.PerfBuildArchive)) | |
| 56 | |
| 57 @mock.patch('fetch_build.bisect_utils') | |
| 58 def test_CreateFullBuildArchive(self, mock_bisect_utils): | |
| 59 self._SetPlatformLinux(mock_bisect_utils) | |
| 60 instance = fetch_build.BuildArchive.Create(fetch_build.FULL_BUILDER) | |
| 61 self.assertEqual('chromium-linux-archive', instance.BucketName()) | |
| 62 self.assertTrue(isinstance(instance, fetch_build.FullBuildArchive)) | |
| 63 | |
| 64 @mock.patch('fetch_build.bisect_utils') | |
| 65 def test_BuildArchive_NonExistentType(self, mock_bisect_utils): | |
| 66 self._SetPlatformLinux(mock_bisect_utils) | |
| 67 self.assertRaises(NotImplementedError, | |
| 68 fetch_build.BuildArchive.Create, 'other') | |
| 69 | |
| 70 @mock.patch('fetch_build.bisect_utils') | |
| 71 def test_FullBuildArchive_Linux(self, mock_bisect_utils): | |
| 72 self._SetPlatformLinux(mock_bisect_utils) | |
| 73 archive = fetch_build.FullBuildArchive() | |
| 74 self.assertEqual('chromium-linux-archive', archive.BucketName()) | |
| 75 self.assertEqual( | |
| 76 'chromium.linux/Linux Builder/full-build-linux_1234567890abcdef.zip', | |
| 77 archive.FilePath('1234567890abcdef')) | |
| 78 | |
| 79 @mock.patch('fetch_build.bisect_utils') | |
| 80 def test_FullBuildArchive_Android(self, mock_bisect_utils): | |
| 81 self._SetPlatformLinux(mock_bisect_utils) | |
| 82 archive = fetch_build.FullBuildArchive(target_platform='android') | |
| 83 self.assertEqual('chromium-android', archive.BucketName()) | |
| 84 self.assertEqual('android_main_rel/full-build-linux_1234567890abcdef.zip', | |
| 85 archive.FilePath('1234567890abcdef')) | |
| 86 | |
| 87 @mock.patch('fetch_build.bisect_utils') | |
| 88 def test_PerfBuildArchive_Linux(self, mock_bisect_utils): | |
| 89 self._SetPlatformLinux(mock_bisect_utils) | |
| 90 archive = fetch_build.PerfBuildArchive() | |
| 91 self.assertEqual('chrome-perf', archive.BucketName()) | |
| 92 self.assertEqual( | |
| 93 'Linux Builder/full-build-linux_1234567890abcdef.zip', | |
| 94 archive.FilePath('1234567890abcdef')) | |
| 95 | |
| 96 @mock.patch('fetch_build.bisect_utils') | |
| 97 def test_PerfBuildArchive_Android(self, mock_bisect_utils): | |
| 98 self._SetPlatformLinux(mock_bisect_utils) | |
| 99 archive = fetch_build.PerfBuildArchive(target_platform='android') | |
| 100 self.assertEqual('chrome-perf', archive.BucketName()) | |
| 101 self.assertEqual( | |
| 102 'android_perf_rel/full-build-linux_123456.zip', | |
| 103 archive.FilePath('123456')) | |
| 104 | |
| 105 @mock.patch('fetch_build.bisect_utils') | |
| 106 def test_PerfBuildArchive_64BitWindows(self, mock_bisect_utils): | |
| 107 mock_bisect_utils.IsLinuxHost.return_value = False | |
| 108 mock_bisect_utils.IsMacHost.return_value = False | |
| 109 mock_bisect_utils.IsWindowsHost.return_value = True | |
| 110 mock_bisect_utils.Is64BitWindows.return_value = True | |
| 111 archive = fetch_build.PerfBuildArchive(target_arch='x64') | |
| 112 self.assertEqual('chrome-perf', archive.BucketName()) | |
| 113 self.assertEqual( | |
| 114 'Win x64 Builder/full-build-win32_123456.zip', | |
| 115 archive.FilePath('123456')) | |
| 116 | |
| 117 @mock.patch('fetch_build.bisect_utils') | |
| 118 def test_PerfBuildArchive_WithDepsPatchSha(self, mock_bisect_utils): | |
| 119 self._SetPlatformLinux(mock_bisect_utils) | |
| 120 archive = fetch_build.PerfBuildArchive() | |
| 121 self.assertEqual( | |
| 122 'Linux Builder/full-build-linux_123456' | |
| 123 '_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.zip', | |
| 124 archive.FilePath(123456, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')) | |
| 125 | |
| 126 @staticmethod | |
| 127 def _SetPlatformLinux(mock_bisect_utils): | |
| 128 mock_bisect_utils.IsLinuxHost.return_value = True | |
| 129 mock_bisect_utils.IsMacHost.return_value = False | |
| 130 mock_bisect_utils.IsWindowsHost.return_value = False | |
| 131 mock_bisect_utils.Is64BitWindows.return_value = False | |
| 132 | |
| 133 | |
| 134 class UnzipTest(unittest.TestCase): | |
| 135 | |
| 136 @mock.patch('fetch_build.bisect_utils') | |
| 137 @mock.patch('fetch_build._MakeDirectory') | |
| 138 @mock.patch('fetch_build._UnzipUsingCommand') | |
| 139 def test_Unzip_Linux( | |
| 140 self, mock_UnzipUsingCommand, mock_MakeDirectory, mock_bisect_utils): | |
| 141 mock_bisect_utils.IsLinuxHost.return_value = True | |
| 142 mock_bisect_utils.IsMacHost.return_value = False | |
| 143 mock_bisect_utils.IsWindowsHost.return_value = False | |
| 144 fetch_build.Unzip('x.zip', 'out_dir', verbose=False) | |
| 145 mock_MakeDirectory.assert_called_with('out_dir') | |
| 146 mock_UnzipUsingCommand.assert_called_with( | |
| 147 ['unzip', '-o'], 'x.zip', 'out_dir') | |
| 148 | |
| 149 @mock.patch('fetch_build.os') | |
| 150 @mock.patch('fetch_build.bisect_utils') | |
| 151 @mock.patch('fetch_build._MakeDirectory') | |
| 152 @mock.patch('fetch_build._UnzipUsingZipFile') | |
| 153 def test_Unzip_Mac_LargeFile( | |
| 154 self, mock_UnzipUsingZipFile, mock_MakeDirectory, mock_bisect_utils, | |
| 155 mock_os): | |
| 156 # The zipfile module is used to unzip on mac when the file is > 4GB. | |
| 157 mock_bisect_utils.IsLinuxHost.return_value = False | |
| 158 mock_bisect_utils.IsMacHost.return_value = True | |
| 159 mock_bisect_utils.IsWindowsHost.return_value = False | |
| 160 mock_os.path.getsize.return_value = 2 ** 33 # 8GB | |
| 161 fetch_build.Unzip('x.zip', 'out_dir', verbose=False) | |
| 162 mock_MakeDirectory.assert_called_with('out_dir') | |
| 163 mock_UnzipUsingZipFile.assert_called_with('x.zip', 'out_dir', False) | |
| 164 | |
| 165 @mock.patch('fetch_build.os') | |
| 166 @mock.patch('fetch_build.bisect_utils.RunProcess') | |
| 167 def test_UnzipUsingCommand(self, mock_RunProcess, mock_os): | |
| 168 # The _UnzipUsingCommand function should move to the output | |
| 169 # directory and run the command with the file's absolute path. | |
| 170 mock_os.path.abspath.return_value = '/foo/some/path/x.zip' | |
| 171 mock_os.getcwd.return_value = 'curr_dir' | |
| 172 mock_RunProcess.return_value = 0 | |
| 173 fetch_build._UnzipUsingCommand(['unzip'], 'x.zip', 'out_dir') | |
| 174 mock_os.chdir.assert_has_calls( | |
| 175 [mock.call('out_dir'), mock.call('curr_dir')]) | |
| 176 mock_RunProcess.assert_called_with(['unzip', '/foo/some/path/x.zip']) | |
| 177 | |
| 178 @mock.patch('fetch_build.os') | |
| 179 def test_MakeDirectory(self, mock_os): | |
| 180 # _MakeDirectory uses os.makedirs. | |
| 181 fetch_build._MakeDirectory('some/path') | |
| 182 mock_os.makedirs.assert_called_with('some/path') | |
| 183 | |
| 184 @mock.patch('fetch_build.os') | |
| 185 def test_MakeDirectory_RaisesError(self, mock_os): | |
| 186 mock_os.makedirs.side_effect = OSError() | |
| 187 self.assertRaises(OSError, fetch_build._MakeDirectory, 'some/path') | |
| 188 | |
| 189 @mock.patch('fetch_build.os') | |
| 190 def test_MakeDirectory_SucceedsIfDirectoryExists(self, mock_os): | |
| 191 already_exists = OSError() | |
| 192 already_exists.errno = errno.EEXIST | |
| 193 mock_os.makedirs.side_effect = already_exists | |
| 194 | |
| 195 | |
| 196 if __name__ == '__main__': | |
| 197 unittest.main() | |
| 198 | |
| OLD | NEW |