-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_test.go
101 lines (76 loc) · 2.14 KB
/
checker_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
package healthz
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type sleep struct {
duration time.Duration
}
func newSleep(dur time.Duration) sleep {
return sleep{
duration: dur,
}
}
func (c sleep) Check(ctx context.Context) *Response {
time.Sleep(c.duration)
return &Response{}
}
func TestChecker(t *testing.T) {
c := NewChecker(WithTimeout(100 * time.Millisecond))
err := c.AddCheck("test", testCheck{})
require.NoError(t, err)
req := httptest.NewRequest("GET", "http://127.0.0.1/healthz", nil)
w := httptest.NewRecorder()
c.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
m, err := parseResponse(w.Body)
require.NoError(t, err)
v, ok := m["test"]
require.True(t, ok)
require.Len(t, v.Metadata, 1)
require.Equal(t, "", v.ErrorMessage)
// Add another check, one designed to fail
require.NoError(t, c.AddCheck("sleep", newSleep(200*time.Millisecond)))
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "http://127.0.0.1/healthz", nil)
w = httptest.NewRecorder()
c.ServeHTTP(w, req)
require.Equal(t, http.StatusInternalServerError, w.Code)
m, err = parseResponse(w.Body)
require.NoError(t, err)
v, ok = m["sleep"]
require.True(t, ok)
require.Equal(t, ErrorCheckTimedOut.Error(), v.ErrorMessage)
}
func TestCheckerEmpty(t *testing.T) {
c := NewChecker(WithTimeout(100 * time.Millisecond))
req := httptest.NewRequest("GET", "http://127.0.0.1/healthz", nil)
w := httptest.NewRecorder()
c.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
}
func TestCheckerUnknownCheck(t *testing.T) {
c := NewChecker(WithTimeout(100 * time.Millisecond))
require.NoError(t, c.AddCheck("test", testCheck{}))
req := httptest.NewRequest("GET", "http://127.0.0.1/healthz/foo", nil)
w := httptest.NewRecorder()
c.ServeHTTP(w, req)
require.Equal(t, http.StatusNotFound, w.Code)
}
func parseResponse(r io.Reader) (map[string]Response, error) {
bytes, err := io.ReadAll(r)
if err != nil {
return nil, err
}
m := make(map[string]Response)
if err = json.Unmarshal(bytes, &m); err != nil {
return nil, err
}
return m, nil
}