2022-08-01 18:28:38 -05:00
|
|
|
package ngalert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-10-18 08:31:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2022-08-01 18:28:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/events"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-10-18 08:31:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2022-08-01 18:28:38 -05:00
|
|
|
models2 "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/schedule"
|
2022-09-30 14:36:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/tests/fakes"
|
2022-08-01 18:28:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_subscribeToFolderChanges(t *testing.T) {
|
|
|
|
orgID := rand.Int63()
|
|
|
|
folder := &models2.Folder{
|
|
|
|
Id: 0,
|
|
|
|
Uid: util.GenerateShortUID(),
|
|
|
|
Title: "Folder" + util.GenerateShortUID(),
|
|
|
|
}
|
|
|
|
rules := models.GenerateAlertRules(5, models.AlertRuleGen(models.WithOrgID(orgID), models.WithNamespace(folder)))
|
|
|
|
|
2022-10-18 08:31:56 -05:00
|
|
|
bus := bus.ProvideBus(tracing.InitializeTracerForTest())
|
2022-09-30 14:36:51 -05:00
|
|
|
db := fakes.NewRuleStore(t)
|
2022-08-01 18:28:38 -05:00
|
|
|
db.Folders[orgID] = append(db.Folders[orgID], folder)
|
|
|
|
db.PutRule(context.Background(), rules...)
|
|
|
|
|
|
|
|
scheduler := &schedule.FakeScheduleService{}
|
2022-08-24 14:33:33 -05:00
|
|
|
scheduler.On("UpdateAlertRule", mock.Anything, mock.Anything).Return()
|
2022-08-01 18:28:38 -05:00
|
|
|
|
|
|
|
subscribeToFolderChanges(log.New("test"), bus, db, scheduler)
|
|
|
|
|
|
|
|
err := bus.Publish(context.Background(), &events.FolderTitleUpdated{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Title: "Folder" + util.GenerateShortUID(),
|
|
|
|
ID: folder.Id,
|
|
|
|
UID: folder.Uid,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Eventuallyf(t, func() bool {
|
|
|
|
return len(db.GetRecordedCommands(func(cmd interface{}) (interface{}, bool) {
|
2022-09-30 14:36:51 -05:00
|
|
|
c, ok := cmd.(fakes.GenericRecordedQuery)
|
2022-08-01 18:28:38 -05:00
|
|
|
if !ok || c.Name != "IncreaseVersionForAllRulesInNamespace" {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return c, true
|
|
|
|
})) > 0
|
|
|
|
}, time.Second, 10*time.Millisecond, "expected to call db store method but nothing was called")
|
|
|
|
|
|
|
|
var calledTimes int
|
|
|
|
require.Eventuallyf(t, func() bool {
|
|
|
|
for _, call := range scheduler.Calls {
|
|
|
|
if call.Method == "UpdateAlertRule" {
|
|
|
|
calledTimes++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return calledTimes == len(rules)
|
|
|
|
}, time.Second, 10*time.Millisecond, "scheduler was expected to be called %d times but called %d", len(rules), calledTimes)
|
|
|
|
|
|
|
|
for _, rule := range rules {
|
|
|
|
scheduler.AssertCalled(t, "UpdateAlertRule", rule.GetKey(), rule.Version)
|
|
|
|
}
|
|
|
|
}
|