From c266a4ac8143c343953b620eab7449b24a4a4240 Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Wed, 13 Apr 2022 19:31:57 -0500 Subject: [PATCH] Alerting: Remove mis-behaving fake and fix masked test failure in AM config API (#47747) * Remove misbehaving fake * Fix bug and inject logger --- pkg/services/ngalert/api/api_alertmanager.go | 6 +++-- .../ngalert/api/api_alertmanager_test.go | 13 ++++++---- pkg/services/ngalert/api/testing.go | 26 ------------------- 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/pkg/services/ngalert/api/api_alertmanager.go b/pkg/services/ngalert/api/api_alertmanager.go index 047a637c32c..c23cfc9985f 100644 --- a/pkg/services/ngalert/api/api_alertmanager.go +++ b/pkg/services/ngalert/api/api_alertmanager.go @@ -57,10 +57,12 @@ func (srv AlertmanagerSrv) loadSecureSettings(ctx context.Context, orgId int64, currentReceiverMap := make(map[string]*apimodels.PostableGrafanaReceiver) if query.Result != nil { 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 { - 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 diff --git a/pkg/services/ngalert/api/api_alertmanager_test.go b/pkg/services/ngalert/api/api_alertmanager_test.go index 13943adc9ed..75ee2176024 100644 --- a/pkg/services/ngalert/api/api_alertmanager_test.go +++ b/pkg/services/ngalert/api/api_alertmanager_test.go @@ -346,15 +346,18 @@ func createSut(t *testing.T, accessControl accesscontrol.AccessControl) Alertman t.Helper() mam := createMultiOrgAlertmanager(t) - store := newFakeAlertingStore(t) - store.Setup(1) - store.Setup(2) - store.Setup(3) + configs := map[int64]*ngmodels.AlertConfiguration{ + 1: {AlertmanagerConfiguration: validConfig, OrgID: 1}, + 2: {AlertmanagerConfiguration: validConfig, OrgID: 2}, + 3: {AlertmanagerConfiguration: brokenConfig, OrgID: 3}, + } + configStore := notifier.NewFakeConfigStore(t, configs) secrets := fakes.NewFakeSecretsService() if accessControl == nil { 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 { diff --git a/pkg/services/ngalert/api/testing.go b/pkg/services/ngalert/api/testing.go index 26344d66b64..68504135781 100644 --- a/pkg/services/ngalert/api/testing.go +++ b/pkg/services/ngalert/api/testing.go @@ -1,43 +1,17 @@ package api import ( - "context" "fmt" "sync" "testing" "time" "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/store" "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 { mtx sync.Mutex // orgID -> RuleID -> States