2021-08-06 07:06:56 -05:00
|
|
|
package schedule
|
|
|
|
|
|
|
|
import (
|
2022-09-27 13:48:12 -05:00
|
|
|
"context"
|
2021-08-06 07:06:56 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2022-09-27 13:48:12 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2021-08-06 07:06:56 -05:00
|
|
|
)
|
|
|
|
|
2021-10-26 12:22:07 -05:00
|
|
|
// waitForTimeChannel blocks the execution until either the channel ch has some data or a timeout of 10 second expires.
|
|
|
|
// Timeout will cause the test to fail.
|
|
|
|
// Returns the data from the channel.
|
|
|
|
func waitForTimeChannel(t *testing.T, ch chan time.Time) time.Time {
|
|
|
|
select {
|
|
|
|
case result := <-ch:
|
|
|
|
return result
|
|
|
|
case <-time.After(time.Duration(10) * time.Second):
|
|
|
|
t.Fatalf("Timeout waiting for data in the time channel")
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitForErrChannel blocks the execution until either the channel ch has some data or a timeout of 10 second expires.
|
|
|
|
// Timeout will cause the test to fail.
|
|
|
|
// Returns the data from the channel.
|
|
|
|
func waitForErrChannel(t *testing.T, ch chan error) error {
|
|
|
|
timeout := time.Duration(10) * time.Second
|
|
|
|
select {
|
|
|
|
case result := <-ch:
|
|
|
|
return result
|
|
|
|
case <-time.After(timeout):
|
|
|
|
t.Fatal("Timeout waiting for data in the error channel")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 13:48:12 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 14:08:57 -06:00
|
|
|
func (f *fakeRulesStore) DeleteRule(rules ...*models.AlertRule) {
|
|
|
|
for _, r := range rules {
|
|
|
|
delete(f.rules, r.UID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 13:48:12 -05:00
|
|
|
func (f *fakeRulesStore) getNamespaceTitle(uid string) string {
|
|
|
|
return "TEST-FOLDER-" + uid
|
|
|
|
}
|