Alerting: Comprehensive payload for Alertmanager convert API tests (#110485)
* do not remove global config * create more comprehensive payload for mimir alertmanager testing
This commit is contained in:
@@ -2,39 +2,21 @@ package alerting
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
"github.com/grafana/grafana/pkg/tests/testinfra"
|
||||
)
|
||||
|
||||
const testAlertmanagerConfigYAML = `
|
||||
route:
|
||||
group_by: ['alertname']
|
||||
group_wait: 10s
|
||||
group_interval: 10s
|
||||
repeat_interval: 1h
|
||||
receiver: webhook
|
||||
|
||||
receivers:
|
||||
- name: webhook
|
||||
webhook_configs:
|
||||
- url: 'http://127.0.0.1:5001/'
|
||||
|
||||
inhibit_rules:
|
||||
- source_match:
|
||||
severity: 'critical'
|
||||
target_match:
|
||||
severity: 'warning'
|
||||
equal: ['alertname', 'dev', 'instance']
|
||||
`
|
||||
|
||||
func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in short mode")
|
||||
|
||||
// Setup Grafana with alerting import feature flag enabled
|
||||
}
|
||||
testinfra.SQLiteIntegrationTest(t)
|
||||
@@ -45,7 +27,7 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
DisableAnonymous: true,
|
||||
AppModeProduction: true,
|
||||
EnableFeatureToggles: []string{
|
||||
"alertingImportAlertmanagerAPI",
|
||||
featuremgmt.FlagAlertingImportAlertmanagerAPI,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -53,6 +35,15 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
|
||||
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
|
||||
|
||||
configYaml, err := testData.ReadFile(path.Join("test-data", "mimir-alertmanager-post.yaml"))
|
||||
require.NoError(t, err)
|
||||
template, err := testData.ReadFile(path.Join("test-data", "mimir-alertmanager.tmpl"))
|
||||
require.NoError(t, err)
|
||||
expected, err := testData.ReadFile(path.Join("test-data", "mimir-alertmanager-get.yaml"))
|
||||
require.NoError(t, err)
|
||||
var expectedConfig map[string]any
|
||||
require.NoError(t, yaml.Unmarshal(expected, &expectedConfig))
|
||||
|
||||
cleanup := func(identifier string) {
|
||||
deleteHeaders := map[string]string{
|
||||
"X-Grafana-Alerting-Config-Identifier": identifier,
|
||||
@@ -73,9 +64,9 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
TemplateFiles: map[string]string{
|
||||
"test.tmpl": `{{ define "test.template" }}Test template{{ end }}`,
|
||||
"mimir-alertmanager.tmpl": string(template),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -86,13 +77,15 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
"X-Grafana-Alerting-Config-Identifier": identifier,
|
||||
}
|
||||
retrievedConfig := apiClient.ConvertPrometheusGetAlertmanagerConfig(t, getHeaders)
|
||||
require.NotEmpty(t, retrievedConfig.AlertmanagerConfig)
|
||||
require.Contains(t, retrievedConfig.TemplateFiles, "test.tmpl")
|
||||
require.Equal(t, `{{ define "test.template" }}Test template{{ end }}`, retrievedConfig.TemplateFiles["test.tmpl"])
|
||||
var actualConfig map[string]any
|
||||
require.NoError(t, yaml.Unmarshal([]byte(retrievedConfig.AlertmanagerConfig), &actualConfig))
|
||||
|
||||
require.Contains(t, retrievedConfig.AlertmanagerConfig, "name: webhook")
|
||||
require.Contains(t, retrievedConfig.AlertmanagerConfig, "receiver: webhook")
|
||||
require.Contains(t, retrievedConfig.AlertmanagerConfig, "webhook_configs:")
|
||||
diff := cmp.Diff(expectedConfig, actualConfig)
|
||||
if diff != "" {
|
||||
t.Fatalf("unexpected config (-want +got):\n%s", diff)
|
||||
}
|
||||
require.Contains(t, retrievedConfig.TemplateFiles, "mimir-alertmanager.tmpl")
|
||||
require.Equal(t, string(template), retrievedConfig.TemplateFiles["mimir-alertmanager.tmpl"])
|
||||
})
|
||||
|
||||
t.Run("delete alertmanager configuration", func(t *testing.T) {
|
||||
@@ -107,9 +100,9 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
TemplateFiles: map[string]string{
|
||||
"test.tmpl": `{{ define "test.template" }}Test template{{ end }}`,
|
||||
"mimir-alertmanager.tmpl": string(template),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -139,7 +132,7 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
}
|
||||
|
||||
_, status, _ := apiClient.RawConvertPrometheusPostAlertmanagerConfig(t, amConfig, headers)
|
||||
@@ -160,7 +153,7 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
}
|
||||
|
||||
_, status, _ := apiClient.RawConvertPrometheusPostAlertmanagerConfig(t, amConfig, headers)
|
||||
@@ -175,7 +168,7 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
}
|
||||
|
||||
_, status, _ := apiClient.RawConvertPrometheusPostAlertmanagerConfig(t, amConfig, headers)
|
||||
@@ -204,7 +197,7 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
}
|
||||
|
||||
_, status, _ := apiClient.RawConvertPrometheusPostAlertmanagerConfig(t, amConfig, createHeaders)
|
||||
@@ -232,7 +225,7 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints(t *testing.T) {
|
||||
}
|
||||
|
||||
amConfig1 := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
TemplateFiles: map[string]string{
|
||||
"config1.tmpl": `{{ define "config1.template" }}Config 1{{ end }}`,
|
||||
},
|
||||
@@ -293,7 +286,7 @@ receivers:
|
||||
}
|
||||
|
||||
amConfig1 := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
TemplateFiles: map[string]string{
|
||||
"first.tmpl": `{{ define "first.template" }}First Config{{ end }}`,
|
||||
},
|
||||
@@ -357,10 +350,12 @@ func TestIntegrationConvertPrometheusAlertmanagerEndpoints_FeatureFlagDisabled(t
|
||||
"X-Grafana-Alerting-Config-Identifier": "test-config",
|
||||
"X-Grafana-Alerting-Merge-Matchers": "environment=test",
|
||||
}
|
||||
configYaml, err := testData.ReadFile(path.Join("test-data", "mimir-alertmanager-post.yaml"))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("POST should return not implemented when feature flag disabled", func(t *testing.T) {
|
||||
amConfig := apimodels.AlertmanagerUserConfig{
|
||||
AlertmanagerConfig: testAlertmanagerConfigYAML,
|
||||
AlertmanagerConfig: string(configYaml),
|
||||
}
|
||||
|
||||
_, status, _ := apiClient.RawConvertPrometheusPostAlertmanagerConfig(t, amConfig, headers)
|
||||
|
||||
Reference in New Issue
Block a user