From f6a7e75e71c39e117fce7067d4be3e7557bf9ceb Mon Sep 17 00:00:00 2001 From: Matt Jacobson Date: Wed, 20 Mar 2024 16:13:34 -0400 Subject: [PATCH] Fix api/v2/status integration tests for simplified routing --- .../api/alerting/api_alertmanager_test.go | 72 +++++++++---------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/pkg/tests/api/alerting/api_alertmanager_test.go b/pkg/tests/api/alerting/api_alertmanager_test.go index 55b4c2438f43..8ea5bdfa20fd 100644 --- a/pkg/tests/api/alerting/api_alertmanager_test.go +++ b/pkg/tests/api/alerting/api_alertmanager_test.go @@ -145,13 +145,11 @@ func TestIntegrationAMConfigAccess(t *testing.T) { }) t.Run("when retrieve alertmanager configuration", func(t *testing.T) { - cfgBody := ` + cfgTemplate := ` { "template_files": null, "alertmanager_config": { - "route": { - "receiver": "grafana-default-email" - }, + "route": %s, "templates": null, "receivers": [{ "name": "grafana-default-email", @@ -169,12 +167,10 @@ func TestIntegrationAMConfigAccess(t *testing.T) { } } ` - cfgBodyWithAutogen := ` - { - "template_files": null, - "alertmanager_config": { - - "route": { + cfgWithoutAutogen := fmt.Sprintf(cfgTemplate, `{ + "receiver": "grafana-default-email" + }`) + cfgWithAutogen := fmt.Sprintf(cfgTemplate, `{ "receiver": "grafana-default-email", "routes": [{ "receiver": "grafana-default-email", @@ -185,24 +181,8 @@ func TestIntegrationAMConfigAccess(t *testing.T) { "object_matchers": [["__grafana_receiver__", "=", "grafana-default-email"]] }] }] - }, - "templates": null, - "receivers": [{ - "name": "grafana-default-email", - "grafana_managed_receiver_configs": [{ - "disableResolveMessage": false, - "uid": "", - "name": "email receiver", - "type": "email", - "secureFields": {}, - "settings": { - "addresses": "" - } - }] - }] - } - } - ` + }`) + testCases := []testCase{ { desc: "un-authenticated request should fail", @@ -214,19 +194,19 @@ func TestIntegrationAMConfigAccess(t *testing.T) { desc: "viewer request should succeed", url: "http://viewer:viewer@%s/api/alertmanager/grafana/config/api/v1/alerts", expStatus: http.StatusOK, - expBody: cfgBody, + expBody: cfgWithoutAutogen, }, { desc: "editor request should succeed", url: "http://editor:editor@%s/api/alertmanager/grafana/config/api/v1/alerts", expStatus: http.StatusOK, - expBody: cfgBody, + expBody: cfgWithoutAutogen, }, { desc: "admin request should succeed", url: "http://admin:admin@%s/api/alertmanager/grafana/config/api/v1/alerts", expStatus: http.StatusOK, - expBody: cfgBodyWithAutogen, + expBody: cfgWithAutogen, }, } @@ -2064,17 +2044,14 @@ func TestIntegrationAlertmanagerStatus(t *testing.T) { expBody string } - cfgBody := ` + cfgTemplate := ` { "cluster": { "peers": [], "status": "disabled" }, "config": { - "route": { - "receiver": "grafana-default-email", - "group_by": ["grafana_folder", "alertname"] - }, + "route": %s, "templates": null, "receivers": [{ "name": "grafana-default-email", @@ -2101,6 +2078,23 @@ func TestIntegrationAlertmanagerStatus(t *testing.T) { } } ` + cfgWithoutAutogen := fmt.Sprintf(cfgTemplate, `{ + "receiver": "grafana-default-email", + "group_by": ["grafana_folder", "alertname"] + }`) + cfgWithAutogen := fmt.Sprintf(cfgTemplate, `{ + "receiver": "grafana-default-email", + "routes": [{ + "receiver": "grafana-default-email", + "object_matchers": [["__grafana_autogenerated__", "=", "true"]], + "routes": [{ + "receiver": "grafana-default-email", + "group_by": ["grafana_folder", "alertname"], + "object_matchers": [["__grafana_receiver__", "=", "grafana-default-email"]] + }] + }], + "group_by": ["grafana_folder", "alertname"] + }`) testCases := []testCase{ { @@ -2113,19 +2107,19 @@ func TestIntegrationAlertmanagerStatus(t *testing.T) { desc: "viewer request should succeed", url: "http://viewer:viewer@%s/api/alertmanager/grafana/api/v2/status", expStatus: http.StatusOK, - expBody: cfgBody, + expBody: cfgWithoutAutogen, }, { desc: "editor request should succeed", url: "http://editor:editor@%s/api/alertmanager/grafana/api/v2/status", expStatus: http.StatusOK, - expBody: cfgBody, + expBody: cfgWithoutAutogen, }, { desc: "admin request should succeed", url: "http://admin:admin@%s/api/alertmanager/grafana/api/v2/status", expStatus: http.StatusOK, - expBody: cfgBody, + expBody: cfgWithAutogen, }, }