forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_context_test.go
108 lines (100 loc) · 2.59 KB
/
handler_context_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package health
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewChecksContextHandler(t *testing.T) {
var tests = []struct {
name string
method string
path string
failHealth bool
failReady bool
expectedCode int
}{
{
name: "Non-existent URL",
method: http.MethodPost,
path: "/neverwhere",
expectedCode: http.StatusNotFound,
},
{
name: "POST Method not allowed health",
method: http.MethodPost,
path: "/healthz",
expectedCode: http.StatusMethodNotAllowed,
},
{
name: "POST Method not allowed ready",
method: http.MethodPost,
path: "/ready",
expectedCode: http.StatusMethodNotAllowed,
},
{
name: "No checks health",
method: http.MethodGet,
path: "/healthz",
expectedCode: http.StatusOK,
},
{
name: "No checks ready",
method: http.MethodGet,
path: "/ready",
expectedCode: http.StatusOK,
},
{
name: "Health succeed Ready fail health",
method: http.MethodGet,
path: "/healthz",
expectedCode: http.StatusOK,
failReady: true,
},
{
name: "Health succeed Ready fail ready",
method: http.MethodGet,
path: "/ready",
expectedCode: http.StatusServiceUnavailable,
failReady: true,
},
{
name: "Health fail Ready succeed health",
method: http.MethodGet,
path: "/healthz",
expectedCode: http.StatusServiceUnavailable,
failHealth: true,
},
{
name: "Health fail Ready succeed ready",
method: http.MethodGet,
path: "/ready",
expectedCode: http.StatusOK,
failHealth: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
h := NewChecksContextHandler("/healthz", "/ready")
if test.failHealth {
h.AddLiveness("Liveness check test", func(_ context.Context) error {
return errors.New("Liveness check failed")
})
}
if test.failReady {
h.AddReadiness("Readiness check test", func(_ context.Context) error {
return errors.New("Readiness check failed")
})
}
req, err := http.NewRequestWithContext(context.TODO(), test.method, test.path, nil)
assert.NoError(t, err)
reqStr := test.method + " " + test.path
httpRecorder := httptest.NewRecorder()
h.Handler().ServeHTTP(httpRecorder, req)
assert.Equal(t, test.expectedCode, httpRecorder.Code,
"Result codes don't match %q. [%s]", reqStr, test.name)
})
}
}