| OLD | NEW |
| (Empty) |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 # | |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 # you may not use this file except in compliance with the License. | |
| 5 # You may obtain a copy of the License at | |
| 6 # | |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 # | |
| 9 # Unless required by applicable law or agreed to in writing, software | |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 # See the License for the specific language governing permissions and | |
| 13 # limitations under the License. | |
| 14 | |
| 15 from gslib.commands.cp import _AppendComponentTrackerToParallelUploadTrackerFile | |
| 16 from gslib.commands.cp import _GetPartitionInfo | |
| 17 from gslib.commands.cp import _HashFilename | |
| 18 from gslib.commands.cp import _ParseParallelUploadTrackerFile | |
| 19 from gslib.commands.cp import _CreateParallelUploadTrackerFile | |
| 20 from gslib.commands.cp import ObjectFromTracker | |
| 21 from gslib.tests.testcase.unit_testcase import GsUtilUnitTestCase | |
| 22 from gslib.util import CreateLock | |
| 23 | |
| 24 | |
| 25 class TestCpFuncs(GsUtilUnitTestCase): | |
| 26 """Unit tests for functions in cp command.""" | |
| 27 | |
| 28 def test_HashFilename(self): | |
| 29 # Tests that _HashFilename function works for both string and unicode | |
| 30 # filenames (without raising any Unicode encode/decode errors). | |
| 31 _HashFilename('file1') | |
| 32 _HashFilename(u'file1') | |
| 33 | |
| 34 def test_GetPartitionInfo(self): | |
| 35 # Simplest case - threshold divides file_size. | |
| 36 (num_components, component_size) = _GetPartitionInfo(300, 200, 10) | |
| 37 self.assertEqual(30, num_components) | |
| 38 self.assertEqual(10, component_size) | |
| 39 | |
| 40 # Threshold = 1 (mod file_size). | |
| 41 (num_components, component_size) = _GetPartitionInfo(301, 200, 10) | |
| 42 self.assertEqual(31, num_components) | |
| 43 self.assertEqual(10, component_size) | |
| 44 | |
| 45 # Threshold = -1 (mod file_size). | |
| 46 (num_components, component_size) = _GetPartitionInfo(299, 200, 10) | |
| 47 self.assertEqual(30, num_components) | |
| 48 self.assertEqual(10, component_size) | |
| 49 | |
| 50 # Too many components needed. | |
| 51 (num_components, component_size) = _GetPartitionInfo(301, 2, 10) | |
| 52 self.assertEqual(2, num_components) | |
| 53 self.assertEqual(151, component_size) | |
| 54 | |
| 55 # Test num_components with huge numbers. | |
| 56 (num_components, component_size) = _GetPartitionInfo((10 ** 150) + 1, | |
| 57 10 ** 200, | |
| 58 10) | |
| 59 self.assertEqual((10 ** 149) + 1, num_components) | |
| 60 self.assertEqual(10, component_size) | |
| 61 | |
| 62 # Test component_size with huge numbers. | |
| 63 (num_components, component_size) = _GetPartitionInfo((10 ** 150) + 1, | |
| 64 10, | |
| 65 10) | |
| 66 self.assertEqual(10, num_components) | |
| 67 self.assertEqual((10 ** 149) + 1, component_size) | |
| 68 | |
| 69 # Test component_size > file_size (make sure we get at least two components. | |
| 70 (num_components, component_size) = _GetPartitionInfo(100, 500, 51) | |
| 71 self.assertEquals(2, num_components) | |
| 72 self.assertEqual(50, component_size) | |
| 73 | |
| 74 def test_ParseParallelUploadTrackerFile(self): | |
| 75 tracker_file_lock = CreateLock() | |
| 76 random_prefix = '123' | |
| 77 objects = ['obj1', '42', 'obj2', '314159'] | |
| 78 contents = '\n'.join([random_prefix] + objects) | |
| 79 fpath = self.CreateTempFile(file_name='foo', | |
| 80 contents=contents) | |
| 81 expected_objects = [ObjectFromTracker(objects[2 * i], objects[2 * i + 1]) | |
| 82 for i in range(0, len(objects) / 2)] | |
| 83 (actual_prefix, actual_objects) = _ParseParallelUploadTrackerFile( | |
| 84 fpath, tracker_file_lock) | |
| 85 self.assertEqual(random_prefix, actual_prefix) | |
| 86 self.assertEqual(expected_objects, actual_objects) | |
| 87 | |
| 88 def test_CreateParallelUploadTrackerFile(self): | |
| 89 tracker_file = self.CreateTempFile(file_name='foo', contents='asdf') | |
| 90 tracker_file_lock = CreateLock() | |
| 91 random_prefix = '123' | |
| 92 objects = ['obj1', '42', 'obj2', '314159'] | |
| 93 expected_contents = [random_prefix] + objects | |
| 94 objects = [ObjectFromTracker(objects[2 * i], objects[2 * i + 1]) | |
| 95 for i in range(0, len(objects) / 2)] | |
| 96 _CreateParallelUploadTrackerFile(tracker_file, random_prefix, objects, | |
| 97 tracker_file_lock) | |
| 98 with open(tracker_file, 'rb') as f: | |
| 99 lines = f.read().splitlines() | |
| 100 self.assertEqual(expected_contents, lines) | |
| 101 | |
| 102 def test_AppendComponentTrackerToParallelUploadTrackerFile(self): | |
| 103 tracker_file = self.CreateTempFile(file_name='foo', contents='asdf') | |
| 104 tracker_file_lock = CreateLock() | |
| 105 random_prefix = '123' | |
| 106 objects = ['obj1', '42', 'obj2', '314159'] | |
| 107 expected_contents = [random_prefix] + objects | |
| 108 objects = [ObjectFromTracker(objects[2 * i], objects[2 * i + 1]) | |
| 109 for i in range(0, len(objects) / 2)] | |
| 110 _CreateParallelUploadTrackerFile(tracker_file, random_prefix, objects, | |
| 111 tracker_file_lock) | |
| 112 | |
| 113 new_object = ['obj2', '1234'] | |
| 114 expected_contents += new_object | |
| 115 new_object = ObjectFromTracker(new_object[0], new_object[1]) | |
| 116 _AppendComponentTrackerToParallelUploadTrackerFile(tracker_file, new_object, | |
| 117 tracker_file_lock) | |
| 118 with open(tracker_file, 'rb') as f: | |
| 119 lines = f.read().splitlines() | |
| 120 self.assertEqual(expected_contents, lines) | |
| OLD | NEW |