Esempio n. 1
0
def test_restore_database_dump_runs_psql_for_all_database_dump():
    database_config = [{'name': 'all'}]
    extract_process = flexmock(stdout=flexmock())

    flexmock(module).should_receive('make_dump_path')
    flexmock(module.dump).should_receive('make_database_dump_filename')
    flexmock(module).should_receive('make_extra_environment').and_return(
        {'PGSSLMODE': 'disable'})
    flexmock(module).should_receive(
        'execute_command_with_processes').with_args(
            ('psql', '--no-password'),
            processes=[extract_process],
            output_log_level=logging.DEBUG,
            input_file=extract_process.stdout,
            extra_environment={
                'PGSSLMODE': 'disable'
            },
            borg_local_path='borg',
        ).once()
    flexmock(module).should_receive('execute_command').with_args(
        ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'),
        extra_environment={
            'PGSSLMODE': 'disable'
        },
    ).once()

    module.restore_database_dump(database_config,
                                 'test.yaml', {},
                                 dry_run=False,
                                 extract_process=extract_process)
Esempio n. 2
0
def test_restore_database_dump_runs_pg_restore_with_username_and_password():
    database_config = [{
        'name': 'foo',
        'username': '******',
        'password': '******'
    }]
    extract_process = flexmock(stdout=flexmock())

    flexmock(module).should_receive('make_dump_path')
    flexmock(module.dump).should_receive('make_database_dump_filename')
    flexmock(module).should_receive('make_extra_environment').and_return({
        'PGPASSWORD':
        '******',
        'PGSSLMODE':
        'disable'
    })
    flexmock(module).should_receive(
        'execute_command_with_processes').with_args(
            (
                'pg_restore',
                '--no-password',
                '--if-exists',
                '--exit-on-error',
                '--clean',
                '--dbname',
                'foo',
                '--username',
                'postgres',
            ),
            processes=[extract_process],
            output_log_level=logging.DEBUG,
            input_file=extract_process.stdout,
            extra_environment={
                'PGPASSWORD': '******',
                'PGSSLMODE': 'disable'
            },
            borg_local_path='borg',
        ).once()
    flexmock(module).should_receive('execute_command').with_args(
        (
            'psql',
            '--no-password',
            '--quiet',
            '--username',
            'postgres',
            '--dbname',
            'foo',
            '--command',
            'ANALYZE',
        ),
        extra_environment={
            'PGPASSWORD': '******',
            'PGSSLMODE': 'disable'
        },
    ).once()

    module.restore_database_dump(database_config,
                                 'test.yaml', {},
                                 dry_run=False,
                                 extract_process=extract_process)
Esempio n. 3
0
def test_restore_database_dump_without_extract_process_restores_from_disk():
    database_config = [{'name': 'foo'}]

    flexmock(module).should_receive('make_dump_path')
    flexmock(module.dump).should_receive(
        'make_database_dump_filename').and_return('/dump/path')
    flexmock(module).should_receive(
        'execute_command_with_processes').with_args(
            (
                'pg_restore',
                '--no-password',
                '--if-exists',
                '--exit-on-error',
                '--clean',
                '--dbname',
                'foo',
                '/dump/path',
            ),
            processes=[],
            output_log_level=logging.DEBUG,
            input_file=None,
            extra_environment=None,
            borg_local_path='borg',
        ).once()
    flexmock(module).should_receive('execute_command').with_args(
        ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command',
         'ANALYZE'),
        extra_environment=None,
    ).once()

    module.restore_database_dump(database_config,
                                 'test.yaml', {},
                                 dry_run=False,
                                 extract_process=None)
Esempio n. 4
0
def test_restore_database_dump_with_dry_run_skips_restore():
    database_config = [{'name': 'foo'}]

    flexmock(module).should_receive('make_dump_path')
    flexmock(module.dump).should_receive('make_database_dump_filename')
    flexmock(module).should_receive('execute_command_with_processes').never()

    module.restore_database_dump(database_config,
                                 'test.yaml', {},
                                 dry_run=True,
                                 extract_process=flexmock())
Esempio n. 5
0
def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
    database_config = [{
        'name': 'foo',
        'hostname': 'database.example.org',
        'port': 5433
    }]
    extract_process = flexmock(stdout=flexmock())

    flexmock(module).should_receive('make_dump_path')
    flexmock(module.dump).should_receive('make_database_dump_filename')
    flexmock(module).should_receive(
        'execute_command_with_processes').with_args(
            (
                'pg_restore',
                '--no-password',
                '--if-exists',
                '--exit-on-error',
                '--clean',
                '--dbname',
                'foo',
                '--host',
                'database.example.org',
                '--port',
                '5433',
            ),
            processes=[extract_process],
            output_log_level=logging.DEBUG,
            input_file=extract_process.stdout,
            extra_environment=None,
            borg_local_path='borg',
        ).once()
    flexmock(module).should_receive('execute_command').with_args(
        (
            'psql',
            '--no-password',
            '--quiet',
            '--host',
            'database.example.org',
            '--port',
            '5433',
            '--dbname',
            'foo',
            '--command',
            'ANALYZE',
        ),
        extra_environment=None,
    ).once()

    module.restore_database_dump(database_config,
                                 'test.yaml', {},
                                 dry_run=False,
                                 extract_process=extract_process)
Esempio n. 6
0
def test_restore_database_dump_errors_on_multiple_database_config():
    database_config = [{'name': 'foo'}, {'name': 'bar'}]

    flexmock(module).should_receive('make_dump_path')
    flexmock(module.dump).should_receive('make_database_dump_filename')
    flexmock(module).should_receive('execute_command_with_processes').never()
    flexmock(module).should_receive('execute_command').never()

    with pytest.raises(ValueError):
        module.restore_database_dump(database_config,
                                     'test.yaml', {},
                                     dry_run=False,
                                     extract_process=flexmock())