Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: tests/gstools_unittest.py

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Added exception types, renamed variables Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
5
6 """Unit tests for download_to/upload_from_google_storage.py."""
7
8 import os
9 import sys
10 import unittest
11 import threading
12 import StringIO
13 import Queue
14 import optparse
15
16 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
17
18 import upload_to_google_storage
19 import download_from_google_storage
20
21 # ../third_party/gsutil/gsutil
22 GSUTIL_DEFAULT_PATH = os.path.join(
23 os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
24 'third_party', 'gsutil', 'gsutil')
25
26
27 class GsutilMock(object):
28 def __init__(self, path, boto_path=None, timeout=None):
29 self.path = path
30 self.timeout = timeout
31 self.boto_path = boto_path
32 self.expected = []
33 self.history = []
34 self.lock = threading.Lock()
35
36 def add_expected(self, return_code, out, err):
37 self.expected.append((return_code, out, err))
38
39 def append_history(self, method, args):
40 with self.lock:
41 self.history.append((method, args))
42
43 def call(self, *args):
44 self.append_history('call', args)
45 if self.expected:
46 return self.expected.pop(0)[0]
47 else:
48 return 0
49
50 def check_call(self, *args):
51 self.append_history('check_call', args)
52 if self.expected:
53 return self.expected.pop(0)
54 else:
55 return (0, '', '')
56
57 def clone(self):
58 return self
59
60
61 class GstoolsUnitTests(unittest.TestCase):
62 def setUp(self):
63 self.base_path = os.path.join(
64 os.path.dirname(os.path.abspath(__file__)), 'gstools')
65
66 def test_gsutil(self):
67 gsutil = download_from_google_storage.Gsutil(GSUTIL_DEFAULT_PATH)
68 self.assertEquals(gsutil.path, GSUTIL_DEFAULT_PATH)
69 code, _, err = gsutil.check_call()
70 self.assertEquals(code, 0)
71 self.assertEquals(err, '')
72
73 def test_gsutil_version(self):
74 gsutil = download_from_google_storage.Gsutil(GSUTIL_DEFAULT_PATH)
75 _, _, err = gsutil.check_call('version')
76 err_lines = err.splitlines()
77 self.assertEquals(err_lines[0], 'gsutil version 3.25')
78 self.assertEquals(
79 err_lines[1],
80 'checksum ce71ac982f1148315e7fa65cff2f83e8 (OK)')
81
82 def test_get_sha1(self):
83 lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt')
84 self.assertEquals(
85 download_from_google_storage.get_sha1(lorem_ipsum),
86 '7871c8e24da15bad8b0be2c36edc9dc77e37727f')
87
88 def test_get_md5(self):
89 lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt')
90 self.assertEquals(
91 upload_to_google_storage.get_md5(lorem_ipsum),
92 '634d7c1ed3545383837428f031840a1e')
93
94 def test_get_md5_cached_read(self):
95 lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt')
96 # Use a fake 'stale' MD5 sum. Expected behavior is to return stale sum.
97 self.assertEquals(
98 upload_to_google_storage.get_md5_cached(lorem_ipsum),
99 '734d7c1ed3545383837428f031840a1e')
100
101 def test_get_md5_cached_write(self):
102 lorem_ipsum2 = os.path.join(self.base_path, 'lorem_ipsum2.txt')
103 lorem_ipsum2_md5 = os.path.join(self.base_path, 'lorem_ipsum2.txt.md5')
104 if os.path.exists(lorem_ipsum2_md5):
105 os.remove(lorem_ipsum2_md5)
106 # Use a fake 'stale' MD5 sum. Expected behavior is to return stale sum.
107 self.assertEquals(
108 upload_to_google_storage.get_md5_cached(lorem_ipsum2),
109 '4c02d1eb455a0f22c575265d17b84b6d')
110 self.assertTrue(os.path.exists(lorem_ipsum2_md5))
111 self.assertEquals(
112 open(lorem_ipsum2_md5, 'rb').read(),
113 '4c02d1eb455a0f22c575265d17b84b6d')
114 os.remove(lorem_ipsum2_md5) # Clean up.
115 self.assertFalse(os.path.exists(lorem_ipsum2_md5))
116
117
118 class UploadTests(unittest.TestCase):
119 def setUp(self):
120 self.gsutil = GsutilMock(GSUTIL_DEFAULT_PATH)
121 self.base_path = os.path.join(
122 os.path.dirname(os.path.abspath(__file__)), 'gstools')
123 self.base_url = 'gs://sometesturl'
124 self.parser = optparse.OptionParser()
125 self.ret_codes = Queue.Queue()
126 self.stdout_queue = Queue.Queue()
127 self.lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt')
128 self.lorem_ipsum_sha1 = '7871c8e24da15bad8b0be2c36edc9dc77e37727f'
129
130 def test_upload_single_file(self):
131 filenames = [self.lorem_ipsum]
132 output_filename = '%s.sha1' % self.lorem_ipsum
133 if os.path.exists(output_filename):
134 os.remove(output_filename)
135 upload_to_google_storage.upload_to_google_storage(
136 filenames, self.base_url, self.gsutil, True, False, 1, False)
137 self.assertEquals(
138 self.gsutil.history,
139 [('check_call',
140 ('ls', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1))),
141 ('check_call',
142 ('cp', '-q', filenames[0], '%s/%s' % (self.base_url,
143 self.lorem_ipsum_sha1)))])
144 self.assertTrue(os.path.exists(output_filename))
145 self.assertEquals(
146 open(output_filename, 'rb').read(),
147 '7871c8e24da15bad8b0be2c36edc9dc77e37727f')
148 os.remove(output_filename)
149
150 def test_upload_single_file_remote_exists(self):
151 filenames = [self.lorem_ipsum]
152 output_filename = '%s.sha1' % self.lorem_ipsum
153 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e'
154 if os.path.exists(output_filename):
155 os.remove(output_filename)
156 self.gsutil.add_expected(0, '', '')
157 self.gsutil.add_expected(0, etag_string, '')
158 upload_to_google_storage.upload_to_google_storage(
159 filenames, self.base_url, self.gsutil, False, False, 1, False)
160 self.assertEquals(
161 self.gsutil.history,
162 [('check_call',
163 ('ls', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1))),
164 ('check_call',
165 ('ls', '-L', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))])
166 self.assertTrue(os.path.exists(output_filename))
167 self.assertEquals(
168 open(output_filename, 'rb').read(),
169 '7871c8e24da15bad8b0be2c36edc9dc77e37727f')
170 os.remove(output_filename)
171
172 def test_upload_worker_errors(self):
173 work_queue = Queue.Queue()
174 work_queue.put((self.lorem_ipsum, self.lorem_ipsum_sha1))
175 work_queue.put((None, None))
176 self.gsutil.add_expected(1, '', '') # For the first ls call.
177 self.gsutil.add_expected(20, '', 'Expected error message')
178 # pylint: disable=W0212
179 upload_to_google_storage._upload_worker(
180 0,
181 work_queue,
182 self.base_url,
183 self.gsutil,
184 threading.Lock(),
185 False,
186 False,
187 self.stdout_queue,
188 self.ret_codes)
189 expected_ret_codes = [
190 (20,
191 'Encountered error on uploading %s to %s/%s\nExpected error message' %
192 (self.lorem_ipsum, self.base_url, self.lorem_ipsum_sha1))]
193 self.assertEquals(list(self.ret_codes.queue), expected_ret_codes)
194
195
196 def test_skip_hashing(self):
197 filenames = [self.lorem_ipsum]
198 output_filename = '%s.sha1' % self.lorem_ipsum
199 fake_hash = '6871c8e24da15bad8b0be2c36edc9dc77e37727f'
200 with open(output_filename, 'wb') as f:
201 f.write(fake_hash) # Fake hash.
202 upload_to_google_storage.upload_to_google_storage(
203 filenames, self.base_url, self.gsutil, False, False, 1, True)
204 self.assertEquals(
205 self.gsutil.history,
206 [('check_call',
207 ('ls', '%s/%s' % (self.base_url, fake_hash))),
208 ('check_call',
209 ('ls', '-L', '%s/%s' % (self.base_url, fake_hash))),
210 ('check_call',
211 ('cp', '-q', filenames[0], '%s/%s' % (self.base_url, fake_hash)))])
212 self.assertEquals(
213 open(output_filename, 'rb').read(), fake_hash)
214 os.remove(output_filename)
215
216 def test_get_targets_no_args(self):
217 try:
218 upload_to_google_storage.get_targets([], self.parser, False)
219 except SystemExit, e:
220 self.assertEquals(type(e), type(SystemExit()))
221 self.assertEquals(e.code, 2)
222 except Exception, e:
223 self.fail('unexpected exception: %s' % e)
224 else:
225 self.fail('SystemExit exception expected')
226
227 def test_get_targets_passthrough(self):
228 result = upload_to_google_storage.get_targets(
229 ['a', 'b', 'c', 'd', 'e'],
230 self.parser,
231 False)
232 self.assertEquals(result, ['a', 'b', 'c', 'd', 'e'])
233
234 def test_get_targets_multiple_stdin(self):
235 inputs = ['a', 'b', 'c', 'd', 'e']
236 sys.stdin = StringIO.StringIO(os.linesep.join(inputs))
237 result = upload_to_google_storage.get_targets(
238 ['-'],
239 self.parser,
240 False)
241 self.assertEquals(result, inputs)
242
243 def test_get_targets_multiple_stdin_null(self):
244 inputs = ['a', 'b', 'c', 'd', 'e']
245 sys.stdin = StringIO.StringIO('\0'.join(inputs))
246 result = upload_to_google_storage.get_targets(
247 ['-'],
248 self.parser,
249 True)
250 self.assertEquals(result, inputs)
251
252
253 class DownloadTests(unittest.TestCase):
254 def setUp(self):
255 self.gsutil = GsutilMock(GSUTIL_DEFAULT_PATH)
256 self.base_path = os.path.join(
257 os.path.dirname(os.path.abspath(__file__)),
258 'gstools',
259 'download_test_data')
260 self.base_url = 'gs://sometesturl'
261 self.parser = optparse.OptionParser()
262 self.queue = Queue.Queue()
263 self.lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt')
264 self.lorem_ipsum_sha1 = '7871c8e24da15bad8b0be2c36edc9dc77e37727f'
265 self.maxDiff = None
266
267 def test_enumerate_files_non_recursive(self):
268 queue_size = download_from_google_storage.enumerate_work_queue(
269 self.base_path, self.queue, True, False, False, None, False)
270 result = list(self.queue.queue)
271 expected_queue = [
272 ('e6c4fbd4fe7607f3e6ebf68b2ea4ef694da7b4fe',
273 os.path.join(self.base_path, 'rootfolder_text.txt')),
274 ('7871c8e24da15bad8b0be2c36edc9dc77e37727f',
275 os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt'))]
276 for item in result:
277 self.assertTrue(item in expected_queue)
278 self.assertEquals(queue_size, 2)
279
280 def test_enumerate_files_recursive(self):
281 queue_size = download_from_google_storage.enumerate_work_queue(
282 self.base_path, self.queue, True, True, False, None, False)
283 expected_queue = [
284 ('e6c4fbd4fe7607f3e6ebf68b2ea4ef694da7b4fe',
285 os.path.join(self.base_path, 'rootfolder_text.txt')),
286 ('7871c8e24da15bad8b0be2c36edc9dc77e37727f',
287 os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt')),
288 ('b5415aa0b64006a95c0c409182e628881d6d6463',
289 os.path.join(self.base_path, 'subfolder', 'subfolder_text.txt'))]
290 result = list(self.queue.queue)
291 for item in result:
292 self.assertTrue(item in expected_queue)
293 self.assertEquals(queue_size, 3)
294
295 def test_download_worker_single_file(self):
296 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f'
297 input_filename = '%s/%s' % (self.base_url, sha1_hash)
298 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt')
299 self.queue.put((sha1_hash, output_filename))
300 self.queue.put((None, None))
301 stdout_queue = Queue.Queue()
302 # pylint: disable=W0212
303 download_from_google_storage._downloader_worker_thread(
304 0, self.queue, False, self.base_url, self.gsutil, stdout_queue)
305 expected_calls = [
306 ('check_call',
307 ('ls', input_filename)),
308 ('check_call',
309 ('cp', '-q', input_filename, output_filename))]
310 expected_output = [
311 'Downloading %s to %s...' % (input_filename, output_filename),
312 'Thread 0 is done']
313 self.assertEquals(list(stdout_queue.queue), expected_output)
314 self.assertEquals(self.gsutil.history, expected_calls)
315
316 def test_download_worker_skips_file(self):
317 sha1_hash = 'e6c4fbd4fe7607f3e6ebf68b2ea4ef694da7b4fe'
318 output_filename = os.path.join(self.base_path, 'rootfolder_text.txt')
319 self.queue.put((sha1_hash, output_filename))
320 self.queue.put((None, None))
321 stdout_queue = Queue.Queue()
322 # pylint: disable=W0212
323 download_from_google_storage._downloader_worker_thread(
324 0, self.queue, False, self.base_url, self.gsutil, stdout_queue)
325 expected_output = [
326 'File %s exists and SHA1 sum (%s) matches. Skipping.' %
327 (output_filename, sha1_hash),
328 'Thread 0 is done'
329 ]
330 self.assertEquals(list(stdout_queue.queue), expected_output)
331 self.assertEquals(self.gsutil.history, [])
332
333 def test_download_worker_skips_not_found_file(self):
334 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f'
335 input_filename = '%s/%s' % (self.base_url, sha1_hash)
336 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt')
337 self.queue.put((sha1_hash, output_filename))
338 self.queue.put((None, None))
339 stdout_queue = Queue.Queue()
340 self.gsutil.add_expected(1, '', '') # Return error when 'ls' is called.
341 # pylint: disable=W0212
342 download_from_google_storage._downloader_worker_thread(
343 0, self.queue, False, self.base_url, self.gsutil, stdout_queue)
344 expected_output = [
345 'File %s for %s does not exist, skipping.' % (
346 input_filename, output_filename),
347 'Thread 0 is done'
348 ]
349 expected_calls = [
350 ('check_call',
351 ('ls', input_filename))
352 ]
353 self.assertEquals(list(stdout_queue.queue), expected_output)
354 self.assertEquals(self.gsutil.history, expected_calls)
355
356 def test_download_directory_no_recursive_non_force(self):
357 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f'
358 input_filename = '%s/%s' % (self.base_url, sha1_hash)
359 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt')
360 download_from_google_storage.download_from_google_storage(
361 input_filename=self.base_path,
362 base_url=self.base_url,
363 gsutil=self.gsutil,
364 num_threads=1,
365 directory=True,
366 recursive=False,
367 force=False,
368 output=None,
369 ignore_errors=False,
370 sha1_file=False)
371 expected_calls = [
372 ('check_call',
373 ('ls', input_filename)),
374 ('check_call',
375 ('cp', '-q', input_filename, output_filename))]
376 self.assertEquals(self.gsutil.history, expected_calls)
377
378
379 if __name__ == '__main__':
380 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698