This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhealthz_test.go
76 lines (67 loc) · 1.67 KB
/
healthz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gocosi
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/doomshrine/must"
"github.com/doomshrine/testcontext"
"github.com/hellofresh/health-go/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHealthcheckProbe(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
assertion func(assert.TestingT, error, ...interface{}) bool
port int
healthz *health.Health
}{
{
name: "default",
assertion: assert.NoError,
port: 30001,
healthz: must.Do(health.New()),
},
{
name: "error",
assertion: assert.Error,
port: 30002,
healthz: must.Do(health.New(health.WithChecks(
health.Config{
Name: "test",
Timeout: time.Second,
Check: func(ctx context.Context) error {
return fmt.Errorf("forced error")
},
},
))),
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := testcontext.FromTimeout(context.Background(), t, time.Second)
defer cancel()
mux := http.NewServeMux()
mux.Handle(HealthcheckEndpoint, tc.healthz.Handler())
server := &http.Server{
Addr: fmt.Sprintf(":%d", tc.port),
Handler: mux,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
}
defer server.Shutdown(ctx) //nolint:errcheck
go func() {
err := server.ListenAndServe()
require.ErrorIs(t, err, http.ErrServerClosed)
}()
err := HealthcheckFunc(ctx, fmt.Sprintf("http://localhost:%d/healthz", tc.port))
tc.assertion(t, err)
})
}
}