-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
170 lines (167 loc) · 3.39 KB
/
handler_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package healthcheck
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestHealthCheck_handler(t *testing.T) {
type fields struct {
checkers map[string]checker
}
type args struct {
r *http.Request
}
type want struct {
code int
body bool
}
tests := []struct {
name string
fields fields
args args
want want
}{
{
"empty",
fields{map[string]checker{}},
args{httptest.NewRequest(http.MethodGet, "/metrics", nil)},
want{
http.StatusOK,
false,
},
},
{
"success",
fields{map[string]checker{
"checker_1": &mockCheck{},
"checker_2": &mockCheck{},
}},
args{httptest.NewRequest(http.MethodGet, "/metrics", nil)},
want{
http.StatusOK,
false,
},
},
{
"fail",
fields{map[string]checker{
"checker_1": &mockCheck{},
"checker_2": &mockCheck{err: errors.New("checker_2 failed")},
}},
args{httptest.NewRequest(http.MethodGet, "/metrics", nil)},
want{
http.StatusServiceUnavailable,
false,
},
},
{
"detail",
fields{map[string]checker{
"checker_1": &mockCheck{},
"checker_2": &mockCheck{err: errors.New("checker_2 failed")},
}},
args{httptest.NewRequest(http.MethodGet, "/metrics?detail", nil)},
want{
http.StatusServiceUnavailable,
true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
h := &HealthCheck{
checkers: tt.fields.checkers,
}
h.handler(w, tt.args.r)
if w.Code != tt.want.code {
t.Errorf("handler() code = %v, want %v", w.Code, tt.want.code)
}
if (w.Body.Len() != 0) != tt.want.body {
t.Errorf("handler() body = %v, want body %v", w.Body, tt.want.body)
}
})
}
}
func TestHealthCheck_handlerDetail(t *testing.T) {
type fields struct {
checkers map[string]checker
}
type args struct {
ctx context.Context
errs map[string]error
}
tests := []struct {
name string
fields fields
args args
want map[string]string
}{
{
"empty",
fields{map[string]checker{}},
args{
context.Background(),
map[string]error{},
},
map[string]string{},
},
{
"no_error",
fields{map[string]checker{
"checker_1": &mockCheck{},
"checker_2": &mockCheck{},
}},
args{
context.Background(),
map[string]error{},
},
map[string]string{
"checker_1": "OK",
"checker_2": "OK",
},
},
{
"2_errors_2_success",
fields{map[string]checker{
"checker_1": &mockCheck{},
"checker_2": &mockCheck{},
"checker_3": &mockCheck{},
"checker_4": &mockCheck{},
}},
args{
context.Background(),
map[string]error{
"checker_2": errors.New("checker_2 failed"),
"checker_4": errors.New("checker_4 failed"),
},
},
map[string]string{
"checker_1": "OK",
"checker_2": "checker_2 failed",
"checker_3": "OK",
"checker_4": "checker_4 failed",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
h := &HealthCheck{
checkers: tt.fields.checkers,
}
h.handlerDetail(tt.args.ctx, w, tt.args.errs)
got := make(map[string]string)
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Errorf("handlerDetail() response is not JSON %v", w.Body.String())
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("handlerDetail() body = %v, want %v", got, tt.want)
}
})
}
}