-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace notify.Factory with a build function that accepts typed confi…
…guration (#63) * replace constructors for each notifier to accept only arguments that are needed. * replace Factory with function BuildReceiverIntegrations * remove NotificationChannel interface (internalized in the function) * remove receivers/factory.go. * move GetDecryptedValueFn to notify package * add notifier uid to log context * update LoggerFactory type to distinguish between logger name and additional context. --------- Co-authored-by: Santiago <[email protected]>
- Loading branch information
1 parent
d1e3c68
commit c7c80f3
Showing
28 changed files
with
401 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package notify | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/alerting/images" | ||
"github.com/grafana/alerting/logging" | ||
"github.com/grafana/alerting/receivers" | ||
"github.com/grafana/alerting/templates" | ||
) | ||
|
||
func TestBuildReceiverIntegrations(t *testing.T) { | ||
var orgID = rand.Int63() | ||
var version = fmt.Sprintf("Grafana v%d", rand.Uint32()) | ||
imageStore := &images.FakeImageStore{} | ||
tmpl := templates.ForTests(t) | ||
|
||
webhookFactory := func(n receivers.Metadata) (receivers.WebhookSender, error) { | ||
return receivers.MockNotificationService(), nil | ||
} | ||
emailFactory := func(n receivers.Metadata) (receivers.EmailSender, error) { | ||
return receivers.MockNotificationService(), nil | ||
} | ||
loggerFactory := func(_ string, _ ...interface{}) logging.Logger { | ||
return &logging.FakeLogger{} | ||
} | ||
|
||
getFullConfig := func(t *testing.T) (GrafanaReceiverConfig, int) { | ||
recCfg := &APIReceiver{ConfigReceiver: ConfigReceiver{Name: "test-receiver"}} | ||
for notifierType, cfg := range allKnownConfigs { | ||
recCfg.Receivers = append(recCfg.Receivers, cfg.getRawNotifierConfig(notifierType)) | ||
} | ||
parsed, err := BuildReceiverConfiguration(context.Background(), recCfg, GetDecryptedValueFnForTesting) | ||
require.NoError(t, err) | ||
return parsed, len(recCfg.Receivers) | ||
} | ||
|
||
t.Run("should build all supported notifiers", func(t *testing.T) { | ||
fullCfg, qty := getFullConfig(t) | ||
|
||
loggerNames := make(map[string]struct{}, qty) | ||
logger := func(name string, _ ...interface{}) logging.Logger { | ||
loggerNames[name] = struct{}{} | ||
return &logging.FakeLogger{} | ||
} | ||
|
||
webhooks := make(map[receivers.Metadata]struct{}, qty) | ||
wh := func(n receivers.Metadata) (receivers.WebhookSender, error) { | ||
webhooks[n] = struct{}{} | ||
return webhookFactory(n) | ||
} | ||
|
||
emails := make(map[receivers.Metadata]struct{}, qty) | ||
em := func(n receivers.Metadata) (receivers.EmailSender, error) { | ||
emails[n] = struct{}{} | ||
return emailFactory(n) | ||
} | ||
|
||
integrations, err := BuildReceiverIntegrations(fullCfg, tmpl, imageStore, logger, wh, em, orgID, version) | ||
|
||
require.NoError(t, err) | ||
require.Len(t, integrations, qty) | ||
|
||
t.Run("should call logger factory for each config", func(t *testing.T) { | ||
require.Len(t, loggerNames, qty) | ||
}) | ||
t.Run("should call webhook factory for each config that needs it", func(t *testing.T) { | ||
require.Len(t, webhooks, 17) // we have 17 notifiers that support webhook | ||
}) | ||
t.Run("should call email factory for each config that needs it", func(t *testing.T) { | ||
require.Len(t, emails, 1) // we have only email notifier that needs sender | ||
}) | ||
}) | ||
t.Run("should return errors if webhook factory fails", func(t *testing.T) { | ||
fullCfg, _ := getFullConfig(t) | ||
calls := 0 | ||
failingFactory := func(n receivers.Metadata) (receivers.WebhookSender, error) { | ||
calls++ | ||
return nil, errors.New("bad-test") | ||
} | ||
|
||
integrations, err := BuildReceiverIntegrations(fullCfg, tmpl, imageStore, loggerFactory, failingFactory, emailFactory, orgID, version) | ||
|
||
require.Empty(t, integrations) | ||
require.NotNil(t, err) | ||
require.ErrorContains(t, err, "bad-test") | ||
require.Greater(t, calls, 0) | ||
}) | ||
t.Run("should return errors if email factory fails", func(t *testing.T) { | ||
fullCfg, _ := getFullConfig(t) | ||
calls := 0 | ||
failingFactory := func(n receivers.Metadata) (receivers.EmailSender, error) { | ||
calls++ | ||
return nil, errors.New("bad-test") | ||
} | ||
|
||
integrations, err := BuildReceiverIntegrations(fullCfg, tmpl, imageStore, loggerFactory, webhookFactory, failingFactory, orgID, version) | ||
|
||
require.Empty(t, integrations) | ||
require.NotNil(t, err) | ||
require.ErrorContains(t, err, "bad-test") | ||
require.Greater(t, calls, 0) | ||
}) | ||
t.Run("should not produce any integration if config is empty", func(t *testing.T) { | ||
cfg := GrafanaReceiverConfig{Name: "test"} | ||
|
||
integrations, err := BuildReceiverIntegrations(cfg, tmpl, imageStore, loggerFactory, webhookFactory, emailFactory, orgID, version) | ||
|
||
require.NoError(t, err) | ||
require.Empty(t, integrations) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.