Alerting: Update test funcs for notifications (#51013)

This commit is contained in:
George Robinson 2022-06-20 09:05:21 +01:00 committed by GitHub
parent 1f5f40b2da
commit 2dbaf259a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 20 deletions

View File

@ -27,8 +27,7 @@ func TestSensuGoNotifier(t *testing.T) {
require.NoError(t, err)
tmpl.ExternalURL = externalURL
images, deleteFunc := newFakeImageStore(t)
defer deleteFunc()
images := newFakeImageStore()
cases := []struct {
name string

View File

@ -12,13 +12,44 @@ import (
)
// deleteFunc deletes the fake image.
// nolint:unused
type deleteFunc func()
// newFakeImageStore creates a fake image on disk and returns an image
// with a token, path and URL. The test should call deleteFunc to delete
// the image from disk at the end of the test.
func newFakeImageStore(t *testing.T) (ImageStore, deleteFunc) {
f, err := os.CreateTemp("", "ngalert-test-image-*.png")
type fakeImageStore struct {
Images []*ngmodels.Image
}
// getImage returns an image with the same token.
func (f *fakeImageStore) GetImage(_ context.Context, token string) (*ngmodels.Image, error) {
for _, img := range f.Images {
if img.Token == token {
return img, nil
}
}
return nil, ngmodels.ErrImageNotFound
}
// newFakeImageStore returns an image store with a test image.
// The image has a token and a URL, but does not have a file on disk.
func newFakeImageStore() ImageStore {
return &fakeImageStore{
Images: []*ngmodels.Image{
{
Token: "test-image",
URL: "https://www.example.com/test-image.jpg",
CreatedAt: time.Now().UTC(),
},
},
}
}
// newFakeImageStoreWithFile returns an image store with a test image.
// The image has a token, path and a URL, where the path is 1x1 transparent
// PNG on disk. The test should call deleteFunc to delete the image from disk
// at the end of the test.
// nolint:deadcode,unused
func newFakeImageStoreWithFile(t *testing.T) (ImageStore, deleteFunc) {
f, err := os.CreateTemp("", "test-image-*.png")
if err != nil {
t.Fatalf("failed to create temp image: %s", err)
}
@ -98,16 +129,3 @@ func (ns *notificationServiceMock) SendEmailCommandHandler(ctx context.Context,
}
func mockNotificationService() *notificationServiceMock { return &notificationServiceMock{} }
type fakeImageStore struct {
Images []*ngmodels.Image
}
func (f *fakeImageStore) GetImage(ctx context.Context, token string) (*ngmodels.Image, error) {
for _, img := range f.Images {
if img.Token == token {
return img, nil
}
}
return nil, ngmodels.ErrImageNotFound
}