def test_create_check_function():
    def insane_check(item, _no_params, info):
        assert item == "Test Item"
        assert _no_params == {}
        assert info == ["info"]
        yield 0, "Main info", [("metric1", 23, 2, 3)]
        yield 1, "still main, but very long\nadditional1", [
            ("metric2", 23, None, None, "0", None)
        ]
        yield 2, "additional2\nadditional3", [("metric3", 23, "wtf is this")]

    new_function = check_plugins_legacy._create_check_function(
        "test_plugin",
        {
            "check_function": insane_check,
            "service_description": "Foo %s",
        },
    )

    fixed_params = inspect.signature(new_function).parameters
    assert list(fixed_params) == ["item", "params", "section"]
    assert inspect.isgeneratorfunction(new_function)

    results = new_function(item="Test Item", section=["info"], params={})
    # we cannot compare the actual Result objects because of
    # the nasty bypassing of validation in the legacy conversion
    assert [tuple(r) for r in results] == [
        (checking_classes.State.OK, "Main info", "Main info"),  # Result
        ("metric1", 23.0, (2.0, 3.0), (None, None)),  # Metric
        (checking_classes.State.WARN, "still main, but very long",
         "still main, but very long\nadditional1"),
        ("metric2", 23.0, (None, None), (0.0, None)),
        (checking_classes.State.CRIT, "", "additional2\nadditional3"),
        ("metric3", 23.0, (None, None), (None, None)),
    ]
Example #2
0
def test_create_check_function():
    def insane_check(item, _no_params, info):
        assert item == "Test Item"
        assert _no_params == {}
        assert info == ["info"]
        yield 0, "Main info", [("mymetric", 23, 2, 3)]
        yield 1, "still main, but very long\nadditional1"
        yield 2, "additional2\nadditional3"

    new_function = check_plugins_legacy._create_check_function(
        "test_plugin",
        {
            "check_function": insane_check,
            "service_description": "Foo %s",
        },
        None,
    )

    fixed_params = inspect.signature(new_function).parameters
    assert list(fixed_params) == ["item", "section"]
    assert inspect.isgeneratorfunction(new_function)

    result = new_function(item="Test Item", section=["info"])
    assert list(result) == [
        checking_types.Result(
            state=checking_types.state.OK,
            summary="Main info",
        ),
        checking_types.Metric("mymetric", 23.0, levels=(2.0, 3.0)),
        checking_types.Result(
            state=checking_types.state.WARN,
            summary="still main, but very long",
            details="additional1",
        ),
        checking_types.Result(state=checking_types.state.CRIT,
                              details="additional2\nadditional3"),
    ]