From 2dbaf259a71ab43495d894f317087bc808d33882 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Mon, 20 Jun 2022 09:05:21 +0100 Subject: [PATCH] Alerting: Update test funcs for notifications (#51013) --- .../ngalert/notifier/channels/sensugo_test.go | 3 +- .../ngalert/notifier/channels/testing.go | 54 ++++++++++++------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/sensugo_test.go b/pkg/services/ngalert/notifier/channels/sensugo_test.go index ef69c264979..ec4d7274ae8 100644 --- a/pkg/services/ngalert/notifier/channels/sensugo_test.go +++ b/pkg/services/ngalert/notifier/channels/sensugo_test.go @@ -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 diff --git a/pkg/services/ngalert/notifier/channels/testing.go b/pkg/services/ngalert/notifier/channels/testing.go index 0f734808e6e..3cd6302d54d 100644 --- a/pkg/services/ngalert/notifier/channels/testing.go +++ b/pkg/services/ngalert/notifier/channels/testing.go @@ -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 ¬ificationServiceMock{} } - -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 -}