abd1050f98
* Alerting: Validate contact point configuration during migration to Unified Alerting (#40717)
* Alerting: Validate contact point configuration during the migration
This minimises the chances of generating broken configuration as part of the migration. Originally, we wanted to generate it and not produce a hard stop in Grafana but this strategy has the chance to avoid delivering notifications for our users.
We now think it's better to hard stop the migration and let the user take care of resolving the configuration manually.
(cherry picked from commit 74fb491b6a)
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package ualert
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_validateAlertmanagerConfig(t *testing.T) {
|
|
tc := []struct {
|
|
name string
|
|
receivers []*PostableGrafanaReceiver
|
|
err error
|
|
}{
|
|
{
|
|
name: "when a slack receiver does not have a valid URL - it should error",
|
|
receivers: []*PostableGrafanaReceiver{
|
|
{
|
|
UID: util.GenerateShortUID(),
|
|
Name: "SlackWithBadURL",
|
|
Type: "slack",
|
|
Settings: simplejson.NewFromAny(map[string]interface{}{}),
|
|
SecureSettings: map[string]string{"url": invalidUri},
|
|
},
|
|
},
|
|
err: fmt.Errorf("failed to validate receiver \"SlackWithBadURL\" of type \"slack\": invalid URL %q: parse %q: net/url: invalid control character in URL", invalidUri, invalidUri),
|
|
},
|
|
{
|
|
name: "when a slack receiver has an invalid recipient - it should error",
|
|
receivers: []*PostableGrafanaReceiver{
|
|
{
|
|
UID: util.GenerateShortUID(),
|
|
Name: "SlackWithBadRecipient",
|
|
Type: "slack",
|
|
Settings: simplejson.NewFromAny(map[string]interface{}{"recipient": "this-doesnt-pass"}),
|
|
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
|
},
|
|
},
|
|
err: errors.New("failed to validate receiver \"SlackWithBadRecipient\" of type \"slack\": recipient on invalid format: \"this-doesnt-pass\""),
|
|
},
|
|
{
|
|
name: "when the configuration is valid - it should not error",
|
|
receivers: []*PostableGrafanaReceiver{
|
|
{
|
|
UID: util.GenerateShortUID(),
|
|
Name: "SlackWithBadURL",
|
|
Type: "slack",
|
|
Settings: simplejson.NewFromAny(map[string]interface{}{"recipient": "#a-good-channel"}),
|
|
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tc {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mg := newTestMigration(t)
|
|
orgID := int64(1)
|
|
|
|
config := configFromReceivers(t, tt.receivers)
|
|
require.NoError(t, config.EncryptSecureSettings()) // make sure we encrypt the settings
|
|
err := mg.validateAlertmanagerConfig(orgID, config)
|
|
if tt.err != nil {
|
|
require.Error(t, err)
|
|
require.EqualError(t, err, tt.err.Error())
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func configFromReceivers(t *testing.T, receivers []*PostableGrafanaReceiver) *PostableUserConfig {
|
|
t.Helper()
|
|
|
|
return &PostableUserConfig{
|
|
AlertmanagerConfig: PostableApiAlertingConfig{
|
|
Receivers: []*PostableApiReceiver{
|
|
{GrafanaManagedReceivers: receivers},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
const invalidUri = "�6�M��)uk譹1(�h`$�o�N>mĕ����cS2�dh![ę� ���`csB�!��OSxP�{�"
|