Example #1
0
def test_failed_over_master_start(app):
    # A master has failed over and restarted, another master has sucessfully advanced
    plugins = setup_plugins(app,
            dcs_lock=False,
            dcs_get_timeline=2,
            pg_get_timeline=1,
            pg_replication_role='master')
    # sync startup
    timeout = app.initialize()
    assert plugins.mock_calls ==  [
            call.initialize(),
            call.get_my_id(),
            # compare our id with the id in the DCS
            call.dcs_get_database_identifier(),
            call.pg_get_database_identifier(),
            # check if I am a replica
            call.pg_replication_role(),
            # no, so check if there is a master
            call.dcs_lock('master'),
            call.dcs_get_lock_owner('master'),
            call.pg_stop(),
            # compare our timeline to what's in the DCS
            call.pg_get_timeline(),
            call.dcs_get_timeline(),
            # we're on an older timeline, so reset
            call.pg_reset(),
            ]
    # Carry on running afterwards
    assert timeout == 5
Example #2
0
async def test_master_lock_changes_owner(app):
    # if the lock changes owner to someone else, shutdown postgresql and exist
    plugins = setup_plugins(app,
            pg_replication_role='master')
    assert app.initialize() == None
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.master_lock_changed('someone else')
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert plugins.mock_calls ==  [
            call.pg_replication_role(),
            call.pg_replication_role(),
            call.pg_stop(),
            call.dcs_disconnect(),
            call.master_lock_changed('someone else')
            ]
    assert app._master_lock_owner == 'someone else'
    # if the lock is owned by us, carry on trucking
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.master_lock_changed(app.my_id)
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert plugins.mock_calls ==  [
            call.pg_replication_role(),
            call.master_lock_changed('42')
            ]
    assert app._master_lock_owner == app.my_id
Example #3
0
async def test_master_lock_changes_owner(app):
    # if the lock changes owner to someone else, shutdown postgresql and exist
    plugins = setup_plugins(app, pg_replication_role='master')
    assert app.initialize() == None
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.master_lock_changed('someone else')
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert plugins.mock_calls == [
        call.pg_replication_role(),
        call.pg_replication_role(),
        call.pg_stop(),
        call.dcs_disconnect(),
        call.master_lock_changed('someone else')
    ]
    assert app._master_lock_owner == 'someone else'
    # if the lock is owned by us, carry on trucking
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.master_lock_changed(app.my_id)
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert plugins.mock_calls == [
        call.pg_replication_role(),
        call.master_lock_changed('42')
    ]
    assert app._master_lock_owner == app.my_id
Example #4
0
def test_failed_over_master_start(app):
    # A master has failed over and restarted, another master has sucessfully advanced
    plugins = setup_plugins(app,
                            dcs_lock=False,
                            dcs_get_timeline=2,
                            pg_get_timeline=1,
                            pg_replication_role='master')
    # sync startup
    timeout = app.initialize()
    assert plugins.mock_calls == [
        call.initialize(),
        call.get_my_id(),
        # compare our id with the id in the DCS
        call.dcs_get_database_identifier(),
        call.pg_get_database_identifier(),
        # check if I am a replica
        call.pg_replication_role(),
        # no, so check if there is a master
        call.dcs_lock('master'),
        call.dcs_get_lock_owner('master'),
        call.pg_stop(),
        # compare our timeline to what's in the DCS
        call.pg_get_timeline(),
        call.dcs_get_timeline(),
        # we're on an older timeline, so reset
        call.pg_reset(),
    ]
    # Carry on running afterwards
    assert timeout == 5
Example #5
0
def test_restart_master(app, event_loop):
    plugins = setup_plugins(app, pg_replication_role='master')
    app.initialize()
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        app.restart(10)
        assert sleep.called_once_with(10)
    event_loop.run_forever()  # must be stopped by restart()
    assert plugins.mock_calls == [
        call.pg_replication_role(),
        call.pg_stop(),
        call.dcs_disconnect()
    ]
Example #6
0
def test_restart_master(app, event_loop):
    plugins = setup_plugins(app,
            pg_replication_role='master')
    app.initialize()
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        app.restart(10)
        assert sleep.called_once_with(10)
    event_loop.run_forever() # must be stopped by restart()
    assert plugins.mock_calls ==  [
            call.pg_replication_role(),
            call.pg_stop(),
            call.dcs_disconnect()
            ]
Example #7
0
def test_restart_master(app):
    plugins = setup_plugins(app,
            pg_replication_role='master')
    app.initialize()
    plugins.reset_mock()
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.restart(10)
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert app._plugins.mock_calls ==  [
            call.pg_replication_role(),
            call.pg_stop(),
            call.dcs_disconnect()
            ]
Example #8
0
async def test_master_lock_broken(app):
    plugins = setup_plugins(app, pg_replication_role='master')
    assert app.initialize() == None
    plugins.reset_mock()
    # if the lock is broken, shutdown postgresql and exist
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.master_lock_changed(None)
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert plugins.mock_calls == [
        call.pg_replication_role(),
        call.pg_replication_role(),
        call.pg_stop(),
        call.dcs_disconnect(),
        call.master_lock_changed(None)
    ]
    assert app._master_lock_owner == None
Example #9
0
async def test_master_unhealthy(app):
    plugins = setup_plugins(app, pg_replication_role='master')
    app.initialize()
    plugins.reset_mock()
    app.unhealthy('boom', 'It went Boom', can_be_replica=True)
    assert plugins.mock_calls == [
        call.dcs_set_state({
            'host': '127.0.0.1',
            'replication_role': 'master',
            'health_problems': {
                'boom': {
                    'reason': 'It went Boom',
                    'can_be_replica': True
                }
            }
        }),
        call.pg_replication_role(),
        call.dcs_delete_conn_info(),
    ]
    plugins.reset_mock()
    # now we should have _handle_unhealthy_master running
    with patch('asyncio.sleep') as sleep, patch(
            'zgres.deadman.App._stop') as exit, patch(
                'time.sleep') as blocking_sleep:
        sleeper = FakeSleeper()
        sleep.side_effect = sleeper
        exit.side_effect = lambda: sleeper.finish()
        # there is no replica, so we just sleep and ping the
        # DCS to find a willing replica
        states = [iter([])]
        plugins.dcs_list_state.side_effect = states
        await sleeper.next()
        assert plugins.mock_calls == [call.dcs_list_state()]
        # we add a willing replica
        states = [iter([('other', {'willing': 1})])]
        plugins.dcs_list_state.side_effect = states
        plugins.reset_mock()
        await sleeper.next()
        assert plugins.mock_calls == [
            call.dcs_list_state(),
            call.pg_replication_role(),
            call.pg_stop(),
            call.dcs_disconnect()
        ]
Example #10
0
async def test_master_lock_broken(app):
    plugins = setup_plugins(app,
            pg_replication_role='master')
    assert app.initialize() == None
    plugins.reset_mock()
    # if the lock is broken, shutdown postgresql and exist
    with patch('time.sleep') as sleep:
        with patch('sys.exit') as exit:
            app.master_lock_changed(None)
            assert exit.called_once_with(0)
        assert sleep.called_once_with(10)
    assert plugins.mock_calls ==  [
            call.pg_replication_role(),
            call.pg_replication_role(),
            call.pg_stop(),
            call.dcs_disconnect(),
            call.master_lock_changed(None)
            ]
    assert app._master_lock_owner == None
Example #11
0
async def test_master_unhealthy(app):
    plugins = setup_plugins(app,
            pg_replication_role='master')
    app.initialize()
    plugins.reset_mock()
    app.unhealthy('boom', 'It went Boom', can_be_replica=True)
    assert plugins.mock_calls ==  [
            call.dcs_set_state({
                'host': '127.0.0.1',
                'replication_role': 'master',
                'health_problems': {'boom': {'reason': 'It went Boom', 'can_be_replica': True}}}),
            call.pg_replication_role(),
            call.dcs_delete_conn_info(),
            ]
    plugins.reset_mock()
    # now we should have _handle_unhealthy_master running
    with patch('asyncio.sleep') as sleep, patch('sys.exit') as exit, patch('time.sleep') as blocking_sleep:
        sleeper = FakeSleeper()
        sleep.side_effect = sleeper
        exit.side_effect = lambda x: sleeper.finish()
        # there is no replica, so we just sleep and ping the
        # DCS to find a willing replica
        states = [iter([])]
        plugins.dcs_get_all_state.side_effect = states
        plugins.willing_replicas.side_effect = [iter([])]
        await sleeper.next()
        assert plugins.mock_calls == [
                call.dcs_get_all_state(),
                call.willing_replicas(states[0])]
        # we add a willing replica
        states = [iter([])]
        plugins.dcs_get_all_state.side_effect = states
        plugins.willing_replicas.side_effect = [iter([('other', {})])]
        plugins.reset_mock()
        await sleeper.next()
        assert plugins.mock_calls == [
                call.dcs_get_all_state(),
                call.willing_replicas(states[0]),
                call.pg_replication_role(),
                call.pg_stop(),
                call.dcs_disconnect()
                ]
Example #12
0
def test_replica_bootstrap(app):
    plugins = setup_plugins(app,
            dcs_get_database_identifier='1234',
            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert app._plugins.mock_calls ==  [
            call.initialize(),
            call.get_my_id(),
            # compare our id with the id in the DCS
            call.dcs_get_database_identifier(),
            call.pg_get_database_identifier(),
            # make sure postgresql is stopped
            call.pg_stop(),
            # postgresql restore
            call.pg_restore(),
            call.pg_setup_replication(),
            call.pg_am_i_replica()
            ]
    # shut down cleanly and immediately
    assert timeout == 0
Example #13
0
def test_replica_bootstrap(app):
    plugins = setup_plugins(app, dcs_get_database_identifier='1234')
    plugins.pg_get_database_identifier.side_effect = ['42', '1234']
    timeout = app.initialize()
    assert plugins.mock_calls == [
        call.initialize(),
        call.get_my_id(),
        # compare our id with the id in the DCS
        call.dcs_get_database_identifier(),
        call.pg_get_database_identifier(),
        # make sure postgresql is stopped
        call.pg_stop(),
        # postgresql restore
        call.pg_initdb(),
        call.pg_restore(),
        call.pg_setup_replication(None),
        call.pg_get_database_identifier(),
        call.pg_replication_role()
    ]
    # shut down cleanly and immediately
    assert timeout == 0
Example #14
0
def test_replica_bootstrap_fails_sanity_test(app):
    plugins = setup_plugins(app,
            pg_am_i_replica=False,
            dcs_get_database_identifier='1234',
            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert app._plugins.mock_calls ==  [
            call.initialize(),
            call.get_my_id(),
            # compare our id with the id in the DCS
            call.dcs_get_database_identifier(),
            call.pg_get_database_identifier(),
            # make sure postgresql is stopped
            call.pg_stop(),
            # postgresql restore
            call.pg_restore(),
            call.pg_setup_replication(),
            call.pg_am_i_replica(),
            call.pg_reset(),
            ]
    # shut down after 5 seconds to try again
    assert timeout == 5
Example #15
0
def test_replica_bootstrap_fails_sanity_test(app):
    plugins = setup_plugins(app,
                            pg_replication_role='master',
                            dcs_get_database_identifier='1234',
                            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert plugins.mock_calls == [
        call.initialize(),
        call.get_my_id(),
        # compare our id with the id in the DCS
        call.dcs_get_database_identifier(),
        call.pg_get_database_identifier(),
        # make sure postgresql is stopped
        call.pg_stop(),
        # postgresql restore
        call.pg_initdb(),
        call.pg_restore(),
        call.pg_setup_replication(None),
        call.pg_get_database_identifier(),
        call.pg_replication_role(),
        call.pg_reset(),
    ]
    # shut down after 5 seconds to try again
    assert timeout == 5