Skip to content

Commit

Permalink
test(monitor): cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed May 9, 2022
1 parent 8f3e172 commit b49bb51
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func TestReadHealthChecksURL(t *testing.T) {
[]mon{&monitor.HealthChecks{
BaseURL: "https://hi.org/1234",
RedactedBaseURL: "https://hi.org/1234",
Timeout: monitor.HeathChecksDefaultTimeout,
Timeout: monitor.HealthChecksDefaultTimeout,
MaxRetries: monitor.HealthChecksDefaultMaxRetries,
}},
true,
Expand All @@ -530,7 +530,7 @@ func TestReadHealthChecksURL(t *testing.T) {
[]mon{&monitor.HealthChecks{
BaseURL: "https://me:pass@hi.org/1234",
RedactedBaseURL: "https://me:xxxxx@hi.org/1234",
Timeout: monitor.HeathChecksDefaultTimeout,
Timeout: monitor.HealthChecksDefaultTimeout,
MaxRetries: monitor.HealthChecksDefaultMaxRetries,
}},
true,
Expand Down
14 changes: 7 additions & 7 deletions internal/monitor/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type HealthChecks struct {
}

const (
HeathChecksDefaultTimeout = 10 * time.Second
HealthChecksDefaultTimeout = 10 * time.Second
HealthChecksDefaultMaxRetries = 5
)

Expand All @@ -40,7 +40,7 @@ func NewHealthChecks(ppfmt pp.PP, rawURL string) (Monitor, bool) {
return &HealthChecks{
BaseURL: url.String(),
RedactedBaseURL: url.Redacted(),
Timeout: HeathChecksDefaultTimeout,
Timeout: HealthChecksDefaultTimeout,
MaxRetries: HealthChecksDefaultMaxRetries,
}, true
}
Expand All @@ -53,7 +53,7 @@ func (h *HealthChecks) DescribeBaseURL() string {
return h.RedactedBaseURL
}

func (h *HealthChecks) reallyPing(ctx context.Context, ppfmt pp.PP, url string, redatedURL string) bool {
func (h *HealthChecks) ping(ctx context.Context, ppfmt pp.PP, url string, redatedURL string) bool {
for retries := 0; retries < h.MaxRetries; retries++ {
ctx, cancel := context.WithTimeout(ctx, h.Timeout)
defer cancel()
Expand Down Expand Up @@ -116,15 +116,15 @@ func (h *HealthChecks) reallyPing(ctx context.Context, ppfmt pp.PP, url string,
}

func (h *HealthChecks) Success(ctx context.Context, ppfmt pp.PP) bool {
return h.reallyPing(ctx, ppfmt, h.BaseURL, h.RedactedBaseURL)
return h.ping(ctx, ppfmt, h.BaseURL, h.RedactedBaseURL)
}

func (h *HealthChecks) Start(ctx context.Context, ppfmt pp.PP) bool {
return h.reallyPing(ctx, ppfmt, h.BaseURL+"/start", h.RedactedBaseURL+"/start")
return h.ping(ctx, ppfmt, h.BaseURL+"/start", h.RedactedBaseURL+"/start")
}

func (h *HealthChecks) Failure(ctx context.Context, ppfmt pp.PP) bool {
return h.reallyPing(ctx, ppfmt, h.BaseURL+"/fail", h.RedactedBaseURL+"/fail")
return h.ping(ctx, ppfmt, h.BaseURL+"/fail", h.RedactedBaseURL+"/fail")
}

func (h *HealthChecks) ExitStatus(ctx context.Context, ppfmt pp.PP, code int) bool {
Expand All @@ -133,5 +133,5 @@ func (h *HealthChecks) ExitStatus(ctx context.Context, ppfmt pp.PP, code int) bo
return false
}

return h.reallyPing(ctx, ppfmt, fmt.Sprintf("%s/%d", h.BaseURL, code), fmt.Sprintf("%s/%d", h.RedactedBaseURL, code))
return h.ping(ctx, ppfmt, fmt.Sprintf("%s/%d", h.BaseURL, code), fmt.Sprintf("%s/%d", h.RedactedBaseURL, code))
}
17 changes: 17 additions & 0 deletions internal/monitor/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,20 @@ func TestEndPoints(t *testing.T) {
})
}
}

func TestEndPointsIllFormed(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)
mockPP := mocks.NewMockPP(mockCtrl)
mockPP.EXPECT().Warningf(pp.EmojiImpossible, "Failed to prepare HTTP(S) request to %q: %v", "blah", gomock.Any())

m := &monitor.HealthChecks{
BaseURL: "://#?",
RedactedBaseURL: "blah",
Timeout: monitor.HealthChecksDefaultTimeout,
MaxRetries: monitor.HealthChecksDefaultMaxRetries,
}

ok := m.Success(context.Background(), mockPP)
require.False(t, ok)
}

0 comments on commit b49bb51

Please sign in to comment.