Alerting: remove UpdateRuleGroup from fake rule store (#46941)

* remove UpdateRuleGroup from fake rule store because It is not part of interface anymore
This commit is contained in:
Yuriy Tseretyan
2022-03-24 19:29:19 -04:00
committed by GitHub
parent 15e4556c2f
commit 6610adf090
2 changed files with 26 additions and 95 deletions

View File

@@ -17,13 +17,11 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/expr"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
"github.com/grafana/grafana/pkg/services/ngalert/models"
@@ -1084,49 +1082,37 @@ func CreateTestAlertRule(t *testing.T, dbstore *store.FakeRuleStore, intervalSec
require.Fail(t, "Alert rule with desired evaluation result NoData is not supported yet")
}
err := dbstore.UpdateRuleGroup(ctx, store.UpdateRuleGroupCmd{
OrgID: orgID,
NamespaceUID: "namespace",
RuleGroupConfig: apimodels.PostableRuleGroupConfig{
Name: ruleGroup,
Interval: model.Duration(time.Duration(intervalSeconds) * time.Second),
Rules: []apimodels.PostableExtendedRuleNode{
{
ApiRuleNode: &apimodels.ApiRuleNode{
Annotations: map[string]string{"testAnnoKey": "testAnnoValue"},
For: model.Duration(forDuration),
},
GrafanaManagedAlert: &apimodels.PostableGrafanaRule{
Title: fmt.Sprintf("an alert definition %d", d),
Condition: "A",
Data: []models.AlertQuery{
{
DatasourceUID: "-100",
Model: json.RawMessage(expression),
RelativeTimeRange: models.RelativeTimeRange{
From: models.Duration(5 * time.Hour),
To: models.Duration(3 * time.Hour),
},
RefID: "A",
},
},
},
rule := &models.AlertRule{
ID: 1,
OrgID: orgID,
Title: fmt.Sprintf("an alert definition %d", d),
Condition: "A",
Data: []models.AlertQuery{
{
DatasourceUID: "-100",
Model: json.RawMessage(expression),
RelativeTimeRange: models.RelativeTimeRange{
From: models.Duration(5 * time.Hour),
To: models.Duration(3 * time.Hour),
},
RefID: "A",
},
},
})
require.NoError(t, err)
q := models.GetAlertRulesQuery{
OrgID: orgID,
NamespaceUID: "namespace",
RuleGroup: &ruleGroup,
Updated: time.Now(),
IntervalSeconds: intervalSeconds,
Version: 1,
UID: util.GenerateShortUID(),
NamespaceUID: "namespace",
RuleGroup: ruleGroup,
NoDataState: models.NoData,
ExecErrState: models.AlertingErrState,
For: forDuration,
Annotations: map[string]string{"testAnnoKey": "testAnnoValue"},
Labels: nil,
}
err = dbstore.GetAlertRules(ctx, &q)
require.NoError(t, err)
require.NotEmpty(t, q.Result)
rule := q.Result[0]
dbstore.PutRule(ctx, rule)
t.Logf("alert definition: %v with interval: %d created", rule.GetKey(), rule.IntervalSeconds)
return rule
}

View File

@@ -8,13 +8,11 @@ import (
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/grafana/grafana/pkg/services/annotations"
models2 "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/util"
amv2 "github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/common/model"
@@ -223,59 +221,6 @@ func (f *FakeRuleStore) UpsertAlertRules(_ context.Context, q []UpsertRule) erro
return nil
}
func (f *FakeRuleStore) UpdateRuleGroup(_ context.Context, cmd UpdateRuleGroupCmd) error {
f.mtx.Lock()
defer f.mtx.Unlock()
f.RecordedOps = append(f.RecordedOps, cmd)
if err := f.Hook(cmd); err != nil {
return err
}
existingRules := f.Rules[cmd.OrgID]
for _, r := range cmd.RuleGroupConfig.Rules {
// TODO: Not sure why this is not being set properly, where is the code that sets this?
for i := range r.GrafanaManagedAlert.Data {
r.GrafanaManagedAlert.Data[i].DatasourceUID = "-100"
}
newRule := &models.AlertRule{
OrgID: cmd.OrgID,
Title: r.GrafanaManagedAlert.Title,
Condition: r.GrafanaManagedAlert.Condition,
Data: r.GrafanaManagedAlert.Data,
UID: util.GenerateShortUID(),
IntervalSeconds: int64(time.Duration(cmd.RuleGroupConfig.Interval).Seconds()),
NamespaceUID: cmd.NamespaceUID,
RuleGroup: cmd.RuleGroupConfig.Name,
NoDataState: models.NoDataState(r.GrafanaManagedAlert.NoDataState),
ExecErrState: models.ExecutionErrorState(r.GrafanaManagedAlert.ExecErrState),
Version: 1,
}
if r.ApiRuleNode != nil {
newRule.For = time.Duration(r.ApiRuleNode.For)
newRule.Annotations = r.ApiRuleNode.Annotations
newRule.Labels = r.ApiRuleNode.Labels
}
if newRule.NoDataState == "" {
newRule.NoDataState = models.NoData
}
if newRule.ExecErrState == "" {
newRule.ExecErrState = models.AlertingErrState
}
err := newRule.PreSave(time.Now)
require.NoError(f.t, err)
existingRules = append(existingRules, newRule)
}
f.Rules[cmd.OrgID] = existingRules
return nil
}
func (f *FakeRuleStore) InTransaction(ctx context.Context, fn func(c context.Context) error) error {
return fn(ctx)
}