Exemplo n.º 1
0
def test_view_backups(
    reset_mockTime,
    patch_datetime_now,
    populate_conf_dir,
    populate_data_dir,
    tmp_path,
    capsys,
):
    expected = "full/TEST_APP_FULL_2020-12-25T170555.tgz\n"
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the click context that backup_manager expects to deal with
    ctx = create_click_ctx(
        Path(populate_conf_dir), Path(populate_data_dir), Path(backup_dir)
    )
    # Create our configuration
    conf = {
        "name": "full",
        "backup_limit": 0,
    }
    # Create a backup to view.
    with capsys.disabled():
        backup_config = BackupConfig.from_dict(conf)
        backup_config.backup(ctx)

    backup_manager = BackupManager(conf)
    backup_manager.view_backups(ctx)

    captured = capsys.readouterr()
    output = captured.out

    assert expected == output
Exemplo n.º 2
0
def test_restore_triggers_config_backups(
    reset_mockTime,
    patch_datetime_now,
    populate_conf_dir,
    populate_data_dir,
    backup_tgz,
    tmp_path,
):
    expected_result = set({"TEST_APP_FULL_2020-12-25T170555.tgz"})
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the temporary conf directory to restore into.
    conf_dir = tmp_path / PurePath(populate_conf_dir).name
    conf_dir.mkdir()
    # Create the temporary data directory to restore into.
    data_dir = tmp_path / PurePath(populate_data_dir).name
    data_dir.mkdir()
    # Create the click context that backup_manager expects to deal with.
    ctx = create_click_ctx(Path(conf_dir), Path(data_dir), Path(backup_dir))
    # Create our configuration
    conf = [{"name": "full"}]

    backup_manager = BackupManager(conf)
    backup_manager.restore(ctx, backup_tgz)

    # Compare as a set so list order does not matter.
    assert set(os.listdir(os.path.join(backup_dir, "full"))) == expected_result
Exemplo n.º 3
0
def test_restore_works_with_existing_files(
    reset_mockTime, populate_conf_dir, populate_data_dir, backup_tgz, tmp_path
):
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the temporary conf directory to restore into.
    conf_dir = tmp_path / PurePath(populate_conf_dir).name
    conf_dir.mkdir()
    # Create the temporary data directory to restore into.
    data_dir = tmp_path / PurePath(populate_data_dir).name
    data_dir.mkdir()
    # Create the click context that backup_manager expects to deal with.
    ctx = create_click_ctx(Path(conf_dir), Path(data_dir), Path(backup_dir))
    # Create our configuration
    conf = [{"name": "full"}]
    # Create an existing file that will be overwritten on restore.
    with open(os.path.join(data_dir, "1.txt"), "w") as f:
        f.write("some sample text")

    backup_manager = BackupManager(conf)
    backup_manager.restore(ctx, backup_tgz)

    contents = Path(os.path.join(data_dir, "1.txt")).read_text()

    assert contents == ""
Exemplo n.º 4
0
def test_restore_fails_no_backup(
    reset_mockTime, populate_conf_dir, populate_data_dir, tmp_path, caplog
):
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the click context that backup_manager expects to deal with
    ctx = create_click_ctx(
        Path(populate_conf_dir), Path(populate_data_dir), Path(backup_dir)
    )
    # Create our configuration
    conf = {"backups": [{"name": "full"}]}

    backup_manager = BackupManager(conf)
    with pytest.raises(SystemExit) as excinfo:
        backup_manager.restore(ctx, "fake_file.tgz")
    assert "1" in str(excinfo.value)
Exemplo n.º 5
0
def test_view_backups_multiple_backup_strategies(
    reset_mockTime,
    patch_datetime_now,
    populate_conf_dir,
    populate_data_dir,
    tmp_path,
    capsys,
):
    expected = (
        "logs/TEST_APP_LOGS_2020-12-25T170556.tgz\n"
        "full/TEST_APP_FULL_2020-12-25T170555.tgz\n"
    )
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the click context that backup_manager expects to deal with
    ctx = create_click_ctx(
        Path(populate_conf_dir), Path(populate_data_dir), Path(backup_dir)
    )
    # Create our configuration
    conf_full = {
        "name": "full",
        "backup_limit": 0,
    }
    conf_logs = {
        "name": "logs",
        "file_filter": {
            "data_dir": {"include_list": ["1.txt", "**/first.txt"]},
            "conf_dir": {"include_list": ["**/*.txt"]},
        },
    }
    # Create a backup to view.
    with capsys.disabled():
        backup_config = BackupConfig.from_dict(conf_full)
        backup_config.backup(ctx)
        mock_time.increment()

        backup_config = BackupConfig.from_dict(conf_logs)
        backup_config.backup(ctx)
        mock_time.increment()

    backup_manager = BackupManager([])
    backup_manager.view_backups(ctx)

    captured = capsys.readouterr()
    output = captured.out

    assert expected == output
Exemplo n.º 6
0
def test_restore_does_not_replace_files_not_in_backup(
    reset_mockTime, populate_conf_dir, populate_data_dir, backup_tgz, tmp_path
):
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the temporary conf directory to restore into.
    conf_dir = tmp_path / PurePath(populate_conf_dir).name
    conf_dir.mkdir()
    # Create the temporary data directory to restore into.
    data_dir = tmp_path / PurePath(populate_data_dir).name
    data_dir.mkdir()
    # Create the click context that backup_manager expects to deal with.
    ctx = create_click_ctx(Path(conf_dir), Path(data_dir), Path(backup_dir))
    # Create our configuration
    conf = [{"name": "full"}]
    # Create existing files in our temp data directory that should be kept on restore.
    with open(os.path.join(data_dir, "existing_file_1.txt"), "w"):
        pass

    with open(os.path.join(data_dir, "existing_file_2.yml"), "w"):
        pass

    backup_manager = BackupManager(conf)
    backup_manager.restore(ctx, backup_tgz)

    data_result = set(
        [
            os.path.join(dp[len(str(data_dir)) :], f).strip("/")  # noqa: E203
            for dp, dn, filenames in os.walk(data_dir)
            for f in filenames
        ]
    )
    conf_result = set(
        [
            os.path.join(dp[len(str(conf_dir)) :], f).strip("/")  # noqa: E203
            for dp, dn, filenames in os.walk(conf_dir)
            for f in filenames
        ]
    )

    assert (
        DATA_FILES_ALL.union(set({"existing_file_1.txt", "existing_file_2.yml"}))
        == data_result
    )
    assert CONF_FILES_ALL == conf_result
Exemplo n.º 7
0
def test_view_multiple_backups_descending_order(
    reset_mockTime,
    patch_datetime_now,
    populate_conf_dir,
    populate_data_dir,
    tmp_path,
    capsys,
):
    expected = (
        "full/TEST_APP_FULL_2020-12-25T170604.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170603.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170602.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170601.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170600.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170559.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170558.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170557.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170556.tgz\n"
        + "full/TEST_APP_FULL_2020-12-25T170555.tgz\n"
    )
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the click context that backup_manager expects to deal with
    ctx = create_click_ctx(
        Path(populate_conf_dir), Path(populate_data_dir), Path(backup_dir)
    )
    # Create our configuration
    conf = {
        "name": "full",
        "backup_limit": 0,
    }
    # Create backups to view.
    with capsys.disabled():
        backup_config = BackupConfig.from_dict(conf)
        for x in range(10):
            backup_config.backup(ctx)
            mock_time.increment()

    backup_manager = BackupManager(conf)
    backup_manager.view_backups(ctx)

    captured = capsys.readouterr()
    output = captured.out

    assert expected == output
Exemplo n.º 8
0
def test_can_restore_with_no_config(
    reset_mockTime, populate_conf_dir, populate_data_dir, backup_tgz, tmp_path
):
    # Set the backup directory.
    backup_dir = tmp_path / BASE_BACKUP_DIR
    # Create the temporary conf directory to restore into.
    conf_dir = tmp_path / PurePath(populate_conf_dir).name
    conf_dir.mkdir()
    # Create the temporary data directory to restore into.
    data_dir = tmp_path / PurePath(populate_data_dir).name
    data_dir.mkdir()
    # Create the click context that backup_manager expects to deal with.
    ctx = create_click_ctx(Path(conf_dir), Path(data_dir), Path(backup_dir))
    # Create our configuration
    conf = []

    backup_manager = BackupManager(conf)
    backup_manager.restore(ctx, backup_tgz)

    data_result = set(
        [
            os.path.join(dp[len(str(data_dir)) :], f).strip("/")  # noqa: E203
            for dp, dn, filenames in os.walk(data_dir)
            for f in filenames
        ]
    )
    conf_result = set(
        [
            os.path.join(dp[len(str(conf_dir)) :], f).strip("/")  # noqa: E203
            for dp, dn, filenames in os.walk(conf_dir)
            for f in filenames
        ]
    )

    assert DATA_FILES_ALL == data_result
    assert CONF_FILES_ALL == conf_result