mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* update AlertingEnabled and UnifiedAlertingSettings.Enabled to be pointers * add a pseudo migration to fix the AlertingEnabled and UnifiedAlertingSettings.Enabled if the latter is not defined * update the default configuration file to make default value for both 'enabled' flags be undefined Misc * update Migrator to expose DB engine. This is needed for a ualert migration to access the database while the list of migrations is created. * add more verbose failure when migrations do not match Co-authored-by: gotjosh <josue@grafana.com> Co-authored-by: Yuriy Tseretyan <yuriy.tseretyan@grafana.com> Co-authored-by: gillesdemey <gilles.de.mey@gmail.com>
187 lines
5.6 KiB
Go
187 lines
5.6 KiB
Go
package ualert
|
||
|
||
import (
|
||
"fmt"
|
||
"testing"
|
||
|
||
"xorm.io/xorm"
|
||
|
||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
||
"github.com/grafana/grafana/pkg/setting"
|
||
"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 not error",
|
||
receivers: []*PostableGrafanaReceiver{
|
||
{
|
||
UID: util.GenerateShortUID(),
|
||
Name: "SlackWithBadRecipient",
|
||
Type: "slack",
|
||
Settings: simplejson.NewFromAny(map[string]interface{}{"recipient": "this passes"}),
|
||
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
||
},
|
||
},
|
||
},
|
||
{
|
||
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 TestCheckUnifiedAlertingEnabledByDefault(t *testing.T) {
|
||
testDB := sqlutil.SQLite3TestDB()
|
||
x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
|
||
require.NoError(t, err)
|
||
_, err = x.Exec("CREATE TABLE alert ( id bigint )")
|
||
require.NoError(t, err)
|
||
t.Cleanup(func() {
|
||
_, err = x.Exec("DROP TABLE alert")
|
||
require.NoError(t, err)
|
||
})
|
||
|
||
tests := []struct {
|
||
title string
|
||
legacyAlertExists bool
|
||
legacyIsDefined bool
|
||
legacyValue bool
|
||
expectedUnifiedAlerting bool
|
||
}{
|
||
{
|
||
title: "enable unified alerting when there are no legacy alerts",
|
||
legacyIsDefined: false,
|
||
legacyAlertExists: false,
|
||
expectedUnifiedAlerting: true,
|
||
},
|
||
{
|
||
title: "enable unified alerting when there are no legacy alerts and legacy enabled",
|
||
legacyIsDefined: true,
|
||
legacyValue: true,
|
||
legacyAlertExists: false,
|
||
expectedUnifiedAlerting: true,
|
||
},
|
||
{
|
||
title: "enable unified alerting when there are no legacy alerts and legacy disabled",
|
||
legacyIsDefined: true,
|
||
legacyValue: false,
|
||
legacyAlertExists: false,
|
||
expectedUnifiedAlerting: true,
|
||
},
|
||
{
|
||
title: "enable unified alerting when there are legacy alerts but legacy disabled",
|
||
legacyIsDefined: true,
|
||
legacyValue: false,
|
||
legacyAlertExists: true,
|
||
expectedUnifiedAlerting: true,
|
||
},
|
||
{
|
||
title: "disable unified alerting when there are legacy alerts",
|
||
legacyIsDefined: false,
|
||
legacyAlertExists: true,
|
||
expectedUnifiedAlerting: false,
|
||
},
|
||
{
|
||
title: "disable unified alerting when there are legacy alerts and it is enabled",
|
||
legacyIsDefined: true,
|
||
legacyValue: true,
|
||
legacyAlertExists: true,
|
||
expectedUnifiedAlerting: false,
|
||
},
|
||
}
|
||
|
||
for _, test := range tests {
|
||
t.Run(test.title, func(t *testing.T) {
|
||
setting.AlertingEnabled = nil
|
||
if test.legacyIsDefined {
|
||
value := test.legacyValue
|
||
setting.AlertingEnabled = &value
|
||
}
|
||
|
||
if test.legacyAlertExists {
|
||
_, err := x.Exec("INSERT INTO alert VALUES (1)")
|
||
require.NoError(t, err)
|
||
} else {
|
||
_, err := x.Exec("DELETE FROM alert")
|
||
require.NoError(t, err)
|
||
}
|
||
|
||
cfg := setting.Cfg{
|
||
UnifiedAlerting: setting.UnifiedAlertingSettings{
|
||
Enabled: nil,
|
||
},
|
||
}
|
||
mg := migrator.NewMigrator(x, &cfg)
|
||
|
||
err := CheckUnifiedAlertingEnabledByDefault(mg)
|
||
require.NoError(t, err)
|
||
require.NotNil(t, setting.AlertingEnabled)
|
||
require.NotNil(t, cfg.UnifiedAlerting.Enabled)
|
||
require.Equal(t, *cfg.UnifiedAlerting.Enabled, test.expectedUnifiedAlerting)
|
||
require.Equal(t, *setting.AlertingEnabled, !test.expectedUnifiedAlerting)
|
||
})
|
||
}
|
||
}
|
||
|
||
func configFromReceivers(t *testing.T, receivers []*PostableGrafanaReceiver) *PostableUserConfig {
|
||
t.Helper()
|
||
|
||
return &PostableUserConfig{
|
||
AlertmanagerConfig: PostableApiAlertingConfig{
|
||
Receivers: []*PostableApiReceiver{
|
||
{GrafanaManagedReceivers: receivers},
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
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>"
|