Skip to content

Commit

Permalink
Add test and marker const for wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweav committed Oct 9, 2023
1 parent 8e16003 commit 5774ce0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func run() int {
level.Info(configLogger).Log("msg", "skipping creation of receiver not referenced by any route", "receiver", rcv.Name)
continue
}
integrations, err := receiver.BuildReceiverIntegrations(rcv, tmpl, logger)
integrations, err := receiver.BuildReceiverIntegrations(rcv, tmpl, logger, receiver.NoWrap)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion notify/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

type Wrapper func(string, notify.Notifier) notify.Notifier

var NoWrap Wrapper = nil

Check warning on line 41 in notify/receiver/receiver.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var NoWrap; it is the zero value (revive)

// BuildReceiverIntegrations builds a list of integration notifiers off of a
// receiver config.
func BuildReceiverIntegrations(nc config.Receiver, tmpl *template.Template, logger log.Logger, wrap Wrapper, httpOpts ...commoncfg.HTTPClientOption) ([]notify.Integration, error) {
Expand All @@ -50,7 +52,9 @@ func BuildReceiverIntegrations(nc config.Receiver, tmpl *template.Template, logg
errs.Add(err)
return
}
n = wrap(name, n)
if wrap != nil {
n = wrap(name, n)
}
integrations = append(integrations, notify.NewIntegration(n, rs, name, i, nc.Name))
}
)
Expand Down
29 changes: 28 additions & 1 deletion notify/receiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestBuildReceiverIntegrations(t *testing.T) {
} {
tc := tc
t.Run("", func(t *testing.T) {
integrations, err := BuildReceiverIntegrations(tc.receiver, nil, nil)
integrations, err := BuildReceiverIntegrations(tc.receiver, nil, nil, NoWrap)
if tc.err {
require.Error(t, err)
return
Expand All @@ -85,4 +85,31 @@ func TestBuildReceiverIntegrations(t *testing.T) {
}
})
}

t.Run("invokes notifier wrapper func", func(t *testing.T) {
calls := 0
wrap := func(_ string, n notify.Notifier) notify.Notifier {
calls += 1

Check warning on line 92 in notify/receiver/receiver_test.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace calls += 1 with calls++ (revive)
return n
}
cfg := config.Receiver{
Name: "foo",
WebhookConfigs: []*config.WebhookConfig{
{
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
{
HTTPConfig: &commoncfg.HTTPClientConfig{},
NotifierConfig: config.NotifierConfig{
VSendResolved: true,
},
},
},
}

_, err := BuildReceiverIntegrations(cfg, nil, nil, wrap)
require.NoError(t, err)

require.Equal(t, 2, calls)
})
}

0 comments on commit 5774ce0

Please sign in to comment.