-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhealthz_test.go
80 lines (61 loc) · 1.7 KB
/
healthz_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
package healthz
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestHealthz(t *testing.T) {
s := httptest.NewServer(Handler())
defer s.Close()
// it should get the health status
status, code, err := get(s.URL)
require.NoError(t, err)
require.Equal(t, http.StatusOK, code)
require.Len(t, status.Metadata, 0)
require.Len(t, status.Failures, 0)
// it should set a metadata value
Set("version", "1.0.0")
status, code, err = get(s.URL)
require.NoError(t, err)
require.Equal(t, http.StatusOK, code)
require.Equal(t, "1.0.0", status.Metadata["version"])
// it should delete a metadata value
Delete("version")
status, code, err = get(s.URL)
require.NoError(t, err)
require.Equal(t, http.StatusOK, code)
require.Nil(t, status.Metadata["version"])
// it register a check that suceedes the first time is check but fails afterwards.
Register("a_check", 3*time.Second, func() CheckFunc {
var err error
return func() error {
if err == nil {
err = errors.New("bad thing happened")
return nil
}
return err
}
}())
status, code, err = get(s.URL)
require.NoError(t, err)
require.Equal(t, http.StatusOK, code)
require.Empty(t, status.Failures["a_check"])
time.Sleep(3 * time.Second)
status, code, err = get(s.URL)
require.NoError(t, err)
require.Equal(t, http.StatusServiceUnavailable, code)
require.Equal(t, "bad thing happened", status.Failures["a_check"])
}
func get(url string) (*Status, int, error) {
res, err := http.Get(url)
if err != nil {
return nil, 0, err
}
defer res.Body.Close()
status := new(Status)
return status, res.StatusCode, json.NewDecoder(res.Body).Decode(status)
}