1 # -*- coding: utf-8 -*- 2 # Copyright 2015 Google Inc. All Rights Reserved. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 """Unit tests for tracker_file.""" 16 17 from gslib.tests.testcase.unit_testcase import GsUtilUnitTestCase 18 from gslib.third_party.storage_apitools import storage_v1_messages as apitools_messages 19 from gslib.tracker_file import _HashFilename 20 from gslib.tracker_file import DeleteTrackerFile 21 from gslib.tracker_file import GetRewriteTrackerFilePath 22 from gslib.tracker_file import HashRewriteParameters 23 from gslib.tracker_file import ReadRewriteTrackerFile 24 from gslib.tracker_file import WriteRewriteTrackerFile 25 26 27 class TestTrackerFile(GsUtilUnitTestCase): 28 """Unit tests for parallel upload functions in cp command.""" 29 30 def test_HashFilename(self): 31 # Tests that _HashFilename function works for both string and unicode 32 # filenames (without raising any Unicode encode/decode errors). 33 _HashFilename('file1') 34 _HashFilename(u'file1') 35 36 def test_RewriteTrackerFile(self): 37 """Tests Rewrite tracker file functions.""" 38 tracker_file_name = GetRewriteTrackerFilePath('bk1', 'obj1', 'bk2', 'obj2', 39 self.test_api) 40 # Should succeed regardless of whether it exists. 41 DeleteTrackerFile(tracker_file_name) 42 src_obj_metadata = apitools_messages.Object( 43 bucket='bk1', name='obj1', etag='etag1', md5Hash='12345') 44 src_obj2_metadata = apitools_messages.Object( 45 bucket='bk1', name='obj1', etag='etag2', md5Hash='67890') 46 dst_obj_metadata = apitools_messages.Object( 47 bucket='bk2', name='obj2') 48 rewrite_token = 'token1' 49 self.assertIsNone(ReadRewriteTrackerFile(tracker_file_name, 50 src_obj_metadata)) 51 rewrite_params_hash = HashRewriteParameters( 52 src_obj_metadata, dst_obj_metadata, 'full') 53 WriteRewriteTrackerFile(tracker_file_name, rewrite_params_hash, 54 rewrite_token) 55 self.assertEqual( 56 ReadRewriteTrackerFile(tracker_file_name, rewrite_params_hash), 57 rewrite_token) 58 59 # Tracker file for an updated source object (with non-matching etag/md5) 60 # should return None. 61 rewrite_params_hash2 = HashRewriteParameters( 62 src_obj2_metadata, dst_obj_metadata, 'full') 63 64 self.assertIsNone(ReadRewriteTrackerFile(tracker_file_name, 65 rewrite_params_hash2)) 66 DeleteTrackerFile(tracker_file_name) 67