Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 # pylint: disable=W0212 | 5 # pylint: disable=W0212 |
| 6 | 6 |
| 7 """Unit tests for download_from_google_storage.py.""" | 7 """Unit tests for download_from_google_storage.py.""" |
| 8 | 8 |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| 11 import Queue | 11 import Queue |
| 12 import shutil | 12 import shutil |
| 13 import sys | 13 import sys |
| 14 import tarfile | |
| 14 import tempfile | 15 import tempfile |
| 15 import threading | 16 import threading |
| 16 import unittest | 17 import unittest |
| 17 | 18 |
| 18 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 19 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 19 | 20 |
| 20 import upload_to_google_storage | 21 import upload_to_google_storage |
| 21 import download_from_google_storage | 22 import download_from_google_storage |
| 22 | 23 |
| 23 # ../third_party/gsutil/gsutil | 24 # ../third_party/gsutil/gsutil |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 50 else: | 51 else: |
| 51 return 0 | 52 return 0 |
| 52 | 53 |
| 53 def check_call(self, *args): | 54 def check_call(self, *args): |
| 54 with self.lock: | 55 with self.lock: |
| 55 self.append_history('check_call', args) | 56 self.append_history('check_call', args) |
| 56 if self.expected: | 57 if self.expected: |
| 57 return self.expected.pop(0) | 58 return self.expected.pop(0) |
| 58 else: | 59 else: |
| 59 return (0, '', '') | 60 return (0, '', '') |
| 60 | 61 |
|
Ryan Tseng
2015/06/25 17:19:14
2 newlines (top level block)
ricow1
2015/06/26 06:18:04
Done.
| |
| 62 class ChangedWorkingDirectory(object): | |
| 63 def __init__(self, working_directory): | |
| 64 self._old_cwd = '' | |
| 65 self._working_directory = working_directory | |
| 66 | |
| 67 def __enter__(self): | |
| 68 self._old_cwd = os.getcwd() | |
| 69 print "Enter directory = ", self._working_directory | |
| 70 os.chdir(self._working_directory) | |
| 71 | |
| 72 def __exit__(self, *_): | |
| 73 print "Enter directory = ", self._old_cwd | |
| 74 os.chdir(self._old_cwd) | |
| 61 | 75 |
|
Ryan Tseng
2015/06/25 17:19:14
2 newlines
ricow1
2015/06/26 06:18:04
Done.
| |
| 62 class GstoolsUnitTests(unittest.TestCase): | 76 class GstoolsUnitTests(unittest.TestCase): |
| 63 def setUp(self): | 77 def setUp(self): |
| 64 self.temp_dir = tempfile.mkdtemp(prefix='gstools_test') | 78 self.temp_dir = tempfile.mkdtemp(prefix='gstools_test') |
| 65 self.base_path = os.path.join(self.temp_dir, 'test_files') | 79 self.base_path = os.path.join(self.temp_dir, 'test_files') |
| 66 shutil.copytree(os.path.join(TEST_DIR, 'gstools'), self.base_path) | 80 shutil.copytree(os.path.join(TEST_DIR, 'gstools'), self.base_path) |
| 67 | 81 |
| 68 def cleanUp(self): | 82 def cleanUp(self): |
| 69 shutil.rmtree(self.temp_dir) | 83 shutil.rmtree(self.temp_dir) |
| 70 | 84 |
| 85 def test_validate_tar_file(self): | |
| 86 lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt') | |
| 87 with ChangedWorkingDirectory(self.base_path): | |
| 88 # Sanity ok check. | |
| 89 tar_dir = 'ok_dir' | |
| 90 os.makedirs(os.path.join(self.base_path, tar_dir)) | |
| 91 tar = 'good.tar.gz' | |
| 92 lorem_ipsum_copy = os.path.join(tar_dir, 'lorem_ipsum.txt') | |
| 93 shutil.copyfile(lorem_ipsum, lorem_ipsum_copy) | |
| 94 with tarfile.open(tar, 'w:gz') as tar: | |
| 95 tar.add(lorem_ipsum_copy) | |
| 96 self.assertTrue( | |
| 97 download_from_google_storage._validate_tar_file(tar, tar_dir)) | |
| 98 | |
| 99 # Test no links. | |
| 100 tar_dir_link = 'for_tar_link' | |
| 101 os.makedirs(tar_dir_link) | |
| 102 link = os.path.join(tar_dir_link, 'link') | |
| 103 os.symlink(lorem_ipsum, link) | |
| 104 tar_with_links = 'with_links.tar.gz' | |
| 105 with tarfile.open(tar_with_links, 'w:gz') as tar: | |
| 106 tar.add(link) | |
| 107 self.assertFalse( | |
| 108 download_from_google_storage._validate_tar_file(tar, tar_dir_link)) | |
| 109 | |
| 110 # Test not outside. | |
| 111 tar_dir_outside = 'outside_tar' | |
| 112 os.makedirs(tar_dir_outside) | |
| 113 tar_with_outside = 'with_outside.tar.gz' | |
| 114 with tarfile.open(tar_with_outside, 'w:gz') as tar: | |
| 115 tar.add(lorem_ipsum) | |
| 116 self.assertFalse( | |
| 117 download_from_google_storage._validate_tar_file(tar, | |
| 118 tar_dir_outside)) | |
| 119 # Test no .. | |
| 120 tar_with_dotdot = 'with_dotdot.tar.gz' | |
| 121 dotdot_file = os.path.join(tar_dir, '..', tar_dir, 'lorem_ipsum.txt') | |
| 122 with tarfile.open(tar_with_dotdot, 'w:gz') as tar: | |
| 123 tar.add(dotdot_file) | |
| 124 self.assertFalse( | |
| 125 download_from_google_storage._validate_tar_file(tar, | |
| 126 tar_dir)) | |
| 127 | |
| 71 def test_gsutil(self): | 128 def test_gsutil(self): |
| 72 gsutil = download_from_google_storage.Gsutil(GSUTIL_DEFAULT_PATH, None) | 129 gsutil = download_from_google_storage.Gsutil(GSUTIL_DEFAULT_PATH, None) |
| 73 self.assertEqual(gsutil.path, GSUTIL_DEFAULT_PATH) | 130 self.assertEqual(gsutil.path, GSUTIL_DEFAULT_PATH) |
| 74 code, _, err = gsutil.check_call() | 131 code, _, err = gsutil.check_call() |
| 75 self.assertEqual(code, 0) | 132 self.assertEqual(code, 0) |
| 76 self.assertEqual(err, '') | 133 self.assertEqual(err, '') |
| 77 | 134 |
| 78 def test_get_sha1(self): | 135 def test_get_sha1(self): |
| 79 lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt') | 136 lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt') |
| 80 self.assertEqual( | 137 self.assertEqual( |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 | 214 |
| 158 def test_download_worker_single_file(self): | 215 def test_download_worker_single_file(self): |
| 159 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' | 216 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' |
| 160 input_filename = '%s/%s' % (self.base_url, sha1_hash) | 217 input_filename = '%s/%s' % (self.base_url, sha1_hash) |
| 161 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') | 218 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') |
| 162 self.queue.put((sha1_hash, output_filename)) | 219 self.queue.put((sha1_hash, output_filename)) |
| 163 self.queue.put((None, None)) | 220 self.queue.put((None, None)) |
| 164 stdout_queue = Queue.Queue() | 221 stdout_queue = Queue.Queue() |
| 165 download_from_google_storage._downloader_worker_thread( | 222 download_from_google_storage._downloader_worker_thread( |
| 166 0, self.queue, False, self.base_url, self.gsutil, | 223 0, self.queue, False, self.base_url, self.gsutil, |
| 167 stdout_queue, self.ret_codes, True) | 224 stdout_queue, self.ret_codes, True, False) |
| 168 expected_calls = [ | 225 expected_calls = [ |
| 169 ('check_call', | 226 ('check_call', |
| 170 ('ls', input_filename)), | 227 ('ls', input_filename)), |
| 171 ('check_call', | 228 ('check_call', |
| 172 ('cp', input_filename, output_filename))] | 229 ('cp', input_filename, output_filename))] |
| 173 if sys.platform != 'win32': | 230 if sys.platform != 'win32': |
| 174 expected_calls.append( | 231 expected_calls.append( |
| 175 ('check_call', | 232 ('check_call', |
| 176 ('stat', | 233 ('stat', |
| 177 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) | 234 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) |
| 178 expected_output = [ | 235 expected_output = [ |
| 179 '0> Downloading %s...' % output_filename] | 236 '0> Downloading %s...' % output_filename] |
| 180 expected_ret_codes = [] | 237 expected_ret_codes = [] |
| 181 self.assertEqual(list(stdout_queue.queue), expected_output) | 238 self.assertEqual(list(stdout_queue.queue), expected_output) |
| 182 self.assertEqual(self.gsutil.history, expected_calls) | 239 self.assertEqual(self.gsutil.history, expected_calls) |
| 183 self.assertEqual(list(self.ret_codes.queue), expected_ret_codes) | 240 self.assertEqual(list(self.ret_codes.queue), expected_ret_codes) |
| 184 | 241 |
| 185 def test_download_worker_skips_file(self): | 242 def test_download_worker_skips_file(self): |
| 186 sha1_hash = 'e6c4fbd4fe7607f3e6ebf68b2ea4ef694da7b4fe' | 243 sha1_hash = 'e6c4fbd4fe7607f3e6ebf68b2ea4ef694da7b4fe' |
| 187 output_filename = os.path.join(self.base_path, 'rootfolder_text.txt') | 244 output_filename = os.path.join(self.base_path, 'rootfolder_text.txt') |
| 188 self.queue.put((sha1_hash, output_filename)) | 245 self.queue.put((sha1_hash, output_filename)) |
| 189 self.queue.put((None, None)) | 246 self.queue.put((None, None)) |
| 190 stdout_queue = Queue.Queue() | 247 stdout_queue = Queue.Queue() |
| 191 download_from_google_storage._downloader_worker_thread( | 248 download_from_google_storage._downloader_worker_thread( |
| 192 0, self.queue, False, self.base_url, self.gsutil, | 249 0, self.queue, False, self.base_url, self.gsutil, |
| 193 stdout_queue, self.ret_codes, True) | 250 stdout_queue, self.ret_codes, True, False) |
| 194 expected_output = [ | 251 expected_output = [ |
| 195 '0> File %s exists and SHA1 matches. Skipping.' % output_filename | 252 '0> File %s exists and SHA1 matches. Skipping.' % output_filename |
| 196 ] | 253 ] |
| 197 self.assertEqual(list(stdout_queue.queue), expected_output) | 254 self.assertEqual(list(stdout_queue.queue), expected_output) |
| 198 self.assertEqual(self.gsutil.history, []) | 255 self.assertEqual(self.gsutil.history, []) |
| 199 | 256 |
| 257 def test_download_extract_archive(self): | |
| 258 # By design we make this not match | |
| 259 sha1_hash = '61223e1ad3d86901a57629fee38313db5ec106ff' | |
| 260 input_filename = '%s/%s' % (self.base_url, sha1_hash) | |
| 261 # Generate a gzipped tarfile | |
| 262 output_filename = os.path.join(self.base_path, 'subfolder.tar.gz') | |
| 263 output_dirname = os.path.join(self.base_path, 'subfolder') | |
| 264 extracted_filename = os.path.join(output_dirname, 'subfolder_text.txt') | |
| 265 with tarfile.open(output_filename, 'w:gz') as tar: | |
| 266 tar.add(output_dirname, arcname='subfolder') | |
| 267 shutil.rmtree(output_dirname) | |
| 268 print(output_dirname) | |
| 269 self.queue.put((sha1_hash, output_filename)) | |
| 270 self.queue.put((None, None)) | |
| 271 stdout_queue = Queue.Queue() | |
| 272 download_from_google_storage._downloader_worker_thread( | |
| 273 0, self.queue, False, self.base_url, self.gsutil, | |
| 274 stdout_queue, self.ret_codes, True, True, delete=False) | |
| 275 expected_calls = [ | |
| 276 ('check_call', | |
| 277 ('ls', input_filename)), | |
| 278 ('check_call', | |
| 279 ('cp', input_filename, output_filename))] | |
| 280 if sys.platform != 'win32': | |
| 281 expected_calls.append( | |
| 282 ('check_call', | |
| 283 ('stat', | |
| 284 'gs://sometesturl/61223e1ad3d86901a57629fee38313db5ec106ff'))) | |
| 285 expected_output = [ | |
| 286 '0> Downloading %s...' % output_filename] | |
| 287 expected_output.extend([ | |
| 288 '0> Extracting 3 entries from %s to %s' % (output_filename, | |
| 289 output_dirname)]) | |
| 290 expected_ret_codes = [] | |
| 291 self.assertEqual(list(stdout_queue.queue), expected_output) | |
| 292 self.assertEqual(self.gsutil.history, expected_calls) | |
| 293 self.assertEqual(list(self.ret_codes.queue), expected_ret_codes) | |
| 294 self.assertTrue(os.path.exists(output_dirname)) | |
| 295 self.assertTrue(os.path.exists(extracted_filename)) | |
| 296 | |
| 297 | |
|
Ryan Tseng
2015/06/25 17:19:14
remove newline (since not a top level block)
ricow1
2015/06/26 06:18:04
Done.
| |
| 200 def test_download_worker_skips_not_found_file(self): | 298 def test_download_worker_skips_not_found_file(self): |
| 201 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' | 299 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' |
| 202 input_filename = '%s/%s' % (self.base_url, sha1_hash) | 300 input_filename = '%s/%s' % (self.base_url, sha1_hash) |
| 203 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') | 301 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') |
| 204 self.queue.put((sha1_hash, output_filename)) | 302 self.queue.put((sha1_hash, output_filename)) |
| 205 self.queue.put((None, None)) | 303 self.queue.put((None, None)) |
| 206 stdout_queue = Queue.Queue() | 304 stdout_queue = Queue.Queue() |
| 207 self.gsutil.add_expected(1, '', '') # Return error when 'ls' is called. | 305 self.gsutil.add_expected(1, '', '') # Return error when 'ls' is called. |
| 208 download_from_google_storage._downloader_worker_thread( | 306 download_from_google_storage._downloader_worker_thread( |
| 209 0, self.queue, False, self.base_url, self.gsutil, | 307 0, self.queue, False, self.base_url, self.gsutil, |
| 210 stdout_queue, self.ret_codes, True) | 308 stdout_queue, self.ret_codes, True, False) |
| 211 expected_output = [ | 309 expected_output = [ |
| 212 '0> Failed to fetch file %s for %s, skipping. [Err: ]' % ( | 310 '0> Failed to fetch file %s for %s, skipping. [Err: ]' % ( |
| 213 input_filename, output_filename), | 311 input_filename, output_filename), |
| 214 ] | 312 ] |
| 215 expected_calls = [ | 313 expected_calls = [ |
| 216 ('check_call', | 314 ('check_call', |
| 217 ('ls', input_filename)) | 315 ('ls', input_filename)) |
| 218 ] | 316 ] |
| 219 expected_ret_codes = [ | 317 expected_ret_codes = [ |
| 220 (1, 'Failed to fetch file %s for %s. [Err: ]' % ( | 318 (1, 'Failed to fetch file %s for %s. [Err: ]' % ( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 235 base_url=self.base_url, | 333 base_url=self.base_url, |
| 236 gsutil=self.gsutil, | 334 gsutil=self.gsutil, |
| 237 num_threads=1, | 335 num_threads=1, |
| 238 directory=False, | 336 directory=False, |
| 239 recursive=False, | 337 recursive=False, |
| 240 force=True, | 338 force=True, |
| 241 output=output_filename, | 339 output=output_filename, |
| 242 ignore_errors=False, | 340 ignore_errors=False, |
| 243 sha1_file=False, | 341 sha1_file=False, |
| 244 verbose=True, | 342 verbose=True, |
| 245 auto_platform=False) | 343 auto_platform=False, |
| 344 extract=False) | |
| 246 expected_calls = [ | 345 expected_calls = [ |
| 247 ('check_call', | 346 ('check_call', |
| 248 ('ls', input_filename)), | 347 ('ls', input_filename)), |
| 249 ('check_call', | 348 ('check_call', |
| 250 ('cp', input_filename, output_filename)) | 349 ('cp', input_filename, output_filename)) |
| 251 ] | 350 ] |
| 252 if sys.platform != 'win32': | 351 if sys.platform != 'win32': |
| 253 expected_calls.append( | 352 expected_calls.append( |
| 254 ('check_call', | 353 ('check_call', |
| 255 ('stat', | 354 ('stat', |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 266 base_url=self.base_url, | 365 base_url=self.base_url, |
| 267 gsutil=self.gsutil, | 366 gsutil=self.gsutil, |
| 268 num_threads=1, | 367 num_threads=1, |
| 269 directory=True, | 368 directory=True, |
| 270 recursive=False, | 369 recursive=False, |
| 271 force=False, | 370 force=False, |
| 272 output=None, | 371 output=None, |
| 273 ignore_errors=False, | 372 ignore_errors=False, |
| 274 sha1_file=False, | 373 sha1_file=False, |
| 275 verbose=True, | 374 verbose=True, |
| 276 auto_platform=False) | 375 auto_platform=False, |
| 376 extract=False) | |
| 277 expected_calls = [ | 377 expected_calls = [ |
| 278 ('check_call', | 378 ('check_call', |
| 279 ('ls', input_filename)), | 379 ('ls', input_filename)), |
| 280 ('check_call', | 380 ('check_call', |
| 281 ('cp', input_filename, output_filename))] | 381 ('cp', input_filename, output_filename))] |
| 282 if sys.platform != 'win32': | 382 if sys.platform != 'win32': |
| 283 expected_calls.append( | 383 expected_calls.append( |
| 284 ('check_call', | 384 ('check_call', |
| 285 ('stat', | 385 ('stat', |
| 286 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) | 386 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) |
| 287 self.assertEqual(self.gsutil.history, expected_calls) | 387 self.assertEqual(self.gsutil.history, expected_calls) |
| 288 self.assertEqual(code, 0) | 388 self.assertEqual(code, 0) |
| 289 | 389 |
| 290 | 390 |
| 291 if __name__ == '__main__': | 391 if __name__ == '__main__': |
| 292 unittest.main() | 392 unittest.main() |
| OLD | NEW |