Skip to content

Commit

Permalink
^ This is a combination of 5 commits.
Browse files Browse the repository at this point in the history
^ This is the 1st commit message:

refactor notifier package

move services to deciated packages
split configurations and implementations

^ The commit message #2 will be skipped:

^ move factory from Grafana

^ The commit message #3 will be skipped:

^ rename settings to config

^ The commit message #4 will be skipped:

^ reaname TemplateForTests to FroTests to appease linter

^ The commit message #5 will be skipped:

^ lint
  • Loading branch information
yuri-tceretian committed Jan 27, 2023
1 parent facc6b2 commit 06b42ff
Show file tree
Hide file tree
Showing 88 changed files with 2,240 additions and 1,802 deletions.
1 change: 0 additions & 1 deletion alerting/crypto.go

This file was deleted.

1 change: 0 additions & 1 deletion alerting/mimir_alertmanager.go

This file was deleted.

26 changes: 0 additions & 26 deletions alerting/notifier/channels/images.go

This file was deleted.

222 changes: 0 additions & 222 deletions alerting/notifier/channels/webhook.go

This file was deleted.

40 changes: 40 additions & 0 deletions images/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package images

import (
"context"
"errors"
"time"
)

var (
ErrImageNotFound = errors.New("image not found")

// ErrImagesDone is used to stop iteration of subsequent images. It should be
// returned from forEachFunc when either the intended image has been found or
// the maximum number of images has been iterated.
ErrImagesDone = errors.New("images done")

ErrImagesUnavailable = errors.New("alert screenshots are unavailable")
)

type Image struct {
Token string
Path string
URL string
CreatedAt time.Time
}

func (i Image) HasURL() bool {
return i.URL != ""
}

type ImageStore interface {
GetImage(ctx context.Context, token string) (*Image, error)
}

type UnavailableImageStore struct{}

// GetImage returns the image with the corresponding token, or ErrImageNotFound.
func (u *UnavailableImageStore) GetImage(ctx context.Context, token string) (*Image, error) {
return nil, ErrImagesUnavailable
}
54 changes: 8 additions & 46 deletions alerting/notifier/channels/testing.go → images/testing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package channels
package images

import (
"context"
Expand All @@ -9,12 +9,12 @@ import (
"time"
)

type fakeImageStore struct {
type FakeImageStore struct {
Images []*Image
}

// getImage returns an image with the same token.
func (f *fakeImageStore) GetImage(_ context.Context, token string) (*Image, error) {
func (f *FakeImageStore) GetImage(_ context.Context, token string) (*Image, error) {
for _, img := range f.Images {
if img.Token == token {
return img, nil
Expand All @@ -25,8 +25,8 @@ func (f *fakeImageStore) GetImage(_ context.Context, token string) (*Image, erro

// newFakeImageStore returns an image store with N test images.
// Each image has a token and a URL, but does not have a file on disk.
func newFakeImageStore(n int) ImageStore {
s := fakeImageStore{}
func NewFakeImageStore(n int) ImageStore {
s := FakeImageStore{}
for i := 1; i <= n; i++ {
s.Images = append(s.Images, &Image{
Token: fmt.Sprintf("test-image-%d", i),
Expand All @@ -37,15 +37,15 @@ func newFakeImageStore(n int) ImageStore {
return &s
}

// newFakeImageStoreWithFile returns an image store with N test images.
// NewFakeImageStoreWithFile returns an image store with N test images.
// Each 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 images from disk
// at the end of the test.
// nolint:deadcode,unused
func newFakeImageStoreWithFile(t *testing.T, n int) ImageStore {
func NewFakeImageStoreWithFile(t *testing.T, n int) ImageStore {
var (
files []string
s fakeImageStore
s FakeImageStore
)

t.Cleanup(func() {
Expand Down Expand Up @@ -74,27 +74,6 @@ func newFakeImageStoreWithFile(t *testing.T, n int) ImageStore {
return &s
}

// mockTimeNow replaces function timeNow to return constant time.
// It returns a function that resets the variable back to its original value.
// This allows usage of this function with defer:
//
// func Test (t *testing.T) {
// now := time.Now()
// defer mockTimeNow(now)()
// ...
// }
func mockTimeNow(constTime time.Time) func() {
timeNow = func() time.Time {
return constTime
}
return resetTimeNow
}

// resetTimeNow resets the global variable timeNow to the default value, which is time.Now
func resetTimeNow() {
timeNow = time.Now
}

// nolint:deadcode,unused
func newTestImage() (string, error) {
f, err := os.CreateTemp("", "test-image-*.png")
Expand All @@ -118,20 +97,3 @@ func newTestImage() (string, error) {

return f.Name(), nil
}

type notificationServiceMock struct {
Webhook SendWebhookSettings
EmailSync SendEmailSettings
ShouldError error
}

func (ns *notificationServiceMock) SendWebhook(ctx context.Context, cmd *SendWebhookSettings) error {
ns.Webhook = *cmd
return ns.ShouldError
}
func (ns *notificationServiceMock) SendEmail(ctx context.Context, cmd *SendEmailSettings) error {
ns.EmailSync = *cmd
return ns.ShouldError
}

func mockNotificationService() *notificationServiceMock { return &notificationServiceMock{} }
Loading

0 comments on commit 06b42ff

Please sign in to comment.