Skip to content

Commit

Permalink
test(config): test ReadHealthChecksURL
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed May 9, 2022
1 parent 801f97b commit 8401951
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 17 deletions.
34 changes: 17 additions & 17 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,6 @@ func ReadPolicy(ppfmt pp.PP, key string, field *detector.Policy) bool {
}
}

// ReadHealthChecksURL reads the base URL of the healthcheck.io endpoint.
func ReadHealthChecksURL(ppfmt pp.PP, key string, field *[]monitor.Monitor) bool {
val := Getenv(key)

if val == "" {
return true
}

h, ok := monitor.NewHealthChecks(ppfmt, val)
if !ok {
return false
}

*field = append(*field, h)
return true
}

// ReadNonnegDuration reads an environment variable and parses it as a time duration.
func ReadNonnegDuration(ppfmt pp.PP, key string, field *time.Duration) bool {
val := Getenv(key)
Expand Down Expand Up @@ -192,3 +175,20 @@ func ReadCron(ppfmt pp.PP, key string, field *cron.Schedule) bool {
*field = c
return true
}

// ReadHealthChecksURL reads the base URL of the healthcheck.io endpoint.
func ReadHealthChecksURL(ppfmt pp.PP, key string, field *[]monitor.Monitor) bool {
val := Getenv(key)

if val == "" {
return true
}

h, ok := monitor.NewHealthChecks(ppfmt, val)
if !ok {
return false
}

*field = append(*field, h)
return true
}
69 changes: 69 additions & 0 deletions internal/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/favonia/cloudflare-ddns/internal/cron"
"github.com/favonia/cloudflare-ddns/internal/detector"
"github.com/favonia/cloudflare-ddns/internal/mocks"
"github.com/favonia/cloudflare-ddns/internal/monitor"
"github.com/favonia/cloudflare-ddns/internal/pp"
)

Expand Down Expand Up @@ -490,3 +491,71 @@ func TestReadCron(t *testing.T) {
})
}
}

//nolint:paralleltest,funlen // paralleltest should not be used because environment vars are global
func TestReadHealthChecksURL(t *testing.T) {
key := keyPrefix + "HEALTHCHECKS"

type mon = monitor.Monitor

for name, tc := range map[string]struct {
set bool
val string
oldField []mon
newField []mon
ok bool
prepareMockPP func(*mocks.MockPP)
}{
"unset": {
false, "", []mon{}, []mon{}, true, nil,
},
"empty": {
true, "", []mon{}, []mon{}, true, nil,
},
"example": {
true, "https://hi.org/1234",
[]mon{},
[]mon{&monitor.HealthChecks{
BaseURL: "https://hi.org/1234",
RedactedBaseURL: "https://hi.org/1234",
Timeout: monitor.HeathChecksDefaultTimeout,
MaxRetries: monitor.HealthChecksDefaultMaxRetries,
}},
true,
nil,
},
"illformed": {
true, "https://hi.org/1234?hello=123",
[]mon{},
[]mon{},
false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(
pp.EmojiUserError,
"The URL %q does not look like a valid Healthchecks URL.",
"https://hi.org/1234?hello=123",
)
m.EXPECT().Errorf(
pp.EmojiUserError,
`A valid example is "https://hc-ping.com/01234567-0123-0123-0123-0123456789abc".`,
)
},
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)

set(t, key, tc.set, tc.val)

field := append([]mon{}, tc.oldField...)
mockPP := mocks.NewMockPP(mockCtrl)
if tc.prepareMockPP != nil {
tc.prepareMockPP(mockPP)
}
ok := config.ReadHealthChecksURL(mockPP, key, &field)
require.Equal(t, tc.ok, ok)
require.Equal(t, tc.newField, field)
})
}
}

0 comments on commit 8401951

Please sign in to comment.