Alerting: Decouple schedule package from store (#55858)

* Separate out fake for scheduler tests

* Delete extracted methods from older fake
This commit is contained in:
Alexander Weaver
2022-09-27 13:48:12 -05:00
committed by GitHub
parent 664aa795c1
commit e6f99fc418
3 changed files with 53 additions and 56 deletions
+43
View File
@@ -1,8 +1,11 @@
package schedule
import (
"context"
"testing"
"time"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
// waitForTimeChannel blocks the execution until either the channel ch has some data or a timeout of 10 second expires.
@@ -31,3 +34,43 @@ func waitForErrChannel(t *testing.T, ch chan error) error {
return nil
}
}
type fakeRulesStore struct {
rules map[string]*models.AlertRule
}
func newFakeRulesStore() *fakeRulesStore {
return &fakeRulesStore{
rules: map[string]*models.AlertRule{},
}
}
func (f *fakeRulesStore) GetAlertRulesKeysForScheduling(ctx context.Context) ([]models.AlertRuleKeyWithVersion, error) {
result := make([]models.AlertRuleKeyWithVersion, 0, len(f.rules))
for _, rule := range f.rules {
result = append(result, models.AlertRuleKeyWithVersion{
Version: rule.Version,
AlertRuleKey: rule.GetKey(),
})
}
return result, nil
}
func (f *fakeRulesStore) GetAlertRulesForScheduling(ctx context.Context, query *models.GetAlertRulesForSchedulingQuery) error {
query.ResultFoldersTitles = map[string]string{}
for _, rule := range f.rules {
query.ResultRules = append(query.ResultRules, rule)
query.ResultFoldersTitles[rule.NamespaceUID] = f.getNamespaceTitle(rule.NamespaceUID)
}
return nil
}
func (f *fakeRulesStore) PutRule(_ context.Context, rules ...*models.AlertRule) {
for _, r := range rules {
f.rules[r.UID] = r
}
}
func (f *fakeRulesStore) getNamespaceTitle(uid string) string {
return "TEST-FOLDER-" + uid
}