2021-03-08 14:19:21 -06:00
|
|
|
package tests
|
2020-12-17 08:00:09 -06:00
|
|
|
|
|
|
|
import (
|
2022-02-08 02:52:03 -06:00
|
|
|
"context"
|
2020-12-17 08:00:09 -06:00
|
|
|
"encoding/json"
|
2022-08-26 08:46:41 -05:00
|
|
|
"errors"
|
2020-12-17 08:00:09 -06:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-14 14:33:06 -05:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-26 08:46:41 -05:00
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2022-10-18 08:31:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2022-11-10 03:41:03 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/appcontext"
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2024-03-22 17:37:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/kvstore"
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-10-18 08:31:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2022-03-10 05:58:18 -06:00
|
|
|
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
2022-09-19 02:54:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
|
2022-05-17 13:52:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-03-10 05:58:18 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-11-10 03:41:03 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2022-10-10 14:47:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder/folderimpl"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert"
|
2021-05-13 13:01:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
|
2021-03-08 14:19:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2023-06-02 09:38:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/testutil"
|
2022-08-26 08:46:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2023-09-11 06:59:24 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
2022-11-14 13:08:10 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
2021-11-09 09:42:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets/database"
|
2021-11-04 11:47:21 -05:00
|
|
|
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
2022-08-26 08:46:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2020-12-17 08:00:09 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-11-09 09:42:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2020-12-17 08:00:09 -06:00
|
|
|
)
|
|
|
|
|
2021-05-13 09:05:33 -05:00
|
|
|
// SetupTestEnv initializes a store to used by the tests.
|
2022-10-06 01:22:58 -05:00
|
|
|
func SetupTestEnv(tb testing.TB, baseInterval time.Duration) (*ngalert.AlertNG, *store.DBstore) {
|
|
|
|
tb.Helper()
|
2021-08-25 08:11:22 -05:00
|
|
|
|
2020-12-17 08:00:09 -06:00
|
|
|
cfg := setting.NewCfg()
|
2022-02-11 15:13:49 -06:00
|
|
|
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{
|
|
|
|
BaseInterval: setting.SchedulerBaseInterval,
|
|
|
|
}
|
2021-09-29 09:16:40 -05:00
|
|
|
// AlertNG database migrations run and the relative database tables are created only when it's enabled
|
2021-11-24 13:56:07 -06:00
|
|
|
cfg.UnifiedAlerting.Enabled = new(bool)
|
|
|
|
*cfg.UnifiedAlerting.Enabled = true
|
2020-12-17 08:00:09 -06:00
|
|
|
|
2021-09-14 06:55:01 -05:00
|
|
|
m := metrics.NewNGAlert(prometheus.NewRegistry())
|
2022-10-19 08:02:15 -05:00
|
|
|
sqlStore := db.InitTestDB(tb)
|
2022-10-06 01:22:58 -05:00
|
|
|
secretsService := secretsManager.SetupTestService(tb, database.ProvideSecretsStore(sqlStore))
|
2022-03-10 05:58:18 -06:00
|
|
|
|
|
|
|
ac := acmock.New()
|
2022-06-17 12:10:49 -05:00
|
|
|
|
2023-01-06 20:21:43 -06:00
|
|
|
tracer := tracing.InitializeTracerForTest()
|
|
|
|
bus := bus.ProvideBus(tracer)
|
2023-04-13 06:55:42 -05:00
|
|
|
folderStore := folderimpl.ProvideDashboardFolderStore(sqlStore)
|
2023-06-02 09:38:02 -05:00
|
|
|
dashboardService, dashboardStore := testutil.SetupDashboardService(tb, sqlStore, folderStore, cfg)
|
2024-02-06 16:12:13 -06:00
|
|
|
features := featuremgmt.WithFeatures()
|
|
|
|
folderService := testutil.SetupFolderService(tb, cfg, sqlStore, dashboardStore, folderStore, bus, features, ac)
|
2023-12-04 03:34:38 -06:00
|
|
|
ruleStore, err := store.ProvideDBStore(cfg, featuremgmt.WithFeatures(), sqlStore, folderService, &dashboards.FakeDashboardService{}, ac)
|
2023-06-02 09:38:02 -05:00
|
|
|
require.NoError(tb, err)
|
2021-10-07 09:33:50 -05:00
|
|
|
ng, err := ngalert.ProvideService(
|
2024-03-22 17:37:33 -05:00
|
|
|
cfg, features, nil, nil, routing.NewRouteRegister(), sqlStore, kvstore.NewFakeKVStore(), nil, nil, quotatest.New(false, nil),
|
2023-06-02 09:38:02 -05:00
|
|
|
secretsService, nil, m, folderService, ac, &dashboards.FakeDashboardService{}, nil, bus, ac,
|
2024-03-14 09:36:35 -05:00
|
|
|
annotationstest.NewFakeAnnotationsRepo(), &pluginstore.FakePluginStore{}, tracer, ruleStore,
|
2021-10-07 09:33:50 -05:00
|
|
|
)
|
2022-10-06 01:22:58 -05:00
|
|
|
require.NoError(tb, err)
|
2021-08-25 08:11:22 -05:00
|
|
|
return ng, &store.DBstore{
|
2024-02-06 16:12:13 -06:00
|
|
|
FeatureToggles: features,
|
2022-10-06 01:22:58 -05:00
|
|
|
SQLStore: ng.SQLStore,
|
2022-07-15 13:13:30 -05:00
|
|
|
Cfg: setting.UnifiedAlertingSettings{
|
|
|
|
BaseInterval: baseInterval * time.Second,
|
|
|
|
},
|
2022-05-24 08:24:55 -05:00
|
|
|
Logger: log.New("ngalert-test"),
|
|
|
|
DashboardService: dashboardService,
|
2022-08-26 08:46:41 -05:00
|
|
|
FolderService: folderService,
|
2021-05-20 07:49:33 -05:00
|
|
|
}
|
2020-12-17 08:00:09 -06:00
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
// CreateTestAlertRule creates a dummy alert definition to be used by the tests.
|
2022-10-06 01:22:58 -05:00
|
|
|
func CreateTestAlertRule(t testing.TB, ctx context.Context, dbstore *store.DBstore, intervalSeconds int64, orgID int64) *models.AlertRule {
|
2022-02-24 04:58:54 -06:00
|
|
|
return CreateTestAlertRuleWithLabels(t, ctx, dbstore, intervalSeconds, orgID, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-06 01:22:58 -05:00
|
|
|
func CreateTestAlertRuleWithLabels(t testing.TB, ctx context.Context, dbstore *store.DBstore, intervalSeconds int64, orgID int64, labels map[string]string) *models.AlertRule {
|
2021-11-09 09:42:58 -06:00
|
|
|
ruleGroup := fmt.Sprintf("ruleGroup-%s", util.GenerateShortUID())
|
2022-08-26 08:46:41 -05:00
|
|
|
folderUID := "namespace"
|
|
|
|
user := &user.SignedInUser{
|
|
|
|
UserID: 1,
|
|
|
|
OrgID: orgID,
|
|
|
|
OrgRole: org.RoleAdmin,
|
|
|
|
IsGrafanaAdmin: true,
|
|
|
|
}
|
2022-11-10 03:41:03 -06:00
|
|
|
|
|
|
|
ctx = appcontext.WithUser(ctx, user)
|
2022-11-23 03:13:47 -06:00
|
|
|
_, err := dbstore.FolderService.Create(ctx, &folder.CreateFolderCommand{OrgID: orgID, Title: "FOLDER-" + util.GenerateShortUID(), UID: folderUID, SignedInUser: user})
|
2022-11-11 07:28:24 -06:00
|
|
|
// var foldr *folder.Folder
|
|
|
|
if errors.Is(err, dashboards.ErrFolderWithSameUIDExists) || errors.Is(err, dashboards.ErrFolderVersionMismatch) {
|
2022-11-23 03:13:47 -06:00
|
|
|
_, err = dbstore.FolderService.Get(ctx, &folder.GetFolderQuery{OrgID: orgID, UID: &folderUID, SignedInUser: user})
|
2022-08-26 08:46:41 -05:00
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = dbstore.InsertAlertRules(ctx, []models.AlertRule{
|
2022-02-23 10:30:04 -06:00
|
|
|
{
|
2022-04-14 07:21:36 -05:00
|
|
|
|
|
|
|
ID: 0,
|
|
|
|
OrgID: orgID,
|
|
|
|
Title: fmt.Sprintf("an alert definition %s", util.GenerateShortUID()),
|
|
|
|
Condition: "A",
|
|
|
|
Data: []models.AlertQuery{
|
|
|
|
{
|
|
|
|
Model: json.RawMessage(`{
|
2023-01-31 11:50:10 -06:00
|
|
|
"datasourceUid": "__expr__",
|
2021-04-03 12:13:29 -05:00
|
|
|
"type":"math",
|
|
|
|
"expression":"2 + 2 > 1"
|
|
|
|
}`),
|
2022-04-14 07:21:36 -05:00
|
|
|
RelativeTimeRange: models.RelativeTimeRange{
|
|
|
|
From: models.Duration(5 * time.Hour),
|
|
|
|
To: models.Duration(3 * time.Hour),
|
2021-04-03 12:13:29 -05:00
|
|
|
},
|
2022-04-14 07:21:36 -05:00
|
|
|
RefID: "A",
|
2021-04-03 12:13:29 -05:00
|
|
|
},
|
|
|
|
},
|
2022-04-14 07:21:36 -05:00
|
|
|
Labels: labels,
|
|
|
|
Annotations: map[string]string{"testAnnoKey": "testAnnoValue"},
|
|
|
|
IntervalSeconds: intervalSeconds,
|
2022-11-11 07:28:24 -06:00
|
|
|
NamespaceUID: folderUID,
|
2022-04-14 07:21:36 -05:00
|
|
|
RuleGroup: ruleGroup,
|
|
|
|
NoDataState: models.NoData,
|
|
|
|
ExecErrState: models.AlertingErrState,
|
2021-04-03 12:13:29 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 05:42:42 -05:00
|
|
|
q := models.ListAlertRulesQuery{
|
|
|
|
OrgID: orgID,
|
2022-11-11 07:28:24 -06:00
|
|
|
NamespaceUIDs: []string{folderUID},
|
2022-04-25 05:42:42 -05:00
|
|
|
RuleGroup: ruleGroup,
|
2021-04-03 12:13:29 -05:00
|
|
|
}
|
2023-03-28 03:34:35 -05:00
|
|
|
ruleList, err := dbstore.ListAlertRules(ctx, &q)
|
2021-04-03 12:13:29 -05:00
|
|
|
require.NoError(t, err)
|
2023-03-28 03:34:35 -05:00
|
|
|
require.NotEmpty(t, ruleList)
|
2021-04-03 12:13:29 -05:00
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
rule := ruleList[0]
|
2022-11-11 07:28:24 -06:00
|
|
|
t.Logf("alert definition: %v with title: %q interval: %d folder: %s created", rule.GetKey(), rule.Title, rule.IntervalSeconds, folderUID)
|
2021-04-03 12:13:29 -05:00
|
|
|
return rule
|
|
|
|
}
|