oncall-engine/engine/apps/labels/tests/test_add_service_label.py
Yulya Artyukhina 3d4ce622cb
Add default service_name label for Alerting integrations (#5373)
# What this PR does
- The `service_name` label will be added to Grafana Alerting integration
when it is created, if it wasn't added by user.
- Adds celery task that should be started manually and will add the
`service_name` dynamic label to all existing Grafana Alerting
integrations.

## Which issue(s) this PR closes

Related to https://github.com/grafana/oncall-private/issues/2975

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.

---------

Co-authored-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
2025-01-14 10:02:23 +00:00

40 lines
1.8 KiB
Python

import pytest
from apps.alerts.constants import SERVICE_LABEL, SERVICE_LABEL_TEMPLATE_FOR_ALERTING_INTEGRATION
from apps.alerts.models import AlertReceiveChannel
from apps.labels.tasks import add_service_label_per_org
@pytest.mark.django_db
def test_add_service_label_per_org(make_organization, make_alert_receive_channel, make_label_key):
organization = make_organization()
alert_receive_channel_alerting_no_labels = make_alert_receive_channel(
organization=organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA_ALERTING
)
alert_receive_channel_alerting_with_label = make_alert_receive_channel(
organization=organization,
integration=AlertReceiveChannel.INTEGRATION_GRAFANA_ALERTING,
alert_group_labels_custom=[["test", None, "test_template"]],
)
alert_receive_channel_grafana = make_alert_receive_channel(
organization=organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA
)
service_name_label_key = make_label_key(organization, key_id="service_label_id", key_name=SERVICE_LABEL)
expected_service_name_label = [service_name_label_key.id, None, SERVICE_LABEL_TEMPLATE_FOR_ALERTING_INTEGRATION]
add_service_label_per_org(organization.id)
for alert_receive_channel in [
alert_receive_channel_alerting_no_labels,
alert_receive_channel_alerting_with_label,
alert_receive_channel_grafana,
]:
alert_receive_channel.refresh_from_db()
assert alert_receive_channel_alerting_no_labels.alert_group_labels_custom == [expected_service_name_label]
assert alert_receive_channel_alerting_with_label.alert_group_labels_custom == [
expected_service_name_label,
["test", None, "test_template"],
]
assert alert_receive_channel_grafana.alert_group_labels_custom is None