mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Remove mis-behaving fake and fix masked test failure in AM config API (#47747)
* Remove misbehaving fake * Fix bug and inject logger
This commit is contained in:
parent
694a7e73dc
commit
c266a4ac81
@ -57,11 +57,13 @@ func (srv AlertmanagerSrv) loadSecureSettings(ctx context.Context, orgId int64,
|
|||||||
currentReceiverMap := make(map[string]*apimodels.PostableGrafanaReceiver)
|
currentReceiverMap := make(map[string]*apimodels.PostableGrafanaReceiver)
|
||||||
if query.Result != nil {
|
if query.Result != nil {
|
||||||
currentConfig, err := notifier.Load([]byte(query.Result.AlertmanagerConfiguration))
|
currentConfig, err := notifier.Load([]byte(query.Result.AlertmanagerConfiguration))
|
||||||
|
// If the current config is un-loadable, treat it as if it never existed. Providing a new, valid config should be able to "fix" this state.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load latest configuration: %w", err)
|
srv.log.Warn("Last known alertmanager configuration was invalid. Overwriting...")
|
||||||
}
|
} else {
|
||||||
currentReceiverMap = currentConfig.GetGrafanaReceiverMap()
|
currentReceiverMap = currentConfig.GetGrafanaReceiverMap()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Copy the previously known secure settings
|
// Copy the previously known secure settings
|
||||||
for i, r := range receivers {
|
for i, r := range receivers {
|
||||||
|
@ -346,15 +346,18 @@ func createSut(t *testing.T, accessControl accesscontrol.AccessControl) Alertman
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
mam := createMultiOrgAlertmanager(t)
|
mam := createMultiOrgAlertmanager(t)
|
||||||
store := newFakeAlertingStore(t)
|
configs := map[int64]*ngmodels.AlertConfiguration{
|
||||||
store.Setup(1)
|
1: {AlertmanagerConfiguration: validConfig, OrgID: 1},
|
||||||
store.Setup(2)
|
2: {AlertmanagerConfiguration: validConfig, OrgID: 2},
|
||||||
store.Setup(3)
|
3: {AlertmanagerConfiguration: brokenConfig, OrgID: 3},
|
||||||
|
}
|
||||||
|
configStore := notifier.NewFakeConfigStore(t, configs)
|
||||||
secrets := fakes.NewFakeSecretsService()
|
secrets := fakes.NewFakeSecretsService()
|
||||||
if accessControl == nil {
|
if accessControl == nil {
|
||||||
accessControl = acMock.New().WithDisabled()
|
accessControl = acMock.New().WithDisabled()
|
||||||
}
|
}
|
||||||
return AlertmanagerSrv{mam: mam, store: store, secrets: secrets, ac: accessControl}
|
log := log.NewNopLogger()
|
||||||
|
return AlertmanagerSrv{mam: mam, store: &configStore, secrets: secrets, ac: accessControl, log: log}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAmConfigRequest(t *testing.T) apimodels.PostableUserConfig {
|
func createAmConfigRequest(t *testing.T) apimodels.PostableUserConfig {
|
||||||
|
@ -1,43 +1,17 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeAlertingStore struct {
|
|
||||||
orgsWithConfig map[int64]bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func newFakeAlertingStore(t *testing.T) FakeAlertingStore {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
return FakeAlertingStore{
|
|
||||||
orgsWithConfig: map[int64]bool{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FakeAlertingStore) Setup(orgID int64) {
|
|
||||||
f.orgsWithConfig[orgID] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FakeAlertingStore) GetLatestAlertmanagerConfiguration(_ context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error {
|
|
||||||
if _, ok := f.orgsWithConfig[query.OrgID]; ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return store.ErrNoAlertmanagerConfiguration
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeAlertInstanceManager struct {
|
type fakeAlertInstanceManager struct {
|
||||||
mtx sync.Mutex
|
mtx sync.Mutex
|
||||||
// orgID -> RuleID -> States
|
// orgID -> RuleID -> States
|
||||||
|
Loading…
Reference in New Issue
Block a user