예제 #1
0
def test_base_sha_corrupted(capsys):
    sleep(1)  # make sure the backup timestamps differ
    with itest_setup(
        test_file_history,
        _TestFileData('bar', 'asdfasdfasdf'),
        _TestFileData('foo', 'asdfasdfasd'),
    ):
        BACKUP_ARGS.dry_run = False
        backup(BACKUP_ARGS)

    backup_store = get_backup_store('data1_backup')
    with backup_store.unlock(preserve_scratch=True):
        backup_store.manifest._cursor.execute(
            'update manifest set key_pair=? where abs_file_name like ? ',
            (b'hjkl', '%bar'),
        )
        backup_store.manifest._commit()

    VERIFY_ARGS.fast = False
    verify(VERIFY_ARGS)
    out, _ = capsys.readouterr()
    assert 'ERROR' in out

    # Shouldn't create duplicate entries when we fix
    with backup_store.unlock(preserve_scratch=True):
        backup_store.manifest._cursor.execute('select * from manifest')
        rows = backup_store.manifest._cursor.fetchall()
        assert len(rows) == 3

    # After the fix, verify should be clean
    verify(VERIFY_ARGS)
    out, _ = capsys.readouterr()
    assert 'ERROR' not in out
예제 #2
0
def check_backup_restore(dry_run):
    BACKUP_ARGS.dry_run = dry_run
    original_state = get_backup_dir_state()
    backup(BACKUP_ARGS)
    if dry_run:
        assert original_state == get_backup_dir_state()
    else:
        assert_backup_store_correct()
        restore(RESTORE_ARGS)
        assert_restore_correct()
예제 #3
0
def test_f2_lbs_atomicity_2():
    sys.settrace(make_trace_func('test_f2_lbs_atomicity_2', abort))
    with itest_setup(test_file_history, ):
        backup(BACKUP_ARGS)
    file_data_path = os.path.join(DATA_DIR, 'new_file_2')
    file_backup_path = os.path.join(
        BACKUP_DIR,
        sha_to_path(test_file_history[file_data_path][0].sha),
    )
    assert os.path.exists(file_backup_path)
예제 #4
0
def setup_manifest():
    clean_up_temp_directories()
    with itest_setup(
            test_file_history,
            _TestFileData('foo', 'asdf'),
            _TestFileData('bar', 'hjkl'),
            _TestFileData('baz/buz', 'qwerty'),
    ):
        backup(BACKUP_ARGS)
        yield
예제 #5
0
def run_backup(use_encryption):
    global test_file_history
    test_file_history = dict()
    clean_up_temp_directories()
    with itest_setup(
        test_file_history,
        _TestFileData('foo', 'asdfasdfasdf'),
    ):
        BACKUP_ARGS.dry_run = False
        backup(BACKUP_ARGS)
예제 #6
0
def test_m1_crash_before_save():
    sys.settrace(make_trace_func('test_m1_crash_before_save', abort))
    with itest_setup(
            test_file_history,
            _TestFileData('foo', 'asdfhjkl'),
    ), pytest.raises(Exception):
        backup(BACKUP_ARGS)
    assert_manifest_correct(before=True)
    file_data_path = os.path.join(DATA_DIR, 'foo')
    file_backup_path = os.path.join(
        BACKUP_DIR,
        sha_to_path(test_file_history[file_data_path][1].sha),
    )
    assert os.path.exists(file_backup_path)
예제 #7
0
def test_m2_crash_after_file_save():
    sys.settrace(make_trace_func('test_m2_crash_after_file_save', abort))
    with itest_setup(test_file_history):
        backup(BACKUP_ARGS)
    manifest_conn = sqlite3.connect(get_latest_manifest())
    manifest_conn.row_factory = sqlite3.Row
    manifest_cursor = manifest_conn.cursor()

    manifest_cursor.execute(
        'select * from manifest where abs_file_name like "%another_file"')
    rows = manifest_cursor.fetchall()
    assert len(rows) == 1
    assert rows[0]['sha'] == test_file_history[os.path.join(
        DATA_DIR, 'another_file')][0].sha
예제 #8
0
def test_m2_crash_before_file_save():
    sys.settrace(make_trace_func('test_m2_crash_before_file_save', abort))
    with itest_setup(
            test_file_history,
            _TestFileData('another_file', '1234'),
    ):
        backup(BACKUP_ARGS)
    manifest_conn = sqlite3.connect(get_latest_manifest())
    manifest_conn.row_factory = sqlite3.Row
    manifest_cursor = manifest_conn.cursor()

    manifest_cursor.execute(
        'select * from manifest where abs_file_name like "%another_file"')
    rows = manifest_cursor.fetchall()
    assert not rows
예제 #9
0
def test_f3_file_changed_while_saving():
    sys.settrace(
        make_trace_func(
            'test_f3_file_changed_while_saving',
            make_modify_file_func(os.path.join(DATA_DIR, 'new_file_3')),
        ))
    with itest_setup(
            test_file_history,
            _TestFileData('new_file_3', '12345'),
    ):
        backup(BACKUP_ARGS)
    file_data_path = os.path.join(DATA_DIR, 'new_file_3')
    file_backup_path = os.path.join(
        BACKUP_DIR,
        sha_to_path(test_file_history[file_data_path][0].sha),
    )
    assert not os.path.exists(file_backup_path)
예제 #10
0
def test_shas_with_bad_key_pairs(both_bad, capsys):
    with itest_setup(
        test_file_history,
        _TestFileData('bar', 'asdfasdfasdf'),
    ):
        BACKUP_ARGS.dry_run = False
        backup(BACKUP_ARGS)

    VERIFY_ARGS.fast = True
    backup_store = get_backup_store('data1_backup')
    with backup_store.unlock(preserve_scratch=True):
        backup_store.manifest._cursor.execute(
            'update manifest set key_pair=? where abs_file_name like ?',
            (b'asdf', '%bar'),
        )
        if both_bad:
            backup_store.manifest._cursor.execute(
                'update manifest set key_pair=? where abs_file_name like ?',
                (b'hjkl', '%foo'),
            )
        backup_store.manifest._commit()
    verify(VERIFY_ARGS)
    out, _ = capsys.readouterr()
    assert 'Found 2 entries for' in out
    if both_bad:
        assert 'No valid entries' in out
    else:
        assert 'seems good' in out

    # Shouldn't create duplicate entries when we fix
    with backup_store.unlock(preserve_scratch=True):
        backup_store.manifest._cursor.execute('select * from manifest')
        rows = backup_store.manifest._cursor.fetchall()
        assert len(rows) == 2

    # After the fix, verify should be clean
    VERIFY_ARGS.fast = False
    verify(VERIFY_ARGS)
    out, _ = capsys.readouterr()
    assert 'ERROR' not in out
예제 #11
0
def test_f1_crash_file_save():
    sys.settrace(make_trace_func('test_f1_crash_file_save', abort))
    with itest_setup(
            test_file_history,
            _TestFileData('foo', 'asdfhjkl'),
            _TestFileData('new_file', 'zxcv'),
    ):
        backup(BACKUP_ARGS)

    first_file_data_path = os.path.join(DATA_DIR, 'foo')
    second_file_data_path = os.path.join(DATA_DIR, 'new_file')
    first_file_backup_path = os.path.join(
        BACKUP_DIR,
        sha_to_path(test_file_history[first_file_data_path][1].sha),
    )
    second_file_backup_path = os.path.join(
        BACKUP_DIR,
        sha_to_path(test_file_history[second_file_data_path][0].sha),
    )
    assert not os.path.exists(first_file_backup_path)
    assert os.path.exists(second_file_backup_path)

    # reset everything back to "normal" before the next test
    backup(BACKUP_ARGS)
예제 #12
0
def test_m1_crash_after_save():
    sys.settrace(make_trace_func('test_m1_crash_after_save', abort))
    with itest_setup(test_file_history), pytest.raises(Exception):
        backup(BACKUP_ARGS)
    assert_manifest_correct(before=False)