2023-10-12 07:43:10 -05:00
|
|
|
package migration
|
2021-12-01 05:45:27 -06:00
|
|
|
|
|
|
|
import (
|
2023-10-12 07:43:10 -05:00
|
|
|
"context"
|
2023-10-12 17:12:40 -05:00
|
|
|
"fmt"
|
2022-09-13 13:53:09 -05:00
|
|
|
"strings"
|
2021-12-01 05:45:27 -06:00
|
|
|
"testing"
|
|
|
|
|
2023-05-24 12:09:17 -05:00
|
|
|
"github.com/google/uuid"
|
2023-12-19 12:25:13 -06:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
2021-12-01 05:45:27 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-30 02:55:35 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2023-10-12 07:43:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2023-05-24 12:09:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log/logtest"
|
2023-10-12 07:43:10 -05:00
|
|
|
legacymodels "github.com/grafana/grafana/pkg/services/alerting/models"
|
2024-01-05 04:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-05-24 12:09:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2023-10-12 07:43:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2021-12-01 05:45:27 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigrateAlertRuleQueries(t *testing.T) {
|
|
|
|
tc := []struct {
|
|
|
|
name string
|
|
|
|
input *simplejson.Json
|
|
|
|
expected string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "when a query has a sub query - it is extracted",
|
2023-08-30 10:46:47 -05:00
|
|
|
input: simplejson.NewFromAny(map[string]any{"targetFull": "thisisafullquery", "target": "ahalfquery"}),
|
2021-12-01 05:45:27 -06:00
|
|
|
expected: `{"target":"thisisafullquery"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when a query does not have a sub query - it no-ops",
|
2023-08-30 10:46:47 -05:00
|
|
|
input: simplejson.NewFromAny(map[string]any{"target": "ahalfquery"}),
|
2021-12-01 05:45:27 -06:00
|
|
|
expected: `{"target":"ahalfquery"}`,
|
|
|
|
},
|
2022-05-04 08:31:05 -05:00
|
|
|
{
|
|
|
|
name: "when query was hidden, it removes the flag",
|
2023-08-30 10:46:47 -05:00
|
|
|
input: simplejson.NewFromAny(map[string]any{"hide": true}),
|
2022-05-04 08:31:05 -05:00
|
|
|
expected: `{}`,
|
|
|
|
},
|
2023-06-28 13:02:57 -05:00
|
|
|
{
|
|
|
|
name: "when prometheus both type query, convert to range",
|
2023-08-30 10:46:47 -05:00
|
|
|
input: simplejson.NewFromAny(map[string]any{
|
2023-06-28 13:02:57 -05:00
|
|
|
"datasource": map[string]string{
|
|
|
|
"type": "prometheus",
|
|
|
|
},
|
|
|
|
"instant": true,
|
|
|
|
"range": true,
|
|
|
|
}),
|
|
|
|
expected: `{"datasource":{"type":"prometheus"},"instant":false,"range":true}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when prometheus instant type query, do nothing",
|
2023-08-30 10:46:47 -05:00
|
|
|
input: simplejson.NewFromAny(map[string]any{
|
2023-06-28 13:02:57 -05:00
|
|
|
"datasource": map[string]string{
|
|
|
|
"type": "prometheus",
|
|
|
|
},
|
|
|
|
"instant": true,
|
|
|
|
}),
|
|
|
|
expected: `{"datasource":{"type":"prometheus"},"instant":true}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when non-prometheus with instant and range, do nothing",
|
2023-08-30 10:46:47 -05:00
|
|
|
input: simplejson.NewFromAny(map[string]any{
|
2023-06-28 13:02:57 -05:00
|
|
|
"datasource": map[string]string{
|
|
|
|
"type": "something",
|
|
|
|
},
|
|
|
|
"instant": true,
|
|
|
|
"range": true,
|
|
|
|
}),
|
|
|
|
expected: `{"datasource":{"type":"something"},"instant":true,"range":true}`,
|
|
|
|
},
|
2021-12-01 05:45:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tc {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
model, err := tt.input.Encode()
|
|
|
|
require.NoError(t, err)
|
2023-10-12 07:43:10 -05:00
|
|
|
queries, err := migrateAlertRuleQueries(&logtest.Fake{}, []models.AlertQuery{{Model: model}})
|
2021-12-01 05:45:27 -06:00
|
|
|
if tt.err != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.EqualError(t, err, tt.err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
r, err := queries[0].Model.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, tt.expected, string(r))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 11:39:17 -05:00
|
|
|
|
|
|
|
func TestAddMigrationInfo(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
2023-12-19 12:25:13 -06:00
|
|
|
alert *legacymodels.Alert
|
2023-10-12 07:43:10 -05:00
|
|
|
dashboard string
|
2023-12-19 12:25:13 -06:00
|
|
|
expectedLabels data.Labels
|
|
|
|
expectedAnnotations data.Labels
|
2022-06-22 11:39:17 -05:00
|
|
|
}{
|
|
|
|
{
|
2023-12-19 12:25:13 -06:00
|
|
|
name: "when alert rule tags are a JSON array, they're ignored.",
|
|
|
|
alert: &legacymodels.Alert{ID: 43, PanelID: 42, Message: "message", Settings: simplejson.NewFromAny(map[string]any{
|
|
|
|
"alertRuleTags": []string{"one", "two", "three", "four"},
|
|
|
|
})},
|
2023-10-12 07:43:10 -05:00
|
|
|
dashboard: "dashboard",
|
2023-12-19 12:25:13 -06:00
|
|
|
expectedLabels: data.Labels{models.MigratedUseLegacyChannelsLabel: "true"},
|
|
|
|
expectedAnnotations: data.Labels{models.MigratedAlertIdAnnotation: "43", models.DashboardUIDAnnotation: "dashboard", models.PanelIDAnnotation: "42", "message": "message"},
|
2022-06-22 11:39:17 -05:00
|
|
|
},
|
|
|
|
{
|
2023-12-19 12:25:13 -06:00
|
|
|
name: "when alert rule tags are a JSON object",
|
|
|
|
alert: &legacymodels.Alert{ID: 43, PanelID: 42, Message: "message", Settings: simplejson.NewFromAny(map[string]any{
|
|
|
|
"alertRuleTags": map[string]any{"key": "value", "key2": "value2"},
|
|
|
|
})}, dashboard: "dashboard",
|
|
|
|
expectedLabels: data.Labels{models.MigratedUseLegacyChannelsLabel: "true", "key": "value", "key2": "value2"},
|
|
|
|
expectedAnnotations: data.Labels{models.MigratedAlertIdAnnotation: "43", models.DashboardUIDAnnotation: "dashboard", models.PanelIDAnnotation: "42", "message": "message"},
|
2022-06-22 11:39:17 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-01-05 04:37:13 -06:00
|
|
|
labels, annotations := addLabelsAndAnnotations(&logtest.Fake{}, tc.alert, tc.dashboard)
|
2022-06-22 11:39:17 -05:00
|
|
|
require.Equal(t, tc.expectedLabels, labels)
|
|
|
|
require.Equal(t, tc.expectedAnnotations, annotations)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 13:53:09 -05:00
|
|
|
|
|
|
|
func TestMakeAlertRule(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
sqlStore := db.InitTestDB(t)
|
2024-01-05 04:37:13 -06:00
|
|
|
dashboard := dashboards.Dashboard{ID: 1, UID: "dashboarduid", Title: "dashboardname"}
|
2022-09-13 13:53:09 -05:00
|
|
|
t.Run("when mapping rule names", func(t *testing.T) {
|
|
|
|
t.Run("leaves basic names untouched", func(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
2022-09-13 13:53:09 -05:00
|
|
|
da := createTestDashAlert()
|
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2022-09-13 13:53:09 -05:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, da.Name, ar.Title)
|
|
|
|
})
|
|
|
|
})
|
2023-02-02 15:49:05 -06:00
|
|
|
|
|
|
|
t.Run("alert is not paused", func(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
2023-02-02 15:49:05 -06:00
|
|
|
da := createTestDashAlert()
|
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2023-02-02 15:49:05 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, ar.IsPaused)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("paused dash alert is paused", func(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
2023-02-02 15:49:05 -06:00
|
|
|
da := createTestDashAlert()
|
|
|
|
da.State = "paused"
|
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2023-02-02 15:49:05 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ar.IsPaused)
|
|
|
|
})
|
2023-05-24 12:09:17 -05:00
|
|
|
|
|
|
|
t.Run("use default if execution of NoData is not known", func(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
2023-05-24 12:09:17 -05:00
|
|
|
da := createTestDashAlert()
|
2023-12-19 12:25:13 -06:00
|
|
|
da.Settings.Set("noDataState", uuid.NewString())
|
2023-05-24 12:09:17 -05:00
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2023-05-24 12:09:17 -05:00
|
|
|
require.Nil(t, err)
|
2023-10-12 07:43:10 -05:00
|
|
|
require.Equal(t, models.NoData, ar.NoDataState)
|
2023-05-24 12:09:17 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("use default if execution of Error is not known", func(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
2023-05-24 12:09:17 -05:00
|
|
|
da := createTestDashAlert()
|
2023-12-19 12:25:13 -06:00
|
|
|
da.Settings.Set("executionErrorState", uuid.NewString())
|
2023-05-24 12:09:17 -05:00
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2023-05-24 12:09:17 -05:00
|
|
|
require.Nil(t, err)
|
2023-10-12 07:43:10 -05:00
|
|
|
require.Equal(t, models.ErrorErrState, ar.ExecErrState)
|
2023-05-24 12:09:17 -05:00
|
|
|
})
|
2023-10-02 10:25:33 -05:00
|
|
|
|
|
|
|
t.Run("migrate message template", func(t *testing.T) {
|
2023-10-12 07:43:10 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
2023-10-02 10:25:33 -05:00
|
|
|
da := createTestDashAlert()
|
|
|
|
da.Message = "Instance ${instance} is down"
|
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2023-10-02 10:25:33 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
expected :=
|
|
|
|
"{{- $mergedLabels := mergeLabelValues $values -}}\n" +
|
|
|
|
"Instance {{$mergedLabels.instance}} is down"
|
|
|
|
require.Equal(t, expected, ar.Annotations["message"])
|
|
|
|
})
|
2023-10-12 17:12:40 -05:00
|
|
|
|
2023-11-29 09:05:00 -06:00
|
|
|
t.Run("create unique group from dashboard title and humanized interval", func(t *testing.T) {
|
2023-10-12 17:12:40 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
|
|
|
da := createTestDashAlert()
|
|
|
|
da.PanelID = 42
|
|
|
|
|
2023-11-29 09:05:00 -06:00
|
|
|
intervalTests := []struct {
|
|
|
|
interval int64
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{interval: 10, expected: "10s"},
|
|
|
|
{interval: 30, expected: "30s"},
|
|
|
|
{interval: 60, expected: "1m"},
|
|
|
|
{interval: 120, expected: "2m"},
|
|
|
|
{interval: 3600, expected: "1h"},
|
|
|
|
{interval: 7200, expected: "2h"},
|
|
|
|
{interval: 86400, expected: "1d"},
|
|
|
|
{interval: 172800, expected: "2d"},
|
|
|
|
{interval: 604800, expected: "1w"},
|
|
|
|
{interval: 1209600, expected: "2w"},
|
|
|
|
{interval: 31536000, expected: "1y"},
|
|
|
|
{interval: 63072000, expected: "2y"},
|
|
|
|
{interval: 60 + 30, expected: "1m30s"},
|
|
|
|
{interval: 3600 + 10, expected: "1h10s"},
|
|
|
|
{interval: 3600 + 60, expected: "1h1m"},
|
|
|
|
{interval: 3600 + 60 + 10, expected: "1h1m10s"},
|
|
|
|
{interval: 86400 + 10, expected: "1d10s"},
|
|
|
|
{interval: 86400 + 60, expected: "1d1m"},
|
|
|
|
{interval: 86400 + 3600, expected: "1d1h"},
|
|
|
|
{interval: 86400 + 3600 + 60, expected: "1d1h1m"},
|
|
|
|
{interval: 86400 + 3600 + 10, expected: "1d1h10s"},
|
|
|
|
{interval: 86400 + 60 + 10, expected: "1d1m10s"},
|
|
|
|
{interval: 86400 + 3600 + 60 + 10, expected: "1d1h1m10s"},
|
|
|
|
{interval: 604800 + 86400 + 3600 + 60 + 10, expected: "8d1h1m10s"},
|
|
|
|
{interval: 31536000 + 604800 + 86400 + 3600 + 60 + 10, expected: "373d1h1m10s"},
|
2023-10-12 17:12:40 -05:00
|
|
|
}
|
|
|
|
|
2023-11-29 09:05:00 -06:00
|
|
|
for _, test := range intervalTests {
|
|
|
|
t.Run(fmt.Sprintf("interval %ds should be %s", test.interval, test.expected), func(t *testing.T) {
|
|
|
|
da.Frequency = test.interval
|
2023-10-12 17:12:40 -05:00
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
2023-11-29 09:05:00 -06:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
2024-01-05 04:37:13 -06:00
|
|
|
require.Equal(t, fmt.Sprintf("%s - %s", dashboard.Title, test.expected), ar.RuleGroup)
|
2023-11-29 09:05:00 -06:00
|
|
|
})
|
|
|
|
}
|
2023-10-12 17:12:40 -05:00
|
|
|
})
|
|
|
|
|
2023-11-29 09:05:00 -06:00
|
|
|
t.Run("truncate dashboard name part of rule group if too long", func(t *testing.T) {
|
2023-10-12 17:12:40 -05:00
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
|
|
|
da := createTestDashAlert()
|
2024-01-05 04:37:13 -06:00
|
|
|
longNamedDashboard := dashboards.Dashboard{UID: "dashboarduid", Title: strings.Repeat("a", store.AlertRuleMaxRuleGroupNameLength-1)}
|
2023-10-12 17:12:40 -05:00
|
|
|
|
2024-01-05 04:37:13 -06:00
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &longNamedDashboard)
|
2023-10-12 17:12:40 -05:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, ar.RuleGroup, store.AlertRuleMaxRuleGroupNameLength)
|
2023-11-29 09:05:00 -06:00
|
|
|
suffix := fmt.Sprintf(" - %ds", ar.IntervalSeconds)
|
|
|
|
require.Equal(t, fmt.Sprintf("%s%s", strings.Repeat("a", store.AlertRuleMaxRuleGroupNameLength-len(suffix)), suffix), ar.RuleGroup)
|
2023-10-12 17:12:40 -05:00
|
|
|
})
|
2024-01-24 14:56:19 -06:00
|
|
|
|
|
|
|
t.Run("keep last state error dash alert is silenced", func(t *testing.T) {
|
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
|
|
|
da := createTestDashAlert()
|
|
|
|
da.Settings.Set("executionErrorState", "keep_state")
|
|
|
|
|
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, ar.Labels[models.MigratedSilenceLabelErrorKeepState], "true")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("keep last state nodata dash alert is silenced", func(t *testing.T) {
|
|
|
|
service := NewTestMigrationService(t, sqlStore, nil)
|
|
|
|
m := service.newOrgMigration(1)
|
|
|
|
da := createTestDashAlert()
|
|
|
|
da.Settings.Set("noDataState", "keep_state")
|
|
|
|
|
|
|
|
ar, err := m.migrateAlert(context.Background(), &logtest.Fake{}, da, &dashboard)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, ar.Labels[models.MigratedSilenceLabelNodataKeepState], "true")
|
|
|
|
})
|
2022-09-13 13:53:09 -05:00
|
|
|
}
|
|
|
|
|
2023-12-19 12:25:13 -06:00
|
|
|
func createTestDashAlert() *legacymodels.Alert {
|
|
|
|
return &legacymodels.Alert{
|
2024-01-05 04:37:13 -06:00
|
|
|
OrgID: 1,
|
2023-12-19 12:25:13 -06:00
|
|
|
ID: 1,
|
|
|
|
Name: "test",
|
|
|
|
Settings: simplejson.New(),
|
2022-09-13 13:53:09 -05:00
|
|
|
}
|
|
|
|
}
|