Skip to content

Commit

Permalink
test: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Nov 6, 2023
1 parent e85d352 commit b1c39c1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/monitor/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (h *Healthchecks) ping(ctx context.Context, ppfmt pp.PP, endpoint string, m
return false
}

ppfmt.Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", endpointDescription)
ppfmt.Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", endpointDescription)
return true
}

Expand Down
12 changes: 6 additions & 6 deletions internal/monitor/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestHealthchecksEndPoints(t *testing.T) {
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `default (root)`), //nolint:lll
m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `default (root)`),
)
},
},
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestHealthchecksEndPoints(t *testing.T) {
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/start"`), //nolint:lll
m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/start"`),
)
},
},
Expand All @@ -174,7 +174,7 @@ func TestHealthchecksEndPoints(t *testing.T) {
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/fail"`), //nolint:lll
m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/fail"`),
)
},
},
Expand All @@ -189,7 +189,7 @@ func TestHealthchecksEndPoints(t *testing.T) {
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/log"`), //nolint:lll
m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/log"`),
)
},
},
Expand All @@ -204,7 +204,7 @@ func TestHealthchecksEndPoints(t *testing.T) {
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/0"`),
m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/0"`),
)
},
},
Expand All @@ -219,7 +219,7 @@ func TestHealthchecksEndPoints(t *testing.T) {
func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, "The Healthchecks URL (redacted) uses HTTP; please consider using HTTPS"),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged the %s endpoint of Healthchecks", `"/1"`),
m.EXPECT().Infof(pp.EmojiPing, "Pinged the %s endpoint of Healthchecks", `"/1"`),
)
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/monitor/uptimekuma.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (h *UptimeKuma) ping(ctx context.Context, ppfmt pp.PP, param UptimeKumaRequ
return false
}

ppfmt.Infof(pp.EmojiNotification, "Successfully pinged Uptime Kuma")
ppfmt.Infof(pp.EmojiPing, "Pinged Uptime Kuma")
return true
}

Expand Down
2 changes: 1 addition & 1 deletion internal/monitor/uptimekuma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestUptimeKumaEndPoints(t *testing.T) {
successPP := func(m *mocks.MockPP) {
gomock.InOrder(
m.EXPECT().Warningf(pp.EmojiUserWarning, httpUnsafeMsg),
m.EXPECT().Infof(pp.EmojiNotification, "Successfully pinged Uptime Kuma"),
m.EXPECT().Infof(pp.EmojiPing, "Pinged Uptime Kuma"),
)
}

Expand Down
5 changes: 4 additions & 1 deletion internal/notifier/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ func (s *Shoutrrr) Send(_ context.Context, ppfmt pp.PP, msg string) bool {
allOk := true
for _, err := range errs {
if err != nil {
ppfmt.Errorf(pp.EmojiUserError, "Failed to send message: %v", err)
ppfmt.Errorf(pp.EmojiError, "Failed to send some shoutrrr message: %v", err)
allOk = false
}
}
if allOk {
ppfmt.Infof(pp.EmojiNotification, "Sent shoutrrr message")
}
return allOk
}
23 changes: 18 additions & 5 deletions internal/notifier/shoutrrr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/favonia/cloudflare-ddns/internal/mocks"
"github.com/favonia/cloudflare-ddns/internal/notifier"
"github.com/favonia/cloudflare-ddns/internal/pp"
)

func TestShoutrrrDescripbe(t *testing.T) {
Expand All @@ -30,17 +31,29 @@ func TestShoutrrrSend(t *testing.T) {
t.Parallel()

for name, tc := range map[string]struct {
url string
path string
service func(serverURL string) string
message string
pinged bool
ok bool
prepareMockPP func(*mocks.MockPP)
}{
"success": {
"/", "hello",
"/greeting",
func(serverURL string) string { return "generic+" + serverURL + "/greeting" },
"hello",
true, true,
func(m *mocks.MockPP) {
gomock.InOrder()
m.EXPECT().Infof(pp.EmojiNotification, "Sent shoutrrr message")
},
},
"ill-formed url": {
"",
func(_serverURL string) string { return "generic+https://0.0.0.0" },
"hello",
false, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiError, "Failed to send some shoutrrr message: %v", gomock.Any())
},
},
} {
Expand All @@ -56,7 +69,7 @@ func TestShoutrrrSend(t *testing.T) {
pinged := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, tc.url, r.URL.EscapedPath())
require.Equal(t, tc.path, r.URL.EscapedPath())

reqBody, err := io.ReadAll(r.Body)
require.NoError(t, err)
Expand All @@ -65,7 +78,7 @@ func TestShoutrrrSend(t *testing.T) {
pinged = true
}))

s, ok := notifier.NewShoutrrr(mockPP, []string{"generic+" + server.URL + tc.url})
s, ok := notifier.NewShoutrrr(mockPP, []string{tc.service(server.URL)})
require.True(t, ok)
ok = s.Send(context.Background(), mockPP, tc.message)
require.Equal(t, tc.ok, ok)
Expand Down
4 changes: 2 additions & 2 deletions internal/pp/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (
EmojiUpdateRecord Emoji = "📡" // updating DNS records
EmojiClearRecord Emoji = "🧹" // clearing DNS records

EmojiNotification Emoji = "🔔" // sending out notifications, pinging, health checks
EmojiRepeat Emoji = "🔁" // repeating things
EmojiPing Emoji = "🔔" // pinging and health checks
EmojiNotification Emoji = "📨" // notifications

EmojiSignal Emoji = "🚨" // catching signals
EmojiAlreadyDone Emoji = "🤷" // DNS records were already up to date
Expand Down

0 comments on commit b1c39c1

Please sign in to comment.