Path: blob/develop/tests/unit/customizations/s3/test_comparator.py
1569 views
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import datetime13import unittest1415from awscli.testutils import mock1617from awscli.customizations.s3.comparator import Comparator18from awscli.customizations.s3.filegenerator import FileStat192021class ComparatorTest(unittest.TestCase):22def setUp(self):23self.sync_strategy = mock.Mock()24self.not_at_src_sync_strategy = mock.Mock()25self.not_at_dest_sync_strategy = mock.Mock()26self.comparator = Comparator(self.sync_strategy,27self.not_at_dest_sync_strategy,28self.not_at_src_sync_strategy)2930def test_compare_key_equal_should_not_sync(self):31"""32Confirm the appropriate action is taken when the soruce compare key33is equal to the destination compare key.34"""35# Try when the sync strategy says not to sync the file.36self.sync_strategy.determine_should_sync.return_value = False3738src_files = []39dest_files = []40ref_list = []41result_list = []42time = datetime.datetime.now()43src_file = FileStat(src='', dest='',44compare_key='comparator_test.py', size=10,45last_update=time, src_type='local',46dest_type='s3', operation_name='upload')47dest_file = FileStat(src='', dest='',48compare_key='comparator_test.py', size=10,49last_update=time, src_type='s3',50dest_type='local', operation_name='')51src_files.append(src_file)52dest_files.append(dest_file)53files = self.comparator.call(iter(src_files), iter(dest_files))54for filename in files:55result_list.append(filename)56self.assertEqual(result_list, ref_list)5758# Try when the sync strategy says to sync the file.59self.sync_strategy.determine_should_sync.return_value = True6061ref_list = []62result_list = []63files = self.comparator.call(iter(src_files), iter(dest_files))64ref_list.append(src_file)65for filename in files:66result_list.append(filename)67self.assertEqual(result_list, ref_list)6869def test_compare_key_less(self):70"""71Confirm the appropriate action is taken when the soruce compare key72is less than the destination compare key.73"""74self.not_at_src_sync_strategy.determine_should_sync.return_value = False7576# Try when the sync strategy says to sync the file.77self.not_at_dest_sync_strategy.determine_should_sync.return_value = True7879src_files = []80dest_files = []81ref_list = []82result_list = []83time = datetime.datetime.now()84src_file = FileStat(src='', dest='',85compare_key='bomparator_test.py', size=10,86last_update=time, src_type='local',87dest_type='s3', operation_name='upload')88dest_file = FileStat(src='', dest='',89compare_key='comparator_test.py', size=10,90last_update=time, src_type='s3',91dest_type='local', operation_name='')92src_files.append(src_file)93dest_files.append(dest_file)94ref_list.append(src_file)95files = self.comparator.call(iter(src_files), iter(dest_files))96for filename in files:97result_list.append(filename)98self.assertEqual(result_list, ref_list)99100# Now try when the sync strategy says not to sync the file.101self.not_at_dest_sync_strategy.determine_should_sync.return_value = False102result_list = []103ref_list = []104files = self.comparator.call(iter(src_files), iter(dest_files))105for filename in files:106result_list.append(filename)107self.assertEqual(result_list, ref_list)108109110def test_compare_key_greater(self):111"""112Confirm the appropriate action is taken when the soruce compare key113is greater than the destination compare key.114"""115self.not_at_dest_sync_strategy.determine_should_sync.return_value = False116117# Try when the sync strategy says to sync the file.118self.not_at_src_sync_strategy.determine_should_sync.return_value = True119120src_files = []121dest_files = []122ref_list = []123result_list = []124time = datetime.datetime.now()125src_file = FileStat(src='', dest='',126compare_key='domparator_test.py', size=10,127last_update=time, src_type='local',128dest_type='s3', operation_name='upload')129dest_file = FileStat(src='', dest='',130compare_key='comparator_test.py', size=10,131last_update=time, src_type='s3',132dest_type='local', operation_name='')133src_files.append(src_file)134dest_files.append(dest_file)135ref_list.append(dest_file)136files = self.comparator.call(iter(src_files), iter(dest_files))137for filename in files:138result_list.append(filename)139self.assertEqual(result_list, ref_list)140141# Now try when the sync strategy says not to sync the file.142self.not_at_src_sync_strategy.determine_should_sync.return_value = False143result_list = []144ref_list = []145files = self.comparator.call(iter(src_files), iter(dest_files))146for filename in files:147result_list.append(filename)148self.assertEqual(result_list, ref_list)149150151def test_empty_src(self):152"""153Confirm the appropriate action is taken when there are no more source154files to take.155"""156# Try when the sync strategy says to sync the file.157self.not_at_src_sync_strategy.determine_should_sync.return_value = True158159src_files = []160dest_files = []161ref_list = []162result_list = []163time = datetime.datetime.now()164dest_file = FileStat(src='', dest='',165compare_key='comparator_test.py', size=10,166last_update=time, src_type='s3',167dest_type='local', operation_name='')168dest_files.append(dest_file)169ref_list.append(dest_file)170files = self.comparator.call(iter(src_files), iter(dest_files))171for filename in files:172result_list.append(filename)173self.assertEqual(result_list, ref_list)174175# Now try when the sync strategy says not to sync the file.176self.not_at_src_sync_strategy.determine_should_sync.return_value = False177result_list = []178ref_list = []179files = self.comparator.call(iter(src_files), iter(dest_files))180for filename in files:181result_list.append(filename)182self.assertEqual(result_list, ref_list)183184def test_empty_dest(self):185"""186Confirm the appropriate action is taken when there are no more dest187files to take.188"""189# Try when the sync strategy says to sync the file.190self.not_at_dest_sync_strategy.determine_should_sync.return_value = True191192src_files = []193dest_files = []194ref_list = []195result_list = []196time = datetime.datetime.now()197src_file = FileStat(src='', dest='',198compare_key='domparator_test.py', size=10,199last_update=time, src_type='local',200dest_type='s3', operation_name='upload')201src_files.append(src_file)202ref_list.append(src_file)203files = self.comparator.call(iter(src_files), iter(dest_files))204for filename in files:205result_list.append(filename)206self.assertEqual(result_list, ref_list)207208# Now try when the sync strategy says not to sync the file.209self.not_at_dest_sync_strategy.determine_should_sync.return_value = False210result_list = []211ref_list = []212files = self.comparator.call(iter(src_files), iter(dest_files))213for filename in files:214result_list.append(filename)215self.assertEqual(result_list, ref_list)216217218def test_empty_src_dest(self):219"""220Confirm the appropriate action is taken when there are no more221files to take for both source and destination.222"""223src_files = []224dest_files = []225ref_list = []226result_list = []227files = self.comparator.call(iter(src_files), iter(dest_files))228for filename in files:229result_list.append(filename)230self.assertEqual(result_list, ref_list)231232233if __name__ == "__main__":234unittest.main()235236237