2 # Copyright (c) 2012 The Chromium OS 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.
6 """Test the urilib module."""
8 from __future__ import print_function
13 fixup_path.FixupPath()
15 from chromite.lib import cros_test_lib
16 from chromite.lib import osutils
18 from chromite.lib.paygen import filelib
19 from chromite.lib.paygen import gslib
20 from chromite.lib.paygen import unittest_lib
21 from chromite.lib.paygen import urilib
24 # We access private members to test them.
25 # pylint: disable-msg=E1101,W0201
28 class FakeHttpResponse(object):
29 """For simulating http response objects."""
31 class FakeHeaders(object):
32 """Helper class for faking HTTP headers in a response."""
33 def __init__(self, headers_dict):
34 self.headers_dict = headers_dict
36 def getheader(self, name):
37 return self.headers_dict.get(name)
39 def __init__(self, code, headers_dict=None):
41 self.headers = FakeHttpResponse.FakeHeaders(headers_dict)
47 class TestFileManipulation(unittest_lib.TestCase):
48 """Test general urilib file methods together."""
49 # pylint: disable-msg=E1101
54 SUBFILE = '%s/file3' % SUBDIR
56 FILE1_CONTENTS = 'Howdy doody there dandy'
57 FILE2_CONTENTS = 'Once upon a time in a galaxy far far away.'
58 SUBFILE_CONTENTS = 'Five little monkeys jumped on the bed.'
60 GS_DIR = 'gs://chromeos-releases-public/unittest'
63 self.file1_local = os.path.join(self.tempdir, self.FILE1)
64 self.file2_local = os.path.join(self.tempdir, self.FILE2)
65 self.subdir_local = os.path.join(self.tempdir, self.SUBDIR)
66 self.subfile_local = os.path.join(self.tempdir, self.SUBFILE)
68 self.file1_gs = os.path.join(self.GS_DIR, self.FILE1)
69 self.file2_gs = os.path.join(self.GS_DIR, self.FILE2)
70 self.subdir_gs = os.path.join(self.GS_DIR, self.SUBDIR)
71 self.subfile_gs = os.path.join(self.GS_DIR, self.SUBFILE)
73 # Pre-populate local dir with contents.
74 with open(self.file1_local, 'w') as out1:
75 out1.write(self.FILE1_CONTENTS)
77 with open(self.file2_local, 'w') as out2:
78 out2.write(self.FILE2_CONTENTS)
80 os.makedirs(self.subdir_local)
82 with open(self.subfile_local, 'w') as out3:
83 out3.write(self.SUBFILE_CONTENTS)
85 # Make sure gs:// directory is ready (empty).
86 gslib.Remove(os.path.join(self.GS_DIR, '*'), recurse=True,
89 @cros_test_lib.NetworkTest()
90 @osutils.TempDirDecorator
91 def testIntegration(self):
94 self.assertTrue(urilib.Exists(self.tempdir, as_dir=True))
95 self.assertTrue(urilib.Exists(self.file1_local))
96 self.assertTrue(urilib.Exists(self.file2_local))
97 self.assertTrue(urilib.Exists(self.subfile_local))
98 self.assertTrue(urilib.Exists(self.subdir_local, as_dir=True))
100 self.assertFalse(urilib.Exists(self.file1_gs))
101 self.assertFalse(urilib.Exists(self.file2_gs))
102 self.assertFalse(urilib.Exists(self.subfile_gs))
104 shallow_local_files = [self.file1_local, self.file2_local]
105 deep_local_files = shallow_local_files + [self.subfile_local]
106 shallow_gs_files = [self.file1_gs, self.file2_gs]
107 deep_gs_files = shallow_gs_files + [self.subfile_gs]
109 # Test ListFiles, local version.
110 self.assertEquals(set(shallow_local_files),
111 set(urilib.ListFiles(self.tempdir)))
112 self.assertEquals(set(deep_local_files),
113 set(urilib.ListFiles(self.tempdir, recurse=True)))
115 # Test CopyFiles, from local to GS.
116 self.assertEquals(set(deep_gs_files),
117 set(urilib.CopyFiles(self.tempdir, self.GS_DIR)))
119 # Test ListFiles, GS version.
120 self.assertEquals(set(shallow_gs_files),
121 set(urilib.ListFiles(self.GS_DIR)))
122 self.assertEquals(set(deep_gs_files),
123 set(urilib.ListFiles(self.GS_DIR, recurse=True)))
125 # Test Cmp between some files.
126 self.assertTrue(urilib.Cmp(self.file1_local, self.file1_gs))
127 self.assertFalse(urilib.Cmp(self.file2_local, self.file1_gs))
129 # Test RemoveDirContents, local version.
130 urilib.RemoveDirContents(self.tempdir)
131 self.assertFalse(urilib.ListFiles(self.tempdir))
133 # Test CopyFiles, from GS to local.
134 self.assertEquals(set(deep_local_files),
135 set(urilib.CopyFiles(self.GS_DIR, self.tempdir)))
137 # Test RemoveDirContents, GS version.
138 urilib.RemoveDirContents(self.GS_DIR)
139 self.assertFalse(urilib.ListFiles(self.GS_DIR))
142 class TestUrilib(unittest_lib.MoxTestCase):
143 """Test urilib module."""
145 def testExtractProtocol(self):
146 tests = {'gs': ['gs://',
160 for protocol in tests:
161 for uri in tests[protocol]:
162 self.assertEquals(protocol, urilib.ExtractProtocol(uri))
164 def testGetUriType(self):
165 tests = {'gs': ['gs://',
180 for uri_type in tests:
181 for uri in tests[uri_type]:
182 self.assertEquals(uri_type, urilib.GetUriType(uri))
184 def testSplitURI(self):
186 ['gs', 'foo', 'gs://foo'],
187 ['gs', 'foo/bar', 'gs://foo/bar'],
188 ['file', '/foo/bar', 'file:///foo/bar'],
189 [None, '/foo/bar', '/foo/bar'],
194 protocol, path = urilib.SplitURI(uri)
195 self.assertEquals(test[0], protocol)
196 self.assertEquals(test[1], path)
198 def testIsGsURI(self):
199 tests_true = ('gs://',
203 for test in tests_true:
204 self.assertTrue(urilib.IsGsURI(test))
206 tests_false = ('gsfoo/bar',
215 for test in tests_false:
216 self.assertFalse(urilib.IsGsURI(test))
218 def testIsFileURI(self):
219 tests_true = ('file://',
227 for test in tests_true:
228 self.assertTrue(urilib.IsFileURI(test))
230 tests_false = ('gs://',
234 for test in tests_false:
235 self.assertFalse(urilib.IsFileURI(test))
237 def testIsHttpURI(self):
238 tests_true = ('http://',
242 for test in tests_true:
243 self.assertTrue(urilib.IsHttpURI(test))
245 tests_https_true = ('https://',
249 for test in tests_https_true:
250 self.assertTrue(urilib.IsHttpURI(test, https_ok=True))
251 for test in tests_https_true:
252 self.assertFalse(urilib.IsHttpURI(test))
254 tests_false = ('httpfoo/bar',
263 for test in tests_false:
264 self.assertFalse(urilib.IsHttpURI(test))
266 def testIsHttpsURI(self):
267 tests_true = ('https://',
271 for test in tests_true:
272 self.assertTrue(urilib.IsHttpsURI(test))
274 tests_false = ('http://',
286 for test in tests_false:
287 self.assertFalse(urilib.IsHttpsURI(test))
289 def testMD5Sum(self):
290 gs_path = 'gs://bucket/some/path'
291 local_path = '/some/local/path'
292 http_path = 'http://host.domain/some/path'
294 self.mox.StubOutWithMock(gslib, 'MD5Sum')
295 self.mox.StubOutWithMock(filelib, 'MD5Sum')
297 # Set up the test replay script.
299 gslib.MD5Sum(gs_path).AndReturn('TheResult')
301 filelib.MD5Sum(local_path).AndReturn('TheResult')
304 # Run the test verification.
305 self.assertEquals('TheResult', urilib.MD5Sum(gs_path))
306 self.assertEquals('TheResult', urilib.MD5Sum(local_path))
307 self.assertRaises(urilib.NotSupportedForType, urilib.MD5Sum, http_path)
311 gs_path = 'gs://bucket/some/path'
312 local_path = '/some/local/path'
313 http_path = 'http://host.domain/some/path'
317 self.mox.StubOutWithMock(gslib, 'Cmp')
318 self.mox.StubOutWithMock(filelib, 'Cmp')
320 # Set up the test replay script.
321 # Run 1, two local files.
322 filelib.Cmp(local_path, local_path + '.1').AndReturn(result)
323 # Run 2, local and GS.
324 gslib.Cmp(local_path, gs_path).AndReturn(result)
326 gslib.Cmp(gs_path, gs_path + '.1').AndReturn(result)
327 # Run 7, local and HTTP
330 # Run the test verification.
331 self.assertEquals(result, urilib.Cmp(local_path, local_path + '.1'))
332 self.assertEquals(result, urilib.Cmp(local_path, gs_path))
333 self.assertEquals(result, urilib.Cmp(gs_path, gs_path + '.1'))
334 self.assertRaises(urilib.NotSupportedBetweenTypes, urilib.Cmp,
335 local_path, http_path)
338 @cros_test_lib.NetworkTest()
339 @osutils.TempDirDecorator
340 def testURLRetrieve(self):
341 good_url = 'https://codereview.chromium.org/download/issue11731004_1_2.diff'
342 bad_domain_url = 'http://notarealdomainireallyhope.com/some/path'
343 bad_path_url = 'https://dl.google.com/dl/edgedl/x/y/z/a/b/c/foobar'
344 local_path = os.path.join(self.tempdir, 'downloaded_file')
345 bad_local_path = '/tmp/a/b/c/d/x/y/z/foobar'
347 git_index1 = 'e6c0d72a5122171deb4c458991d1c7547f31a2f0'
348 git_index2 = '3d0f7d3edfd8146031e66dc3f45926920d3ded78'
349 expected_contents = """Index: LICENSE
350 diff --git a/LICENSE b/LICENSE
355 -// Copyright (c) 2012 The Chromium Authors. All rights reserved.
356 +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
358 // Redistribution and use in source and binary forms, with or without
359 // modification, are permitted provided that the following conditions are
360 """ % (git_index1, git_index2)
362 self.assertRaises(urilib.MissingURLError, urilib.URLRetrieve,
363 bad_path_url, local_path)
364 self.assertRaises(urilib.MissingURLError, urilib.URLRetrieve,
365 bad_domain_url, local_path)
367 urilib.URLRetrieve(good_url, local_path)
368 with open(local_path, 'r') as f:
369 actual_contents = f.read()
370 self.assertEqual(expected_contents, actual_contents)
372 self.assertRaises(IOError, urilib.URLRetrieve, good_url, bad_local_path)
376 gs_path = 'gs://bucket/some/path'
377 local_path = '/some/local/path'
378 http_path = 'http://host.domain/some/path'
382 self.mox.StubOutWithMock(gslib, 'Copy')
383 self.mox.StubOutWithMock(filelib, 'Copy')
384 self.mox.StubOutWithMock(urilib, 'URLRetrieve')
386 # Set up the test replay script.
387 # Run 1, two local files.
388 filelib.Copy(local_path, local_path + '.1').AndReturn(result)
389 # Run 2, local and GS.
390 gslib.Copy(local_path, gs_path).AndReturn(result)
392 gslib.Copy(gs_path, gs_path + '.1').AndReturn(result)
393 # Run 7, HTTP and local
394 urilib.URLRetrieve(http_path, local_path).AndReturn(result)
395 # Run 8, local and HTTP
398 # Run the test verification.
399 self.assertEquals(result, urilib.Copy(local_path, local_path + '.1'))
400 self.assertEquals(result, urilib.Copy(local_path, gs_path))
401 self.assertEquals(result, urilib.Copy(gs_path, gs_path + '.1'))
402 self.assertEquals(result, urilib.Copy(http_path, local_path))
403 self.assertRaises(urilib.NotSupportedBetweenTypes, urilib.Copy,
404 local_path, http_path)
407 def testRemove(self):
408 gs_path = 'gs://bucket/some/path'
409 local_path = '/some/local/path'
410 http_path = 'http://host.domain/some/path'
412 self.mox.StubOutWithMock(gslib, 'Remove')
413 self.mox.StubOutWithMock(filelib, 'Remove')
415 # Set up the test replay script.
416 # Run 1, two local files.
417 filelib.Remove(local_path, local_path + '.1')
418 # Run 2, local and GS.
419 gslib.Remove(local_path, gs_path, ignore_no_match=True)
421 gslib.Remove(gs_path, gs_path + '.1',
422 ignore_no_match=True, recurse=True)
423 # Run 7, local and HTTP
426 # Run the test verification.
427 urilib.Remove(local_path, local_path + '.1')
428 urilib.Remove(local_path, gs_path, ignore_no_match=True)
429 urilib.Remove(gs_path, gs_path + '.1', ignore_no_match=True, recurse=True)
430 self.assertRaises(urilib.NotSupportedForTypes, urilib.Remove,
431 local_path, http_path)
435 gs_path = 'gs://bucket/some/path'
436 local_path = '/some/local/path'
437 http_path = 'http://host.domain/some/path'
438 ftp_path = 'ftp://host.domain/some/path'
441 http_response = FakeHttpResponse(200, {'Content-Length': str(result)})
443 self.mox.StubOutWithMock(gslib, 'FileSize')
444 self.mox.StubOutWithMock(filelib, 'Size')
445 self.mox.StubOutWithMock(urilib.urllib2, 'urlopen')
447 # Set up the test replay script.
449 filelib.Size(local_path).AndReturn(result)
451 gslib.FileSize(gs_path).AndReturn(result)
453 urilib.urllib2.urlopen(http_path).AndReturn(http_response)
457 # Run the test verification.
458 self.assertEquals(result, urilib.Size(local_path))
459 self.assertEquals(result, urilib.Size(gs_path))
460 self.assertEquals(result, urilib.Size(http_path))
461 self.assertRaises(urilib.NotSupportedForType, urilib.Size, ftp_path)
464 def testExists(self):
465 gs_path = 'gs://bucket/some/path'
466 local_path = '/some/local/path'
467 http_path = 'http://host.domain/some/path'
468 ftp_path = 'ftp://host.domain/some/path'
472 self.mox.StubOutWithMock(gslib, 'Exists')
473 self.mox.StubOutWithMock(filelib, 'Exists')
474 self.mox.StubOutWithMock(urilib.urllib2, 'urlopen')
476 # Set up the test replay script.
477 # Run 1, local, as_dir=False
478 filelib.Exists(local_path, as_dir=False).AndReturn(result)
479 # Run 2, GS, as_dir=False.
480 gslib.Exists(gs_path).AndReturn(result)
481 # Run 3, GS, as_dir=True.
482 # Run 6, HTTP, as_dir=False, code=200.
483 urilib.urllib2.urlopen(http_path).AndReturn(FakeHttpResponse(200))
484 # Run 7, HTTP, as_dir=False, code=404.
485 urilib.urllib2.urlopen(http_path).AndReturn(FakeHttpResponse(404))
486 # Run 8, HTTP, as_dir=False, HTTPError.
487 urilib.urllib2.urlopen(http_path).AndRaise(
488 urilib.urllib2.HTTPError('url', 404, 'msg', None, None))
489 # Run 9, HTTP, as_dir=True.
490 # Run 10, FTP, as_dir=False.
493 # Run the test verification.
494 self.assertEquals(result, urilib.Exists(local_path))
495 self.assertEquals(result, urilib.Exists(gs_path))
496 self.assertEquals(False, urilib.Exists(gs_path, as_dir=True))
497 self.assertTrue(urilib.Exists(http_path))
498 self.assertFalse(urilib.Exists(http_path))
499 self.assertFalse(urilib.Exists(http_path))
500 self.assertRaises(urilib.NotSupportedForType,
501 urilib.Exists, http_path, as_dir=True)
502 self.assertRaises(urilib.NotSupportedForType, urilib.Exists, ftp_path)
505 def testListFiles(self):
506 gs_path = 'gs://bucket/some/path'
507 local_path = '/some/local/path'
508 http_path = 'http://host.domain/some/path'
511 patt = 'TheFilePattern'
513 self.mox.StubOutWithMock(gslib, 'ListFiles')
514 self.mox.StubOutWithMock(filelib, 'ListFiles')
516 # Set up the test replay script.
518 filelib.ListFiles(local_path, recurse=True, filepattern=None, sort=False
521 gslib.ListFiles(gs_path, recurse=False, filepattern=patt, sort=True
526 # Run the test verification.
527 self.assertEquals(result, urilib.ListFiles(local_path, recurse=True))
528 self.assertEquals(result, urilib.ListFiles(gs_path, filepattern=patt,
530 self.assertRaises(urilib.NotSupportedForType, urilib.ListFiles, http_path)
534 if __name__ == '__main__':