Ejemplo n.º 1
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.º 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_synchronise_repository_with_multiple_volumes():

    mockbackups = [
        'easybackup-myproject-db-20200420_130000.tar',
        'easybackup-myproject-db-20200420_130100.tar',
        'easybackup-myproject-folder-20200420_130000.tar',
        'easybackup-myproject-folder-20200420_130100.tar',
    ]
    Clock.monkey_now('20200423_130000')
    A = Repository(
        adapter=MemoryRepositoryAdapter(bucket='A', backups=mockbackups))
    B = Repository(adapter=MemoryRepositoryAdapter(bucket='B', backups=[]))

    volume = Volume(name='db', project='myproject')
    policy = SynchronizeRecentPolicy(minimum=4, volume=volume)
    tocopy = policy.to_copy(A, B)

    assert len(tocopy) == 2
    assert tocopy[0].volume == 'db'
    assert tocopy[1].volume == 'db'
Ejemplo n.º 4
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.º 5
0
def test_compare_datetime(date, reference, max_age, assertion):
    Clock.monkey_now(reference)
    policy = LifetimeCleanupPolicy(max_age=max_age, minimum=0)
    outdated = policy.outdated(date)
    assert outdated is assertion
    Clock.monkey_now(False)
Ejemplo n.º 6
0
 def wrapper(*args, **kwargs):
     Clock.monkey_now(datetime)
     func(*args, **kwargs)
     Clock.monkey_now(False)
Ejemplo n.º 7
0
def test_synchronize_recent_policy_cleanup_on_target():
    """
    It Guarantee a `minimum` number of backups on the
    target repository.
    If `age_mini` is greater than 0, all outdated backups
    are deleted if the number of backups is enought.
    """

    mockbackups = [
        'easybackup-myproject-db-20200420_130000.tar',
        'easybackup-myproject-db-20200420_130100.tar',
        'easybackup-myproject-db-20200421_130000.tar',
        'easybackup-myproject-db-20200422_130000.tar',
    ]
    A = Repository(
        adapter=MemoryRepositoryAdapter(bucket='A', backups=mockbackups))
    B = Repository(
        adapter=MemoryRepositoryAdapter(bucket='B', force_clear=True))

    # copy the most recent from A to B
    policy = SynchronizeRecentPolicy(minimum=1)
    tocopy = policy.to_copy(A, B)
    todelete = policy.to_delete(A, B)
    assert len(todelete) == 0
    assert len(tocopy) == 1

    # copy all from A to B, and delete all on B
    Clock.monkey_now('20200422_130000')
    A = Repository(adapter=MemoryRepositoryAdapter(
        bucket='A', backups=[mockbackups[2], mockbackups[3]]))
    B = Repository(adapter=MemoryRepositoryAdapter(
        bucket='B', backups=[mockbackups[0], mockbackups[1]]))
    policy = SynchronizeRecentPolicy(minimum=2)
    tocopy = policy.to_copy(A, B)
    todelete = policy.to_delete(A, B)
    assert len(todelete) == 2
    assert len(tocopy) == 2

    # nothing to sync, nothing to delete
    Clock.monkey_now('20200422_130000')
    A = Repository(
        adapter=MemoryRepositoryAdapter(bucket='A', backups=mockbackups))
    B = Repository(
        adapter=MemoryRepositoryAdapter(bucket='B', backups=mockbackups))
    policy = SynchronizeRecentPolicy(minimum=5)
    tocopy = policy.to_copy(A, B)
    todelete = policy.to_delete(A, B)
    assert len(todelete) == 0
    assert len(tocopy) == 0

    # nothing to copy, delete the oldest on B
    Clock.monkey_now('20200422_130000')
    A = Repository(
        adapter=MemoryRepositoryAdapter(bucket='A', backups=mockbackups))
    B = Repository(
        adapter=MemoryRepositoryAdapter(bucket='B', backups=mockbackups))
    policy = SynchronizeRecentPolicy(minimum=3)
    tocopy = policy.to_copy(A, B)
    todelete = policy.to_delete(A, B)
    assert len(todelete) == 1
    assert len(tocopy) == 0

    # copy the two last ones from A to B, and delete two oldest on B
    mockbackups = [
        'easybackup-myproject-db-20200420_130000.tar',
        'easybackup-myproject-db-20200420_130100.tar',
        'easybackup-myproject-db-20200421_130000.tar',
        'easybackup-myproject-db-20200422_130000.tar',
        'easybackup-myproject-db-20200422_140000.tar',
        'easybackup-myproject-db-20200422_150000.tar',
    ]
    Clock.monkey_now('20200423_130000')
    A = Repository(
        adapter=MemoryRepositoryAdapter(bucket='A', backups=mockbackups))
    B = Repository(
        adapter=MemoryRepositoryAdapter(bucket='B', backups=mockbackups[:3]))
    policy = SynchronizeRecentPolicy(minimum=2)
    tocopy = policy.to_copy(A, B)
    todelete = policy.to_delete(A, B)
    assert len(todelete) == 3
    assert len(tocopy) == 2