-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
128 lines (120 loc) · 4.57 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/pkg/errors"
)
func handlerBuilder() *healthHandler {
var opsReport = make(chan *OpsStatus, 10)
defaultReport := OpsStatus{isSuccess: true, timestamp: time.Now()}
hhandler := &healthHandler{opsStatus: opsReport, cacheExpirationTime: REFRESHINTERVAL, lastReport: &defaultReport}
return hhandler
}
func TestBootstrapHealthCheck_should_return_initial_cache_when_no_report(t *testing.T) {
hhandler := handlerBuilder()
r, _ := http.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
if w.Code != 200 {
t.Errorf("wrong code returned")
}
}
func TestBootstrapHealthCheck_should_return_500_when_cache_has_expired(t *testing.T) {
hhandler := handlerBuilder()
r, _ := http.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
time.Sleep(REFRESHINTERVAL) //sleep interval so the cache expires
r, _ = http.NewRequest("GET", "/health", nil)
w = httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
if w.Code != 500 {
t.Errorf("Should return 500 when cache has expired, got: %d, expected %d", w.Code, 500)
}
}
func TestBootstrapHealthCheck_should_not_return_cache_when_reporting_ops(t *testing.T) {
hhandler := handlerBuilder()
statusError := OpsStatus{isSuccess: false, timestamp: time.Now(), error: errors.New("This one failed")}
hhandler.opsStatus <- &statusError
r, _ := http.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
if w.Code != 500 {
t.Errorf("Should return 500 from the published status error, got: %d, expected %d", w.Code, 500)
}
body, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Errorf("Should return non empty body. Something went wrong: %s", err)
}
expectedBody := fmt.Sprintf("Unhealthy: %s !\n", statusError.error)
if string(body) != expectedBody {
t.Errorf("Body is wrong, got: %s, expected: %s", string(body), expectedBody)
}
}
func TestBootstrapHealthCheck_should_return_last_report_as_cache_when_no_report(t *testing.T) {
hhandler := handlerBuilder()
statusError := OpsStatus{isSuccess: false, timestamp: time.Now(), error: errors.New("This one failed")}
hhandler.opsStatus <- &statusError
r, _ := http.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
if w.Code != 500 {
t.Errorf("Should return 500 from the published status error, got: %d, expected %d", w.Code, 500)
}
body, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Errorf("Should return non empty body. Something went wrong: %s", err)
}
expectedBody := fmt.Sprintf("Unhealthy: %s !\n", statusError.error)
if string(body) != expectedBody {
t.Errorf("Body is wrong, got: %s, expected: %s", string(body), expectedBody)
}
// call again without making report should give us the last response again
r, _ = http.NewRequest("GET", "/health", nil)
w = httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
body, err = ioutil.ReadAll(w.Body)
if err != nil {
t.Errorf("Should return 500 when cache has expired, got: %d, expected %d", w.Code, 500)
}
expectedBody = fmt.Sprintf("Unhealthy: %s !\n", statusError.error)
if string(body) != expectedBody {
t.Errorf("Body is wrong, got: %s, expected: %s", string(body), expectedBody)
}
}
func TestBootstrapHealthCheck_should_not_return_cache_after_report_and_cache_expiration(t *testing.T) {
hhandler := handlerBuilder()
statusError := OpsStatus{isSuccess: false, timestamp: time.Now(), error: errors.New("This one failed")}
hhandler.opsStatus <- &statusError
r, _ := http.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
if w.Code != 500 {
t.Errorf("Should return 500 from the published status error, got: %d, expected %d", w.Code, 500)
}
body, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Errorf("Should return non empty body. Something went wrong: %s", err)
}
expectedBody := fmt.Sprintf("Unhealthy: %s !\n", statusError.error)
if string(body) != expectedBody {
t.Errorf("Body is wrong, got: %s, expected: %s", string(body), expectedBody)
}
time.Sleep(REFRESHINTERVAL)
// call again without making report should gives us an error since cache expired
r, _ = http.NewRequest("GET", "/health", nil)
w = httptest.NewRecorder()
hhandler.ServeHTTP(w, r)
body, err = ioutil.ReadAll(w.Body)
if err != nil {
t.Errorf("Should return 500 when cache has expired, got: %d, expected %d", w.Code, 500)
}
expectedBody = fmt.Sprintf("Unhealthy: %s !\n", statusError.error)
if string(body) == expectedBody {
t.Errorf("Body is wrong, got: %s, expected: %s", string(body), expectedBody)
}
}