-
Notifications
You must be signed in to change notification settings - Fork 30
/
health_test.go
122 lines (120 loc) · 3.22 KB
/
health_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
package healthcheck
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
func TestNewHandlerFunc(t *testing.T) {
type args struct {
opts []Option
}
tests := []struct {
name string
args []Option
statusCode int
response response
}{
{
name: "returns 200 status if no errors",
statusCode: http.StatusOK,
response: response{
Status: http.StatusText(http.StatusOK),
},
},
{
name: "returns 503 status if errors",
statusCode: http.StatusServiceUnavailable,
args: []Option{
WithChecker("database", CheckerFunc(func(ctx context.Context) error {
return fmt.Errorf("connection to db timed out")
})),
WithChecker("testService", CheckerFunc(func(ctx context.Context) error {
return fmt.Errorf("connection refused")
})),
},
response: response{
Status: http.StatusText(http.StatusServiceUnavailable),
Errors: map[string]string{
"database": "connection to db timed out",
"testService": "connection refused",
},
},
},
{
name: "returns 503 status if checkers timeout",
statusCode: http.StatusServiceUnavailable,
args: []Option{
WithTimeout(1 * time.Millisecond),
WithChecker("database", CheckerFunc(func(ctx context.Context) error {
time.Sleep(10 * time.Millisecond)
return nil
})),
},
response: response{
Status: http.StatusText(http.StatusServiceUnavailable),
Errors: map[string]string{
"database": "max check time exceeded",
},
},
},
{
name: "returns 200 status if errors are observable",
statusCode: http.StatusOK,
args: []Option{
WithObserver("observableService", CheckerFunc(func(ctx context.Context) error {
return fmt.Errorf("i fail but it is okay")
})),
},
response: response{
Status: http.StatusText(http.StatusOK),
Errors: map[string]string{
"observableService": "i fail but it is okay",
},
},
},
{
name: "returns 503 status if errors with observable fails",
statusCode: http.StatusServiceUnavailable,
args: []Option{
WithObserver("database", CheckerFunc(func(ctx context.Context) error {
return fmt.Errorf("connection to db timed out")
})),
WithChecker("testService", CheckerFunc(func(ctx context.Context) error {
return fmt.Errorf("connection refused")
})),
},
response: response{
Status: http.StatusText(http.StatusServiceUnavailable),
Errors: map[string]string{
"database": "connection to db timed out",
"testService": "connection refused",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://localhost/health", nil)
if err != nil {
t.Errorf("Failed to create request.")
}
HandlerFunc(tt.args...)(res, req)
if res.Code != tt.statusCode {
t.Errorf("expected code %d, got %d", tt.statusCode, res.Code)
}
var respBody response
if err := json.NewDecoder(res.Body).Decode(&respBody); err != nil {
t.Fatal("failed to parse the body")
}
if !reflect.DeepEqual(respBody, tt.response) {
t.Errorf("NewHandlerFunc() = %v, want %v", respBody, tt.response)
}
})
}
}