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