mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 11:44:26 -06:00
165 lines
3.8 KiB
Go
165 lines
3.8 KiB
Go
package notifier
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/kvstore"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
|
)
|
|
|
|
type FakeConfigStore struct {
|
|
configs map[int64]*models.AlertConfiguration
|
|
}
|
|
|
|
func (f *FakeConfigStore) GetAllLatestAlertmanagerConfiguration(context.Context) ([]*models.AlertConfiguration, error) {
|
|
result := make([]*models.AlertConfiguration, 0, len(f.configs))
|
|
for _, configuration := range f.configs {
|
|
result = append(result, configuration)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (f *FakeConfigStore) GetLatestAlertmanagerConfiguration(query *models.GetLatestAlertmanagerConfigurationQuery) error {
|
|
var ok bool
|
|
query.Result, ok = f.configs[query.OrgID]
|
|
if !ok {
|
|
return store.ErrNoAlertmanagerConfiguration
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *FakeConfigStore) SaveAlertmanagerConfiguration(cmd *models.SaveAlertmanagerConfigurationCmd) error {
|
|
f.configs[cmd.OrgID] = &models.AlertConfiguration{
|
|
AlertmanagerConfiguration: cmd.AlertmanagerConfiguration,
|
|
OrgID: cmd.OrgID,
|
|
ConfigurationVersion: "v1",
|
|
Default: cmd.Default,
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *FakeConfigStore) SaveAlertmanagerConfigurationWithCallback(cmd *models.SaveAlertmanagerConfigurationCmd, callback store.SaveCallback) error {
|
|
f.configs[cmd.OrgID] = &models.AlertConfiguration{
|
|
AlertmanagerConfiguration: cmd.AlertmanagerConfiguration,
|
|
OrgID: cmd.OrgID,
|
|
ConfigurationVersion: "v1",
|
|
Default: cmd.Default,
|
|
}
|
|
|
|
if err := callback(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type FakeOrgStore struct {
|
|
orgs []int64
|
|
}
|
|
|
|
func (f *FakeOrgStore) GetOrgs(_ context.Context) ([]int64, error) {
|
|
return f.orgs, nil
|
|
}
|
|
|
|
type FakeKVStore struct {
|
|
mtx sync.Mutex
|
|
store map[int64]map[string]map[string]string
|
|
}
|
|
|
|
func newFakeKVStore(t *testing.T) *FakeKVStore {
|
|
t.Helper()
|
|
|
|
return &FakeKVStore{
|
|
store: map[int64]map[string]map[string]string{},
|
|
}
|
|
}
|
|
|
|
func (fkv *FakeKVStore) Get(_ context.Context, orgId int64, namespace string, key string) (string, bool, error) {
|
|
fkv.mtx.Lock()
|
|
defer fkv.mtx.Unlock()
|
|
org, ok := fkv.store[orgId]
|
|
if !ok {
|
|
return "", false, nil
|
|
}
|
|
k, ok := org[namespace]
|
|
if !ok {
|
|
return "", false, nil
|
|
}
|
|
|
|
v, ok := k[key]
|
|
if !ok {
|
|
return "", false, nil
|
|
}
|
|
|
|
return v, true, nil
|
|
}
|
|
func (fkv *FakeKVStore) Set(_ context.Context, orgId int64, namespace string, key string, value string) error {
|
|
fkv.mtx.Lock()
|
|
defer fkv.mtx.Unlock()
|
|
org, ok := fkv.store[orgId]
|
|
if !ok {
|
|
fkv.store[orgId] = map[string]map[string]string{}
|
|
}
|
|
_, ok = org[namespace]
|
|
if !ok {
|
|
fkv.store[orgId][namespace] = map[string]string{}
|
|
}
|
|
|
|
fkv.store[orgId][namespace][key] = value
|
|
|
|
return nil
|
|
}
|
|
func (fkv *FakeKVStore) Del(_ context.Context, orgId int64, namespace string, key string) error {
|
|
fkv.mtx.Lock()
|
|
defer fkv.mtx.Unlock()
|
|
org, ok := fkv.store[orgId]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
_, ok = org[namespace]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
delete(fkv.store[orgId][namespace], key)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (fkv *FakeKVStore) Keys(ctx context.Context, orgID int64, namespace string, keyPrefix string) ([]kvstore.Key, error) {
|
|
fkv.mtx.Lock()
|
|
defer fkv.mtx.Unlock()
|
|
var keys []kvstore.Key
|
|
for orgIDFromStore, namespaceMap := range fkv.store {
|
|
if orgID != kvstore.AllOrganizations && orgID != orgIDFromStore {
|
|
continue
|
|
}
|
|
if keyMap, exists := namespaceMap[namespace]; exists {
|
|
for k := range keyMap {
|
|
if strings.HasPrefix(k, keyPrefix) {
|
|
keys = append(keys, kvstore.Key{
|
|
OrgId: orgIDFromStore,
|
|
Namespace: namespace,
|
|
Key: keyPrefix,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return keys, nil
|
|
}
|
|
|
|
type fakeState struct {
|
|
data string
|
|
}
|
|
|
|
func (fs *fakeState) MarshalBinary() ([]byte, error) {
|
|
return []byte(fs.data), nil
|
|
}
|