Ejemplo n.º 1
0
def test_compute_if_should_backup_with_a_time_interval_policy_and_no_backups():
    memory_adapter = MemoryRepositoryAdapter(backups=[])
    backups = Repository(adapter=memory_adapter).fetch()
    policy = TimeIntervalBackupPolicy(
        interval=24*60*60
    )
    should = policy.should_backup(backups)
    assert should is True
Ejemplo n.º 2
0
def test_compute_if_should_backup_with_a_time_interval_policy(clock, interval, expected):
    memory_adapter = MemoryRepositoryAdapter(backups=mockbackups)
    backups = Repository(adapter=memory_adapter).fetch()

    policy = TimeIntervalBackupPolicy(
        interval=interval
    )
    Clock.monkey_now(clock)
    should = policy.should_backup(backups)
    Clock.monkey_now(False)

    assert should == expected
Ejemplo n.º 3
0
def test_delete_backup_on_ftp_repository(temp_directory):

    randomfile(temp_directory('production-A.txt'))
    randomfile(temp_directory('production-B.txt'))
    ftp_adapter = FtpRepositoryAdapter(**FTP_CONF)

    local_creator_A = LocalBackupCreator(
        source=temp_directory('production-A.txt'),
        backup_directory=temp_directory('backups'))
    local_creator_B = LocalBackupCreator(
        source=temp_directory('production-B.txt'),
        backup_directory=temp_directory('backups-twine'))

    composerA = BackupSupervisor(project='myprojectA',
                                 volume='db',
                                 creator=local_creator_A,
                                 repository=local_creator_A.target_repository,
                                 cleanup_policy=False,
                                 backup_policy=TimeIntervalBackupPolicy(10))

    composerB = BackupSupervisor(project='myprojectB',
                                 volume='db',
                                 creator=local_creator_B,
                                 repository=local_creator_B.target_repository,
                                 cleanup_policy=ClearAllCleanupPolicy(),
                                 backup_policy=TimeIntervalBackupPolicy(10))

    # Backup
    composerA.run()
    composerB.run()

    assert len(composerA.fetch()) == 1
    assert len(composerB.fetch()) == 1

    link = LocalToFtp(source=local_creator_A.target_adapter(),
                      target=ftp_adapter)
    link.synchronize()

    link = LocalToFtp(source=local_creator_B.target_adapter(),
                      target=ftp_adapter)
    link.synchronize()

    # Clear
    rep = Repository(adapter=ftp_adapter)
    rep.cleanup_backups(local_creator_A.target_adapter().fetch_backups())
    backups = rep.fetch()

    assert len(Volume('db', 'myprojectA').match(backups)) == 0
    assert len(Volume('db', 'myprojectB').match(backups)) == 1
Ejemplo n.º 4
0
def test_should_create_new_backup_with_time_interval_backup_policy(clock, backup_interval, should):
    Clock.monkey_now(clock)
    memory_adapter = MemoryRepositoryAdapter(backups=mockbackups)

    rep = Repository(
        adapter=memory_adapter
    )
    last = rep.last_backup()

    backup_policy = TimeIntervalBackupPolicy(backup_interval)

    composer = BackupSupervisor(
        project='myproject',
        volume='db',
        creator=memory_backup_creator,
        repository=rep,
        cleanup_policy=False,
        backup_policy=backup_policy
    )

    composer.run()

    backups = rep.fetch()
    assert backups[-1].datetime == clock if should else last.datetime
    Clock.monkey_now(False)
Ejemplo n.º 5
0
def test_setup_a_cleanup_policy_on_synchronized_repository():

    adapterA = MemoryRepositoryAdapter(bucket='A', force_clear=True)
    adapterB = MemoryRepositoryAdapter(bucket='B', force_clear=True)

    link = MemoryRepositoryLink(adapterA, adapterB)
    creator = MemoryBackupCreator(bucket='A')

    composer = BackupSupervisor(project='myproject',
                                volume='db',
                                creator=creator,
                                backup_policy=TimeIntervalBackupPolicy(1000),
                                cleanup_policy=False,
                                synchronizers=[
                                    Synchroniser(
                                        link,
                                        SynchronizeRecentPolicy(minimum=2)),
                                ])

    Clock.monkey_now('20200101_000000')
    composer.run()

    Clock.monkey_now('20200102_000000')
    composer.run()

    assert len(adapterB.fetch_backups()) == 2

    Clock.monkey_now('20200112_000000')
    composer.run()
    assert len(adapterB.fetch_backups()) == 2
Ejemplo n.º 6
0
def test_backup_then_restore_local_file_to_ftp_repository(temp_directory):

    with assert_backup_and_restore(
            source_file=temp_directory('production.txt'),
            archive_file=temp_directory(
                'restore/easybackup-myproject-db-20200102_120000.tar')):

        local_creator = LocalBackupCreator(
            source=temp_directory('production.txt'),
            backup_directory=temp_directory('backups'))

        ftp_adapter = FtpRepositoryAdapter(**FTP_CONF)
        volume = Volume(name='db', project='myproject')

        composer = BackupSupervisor(project='myproject',
                                    volume='db',
                                    creator=local_creator,
                                    repository=local_creator.target_repository,
                                    cleanup_policy=False,
                                    backup_policy=TimeIntervalBackupPolicy(10))
        composer.run()

        link = LocalToFtp(source=local_creator.target_adapter(),
                          target=ftp_adapter,
                          volume=volume)
        link.synchronize()

        target = link.target_adapter
        backups = target.fetch_backups()

        backup = volume.match(backups)[0]

        local_restore = LocalRepositoryAdapter(
            directory=temp_directory('restore'))

        link = FtpToLocal(source=ftp_adapter,
                          target=local_restore,
                          volume=Volume(name='db', project='myproject'))
        link.copy_backup(backup)

    backups = link.source_repository.fetch()
    assert len(backups) > 0

    link.source_repository.cleanup(policy=ClearAllCleanupPolicy())

    backups = link.source_repository.fetch()
    assert len(backups) == 0
Ejemplo n.º 7
0
def test_composer_can_infer_repository_from_creator():

    repository = Repository(
        adapter=MemoryRepositoryAdapter(bucket='C', force_clear=True))
    creator = MemoryBackupCreator(target_bucket='C')
    backup_policy = TimeIntervalBackupPolicy(1000)

    composer = BackupSupervisor(project='myproject',
                                volume='db',
                                creator=creator,
                                cleanup_policy=False,
                                backup_policy=backup_policy)

    backups = repository.fetch()
    assert len(backups) == 0

    composer.run()

    backups = repository.fetch()
    assert len(backups) == 1
Ejemplo n.º 8
0
def test_composer_backup_can_be_synchronizeed_to_other_repositories():

    adapterA = MemoryRepositoryAdapter(bucket='A', force_clear=True)
    adapterB = MemoryRepositoryAdapter(bucket='B', force_clear=True)
    adapterC = MemoryRepositoryAdapter(bucket='C', force_clear=True)

    repository_a = Repository(adapter=adapterA)
    repository_b = Repository(adapter=adapterB)
    repository_c = Repository(adapter=adapterC)
    backup_policy = TimeIntervalBackupPolicy(1000)

    link_to_b = MemoryRepositoryLink(adapterA, adapterB)
    link_to_c = MemoryRepositoryLink(adapterA, adapterC)

    creator = MemoryBackupCreator(bucket='A')
    composer = BackupSupervisor(project='myproject',
                                volume='db',
                                creator=creator,
                                backup_policy=backup_policy,
                                cleanup_policy=False,
                                synchronizers=[
                                    Synchroniser(link_to_b, CopyPastePolicy()),
                                    Synchroniser(link_to_c, CopyPastePolicy())
                                ])

    backups_on_a = repository_a.fetch()
    backups_on_b = repository_b.fetch()
    backups_on_c = repository_c.fetch()
    assert len(backups_on_a) == 0
    assert len(backups_on_b) == 0
    assert len(backups_on_c) == 0

    composer.run()

    backups_on_a = repository_a.fetch()
    backups_on_b = repository_b.fetch()
    backups_on_c = repository_c.fetch()
    assert len(backups_on_a) == 1
    assert len(backups_on_c) == 1