-
Notifications
You must be signed in to change notification settings - Fork 4
/
health_test.go
170 lines (121 loc) · 3.53 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
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 health
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
type mockChecker struct {
err error
sleepTime time.Duration
}
func (mc *mockChecker) Check() error {
if mc.sleepTime > 0 {
time.Sleep(mc.sleepTime)
}
return mc.err
}
func TestGetStatus(t *testing.T) {
h := New("service_test", Options{})
s := h.GetStatus()
if s.Service != "service_test" {
t.Errorf("Expect service_test and got %s", s.Service)
}
}
func TestCheckers(t *testing.T) {
h := New("service_test", Options{})
h.RegisterChecker("checker1", &mockChecker{})
s := h.GetStatus()
if s.HealthCheckers["checker1"].Status != "CHECKED" {
t.Errorf("Expect CHECKED and got %s", s.HealthCheckers["checker1"].Status)
}
}
func TestCheckersError(t *testing.T) {
h := New("service_test", Options{})
h.RegisterChecker("checker1", &mockChecker{err: errors.New("Service unreachable")})
s := h.GetStatus()
if s.HealthCheckers["checker1"].Error.Error() != "Service unreachable" {
t.Errorf("Expect Service unreachable and got %s", s.HealthCheckers["checker1"].Error)
}
}
func TestMultipleCheckers(t *testing.T) {
results := []string{"CHECKED", "CHECKED", "TIMEOUT", "CHECKED"}
h := New("service_test", Options{CheckersTimeout: time.Second * 1})
h.RegisterChecker("checker1", &mockChecker{})
h.RegisterChecker("checker2", &mockChecker{sleepTime: time.Millisecond * 300})
h.RegisterChecker("checker3", &mockChecker{sleepTime: time.Second * 5})
h.RegisterChecker("checker4", &mockChecker{err: errors.New("Error connections to db"), sleepTime: time.Millisecond * 500})
s := h.GetStatus()
for i, expected := range results {
cs := s.HealthCheckers[fmt.Sprintf("checker%d", i+1)].Status
if cs != expected {
t.Errorf("Expect checker%d to be %s and got %s", i+1, expected, cs)
}
}
}
func TestServeHTTP(t *testing.T) {
h := New("service_test", Options{})
req, err := http.NewRequest("GET", "localhost/health", nil)
if err != nil {
t.Error(err)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Result().StatusCode != 200 {
t.Errorf("Expect 200 and got %d", rec.Result().StatusCode)
}
s := Status{}
bytes, err := ioutil.ReadAll(rec.Body)
if err != nil {
t.Error(err)
}
err = json.Unmarshal(bytes, &s)
if err != nil {
t.Error(err)
}
if s.Service != "service_test" {
t.Errorf("Expect service_test and got %s", s.Service)
}
}
func TestServeHTTPNotFound(t *testing.T) {
h := New("service_test", Options{})
req, err := http.NewRequest("PUT", "localhost/health", nil)
if err != nil {
t.Error(err)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Result().StatusCode != 404 {
t.Errorf("Expect 404 and got %d", rec.Result().StatusCode)
}
}
func TestShutdown(t *testing.T) {
h := New("service_test", Options{})
h.Shutdown()
req, err := http.NewRequest("GET", "localhost/health", nil)
if err != nil {
t.Error(err)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Result().StatusCode != http.StatusServiceUnavailable {
t.Errorf("Expect %d and got %d", http.StatusServiceUnavailable, rec.Result().StatusCode)
}
}
func TestServeHTTPCheckerFailure(t *testing.T) {
h := New("service_test", Options{})
h.RegisterChecker("checker1", &mockChecker{err: errors.New("Service unreachable")})
req, err := http.NewRequest("GET", "localhost/health", nil)
if err != nil {
t.Error(err)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Result().StatusCode != 503 {
t.Errorf("Expect 503 and got %d", rec.Result().StatusCode)
}
}