Skip to content

Commit

Permalink
test(config): test ReadHealthChecksURL and Config.Print
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed May 9, 2022
1 parent 801f97b commit 100ee37
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 17 deletions.
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func (c *Config) Print(ppfmt pp.PP) {
for _, m := range c.Monitors {
inner.Infof(pp.EmojiBullet, "%-17s %v", m.DescribeService()+":", m.DescribeBaseURL())
}
} else {
ppfmt.Infof(pp.EmojiConfig, "Monitors: (none)")
}
}

Expand Down
46 changes: 46 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/favonia/cloudflare-ddns/internal/file"
"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/pp"
)

Expand Down Expand Up @@ -359,6 +360,7 @@ func TestPrintDefault(t *testing.T) {
mockPP.EXPECT().Infof(pp.EmojiConfig, "Timeouts:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IP detection: %v", time.Second*5),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Record updating: %v", time.Second*30),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Monitors: (none)"),
)
config.Default().Print(mockPP)
}
Expand Down Expand Up @@ -391,11 +393,55 @@ func TestPrintEmpty(t *testing.T) {
mockPP.EXPECT().Infof(pp.EmojiConfig, "Timeouts:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IP detection: %v", time.Duration(0)),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Record updating: %v", time.Duration(0)),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Monitors: (none)"),
)
var cfg config.Config
cfg.Print(mockPP)
}

func TestPrintMonitors(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)

store(t, "TZ", "UTC")

c := config.Default()

mockPP := mocks.NewMockPP(mockCtrl)
innerMockPP := mocks.NewMockPP(mockCtrl)
gomock.InOrder(
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(true),
mockPP.EXPECT().Infof(pp.EmojiEnvVars, "Current settings:"),
mockPP.EXPECT().IncIndent().Return(mockPP),
mockPP.EXPECT().IncIndent().Return(innerMockPP),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Policies:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IPv4 policy: %s", "cloudflare.trace"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IPv4 domains: %v", []api.Domain(nil)),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IPv6 policy: %s", "cloudflare.trace"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IPv6 domains: %v", []api.Domain(nil)),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Scheduling:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Timezone: %s", Some("UTC (UTC+00 now)", "Local (UTC+00 now)")),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Update frequency: %v", cron.MustNew("@every 5m")),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Update on start? %t", true),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Delete on stop? %t", false),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Cache expiration: %v", time.Hour*6),
mockPP.EXPECT().Infof(pp.EmojiConfig, "New DNS records:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "TTL: %s", "1 (automatic)"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Proxied: %t", false),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Timeouts:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IP detection: %v", time.Second*5),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Record updating: %v", time.Second*30),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Monitors:"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "%-17s %v", "Healthchecks.io:", "http://user:xxxxx@host/path"),
)

m, ok := monitor.NewHealthChecks(mockPP, "http://user:pass@host/path")
require.True(t, ok)

c.Monitors = []monitor.Monitor{m}
c.Print(mockPP)
}

func TestPrintHidden(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)
Expand Down
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
}
81 changes: 81 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,83 @@ 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,
},
"password": {
true, "https://me:pass@hi.org/1234",
[]mon{},
[]mon{&monitor.HealthChecks{
BaseURL: "https://me:pass@hi.org/1234",
RedactedBaseURL: "https://me:xxxxx@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 100ee37

Please sign in to comment.