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:
Alexander Weaver 2022-04-13 19:31:57 -05:00 committed by GitHub
parent 694a7e73dc
commit c266a4ac81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 33 deletions

View File

@ -57,11 +57,13 @@ 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()
}
}
// Copy the previously known secure settings
for i, r := range receivers {

View File

@ -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 {

View File

@ -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