def test_when_unknown_container_then_nothing(client):
    container_name = 'unknown'
    date = utcnow().format('YYYY-MM-DD')
    backup_container(container_name)

    assert not client.images.exists('_'.join([date, container_name]),
                                    alias=True)
def test_when_nominal_then_container_is_running_after_backup(
        given_running_container, client):
    container_name = given_running_container
    date = utcnow().format('YYYY-MM-DD')

    backup_container(container_name)
    client.images.get_by_alias('_'.join([date, container_name])).delete()
    assert client.containers.get(container_name).status == 'Running'
def test_when_container_stopped_then_container_stopped_after_backup(
        given_stopped_container, client):
    container_name = given_stopped_container
    date = utcnow().format('YYYY-MM-DD')

    backup_container(container_name)

    client.images.get_by_alias('_'.join([date, container_name])).delete()
    assert client.containers.get(container_name).status == 'Stopped'
Beispiel #4
0
def test_when_given_no_script(given_stopped_container, mocker, client):
    date = '2017-12-14'
    mocker.patch('lxd_backup.backup.today', return_value=date)
    container_name = given_stopped_container
    expected_image = '_'.join([date, container_name])

    backup_container(container_name, {})

    client.images.get_by_alias(expected_image).delete()
def test_when_nominal_then_image_exists(given_stopped_container, client):
    container_name = given_stopped_container
    date = utcnow().format('YYYY-MM-DD')

    backup_container(container_name)
    result = client.images.exists('_'.join([date, container_name]), alias=True)

    client.images.get_by_alias('_'.join([date, container_name])).delete()

    assert result
def test_when_nominal_then_image_alias_contains_date(given_stopped_container,
                                                     client, mocker):
    container_name = given_stopped_container
    date = '1999-08-12'
    mocker.patch('lxd_backup.backup.today', return_value=date)

    backup_container(container_name)

    result = client.images.exists('_'.join([date, container_name]), alias=True)

    client.images.get_by_alias('_'.join([date, container_name])).delete()
    assert result
def test_when_lifetime_givent_then_image_alias_contains_lifetime(
        given_stopped_container, client, mocker):
    container_name = given_stopped_container
    date = '2017-12-14'
    mocker.patch('lxd_backup.backup.today', return_value=date)
    mocker.patch('lxd_backup.time.Arrow.utcnow', return_value=get(date))

    config = {'lifetime': {'days': 295}}
    last_valid_date = '2018-10-05'
    expected_image = '_'.join([date, 'until', last_valid_date, container_name])

    backup_container(container_name, config)

    result = client.images.exists(expected_image, alias=True)

    client.images.get_by_alias(expected_image).delete()
    assert result
Beispiel #8
0
def test_when_given_after_script_then_script_is_run(given_stopped_container, mocker, client):
    date = '2017-12-14'
    mocker.patch('lxd_backup.backup.today', return_value=date)
    container_name = given_stopped_container
    expected_image = '_'.join([date, container_name])
    config = {'after_script': './tests/test_after_script.sh'} 

    backup_container(container_name, config)

    client.images.get_by_alias(expected_image).delete()

    with open('tmp/after_script_was_run') as f:
        result = f.readline() == 'True\n'

    shutil.rmtree('tmp')

    assert result
Beispiel #9
0
def test_export_one_container_then_image_deleted(given_stopped_container,
                                                 client, storage):
    container_name = given_stopped_container
    image = backup_container(container_name)

    storage.export(image)

    unexpected_file = '_'.join([utcnow().format('YYYY-MM-DD'), container_name])
    assert not client.images.exists(unexpected_file, alias=True)
Beispiel #10
0
def test_export_one_container_then_file_exists(given_stopped_container,
                                               storage):
    container_name = given_stopped_container
    image = backup_container(container_name)

    storage.export(image)

    expected_file = '_'.join([utcnow().format('YYYY-MM-DD'), container_name])
    assert storage.exists(expected_file)
Beispiel #11
0
def test_when_export_image_then_export_hash(mocker, storage,
                                            given_stopped_container):
    container_name = given_stopped_container
    image = backup_container(container_name)

    storage.export(image)

    expected_file = '_'.join([utcnow().format('YYYY-MM-DD'), container_name])
    expected_hash_file = ''.join([expected_file, '.md5'])
    assert storage.exists(expected_file)
    assert storage.exists(expected_hash_file)
Beispiel #12
0
def parse_config(config):

    with open(config, 'rb') as f:
        config = json.load(f)

    for container in config:
        container_config = config[container]

        rule = longest_lived_rule(container_config)

        backup_config = {
            'lifetime': rule['lifetime'],
            'before_script': container_config.get("before_script"),
            'after_script': container_config.get("after_script")
        }

        image = backup_container(container, backup_config)
        storage = get_storage_backend(rule)
        storage.export(image)
        storage.cleanup()