コード例 #1
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def test_publish_switch_off(
        universal_webhooks, test_application, mocker, faker):
    with mocker.patch.object(switch, 'is_switched_on', return_value=False):
        notifier = Notifier()
        action_type = action_types.UPDATE_CONFIG
        action_data = {'application_name': faker.uuid4()[:8]}
        notifier.publish_universal(
            action_type, faker.first_name(), NORMAL_USER, action_data,
            severity=1)
        assert notifier.hook_queue.qsize() == 0
コード例 #2
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def test_publish_normal(test_webhook, test_application, normal_webhooks,
                        mock_response, faker, mocker, mock_logger,
                        severity):
    notifier = Notifier()
    application_name = test_application.application_name
    user_name = 'foo'
    for action_name in action_types._action_map:
        action_type = getattr(action_types, action_name)
        action_data = {'application_name': faker.uuid4()[:8]}
        notifier.publish([application_name], user_name, NORMAL_USER,
                         action_type, action_data, severity=severity)
    assert notifier.hook_queue.qsize() == (
        len(normal_webhooks) * len(action_types._action_map))

    request = mocker.patch.object(Session, 'request',
                                  return_value=mock_response())
    notifier.start()
    assert notifier.hook_queue.qsize() != 0
    gevent.sleep(0.1)
    assert notifier.hook_queue.qsize() == 0

    total = len(normal_webhooks) * len(action_types._action_map)
    assert request.call_count == total
    assert request.call_args[1]['json']['severity'] == severity
    assert mock_logger.exception.call_count == 0
コード例 #3
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def test_publish_universal(
        test_webhook, test_application, universal_webhooks,
        mock_response, faker, mocker, mock_logger, severity):
    notifier = Notifier()
    for action_name in action_types._action_map:
        action_type = getattr(action_types, action_name)
        action_data = {'fake_msg': faker.paragraph()}
        notifier.publish_universal(
            action_type, faker.first_name(), NORMAL_USER, action_data,
            severity=severity)
    assert notifier.hook_queue.qsize() == (
        len(universal_webhooks) * len(action_types._action_map))

    total = len(universal_webhooks) * len(action_types._action_map)
    request = mocker.patch.object(
        Session, 'request', return_value=mock_response())
    notifier.start()
    assert notifier.hook_queue.qsize() != 0
    gevent.sleep(0.1)
    assert notifier.hook_queue.qsize() == 0

    assert request.call_count == total
    assert request.call_args[1]['json']['application_names'] == []
    assert request.call_args[1]['json']['severity'] == severity
    assert mock_logger.exception.call_count == 0
コード例 #4
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def test_notifier_start(test_webhook):
    notifier = Notifier()
    assert notifier._running is False
    notifier.start()
    assert notifier._running

    worker = notifier._worker
    notifier.start()
    assert notifier._running
    assert id(worker) == id(notifier._worker)
コード例 #5
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def test_catch_unexpected_callback_error(
        faker, mock_bad_callback, universal_webhooks, mock_logger):
    mock_bad_callback(ValueError)
    notifier = Notifier()
    action_type = getattr(action_types, 'CREATE_CONFIG_CLUSTER')
    action_data = {'application_name': faker.uuid4()[:8]}
    notifier.publish_universal(
        action_type, faker.first_name(), NORMAL_USER, action_data,
        severity=1)
    assert notifier.hook_queue.qsize() == len(universal_webhooks)

    notifier.start()
    assert notifier.hook_queue.qsize() != 0
    gevent.sleep(0.1)
    assert notifier.hook_queue.qsize() == 0

    assert mock_logger.exception.call_count == len(universal_webhooks)
コード例 #6
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def test_catch_remote_error(faker, mock_bad_callback, universal_webhooks,
                            mock_logger, error):
    mock_bad_callback(error)
    notifier = Notifier()
    action_type = getattr(action_types, 'CREATE_CONFIG_CLUSTER')
    action_data = {'application_name': faker.uuid4()[:8]}
    notifier.publish_universal(
        action_type, faker.first_name(), NORMAL_USER, action_data,
        severity=1)
    assert notifier.hook_queue.qsize() == len(universal_webhooks)

    notifier.start()
    assert notifier.hook_queue.qsize() != 0
    gevent.sleep(0.1)
    assert notifier.hook_queue.qsize() == 0
    url_summary = hashlib.md5('http://u.foo.me').hexdigest()
    mock_logger.warn.assert_called_with(
        'Remote Request Failed: %s, %s', url_summary, str(error()))
    assert mock_logger.warn.call_count == len(universal_webhooks)
コード例 #7
0
ファイル: test_notify.py プロジェクト: zhoudaqing/huskar
def notifier():
    notifier = Notifier()
    notifier.start()
    yield notifier
    notifier._running = False