def test_add_single_annotation_non_specific(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.alerts[0].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }
    assert alert_group.alerts[0].specific_annotations == {}

    alert_group.add(annotations={"hello": "world"})

    assert alert_group.alerts[0].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "hello": "world",
    }
    assert alert_group.alerts[0].specific_annotations == {}

    assert alert_group.alerts[1].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "this": "isspecific",
        "hello": "world",
    }
def test_add_single_label(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.alerts[0].labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "mu": "sik",
        "severity": "warning",
    }
    assert alert_group.alerts[0].specific_labels == {"mu": "sik"}

    alert_group.add(labels={"hello": "world"})

    assert alert_group.alerts[0].labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "mu": "sik",
        "severity": "warning",
        "hello": "world",
    }
    assert alert_group.alerts[0].specific_labels == {"mu": "sik"}

    assert alert_group.alerts[1].labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "severity": "warning",
        "hello": "world",
    }
def test_update_common_elements(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    for alert in alert_group.alerts:
        alert.labels["a"] = "a"
        alert.annotations["a"] = "a"

    alert_group.update_common_elements(targets=["annotations", "labels"])

    assert alert_group.common_labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "severity": "warning",
        "a": "a",
    }

    assert alert_group.common_annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "a": "a",
    }
def test_update_common_labels_v1(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "severity": "warning",
    }

    for alert in alert_group.alerts:
        alert.labels["a"] = "a"

    alert_group.update_common_labels()
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "severity": "warning",
        "a": "a",
    }
Exemple #5
0
def test_remove_annotations(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group, description="Alert group before remove")

    # Annotations before

    assert alert_group.alerts[0].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }
    assert alert_group.alerts[0].specific_annotations == {}

    assert alert_group.alerts[1].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "this": "isspecific",
    }
    assert alert_group.alerts[1].specific_annotations == {
        "this": "isspecific",
    }

    alert_group.remove(annotations=["this", "whatever"])
    helpers.wrapped_debug(alert_group, description="Alert group after remove")

    # Annotations after

    assert alert_group.alerts[0].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }
    assert alert_group.alerts[0].specific_annotations == {}

    assert alert_group.alerts[1].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }
    assert alert_group.alerts[1].specific_annotations == {}

    # Labels stayed the same

    assert alert_group.alerts[0].labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "mu": "sik",
        "severity": "warning",
    }
    assert alert_group.alerts[0].specific_labels == {
        "mu": "sik",
    }

    assert alert_group.alerts[1].labels == {
        "alertname": "WhatEver",
        "foo_bar_qux": "foo_moo_zoom",
        "severity": "warning",
    }
    assert alert_group.alerts[1].specific_labels == {}
Exemple #6
0
def test_remove_label(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group, description="Alert group before remove")

    assert "mu" in alert_group.alerts[0].labels

    alert_group.remove(labels="this")

    assert "this" not in alert_group.alerts[0].labels
Exemple #7
0
def test_update_specific_annotations(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    alert_group.alerts[0].annotations["hallo"] = "world"

    alert_group.update_specific_annotations()

    assert alert_group.alerts[0].specific_annotations == {"hallo": "world"}
Exemple #8
0
def test_update_specific_labels(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.alerts[0].specific_labels == {"mu": "sik"}

    alert_group.alerts[0].labels["mu"] = "sik"

    alert_group.update_specific_labels()

    assert alert_group.alerts[0].specific_labels == {"mu": "sik"}
Exemple #9
0
def test_update_specific_elements_annotations_invalid(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.alerts[0].specific_annotations == {}
    assert alert_group.alerts[1].specific_annotations == {"this": "isspecific"}

    alert_group.alerts[0].annotations["hallo"] = "world"

    with pytest.raises(KeyError):
        alert_group.update_specific_elements("anotations")
def test_remove_re_multiple_annotations(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)

    alert_group.remove_re(
        annotations=[r"^(description|summary)$", r"^(this|that)$"])

    assert alert_group.common_annotations == {}

    for alert in alert_group.alerts:
        assert alert.annotations == {}
        assert alert.specific_annotations == {}
Exemple #11
0
def test_add_prefix_labels(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_labels["alertname"] == "WhatEver"
    assert alert_group.alerts[0].labels["alertname"] == "WhatEver"
    assert alert_group.alerts[1].labels["alertname"] == "WhatEver"

    alert_group.add_prefix(labels={"alertname": "PREFIX: "})
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_labels["alertname"] == "PREFIX: WhatEver"
    assert alert_group.alerts[0].labels["alertname"] == "PREFIX: WhatEver"
    assert alert_group.alerts[1].labels["alertname"] == "PREFIX: WhatEver"
def test_override_label(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_labels["alertname"] == "WhatEver"
    assert alert_group.alerts[0].labels["alertname"] == "WhatEver"
    assert alert_group.alerts[1].labels["alertname"] == "WhatEver"

    alert_group.override(labels={"alertname": "foo"})
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_labels["alertname"] == "foo"
    assert alert_group.alerts[0].labels["alertname"] == "foo"
    assert alert_group.alerts[1].labels["alertname"] == "foo"
def test_override_annotation(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_annotations[
        "description"] == "A Prometheus job has disappe"
    assert (alert_group.alerts[0].annotations["description"] ==
            "A Prometheus job has disappe")
    assert (alert_group.alerts[1].annotations["description"] ==
            "A Prometheus job has disappe")

    alert_group.override(annotations={"description": "foo"})
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_annotations["description"] == "foo"
    assert alert_group.alerts[0].annotations["description"] == "foo"
    assert alert_group.alerts[1].annotations["description"] == "foo"
def test_remove_re_single_label_as_str(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group,
                          description="Alert group BEFORE remove_re")

    assert "mu" in alert_group.alerts[0].labels
    assert "mu" in alert_group.alerts[0].specific_labels
    assert "foo_bar_qux" in alert_group.alerts[0].labels
    assert "foo_bar_qux" in alert_group.alerts[1].labels

    alert_group.remove_re(labels=r"^(foo|mu).*$")
    helpers.wrapped_debug(alert_group,
                          description="Alert group AFTER remove_re")

    assert "mu" not in alert_group.alerts[0].labels
    assert "mu" not in alert_group.alerts[0].specific_labels
    assert "foo_bar_qux" not in alert_group.alerts[0].labels
    assert "foo_bar_qux" not in alert_group.alerts[1].labels
def test_add_multiple_anotations_and_labels(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    alert_group.add(labels={"hello": "world"}, annotations={"hello": "world"})

    assert alert_group.common_annotations["hello"] == "world"
    assert alert_group.common_labels["hello"] == "world"

    assert alert_group.alerts[0].annotations["hello"] == "world"
    assert alert_group.alerts[1].annotations["hello"] == "world"
    assert alert_group.alerts[0].labels["hello"] == "world"
    assert alert_group.alerts[1].labels["hello"] == "world"

    assert not alert_group.alerts[0].specific_annotations.get("hello", None)
    assert not alert_group.alerts[1].specific_labels.get("hello", None)
    assert not alert_group.alerts[0].specific_annotations.get("hello", None)
    assert not alert_group.alerts[1].specific_labels.get("hello", None)
Exemple #16
0
def test_update_specific_elements_all_defaults(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.alerts[0].specific_annotations == {}
    assert alert_group.alerts[1].specific_annotations == {"this": "isspecific"}

    assert alert_group.alerts[0].specific_labels == {"mu": "sik"}
    assert alert_group.alerts[1].specific_labels == {}

    alert_group.alerts[1].labels["hallo"] = "world"
    alert_group.alerts[0].annotations["hallo"] = "world"
    alert_group.update_specific_elements()

    assert alert_group.alerts[0].specific_annotations == {"hallo": "world"}
    assert alert_group.alerts[1].specific_annotations == {"this": "isspecific"}

    assert alert_group.alerts[0].specific_labels == {"mu": "sik"}
    assert alert_group.alerts[1].specific_labels == {"hallo": "world"}
def test_update_common_annotations_v2(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }

    alert_group.alerts[0].annotations["a"] = "a"
    alert_group.alerts[1].annotations["a"] = "aa"

    alert_group.update_common_annotations()
    helpers.wrapped_debug(alert_group)

    assert alert_group.common_annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }
def test_add_none_none(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)

    alert_group.add()

    assert alert_group.dict() == AlertGroup(**payload)
Exemple #19
0
def test_remove_nothing(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    alert_group.remove()

    assert alert_group.dict() == AlertGroup(**payload)
Exemple #20
0
def test_create_alert_group(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert alert_group.receiver == "generic"
    assert alert_group.status == "firing"
    assert alert_group.group_labels == {"alertname": "WhatEver"}
    assert len(alert_group.common_labels) == 3
    assert len(alert_group.common_annotations) == 2
    assert alert_group.external_url == "http://1217896f2a1d:9093"
    assert alert_group.version == "4"
    assert alert_group.group_key == '{}:{alertname="WhatEver"}'
    assert alert_group.truncated_alerts == 0
Exemple #21
0
def test_create_alert_group_alerts(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group)

    assert len(alert_group.alerts[0].labels) == 4
    assert len(alert_group.alerts[0].annotations) == 2
    assert len(alert_group.alerts[1].labels) == 3
    assert len(alert_group.alerts[1].annotations) == 3

    assert len(alert_group.alerts[0].specific_labels) == 1
    assert len(alert_group.alerts[0].specific_annotations) == 0
    assert len(alert_group.alerts[1].specific_labels) == 0
    assert len(alert_group.alerts[1].specific_annotations) == 1
def test_add_single_annotation_specific(helpers, data_path):
    with data_path.joinpath("payload-simple-01.json").open() as file:
        payload = json.load(file)

    alert_group = AlertGroup(**payload)
    helpers.wrapped_debug(alert_group, "original")

    alert_group.alerts[1].annotations["hallo"] = "bump"
    alert_group.update_specific_elements(targets="annotations")
    helpers.wrapped_debug(alert_group, "after adding hallo bump to alert1")

    assert alert_group.alerts[0].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
    }
    assert alert_group.alerts[0].specific_annotations == {}

    assert alert_group.alerts[1].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "this": "isspecific",
        "hallo": "bump",
    }
    assert alert_group.alerts[1].specific_annotations == {
        "hallo": "bump",
        "this": "isspecific",
    }

    alert_group.add(annotations={"hallo": "world"})
    helpers.wrapped_debug(alert_group, "after using add method")

    assert alert_group.alerts[0].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "hallo": "world",
    }
    assert alert_group.alerts[0].specific_annotations == {"hallo": "world"}

    assert alert_group.alerts[1].annotations == {
        "description": "A Prometheus job has disappe",
        "summary": "Prometheus job missing (instance )",
        "this": "isspecific",
        "hallo": "bump",
    }
    assert alert_group.alerts[1].specific_annotations == {
        "hallo": "bump",
        "this": "isspecific",
    }