Path: blob/develop/tests/unit/customizations/s3/syncstrategy/test_base.py
1569 views
# Copyright 2014 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 datetime1314from awscli.customizations.s3.filegenerator import FileStat15from awscli.customizations.s3.syncstrategy.base import BaseSync, \16SizeAndLastModifiedSync, MissingFileSync, NeverSync17from awscli.testutils import mock, unittest181920class TestBaseSync(unittest.TestCase):21def setUp(self):22self.sync_strategy = BaseSync()2324def test_init(self):25valid_sync_types = ['file_at_src_and_dest', 'file_not_at_dest',26'file_not_at_src']27for sync_type in valid_sync_types:28strategy = BaseSync(sync_type)29self.assertEqual(strategy.sync_type, sync_type)3031# Check for invalid ``sync_type`` options.32with self.assertRaises(ValueError):33BaseSync('wrong_sync_type')3435def test_register_strategy(self):36"""37Ensures that the class registers all of the necessary handlers38"""39session = mock.Mock()40self.sync_strategy.register_strategy(session)41register_args = session.register.call_args_list42self.assertEqual(register_args[0][0][0],43'building-arg-table.sync')44self.assertEqual(register_args[0][0][1],45self.sync_strategy.add_sync_argument)46self.assertEqual(register_args[1][0][0], 'choosing-s3-sync-strategy')47self.assertEqual(register_args[1][0][1],48self.sync_strategy.use_sync_strategy)4950def test_determine_should_sync(self):51"""52Ensure that this class cannot be directly used as the sync strategy.53"""54with self.assertRaises(NotImplementedError):55self.sync_strategy.determine_should_sync(None, None)5657def test_arg_name(self):58"""59Ensure that the ``arg_name`` property works as expected.60"""61self.assertEqual(self.sync_strategy.arg_name, None)62self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}63self.assertEqual(self.sync_strategy.arg_name, 'my-sync-strategy')6465def test_arg_dest(self):66"""67Ensure that the ``arg_dest`` property works as expected.68"""69self.assertEqual(self.sync_strategy.arg_dest, None)70self.sync_strategy.ARGUMENT = {'dest': 'my-dest'}71self.assertEqual(self.sync_strategy.arg_dest, 'my-dest')7273def test_add_sync_argument(self):74"""75Ensures the sync argument is properly added to the76the command's ``arg_table``.77"""78arg_table = [{'name': 'original_argument'}]79self.sync_strategy.ARGUMENT = {'name': 'sync_argument'}80self.sync_strategy.add_sync_argument(arg_table)81self.assertEqual(arg_table,82[{'name': 'original_argument'},83{'name': 'sync_argument'}])8485def test_no_add_sync_argument_for_no_argument_specified(self):86"""87Ensures nothing is added to the command's ``arg_table`` if no88``ARGUMENT`` table is specified.89"""90arg_table = [{'name': 'original_argument'}]91self.sync_strategy.add_sync_argument(arg_table)92self.assertEqual(arg_table, [{'name': 'original_argument'}])9394def test_no_use_sync_strategy_for_no_argument_specified(self):95"""96Test if that the sync strategy is not returned if it has no argument.97"""98params = {'my_sync_strategy': True}99self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)100101def test_use_sync_strategy_for_name_and_no_dest(self):102"""103Test if sync strategy argument has ``name`` but no ``dest`` and the104strategy was called in ``params``.105"""106self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}107params = {'my_sync_strategy': True}108self.assertEqual(self.sync_strategy.use_sync_strategy(params),109self.sync_strategy)110111def test_no_use_sync_strategy_for_name_and_no_dest(self):112"""113Test if sync strategy argument has ``name`` but no ``dest`` but114the strategy was not called in ``params``.115"""116self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}117params = {'my_sync_strategy': False}118self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)119120def test_no_use_sync_strategy_for_not_in_params(self):121"""122Test if sync strategy argument has a ``name`` but for whatever reason123the strategy is not in ``params``.124"""125self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}126self.assertEqual(self.sync_strategy.use_sync_strategy({}), None)127128def test_use_sync_strategy_for_name_and_dest(self):129"""130Test if sync strategy argument has ``name`` and ``dest`` and the131strategy was called in ``params``.132"""133self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy',134'dest': 'my-dest'}135params = {'my-dest': True}136self.assertEqual(self.sync_strategy.use_sync_strategy(params),137self.sync_strategy)138139def test_no_use_sync_strategy_for_name_and_dest(self):140"""141Test if sync strategy argument has ``name`` and ``dest`` but the142the strategy was not called in ``params``.143"""144self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy',145'dest': 'my-dest'}146params = {'my-dest': False}147self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)148149def test_no_use_sync_strategy_for_dest_but_only_name_in_params(self):150"""151Test if sync strategy argument has ``name`` and ``dest`` but the152the strategy was not called in ``params`` even though the ``name`` was153called in ``params``.154"""155self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy',156'dest': 'my-dest'}157params = {'my-sync-strategy': True}158self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)159160161class TestSizeAndLastModifiedSync(unittest.TestCase):162def setUp(self):163self.sync_strategy = SizeAndLastModifiedSync()164165def test_compare_size(self):166"""167Confirms compare size works.168"""169time = datetime.datetime.now()170src_file = FileStat(src='', dest='',171compare_key='comparator_test.py', size=11,172last_update=time, src_type='local',173dest_type='s3', operation_name='upload')174dest_file = FileStat(src='', dest='',175compare_key='comparator_test.py', size=10,176last_update=time, src_type='s3',177dest_type='local', operation_name='')178should_sync = self.sync_strategy.determine_should_sync(179src_file, dest_file)180self.assertTrue(should_sync)181182def test_compare_lastmod_upload(self):183"""184Confirms compare time works for uploads.185"""186time = datetime.datetime.now()187future_time = time + datetime.timedelta(0, 3)188src_file = FileStat(src='', dest='',189compare_key='comparator_test.py', size=10,190last_update=future_time, src_type='local',191dest_type='s3', operation_name='upload')192dest_file = FileStat(src='', dest='',193compare_key='comparator_test.py', size=10,194last_update=time, src_type='s3',195dest_type='local', operation_name='')196should_sync = self.sync_strategy.determine_should_sync(197src_file, dest_file)198self.assertTrue(should_sync)199200def test_compare_lastmod_copy(self):201"""202Confirms compare time works for copies.203"""204time = datetime.datetime.now()205future_time = time + datetime.timedelta(0, 3)206src_file = FileStat(src='', dest='',207compare_key='comparator_test.py', size=10,208last_update=future_time, src_type='s3',209dest_type='s3', operation_name='copy')210dest_file = FileStat(src='', dest='',211compare_key='comparator_test.py', size=10,212last_update=time, src_type='s3',213dest_type='s3', operation_name='')214should_sync = self.sync_strategy.determine_should_sync(215src_file, dest_file)216self.assertTrue(should_sync)217218def test_compare_lastmod_download(self):219"""220Confirms compare time works for downloads.221"""222time = datetime.datetime.now()223future_time = time + datetime.timedelta(0, 3)224src_file = FileStat(src='', dest='',225compare_key='comparator_test.py', size=10,226last_update=time, src_type='s3',227dest_type='local', operation_name='download')228dest_file = FileStat(src='', dest='',229compare_key='comparator_test.py', size=10,230last_update=future_time, src_type='local',231dest_type='s3', operation_name='')232233should_sync = self.sync_strategy.determine_should_sync(234src_file, dest_file)235self.assertTrue(should_sync)236237# If the source is newer than the destination do not download.238src_file = FileStat(src='', dest='',239compare_key='comparator_test.py', size=10,240last_update=future_time, src_type='s3',241dest_type='local', operation_name='download')242dest_file = FileStat(src='', dest='',243compare_key='comparator_test.py', size=10,244last_update=time, src_type='local',245dest_type='s3', operation_name='')246247should_sync = self.sync_strategy.determine_should_sync(248src_file, dest_file)249self.assertFalse(should_sync)250251252class TestNeverSync(unittest.TestCase):253def setUp(self):254self.sync_strategy = NeverSync()255256def test_constructor(self):257self.assertEqual(self.sync_strategy.sync_type, 'file_not_at_src')258259def test_determine_should_sync(self):260time_dst = datetime.datetime.now()261262dst_file = FileStat(src='', dest='',263compare_key='test.py', size=10,264last_update=time_dst, src_type='s3',265dest_type='local', operation_name='')266267should_sync = self.sync_strategy.determine_should_sync(268None, dst_file)269self.assertFalse(should_sync)270271272class TestMissingFileSync(unittest.TestCase):273def setUp(self):274self.sync_strategy = MissingFileSync()275276def test_constructor(self):277self.assertEqual(self.sync_strategy.sync_type, 'file_not_at_dest')278279def test_determine_should_sync(self):280time_src = datetime.datetime.now()281282src_file = FileStat(src='', dest='',283compare_key='test.py', size=10,284last_update=time_src, src_type='s3',285dest_type='local', operation_name='')286287should_sync = self.sync_strategy.determine_should_sync(288src_file, None)289self.assertTrue(should_sync)290291292if __name__ == "__main__":293unittest.main()294295296