Beispiel #1
0
async def test_master_start(app):
    plugins = setup_plugins(app,
            dcs_get_database_identifier='1234',
            dcs_lock=True,
            pg_replication_role='master',
            pg_get_database_identifier='1234')
    def start_monitoring():
        app.unhealthy('test_monitor', 'Waiting for first check')
    plugins.start_monitoring.side_effect = start_monitoring
    # 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'),
            # no master, so sure the DB is running
            call.pg_start(),
            # start monitoring
            call.start_monitoring(),
            call.dcs_watch(
                app.master_lock_changed,
                app._notify_state,
                app._notify_conn_info,
            ),
            call.get_conn_info(),
            # set our first state
            call.dcs_set_state({
                'host': '127.0.0.1',
                'replication_role': 'master',
                'health_problems': {'test_monitor':
                    {'can_be_replica': False, 'reason': 'Waiting for first check'}}})
            ]
    # Carry on running afterwards
    assert timeout == None
    assert app.health_problems == {'test_monitor': {'can_be_replica': False, 'reason': 'Waiting for first check'}}
    # Our test monitor becomes healthy
    plugins.reset_mock()
    app.healthy('test_monitor')
    assert plugins.mock_calls ==  [
            call.dcs_set_state({
                'host': '127.0.0.1',
                'replication_role': 'master',
                'health_problems': {}}),
            call.pg_replication_role(),
            call.dcs_lock('master'),
            call.dcs_set_conn_info({'host': '127.0.0.1'}),
           ]
Beispiel #2
0
def test_master_bootstrap(app):
    plugins = setup_plugins(app,
            dcs_get_database_identifier=None,
            dcs_lock=True,
            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert plugins.mock_calls ==  [
            call.initialize(),
            call.get_my_id(),
            # check if we have a db identifier set
            call.dcs_get_database_identifier(),
            # no, ok, init our db
            call.pg_initdb(),
            # make sure it starts
            call.pg_start(),
            call.pg_get_database_identifier(),
            # lock the database identifier so no-one else gets here
            call.dcs_lock('database_identifier'),
            # while locked make sure there is no id set in the DCS before we got the lock
            call.dcs_get_database_identifier(),
            # Make the first backup while locked with no DCS
            call.pg_backup(),
            # set the database identifier AFTER
            call.dcs_set_database_identifier('42')
            ]
    # shut down cleanly and immediately
    assert timeout == 0
Beispiel #3
0
async def test_replica_tries_to_take_over(app):
    plugins = setup_plugins(app,
            pg_replication_role='replica')
    assert app.initialize() == None
    plugins.reset_mock()
    # if there is no lock owner, we start looping trying to become master
    app.master_lock_changed(None)
    assert plugins.mock_calls ==  [call.pg_replication_role(), call.master_lock_changed(None)]
    plugins.reset_mock()
    from asyncio import sleep as real_sleep
    with patch('asyncio.sleep') as sleep:
        sleeper = FakeSleeper()
        sleep.side_effect = sleeper
        # the first thing is to sleep a bit
        await sleeper.next()
        assert sleeper.log == [3]
        assert plugins.mock_calls == []
        # takeover attempted
        states = [(app.my_id, {'willing': 99.0}), (app.my_id, {'willing': 100.0})]
        plugins.dcs_list_state.return_value = states
        await sleeper.next()
        assert sleeper.log == [3, 3]
        assert plugins.mock_calls ==  [
                call.dcs_list_state(),
                call.best_replicas([('42', {'willing': 99.0}), ('42', {'willing': 100.0})]),
                call.dcs_lock('master')]
Beispiel #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
Beispiel #5
0
def test_master_bootstrap(app):
    plugins = setup_plugins(app,
                            dcs_get_database_identifier=None,
                            dcs_lock=True,
                            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert plugins.mock_calls == [
        call.initialize(),
        call.get_my_id(),
        # check if we have a db identifier set
        call.dcs_get_database_identifier(),
        # no, ok, init our db
        call.pg_initdb(),
        # make sure it starts
        call.pg_start(),
        call.pg_get_database_identifier(),
        # lock the database identifier so no-one else gets here
        call.dcs_lock('database_identifier'),
        # while locked make sure there is no id set in the DCS before we got the lock
        call.dcs_get_database_identifier(),
        # Make the first backup while locked with no DCS
        call.pg_backup(),
        # set the database identifier AFTER
        call.dcs_set_database_identifier('42')
    ]
    # shut down cleanly and immediately
    assert timeout == 0
Beispiel #6
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
Beispiel #7
0
def test_master_boostrap_fails_to_lock_db_id(app):
    plugins = setup_plugins(app,
            dcs_get_database_identifier=None,
            dcs_lock=False,
            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert plugins.mock_calls ==  [
            call.initialize(),
            call.get_my_id(),
            # check if we have a db identifier set
            call.dcs_get_database_identifier(),
            # no, ok, init our db
            call.pg_initdb(),
            # make sure it starts
            call.pg_start(),
            call.pg_get_database_identifier(),
            # lock the database identifier so no-one else gets here
            call.dcs_lock('database_identifier')
            ]
    # shut down cleanly
    assert timeout == 5
Beispiel #8
0
def test_master_boostrap_fails_to_lock_db_id(app):
    plugins = setup_plugins(app,
                            dcs_get_database_identifier=None,
                            dcs_lock=False,
                            pg_get_database_identifier='42')
    timeout = app.initialize()
    assert plugins.mock_calls == [
        call.initialize(),
        call.get_my_id(),
        # check if we have a db identifier set
        call.dcs_get_database_identifier(),
        # no, ok, init our db
        call.pg_initdb(),
        # make sure it starts
        call.pg_start(),
        call.pg_get_database_identifier(),
        # lock the database identifier so no-one else gets here
        call.dcs_lock('database_identifier')
    ]
    # shut down cleanly
    assert timeout == 5
Beispiel #9
0
async def test_replica_tries_to_take_over(app):
    plugins = setup_plugins(app,
            pg_am_i_replica=True)
    assert app.initialize() == None
    plugins.reset_mock()
    # if there is no lock owner, we start looping trying to become master
    app.master_lock_changed(None)
    assert app._plugins.mock_calls ==  [call.pg_am_i_replica()]
    plugins.reset_mock()
    from asyncio import sleep as real_sleep
    with patch('asyncio.sleep') as sleep:
        sleeper = FakeSleeper()
        sleep.side_effect = sleeper
        # the first thing is to sleep a bit
        await sleeper.next()
        assert sleeper.log == [3]
        # takeover attempted
        await sleeper.next()
        assert sleeper.log == [3, 3]
        assert app._plugins.mock_calls ==  [
                call.dcs_get_all_state(),
                call.dcs_lock('master')]
Beispiel #10
0
async def test_replica_tries_to_take_over(app):
    plugins = setup_plugins(app, pg_replication_role='replica')
    assert app.initialize() == None
    plugins.reset_mock()
    # if there is no lock owner, we start looping trying to become master
    app.master_lock_changed(None)
    assert plugins.mock_calls == [
        call.pg_replication_role(),
        call.master_lock_changed(None)
    ]
    plugins.reset_mock()
    from asyncio import sleep as real_sleep
    with patch('asyncio.sleep') as sleep:
        sleeper = FakeSleeper()
        sleep.side_effect = sleeper
        # the first thing is to sleep a bit
        await sleeper.next()
        assert sleeper.log == [3]
        assert plugins.mock_calls == []
        # takeover attempted
        states = [(app.my_id, {
            'willing': 99.0
        }), (app.my_id, {
            'willing': 100.0
        })]
        plugins.dcs_list_state.return_value = states
        await sleeper.next()
        assert sleeper.log == [3, 3]
        assert plugins.mock_calls == [
            call.dcs_list_state(),
            call.best_replicas([('42', {
                'willing': 99.0
            }), ('42', {
                'willing': 100.0
            })]),
            call.dcs_lock('master')
        ]
Beispiel #11
0
async def test_master_start(app):
    plugins = setup_plugins(app,
                            dcs_get_database_identifier='1234',
                            dcs_lock=True,
                            pg_replication_role='master',
                            pg_get_database_identifier='1234')

    def start_monitoring():
        app.unhealthy('test_monitor', 'Waiting for first check')

    plugins.start_monitoring.side_effect = start_monitoring
    # 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'),
        # no master, so sure the DB is running
        call.pg_start(),
        # start monitoring
        call.start_monitoring(),
        call.dcs_watch(
            app.master_lock_changed,
            app._notify_state,
            app._notify_conn_info,
        ),
        call.get_conn_info(),
        # set our first state
        call.dcs_set_state({
            'host': '127.0.0.1',
            'replication_role': 'master',
            'health_problems': {
                'test_monitor': {
                    'can_be_replica': False,
                    'reason': 'Waiting for first check'
                }
            }
        })
    ]
    # Carry on running afterwards
    assert timeout == None
    assert app.health_problems == {
        'test_monitor': {
            'can_be_replica': False,
            'reason': 'Waiting for first check'
        }
    }
    # Our test monitor becomes healthy
    plugins.reset_mock()
    app.healthy('test_monitor')
    assert plugins.mock_calls == [
        call.dcs_set_state({
            'host': '127.0.0.1',
            'replication_role': 'master',
            'health_problems': {}
        }),
        call.pg_replication_role(),
        call.dcs_lock('master'),
        call.dcs_set_conn_info({'host': '127.0.0.1'}),
    ]