grafana/pkg/services/ngalert/store/testing.go
Arati R 6cb1a5e368
Nested folders: Add alert rule counts and deletion to folder registry (#67259)
* Let alert rule service implement registry service
* Add count method to RuleStore interface
* Add implementation for deletion of alert rules
* Rename uid to folderUID in registry methods
* Check forceDeleteRule value for registry deletion
* Register alerting store with folder service
* Move folder test functions to separate package
* Add testing for alert rule counting, deletion
* Remove redundant count method
* Fix deleteChildrenInFolder signature
* Update pkg/services/ngalert/store/alert_rule.go
Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
* Add tests for nested folder deletion
* Refactor TestIntegrationNestedFolderService
* Add rules store as parameter for alertng provider

---------

Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
2023-06-02 16:38:02 +02:00

115 lines
2.7 KiB
Go

package store
import (
"context"
"strings"
"sync"
"testing"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
func NewFakeImageStore(t *testing.T) *FakeImageStore {
return &FakeImageStore{
t: t,
images: make(map[string]*models.Image),
}
}
type FakeImageStore struct {
t *testing.T
mtx sync.Mutex
images map[string]*models.Image
}
func (s *FakeImageStore) GetImage(_ context.Context, token string) (*models.Image, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
if image, ok := s.images[token]; ok {
return image, nil
}
return nil, models.ErrImageNotFound
}
func (s *FakeImageStore) GetImageByURL(_ context.Context, url string) (*models.Image, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, image := range s.images {
if image.URL == url {
return image, nil
}
}
return nil, models.ErrImageNotFound
}
func (s *FakeImageStore) GetImages(_ context.Context, tokens []string) ([]models.Image, []string, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
images := make([]models.Image, 0, len(tokens))
for _, token := range tokens {
if image, ok := s.images[token]; ok {
images = append(images, *image)
}
}
if len(images) < len(tokens) {
return images, unmatchedTokens(tokens, images), models.ErrImageNotFound
}
return images, nil, nil
}
func (s *FakeImageStore) SaveImage(_ context.Context, image *models.Image) error {
s.mtx.Lock()
defer s.mtx.Unlock()
if image.ID == 0 {
image.ID = int64(len(s.images)) + 1
}
if image.Token == "" {
tmp := strings.Split(image.Path, ".")
image.Token = strings.Join(tmp[:len(tmp)-1], ".")
}
s.images[image.Token] = image
return nil
}
func NewFakeAdminConfigStore(t *testing.T) *FakeAdminConfigStore {
t.Helper()
return &FakeAdminConfigStore{Configs: map[int64]*models.AdminConfiguration{}}
}
type FakeAdminConfigStore struct {
mtx sync.Mutex
Configs map[int64]*models.AdminConfiguration
}
func (f *FakeAdminConfigStore) GetAdminConfiguration(orgID int64) (*models.AdminConfiguration, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
return f.Configs[orgID], nil
}
func (f *FakeAdminConfigStore) GetAdminConfigurations() ([]*models.AdminConfiguration, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
acs := make([]*models.AdminConfiguration, 0, len(f.Configs))
for _, ac := range f.Configs {
acs = append(acs, ac)
}
return acs, nil
}
func (f *FakeAdminConfigStore) DeleteAdminConfiguration(orgID int64) error {
f.mtx.Lock()
defer f.mtx.Unlock()
delete(f.Configs, orgID)
return nil
}
func (f *FakeAdminConfigStore) UpdateAdminConfiguration(cmd UpdateAdminConfigurationCmd) error {
f.mtx.Lock()
defer f.mtx.Unlock()
f.Configs[cmd.AdminConfiguration.OrgID] = cmd.AdminConfiguration
return nil
}