-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhealth_test.go
127 lines (104 loc) · 3.48 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
package atomicasset
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_GetHealth(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.String() == "/health" {
payload := `{
"success":true,
"data":{
"version":"1.0.0",
"postgres":{
"status":"OK",
"readers":[
{
"block_num":"167836036"
},
{
"block_num":"167836034"
}
]
},
"redis":{
"status":"OK"
},
"chain":{
"status":"OK",
"head_block":167836035,
"head_time":1645374771500
}
},
"query_time":1645374772067
}`
res.Header().Add("Content-type", "application/json; charset=utf-8")
_, err := res.Write([]byte(payload))
assert.NoError(t, err)
}
}))
client := New(srv.URL)
h, err := client.GetHealth()
require.NoError(t, err)
assert.Equal(t, 200, h.HTTPStatusCode)
assert.True(t, h.Success)
assert.Equal(t, time.Date(2022, time.February, 20, 16, 32, 52, int(time.Millisecond)*67, time.UTC), h.QueryTime.Time())
// Data
assert.Equal(t, "1.0.0", h.Data.Version)
// Postgres
assert.Equal(t, "OK", h.Data.Postgres.Status)
// Redis
assert.Equal(t, "OK", h.Data.Redis.Status)
// Chain
assert.Equal(t, "OK", h.Data.Chain.Status)
assert.Equal(t, int64(167836035), h.Data.Chain.HeadBlock)
assert.Equal(t, time.Date(2022, time.February, 20, 16, 32, 51, int(time.Millisecond)*500, time.UTC), h.Data.Chain.HeadTime.Time())
}
func TestClient_GetHealthFailed(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.String() == "/health" {
payload := `{
"success":true,
"data":{
"version":"1.0.0",
"postgres":{
"status":"ERROR",
"readers":[]
},
"redis":{
"status":"ERROR"
},
"chain":{
"status":"ERROR",
"head_block":0,
"head_time":0
}
},
"query_time":1645374772067
}`
res.Header().Add("Content-type", "application/json")
_, err := res.Write([]byte(payload))
assert.NoError(t, err)
}
}))
client := New(srv.URL)
h, err := client.GetHealth()
require.NoError(t, err)
assert.Equal(t, 200, h.HTTPStatusCode)
assert.True(t, h.Success)
assert.Equal(t, time.Date(2022, time.February, 20, 16, 32, 52, int(time.Millisecond)*67, time.UTC), h.QueryTime.Time())
// Data
assert.Equal(t, "1.0.0", h.Data.Version)
// Postgres
assert.Equal(t, "ERROR", h.Data.Postgres.Status)
// Redis
assert.Equal(t, "ERROR", h.Data.Redis.Status)
// Chain
assert.Equal(t, "ERROR", h.Data.Chain.Status)
assert.Equal(t, int64(0), h.Data.Chain.HeadBlock)
assert.Equal(t, time.Unix(0, 0).UTC(), h.Data.Chain.HeadTime.Time())
}