Skip to content

Commit

Permalink
test(config): test printing of notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Nov 5, 2023
1 parent fe19aff commit 831628b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
5 changes: 3 additions & 2 deletions internal/config/config_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/favonia/cloudflare-ddns/internal/domain"
"github.com/favonia/cloudflare-ddns/internal/ipnet"
"github.com/favonia/cloudflare-ddns/internal/monitor"
"github.com/favonia/cloudflare-ddns/internal/notifier"
"github.com/favonia/cloudflare-ddns/internal/pp"
"github.com/favonia/cloudflare-ddns/internal/provider"
)
Expand Down Expand Up @@ -97,8 +98,8 @@ func (c *Config) Print(ppfmt pp.PP) {

if len(c.Notifiers) > 0 {
section("Notifiers (via shoutrrr):")
monitor.DescribeAll(func(service, params string) {
notifier.DescribeAll(func(service, params string) {

Check failure on line 101 in internal/config/config_print.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: notifier.DescribeAll (typecheck)

Check failure on line 101 in internal/config/config_print.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: notifier.DescribeAll) (typecheck)

Check failure on line 101 in internal/config/config_print.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: notifier.DescribeAll) (typecheck)

Check failure on line 101 in internal/config/config_print.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: notifier.DescribeAll) (typecheck)

Check failure on line 101 in internal/config/config_print.go

View workflow job for this annotation

GitHub Actions / Test

undefined: notifier.DescribeAll
item(service+":", "%s", params)
}, c.Monitors)
}, c.Notifiers)
}
}
20 changes: 16 additions & 4 deletions internal/config/config_print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/favonia/cloudflare-ddns/internal/config"
"github.com/favonia/cloudflare-ddns/internal/domain"
"github.com/favonia/cloudflare-ddns/internal/ipnet"
"github.com/favonia/cloudflare-ddns/internal/mocks"
"github.com/favonia/cloudflare-ddns/internal/monitor"
"github.com/favonia/cloudflare-ddns/internal/notifier"
"github.com/favonia/cloudflare-ddns/internal/pp"
)

Expand Down Expand Up @@ -117,7 +117,9 @@ func TestPrintMaps(t *testing.T) {
printItem(innerMockPP, "IP detection:", "5s"),
printItem(innerMockPP, "Record updating:", "30s"),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Monitors:"),
printItem(innerMockPP, "Healthchecks:", "(URL redacted)"),
printItem(innerMockPP, "Meow:", "purrrr"),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Notifiers (via shoutrrr):"),
printItem(innerMockPP, "Snake:", "hissss"),
)

c := config.Default()
Expand All @@ -133,10 +135,20 @@ func TestPrintMaps(t *testing.T) {
c.Proxied[domain.FQDN("c")] = false
c.Proxied[domain.FQDN("d")] = false

m, ok := monitor.NewHealthchecks(mockPP, "https://user:pass@host/path")
require.True(t, ok)
m := mocks.NewMockMonitor(mockCtrl)
m.EXPECT().Describe(gomock.Any()).
DoAndReturn(func(f func(string, string)) {
f("Meow", "purrrr")
}).AnyTimes()
c.Monitors = []monitor.Monitor{m}

n := mocks.NewMockNotifier(mockCtrl)
n.EXPECT().Describe(gomock.Any()).
DoAndReturn(func(f func(string, string)) {
f("Snake", "hissss")
}).AnyTimes()
c.Notifiers = []notifier.Notifier{n}

c.Print(mockPP)
}

Expand Down
14 changes: 8 additions & 6 deletions internal/mocks/mock_notifier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/notifier/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type Notifier interface {
Describe(callback func(service, params string))

// Send out a message.
Send(ctx context.Context, ppfmt pp.PP, msg string)
Send(ctx context.Context, ppfmt pp.PP, msg string) bool
}
13 changes: 9 additions & 4 deletions internal/notifier/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ func NewShoutrrr(ppfmt pp.PP, rawURLs []string) (*Shoutrrr, bool) {
return &Shoutrrr{Router: r, ServiceNames: serviceNames}, true
}

func (s *Shoutrrr) Send(_ context.Context, _ pp.PP, msg string) {
s.Router.Send(msg, &types.Params{})
}

func (s *Shoutrrr) Describe(callback func(service, params string)) {
for _, n := range s.ServiceNames {
callback(n, "(URL redacted)")
}
}

func (s *Shoutrrr) Send(_ context.Context, ppfmt pp.PP, msg string) bool {
err := s.Router.Send(msg, &types.Params{})
if err != nil {
ppfmt.Errorf(pp.EmojiUserError, "Failed to send message: %v", err)
return false
}
return true
}

0 comments on commit 831628b

Please sign in to comment.