Example #1
0
async def test_handle_pubsub_msg(mocker, monkeypatch, consumer, raw_msg_data,
                                 creation_audit_log_data, caplog, pubsub_msg,
                                 mock_get_and_validate,
                                 mock_create_gevent_msg):
    """Validate pubsub msg, create GEventMessage, and add to success chnl."""
    event_msg = event_consumer.GEventMessage(pubsub_msg,
                                             creation_audit_log_data,
                                             phase='consume')
    mock_create_gevent_msg.return_value = event_msg

    mock_run_coro_threadsafe = mocker.Mock()
    patch = ('gordon_gcp.plugins.service.event_consumer.asyncio.'
             'run_coroutine_threadsafe')
    monkeypatch.setattr(patch, mock_run_coro_threadsafe)

    await consumer._handle_pubsub_msg(pubsub_msg)

    creation_audit_log_data.update({'resourceRecords': []})
    mock_get_and_validate.assert_called_once_with(raw_msg_data)
    mock_create_gevent_msg.assert_called_once_with(pubsub_msg,
                                                   creation_audit_log_data,
                                                   'audit-log')

    assert 4 == len(caplog.records)
    assert 'consume' == event_msg.phase
    mock_run_coro_threadsafe.assert_called_once()
Example #2
0
def deletions_gevent_msg(mocker, deletion_audit_log_data):
    fake_data = {
            'action': deletion_audit_log_data['action'],
            'resourceName': deletion_audit_log_data['resourceName'],
            'resourceRecords': [],
            'timestamp': deletion_audit_log_data['timestamp']
        }
    pubsub_msg = mocker.Mock()
    pubsub_msg._ack_id = '1:2'
    return event_consumer.GEventMessage(
        pubsub_msg, fake_data)
Example #3
0
def test_implements_message_interface(phase, pubsub_msg, raw_msg_data):
    """GEventMessage implements IEventMessage."""
    args = [pubsub_msg, raw_msg_data]
    if phase:
        args.append(phase)

    msg = event_consumer.GEventMessage(*args)

    assert pubsub_msg is msg._pubsub_msg
    assert pubsub_msg.message_id is msg.msg_id
    assert raw_msg_data == msg.data
    assert not msg.history_log
    assert phase == msg.phase
Example #4
0
def test_event_msg_update_phase(mocker, pubsub_msg, raw_msg_data):
    """Update phase of the message and write to history log."""
    mocker.patch(DATETIME_PATCH, conftest.MockDatetime)
    msg = event_consumer.GEventMessage(pubsub_msg, raw_msg_data)

    msg.update_phase('emo')

    assert 'emo' == msg.phase
    expected = [{
        'timestamp': '2018-01-01T11:30:00.000000Z',
        'plugin': None,
        'message': 'Updated phase from None to emo'
    }]
    assert expected == msg.history_log
Example #5
0
def test_event_msg_append_to_history(mocker, pubsub_msg, raw_msg_data):
    """Write entry to event message history log."""
    mocker.patch(DATETIME_PATCH, conftest.MockDatetime)
    msg = event_consumer.GEventMessage(pubsub_msg, raw_msg_data)
    entry_msg = 'an entry'
    plugin = 'test-plugin'
    msg.append_to_history(entry_msg, plugin)

    expected = [{
        'timestamp': '2018-01-01T11:30:00.000000Z',
        'plugin': plugin,
        'message': entry_msg
    }]

    assert expected == msg.history_log
Example #6
0
async def test_handle_message(mocker, consumer, caplog, pubsub_msg):
    """Consumer acks Pub/Sub message."""
    mocker.patch(DATETIME_PATCH, conftest.MockDatetime)

    event_msg = event_consumer.GEventMessage(pubsub_msg, {})

    await consumer.handle_message(event_msg)

    pubsub_msg.ack.assert_called_once_with()

    exp_entry = {
        'timestamp': '2018-01-01T11:30:00.000000Z',
        'plugin': 'cleanup',
        'message': 'Acknowledged message in Pub/Sub.',
    }
    assert exp_entry in event_msg.history_log
    assert 2 == len(caplog.records)