mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
3537c5440f
Some refactoring that will simplify next changes for dry-run PRs. This should be no-op as far as the created ngalert resources and database state, though it does change some logs. The key change here is to modify migrateOrg to return pairs of legacy struct + ngalert struct instead of actually persisting the alerts and alertmanager config. This will allow us to capture error information during dry-run migration. It also moves most persistence-related operations such as title deduplication and folder creation to the right before we persist. This will simplify eventual partial migrations (individual alerts, dashboards, channels, ...). Additionally it changes channel code to deal with PostableGrafanaReceiver instead of PostableApiReceiver (integration instead of contact point).
140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
package migration
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"testing"
|
||
|
||
"github.com/stretchr/testify/require"
|
||
|
||
"github.com/grafana/grafana/pkg/infra/db"
|
||
"github.com/grafana/grafana/pkg/services/folder"
|
||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||
"github.com/grafana/grafana/pkg/util"
|
||
)
|
||
|
||
func Test_validateAlertmanagerConfig(t *testing.T) {
|
||
tc := []struct {
|
||
name string
|
||
receivers []*apimodels.PostableGrafanaReceiver
|
||
err error
|
||
}{
|
||
{
|
||
name: "when a slack receiver does not have a valid URL - it should error",
|
||
receivers: []*apimodels.PostableGrafanaReceiver{
|
||
{
|
||
UID: "test-uid",
|
||
Name: "SlackWithBadURL",
|
||
Type: "slack",
|
||
Settings: mustRawMessage(map[string]any{}),
|
||
SecureSettings: map[string]string{"url": invalidUri},
|
||
},
|
||
},
|
||
err: fmt.Errorf("failed to validate integration \"SlackWithBadURL\" (UID test-uid) of type \"slack\": invalid URL %q", invalidUri),
|
||
},
|
||
{
|
||
name: "when a slack receiver has an invalid recipient - it should not error",
|
||
receivers: []*apimodels.PostableGrafanaReceiver{
|
||
{
|
||
UID: util.GenerateShortUID(),
|
||
Name: "SlackWithBadRecipient",
|
||
Type: "slack",
|
||
Settings: mustRawMessage(map[string]any{"recipient": "this passes"}),
|
||
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
||
},
|
||
},
|
||
},
|
||
{
|
||
name: "when the configuration is valid - it should not error",
|
||
receivers: []*apimodels.PostableGrafanaReceiver{
|
||
{
|
||
UID: util.GenerateShortUID(),
|
||
Name: "SlackWithBadURL",
|
||
Type: "slack",
|
||
Settings: mustRawMessage(map[string]interface{}{"recipient": "#a-good-channel"}),
|
||
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
sqlStore := db.InitTestDB(t)
|
||
for _, tt := range tc {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
service := NewTestMigrationService(t, sqlStore, nil)
|
||
mg := service.newOrgMigration(1)
|
||
|
||
config := configFromReceivers(t, tt.receivers)
|
||
require.NoError(t, encryptSecureSettings(config, mg)) // make sure we encrypt the settings
|
||
err := service.newSync(1).validateAlertmanagerConfig(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 []*apimodels.PostableGrafanaReceiver) *apimodels.PostableUserConfig {
|
||
t.Helper()
|
||
|
||
return &apimodels.PostableUserConfig{
|
||
AlertmanagerConfig: apimodels.PostableApiAlertingConfig{
|
||
Receivers: []*apimodels.PostableApiReceiver{
|
||
{PostableGrafanaReceivers: apimodels.PostableGrafanaReceivers{GrafanaManagedReceivers: receivers}},
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
func encryptSecureSettings(c *apimodels.PostableUserConfig, m *OrgMigration) error {
|
||
for _, r := range c.AlertmanagerConfig.Receivers {
|
||
for _, gr := range r.GrafanaManagedReceivers {
|
||
err := m.encryptSecureSettings(gr.SecureSettings)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
const invalidUri = "<22>6<EFBFBD>M<EFBFBD><4D>)uk譹1(<28>h`$<24>o<EFBFBD>N>mĕ<6D><C495><EFBFBD><EFBFBD>cS2<53>dh![ę<> <09><><EFBFBD>`csB<73>!<21><>OSxP<78>{<7B>"
|
||
|
||
func Test_getAlertFolderNameFromDashboard(t *testing.T) {
|
||
t.Run("should include full title", func(t *testing.T) {
|
||
hash := util.GenerateShortUID()
|
||
f := &folder.Folder{
|
||
Title: "TEST",
|
||
}
|
||
name := generateAlertFolderName(f, permissionHash(hash))
|
||
require.Contains(t, name, f.Title)
|
||
require.Contains(t, name, hash)
|
||
})
|
||
t.Run("should cut title to the length", func(t *testing.T) {
|
||
title := ""
|
||
for {
|
||
title += util.GenerateShortUID()
|
||
if len(title) > MaxFolderName {
|
||
title = title[:MaxFolderName]
|
||
break
|
||
}
|
||
}
|
||
|
||
hash := util.GenerateShortUID()
|
||
f := &folder.Folder{
|
||
Title: title,
|
||
}
|
||
name := generateAlertFolderName(f, permissionHash(hash))
|
||
require.Len(t, name, MaxFolderName)
|
||
require.Contains(t, name, hash)
|
||
})
|
||
}
|
||
|
||
func mustRawMessage[T any](s T) apimodels.RawMessage {
|
||
js, _ := json.Marshal(s)
|
||
return js
|
||
}
|