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 unittest |
| 9 |
| 10 # The third-party mock module is expected to be available in PYTHONPATH. |
| 11 import mock |
| 12 |
| 13 import fetch_build |
| 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 |
| 21 def setUp(self): |
| 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) |
| 26 |
| 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') |
| 36 |
| 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) |
| 43 |
| 44 |
| 45 class BuildArchiveTest(unittest.TestCase): |
| 46 |
| 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)) |
| 51 |
| 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)) |
| 57 |
| 58 def test_BuildArchive_NonExistentType(self): |
| 59 self.assertRaises( |
| 60 NotImplementedError, fetch_build.BuildArchive.Create, 'other') |
| 61 |
| 62 def test_FullBuildArchive_Linux(self): |
| 63 archive = fetch_build.FullBuildArchive() |
| 64 archive._platform = 'linux' |
| 65 self.assertEqual('chromium-linux-archive', archive.BucketName()) |
| 66 self.assertEqual( |
| 67 'chromium.linux/Linux Builder/full-build-linux_1234567890abcdef.zip', |
| 68 archive.FilePath('1234567890abcdef')) |
| 69 |
| 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')) |
| 76 |
| 77 def test_PerfBuildArchive_Linux(self): |
| 78 archive = fetch_build.PerfBuildArchive() |
| 79 archive._platform = 'linux' |
| 80 self.assertEqual('chrome-perf', archive.BucketName()) |
| 81 self.assertEqual( |
| 82 'Linux Builder/full-build-linux_1234567890abcdef.zip', |
| 83 archive.FilePath('1234567890abcdef')) |
| 84 |
| 85 def test_PerfBuildArchive_Android(self): |
| 86 archive = fetch_build.PerfBuildArchive() |
| 87 archive._platform = 'android' |
| 88 self.assertEqual('chrome-perf', archive.BucketName()) |
| 89 self.assertEqual( |
| 90 'android_perf_rel/full-build-linux_123456.zip', |
| 91 archive.FilePath('123456')) |
| 92 |
| 93 def test_PerfBuildArchive_64BitWindows(self): |
| 94 archive = fetch_build.PerfBuildArchive(target_arch='x64') |
| 95 archive._platform = 'win64' |
| 96 self.assertEqual('chrome-perf', archive.BucketName()) |
| 97 self.assertEqual( |
| 98 'Win x64 Builder/full-build-win32_123456.zip', |
| 99 archive.FilePath('123456')) |
| 100 |
| 101 def test_PerfBuildArchive_WithDepsPatchSha(self): |
| 102 archive = fetch_build.PerfBuildArchive() |
| 103 archive._platform = 'linux' |
| 104 self.assertEqual( |
| 105 'Linux Builder/full-build-linux_123456' |
| 106 '_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.zip', |
| 107 archive.FilePath(123456, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')) |
| 108 |
| 109 |
| 110 class UnzipTest(unittest.TestCase): |
| 111 |
| 112 def setUp(self): |
| 113 # Mocks of the os and bisect_utils modules are used in the methods below. |
| 114 os_patcher = mock.patch('fetch_build.os') |
| 115 self.mock_os = os_patcher.start() |
| 116 self.addCleanup(os_patcher.stop) |
| 117 |
| 118 bisect_utils_patcher = mock.patch('fetch_build.bisect_utils') |
| 119 self.mock_bisect_utils = bisect_utils_patcher.start() |
| 120 self.addCleanup(bisect_utils_patcher.stop) |
| 121 |
| 122 @mock.patch('fetch_build._MakeDirectory') |
| 123 @mock.patch('fetch_build._UnzipUsingCommand') |
| 124 def test_Unzip_Linux(self, mock_UnzipUsingCommand, mock_MakeDirectory): |
| 125 self.mock_bisect_utils.IsLinuxHost.return_value = True |
| 126 self.mock_bisect_utils.IsMacHost.return_value = False |
| 127 self.mock_bisect_utils.IsWindowsHost.return_value = False |
| 128 fetch_build.Unzip('x.zip', 'out_dir', verbose=False) |
| 129 mock_MakeDirectory.assert_called_with('out_dir') |
| 130 mock_UnzipUsingCommand.assert_called_with( |
| 131 ['unzip', '-o'], 'x.zip', 'out_dir') |
| 132 |
| 133 @mock.patch('fetch_build._MakeDirectory') |
| 134 @mock.patch('fetch_build._UnzipUsingZipFile') |
| 135 def test_Unzip_Mac_LargeFile( |
| 136 self, mock_UnzipUsingZipFile, mock_MakeDirectory): |
| 137 # The zipfile module is used to unzip on mac when the file is > 4GB. |
| 138 self.mock_bisect_utils.IsLinuxHost.return_value = False |
| 139 self.mock_bisect_utils.IsMacHost.return_value = True |
| 140 self.mock_bisect_utils.IsWindowsHost.return_value = False |
| 141 self.mock_os.path.getsize.return_value = 2 ** 33 # 8GB |
| 142 fetch_build.Unzip('x.zip', 'out_dir', verbose=False) |
| 143 mock_MakeDirectory.assert_called_with('out_dir') |
| 144 mock_UnzipUsingZipFile.assert_called_with('x.zip', 'out_dir', False) |
| 145 |
| 146 def test_UnzipUsingCommand(self): |
| 147 # The _UnzipUsingCommand function should move to the output |
| 148 # directory and run the command with the file's absolute path. |
| 149 self.mock_os.path.abspath.return_value = '/foo/some/path/x.zip' |
| 150 self.mock_os.getcwd.return_value = 'curr_dir' |
| 151 self.mock_bisect_utils.RunProcess.return_value = 0 |
| 152 fetch_build._UnzipUsingCommand(['unzip'], 'x.zip', 'out_dir') |
| 153 self.mock_os.chdir.assert_has_calls( |
| 154 [mock.call('out_dir'), mock.call('curr_dir')]) |
| 155 self.mock_bisect_utils.RunProcess.assert_called_with( |
| 156 ['unzip', '/foo/some/path/x.zip']) |
| 157 |
| 158 def test_MakeDirectory(self): |
| 159 # _MakeDirectory uses os.makedirs. |
| 160 fetch_build._MakeDirectory('some/path') |
| 161 self.mock_os.makedirs.assert_called_with('some/path') |
| 162 |
| 163 def test_MakeDirectory_RaisesError(self): |
| 164 self.mock_os.makedirs.side_effect = OSError() |
| 165 self.assertRaises(OSError, fetch_build._MakeDirectory, 'some/path') |
| 166 |
| 167 def test_MakeDirectory_NoErrorIfDirectoryAlreadyExists(self): |
| 168 already_exists = OSError() |
| 169 already_exists.errno = errno.EEXIST |
| 170 self.mock_os.makedirs.side_effect = already_exists |
| 171 fetch_build._MakeDirectory('some/path') |
| 172 |
| 173 @mock.patch('fetch_build.shutil') |
| 174 def test_RemoveDirectoryTree(self, mock_shutil): |
| 175 # _RemoveDirectoryTree uses shutil.rmtree. |
| 176 fetch_build._RemoveDirectoryTree('some/path') |
| 177 mock_shutil.rmtree.assert_called_with('some/path') |
| 178 |
| 179 |
| 180 if __name__ == '__main__': |
| 181 unittest.main() |
| 182 |
OLD | NEW |