Ejemplo n.º 1
0
def test_dump_databases_runs_mysqldump_with_hostname_and_port():
    databases = [{
        'name': 'foo',
        'hostname': 'database.example.org',
        'port': 5433
    }]
    output_file = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(
        module.dump).should_receive('make_database_dump_filename').and_return(
            'databases/database.example.org/foo')
    flexmock(module.os).should_receive('makedirs')
    flexmock(
        sys.modules['builtins']).should_receive('open').and_return(output_file)

    flexmock(module).should_receive('execute_command').with_args(
        (
            'mysqldump',
            '--add-drop-database',
            '--host',
            'database.example.org',
            '--port',
            '5433',
            '--protocol',
            'tcp',
            '--databases',
            'foo',
        ),
        output_file=output_file,
        extra_environment=None,
    ).once()

    module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
Ejemplo n.º 2
0
def test_dump_databases_with_dry_run_skips_mysqldump():
    databases = [{'name': 'foo'}, {'name': 'bar'}]
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(
        module.dump).should_receive('make_database_dump_filename').and_return(
            'databases/localhost/foo').and_return('databases/localhost/bar')
    flexmock(module.os).should_receive('makedirs').never()
    flexmock(module).should_receive('execute_command').never()

    module.dump_databases(databases, 'test.yaml', {}, dry_run=True)
Ejemplo n.º 3
0
def test_dump_databases_runs_mysqldump_for_each_database():
    databases = [{'name': 'foo'}, {'name': 'bar'}]
    processes = [flexmock(), flexmock()]
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(
        module.dump).should_receive('make_database_dump_filename').and_return(
            'databases/localhost/foo').and_return('databases/localhost/bar')
    flexmock(module).should_receive('database_names_to_dump').and_return(
        ('foo', )).and_return(('bar', ))
    flexmock(module.dump).should_receive('create_named_pipe_for_dump')

    for name, process in zip(('foo', 'bar'), processes):
        flexmock(module).should_receive('execute_command').with_args(
            (
                'mysqldump',
                '--add-drop-database',
                '--databases',
                name,
                '>',
                'databases/localhost/{}'.format(name),
            ),
            shell=True,
            extra_environment=None,
            run_to_completion=False,
        ).and_return(process).once()

    assert module.dump_databases(databases, 'test.yaml', {},
                                 dry_run=False) == processes
Ejemplo n.º 4
0
def test_dump_databases_runs_mysqldump_for_all_databases():
    databases = [{'name': 'all'}]
    process = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(module.dump).should_receive(
        'make_database_dump_filename').and_return('databases/localhost/all')
    flexmock(module).should_receive('database_names_to_dump').and_return(
        ('foo', 'bar'))
    flexmock(module.dump).should_receive('create_named_pipe_for_dump')

    flexmock(module).should_receive('execute_command').with_args(
        (
            'mysqldump',
            '--add-drop-database',
            '--databases',
            'foo',
            'bar',
            '>',
            'databases/localhost/all',
        ),
        shell=True,
        extra_environment=None,
        run_to_completion=False,
    ).and_return(process).once()

    assert module.dump_databases(databases, 'test.yaml', {},
                                 dry_run=False) == [process]
Ejemplo n.º 5
0
def test_dump_databases_runs_mysqldump_with_username_and_password():
    databases = [{'name': 'foo', 'username': '******', 'password': '******'}]
    process = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(module.dump).should_receive(
        'make_database_dump_filename').and_return('databases/localhost/foo')
    flexmock(module).should_receive('database_names_to_dump').and_return(
        ('foo', ))
    flexmock(module.dump).should_receive('create_named_pipe_for_dump')

    flexmock(module).should_receive('execute_command').with_args(
        (
            'mysqldump',
            '--add-drop-database',
            '--user',
            'root',
            '--databases',
            'foo',
            '>',
            'databases/localhost/foo',
        ),
        shell=True,
        extra_environment={
            'MYSQL_PWD': 'trustsome1'
        },
        run_to_completion=False,
    ).and_return(process).once()

    assert module.dump_databases(databases, 'test.yaml', {},
                                 dry_run=False) == [process]
Ejemplo n.º 6
0
def test_dump_databases_runs_mysqldump_for_all_databases():
    databases = [{'name': 'all'}]
    output_file = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(module.dump).should_receive(
        'make_database_dump_filename').and_return('databases/localhost/all')
    flexmock(module.os).should_receive('makedirs')
    flexmock(
        sys.modules['builtins']).should_receive('open').and_return(output_file)

    flexmock(module).should_receive('execute_command').with_args(
        ('mysqldump', '--add-drop-database', '--all-databases'),
        output_file=output_file,
        extra_environment=None,
    ).once()

    module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
Ejemplo n.º 7
0
def test_dump_databases_runs_mysqldump_with_username_and_password():
    databases = [{'name': 'foo', 'username': '******', 'password': '******'}]
    output_file = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(module.dump).should_receive(
        'make_database_dump_filename').and_return('databases/localhost/foo')
    flexmock(module.os).should_receive('makedirs')
    flexmock(
        sys.modules['builtins']).should_receive('open').and_return(output_file)

    flexmock(module).should_receive('execute_command').with_args(
        ('mysqldump', '--add-drop-database', '--user', 'root', '--databases',
         'foo'),
        output_file=output_file,
        extra_environment={
            'MYSQL_PWD': 'trustsome1'
        },
    ).once()

    module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
Ejemplo n.º 8
0
def test_dump_databases_errors_for_missing_all_databases():
    databases = [{'name': 'all'}]
    process = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(module.dump).should_receive(
        'make_database_dump_filename').and_return('databases/localhost/all')
    flexmock(module).should_receive('database_names_to_dump').and_return(())

    with pytest.raises(ValueError):
        assert module.dump_databases(databases, 'test.yaml', {},
                                     dry_run=False) == [process]
Ejemplo n.º 9
0
def test_dump_databases_runs_mysqldump_with_hostname_and_port():
    databases = [{
        'name': 'foo',
        'hostname': 'database.example.org',
        'port': 5433
    }]
    process = flexmock()
    flexmock(module).should_receive('make_dump_path').and_return('')
    flexmock(
        module.dump).should_receive('make_database_dump_filename').and_return(
            'databases/database.example.org/foo')
    flexmock(module).should_receive('database_names_to_dump').and_return(
        ('foo', ))
    flexmock(module.dump).should_receive('create_named_pipe_for_dump')

    flexmock(module).should_receive('execute_command').with_args(
        (
            'mysqldump',
            '--add-drop-database',
            '--host',
            'database.example.org',
            '--port',
            '5433',
            '--protocol',
            'tcp',
            '--databases',
            'foo',
            '>',
            'databases/database.example.org/foo',
        ),
        shell=True,
        extra_environment=None,
        run_to_completion=False,
    ).and_return(process).once()

    assert module.dump_databases(databases, 'test.yaml', {},
                                 dry_run=False) == [process]