-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck_test.go
98 lines (90 loc) · 2.11 KB
/
healthcheck_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
package healthcheck_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/keloran/go-healthcheck"
"github.com/stretchr/testify/assert"
)
func TestHealthCheck_Check(t *testing.T) {
tests := []struct {
request healthcheck.HealthCheck
expect healthcheck.Health
err error
}{
{
request: healthcheck.HealthCheck{
Name: "test1",
URL: "keloran.dev",
Dependencies: "",
},
expect: healthcheck.Health{
Name: "test1",
URL: "keloran.dev",
Status: healthcheck.HealthPass,
Dependencies: nil,
},
err: nil,
},
{
request: healthcheck.HealthCheck{
Name: "test2",
URL: "keloran.dev",
Dependencies: fmt.Sprintf(`{"dependencies":[{"name":"%s","url":"%s","ping":true}]}`, "test1", "keloran.dev"),
},
expect: healthcheck.Health{
Name: "test2",
URL: "keloran.dev",
Status: healthcheck.HealthPass,
Dependencies: []healthcheck.Health{
{
Name: "test1",
URL: "keloran.dev",
Status: "pass",
Dependencies: nil,
},
},
},
},
}
for _, test := range tests {
response, err := test.request.Check()
assert.Equal(t, test.err, err)
assert.Equal(t, test.expect, response)
}
}
func TestHTTP(t *testing.T) {
tests := []struct {
request healthcheck.HealthCheck
expect healthcheck.Health
}{
{
request: healthcheck.HealthCheck{
Name: "test1",
URL: "chewedfeed.com",
},
expect: healthcheck.Health{
Name: "test1",
Dependencies: nil,
Status: healthcheck.HealthPass,
},
},
}
for _, test := range tests {
os.Setenv("SERVICE_NAME", test.request.Name)
jsonRequest, _ := json.Marshal(test.request)
request, _ := http.NewRequest("GET", "/", bytes.NewBuffer(jsonRequest))
response := httptest.NewRecorder()
healthcheck.HTTP(response, request)
assert.Equal(t, 200, response.Code)
body, _ := io.ReadAll(response.Body)
healthy := healthcheck.Health{}
_ = json.Unmarshal(body, &healthy)
assert.Equal(t, test.expect, healthy)
}
}