Example #1
0
    def choose_sync_strategies(self):
        """Determines the sync strategy for the command.

        It defaults to the default sync strategies but a customizable sync
        strategy can overide the default strategy if it returns the instance
        of its self when the event is emitted.
        """
        sync_strategies = {}
        # Set the default strategies.
        sync_strategies['file_at_src_and_dest_sync_strategy'] = \
            SizeAndLastModifiedSync()
        sync_strategies['file_not_at_dest_sync_strategy'] = MissingFileSync()
        sync_strategies['file_not_at_src_sync_strategy'] = NeverSync()

        # Determine what strategies to overide if any.
        responses = self.session.emit('choosing-s3-sync-strategy',
                                      params=self.parameters)
        if responses is not None:
            for response in responses:
                override_sync_strategy = response[1]
                if override_sync_strategy is not None:
                    sync_type = override_sync_strategy.sync_type
                    sync_type += '_sync_strategy'
                    sync_strategies[sync_type] = override_sync_strategy

        return sync_strategies
Example #2
0
 def setUp(self):
     self.sync_strategy = SizeAndLastModifiedSync()
Example #3
0
 def setUp(self):
     self.sync_strategy = SizeAndLastModifiedSync()
Example #4
0
class TestSizeAndLastModifiedSync(unittest.TestCase):
    def setUp(self):
        self.sync_strategy = SizeAndLastModifiedSync()

    def test_compare_size(self):
        """
        Confirms compare size works.
        """
        time = datetime.datetime.now()
        src_file = FileStat(src='', dest='',
                            compare_key='comparator_test.py', size=11,
                            last_update=time, src_type='local',
                            dest_type='s3', operation_name='upload')
        dest_file = FileStat(src='', dest='',
                             compare_key='comparator_test.py', size=10,
                             last_update=time, src_type='s3',
                             dest_type='local', operation_name='')
        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

    def test_compare_lastmod_upload(self):
        """
        Confirms compare time works for uploads.
        """
        time = datetime.datetime.now()
        future_time = time + datetime.timedelta(0, 3)
        src_file = FileStat(src='', dest='',
                            compare_key='comparator_test.py', size=10,
                            last_update=future_time, src_type='local',
                            dest_type='s3', operation_name='upload')
        dest_file = FileStat(src='', dest='',
                             compare_key='comparator_test.py', size=10,
                             last_update=time, src_type='s3',
                             dest_type='local', operation_name='')
        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

    def test_compare_lastmod_copy(self):
        """
        Confirms compare time works for copies.
        """
        time = datetime.datetime.now()
        future_time = time + datetime.timedelta(0, 3)
        src_file = FileStat(src='', dest='',
                            compare_key='comparator_test.py', size=10,
                            last_update=future_time, src_type='s3',
                            dest_type='s3', operation_name='copy')
        dest_file = FileStat(src='', dest='',
                             compare_key='comparator_test.py', size=10,
                             last_update=time, src_type='s3',
                             dest_type='s3', operation_name='')
        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

    def test_compare_lastmod_download(self):
        """
        Confirms compare time works for downloads.
        """
        time = datetime.datetime.now()
        future_time = time + datetime.timedelta(0, 3)
        src_file = FileStat(src='', dest='',
                            compare_key='comparator_test.py', size=10,
                            last_update=time, src_type='s3',
                            dest_type='local', operation_name='download')
        dest_file = FileStat(src='', dest='',
                             compare_key='comparator_test.py', size=10,
                             last_update=future_time, src_type='local',
                             dest_type='s3', operation_name='')

        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

        # If the source is newer than the destination do not download.
        src_file = FileStat(src='', dest='',
                            compare_key='comparator_test.py', size=10,
                            last_update=future_time, src_type='s3',
                            dest_type='local', operation_name='download')
        dest_file = FileStat(src='', dest='',
                             compare_key='comparator_test.py', size=10,
                             last_update=time, src_type='local',
                             dest_type='s3', operation_name='')

        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertFalse(should_sync)
Example #5
0
class TestSizeAndLastModifiedSync(unittest.TestCase):
    def setUp(self):
        self.sync_strategy = SizeAndLastModifiedSync()

    def test_compare_size(self):
        """
        Confirms compare size works.
        """
        time = datetime.datetime.now()
        src_file = FileStat(src='',
                            dest='',
                            compare_key='comparator_test.py',
                            size=11,
                            last_update=time,
                            src_type='local',
                            dest_type='s3',
                            operation_name='upload')
        dest_file = FileStat(src='',
                             dest='',
                             compare_key='comparator_test.py',
                             size=10,
                             last_update=time,
                             src_type='s3',
                             dest_type='local',
                             operation_name='')
        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

    def test_compare_lastmod_upload(self):
        """
        Confirms compare time works for uploads.
        """
        time = datetime.datetime.now()
        future_time = time + datetime.timedelta(0, 3)
        src_file = FileStat(src='',
                            dest='',
                            compare_key='comparator_test.py',
                            size=10,
                            last_update=future_time,
                            src_type='local',
                            dest_type='s3',
                            operation_name='upload')
        dest_file = FileStat(src='',
                             dest='',
                             compare_key='comparator_test.py',
                             size=10,
                             last_update=time,
                             src_type='s3',
                             dest_type='local',
                             operation_name='')
        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

    def test_compare_lastmod_copy(self):
        """
        Confirms compare time works for copies.
        """
        time = datetime.datetime.now()
        future_time = time + datetime.timedelta(0, 3)
        src_file = FileStat(src='',
                            dest='',
                            compare_key='comparator_test.py',
                            size=10,
                            last_update=future_time,
                            src_type='s3',
                            dest_type='s3',
                            operation_name='copy')
        dest_file = FileStat(src='',
                             dest='',
                             compare_key='comparator_test.py',
                             size=10,
                             last_update=time,
                             src_type='s3',
                             dest_type='s3',
                             operation_name='')
        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

    def test_compare_lastmod_download(self):
        """
        Confirms compare time works for downloads.
        """
        time = datetime.datetime.now()
        future_time = time + datetime.timedelta(0, 3)
        src_file = FileStat(src='',
                            dest='',
                            compare_key='comparator_test.py',
                            size=10,
                            last_update=time,
                            src_type='s3',
                            dest_type='local',
                            operation_name='download')
        dest_file = FileStat(src='',
                             dest='',
                             compare_key='comparator_test.py',
                             size=10,
                             last_update=future_time,
                             src_type='local',
                             dest_type='s3',
                             operation_name='')

        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertTrue(should_sync)

        # If the source is newer than the destination do not download.
        src_file = FileStat(src='',
                            dest='',
                            compare_key='comparator_test.py',
                            size=10,
                            last_update=future_time,
                            src_type='s3',
                            dest_type='local',
                            operation_name='download')
        dest_file = FileStat(src='',
                             dest='',
                             compare_key='comparator_test.py',
                             size=10,
                             last_update=time,
                             src_type='local',
                             dest_type='s3',
                             operation_name='')

        should_sync = self.sync_strategy.determine_should_sync(
            src_file, dest_file)
        self.assertFalse(should_sync)