Exemplo n.º 1
0
def test_evaluate_alert(mock_deliver_alert, setup_database):
    dbsession = setup_database

    # Test error with Observer SQL statement
    alert1 = create_alert(dbsession, "$%^&")
    evaluate_alert(alert1.id, alert1.label, dbsession)
    assert alert1.logs[-1].state == AlertState.ERROR

    # Test error with alert lacking observer
    alert2 = dbsession.query(Alert).filter_by(label="No Observer").one()
    evaluate_alert(alert2.id, alert2.label, dbsession)
    assert alert2.logs[-1].state == AlertState.ERROR

    # Test pass on alert lacking validator
    alert3 = create_alert(dbsession, "SELECT 55")
    evaluate_alert(alert3.id, alert3.label, dbsession)
    assert alert3.logs[-1].state == AlertState.PASS

    # Test triggering successful alert
    alert4 = create_alert(dbsession, "SELECT 55", "not null", "{}")
    evaluate_alert(alert4.id, alert4.label, dbsession)
    assert mock_deliver_alert.call_count == 1
    assert alert4.logs[-1].state == AlertState.TRIGGER
Exemplo n.º 2
0
def test_evaluate_alert(mock_deliver_alert, setup_database):
    db_session = setup_database

    # Test error with Observer SQL statement
    alert1 = create_alert(db_session, "$%^&")
    evaluate_alert(alert1.id, alert1.label, db_session)
    assert alert1.logs[-1].state == AlertState.ERROR

    # Test pass on alert lacking validator config
    alert2 = create_alert(db_session, "SELECT 55")
    # evaluation fails if config is malformed
    with pytest.raises(json.decoder.JSONDecodeError):
        evaluate_alert(alert2.id, alert2.label, db_session)
    assert not alert2.logs

    # Test triggering successful alert
    alert3 = create_alert(db_session, "SELECT 55", "not null", "{}")
    evaluate_alert(alert3.id, alert3.label, db_session)
    assert mock_deliver_alert.call_count == 1
    assert alert3.logs[-1].state == AlertState.TRIGGER