-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_test.go
169 lines (149 loc) · 3.74 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Author: Adrián López (https://github.com/adrianlop)
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package health
import (
"encoding/json"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type tcp struct {
address string
}
func (s *tcp) Check() error {
conn, err := net.DialTimeout("tcp", s.address, 3*time.Second)
if err == nil {
conn.Close()
}
return err
}
func TestHealthSimple(t *testing.T) {
collector := NewCollector(-1)
googleDNS := &tcp{
address: "google.com:443",
}
rep1 := &Config{
Name: "GoogleDNS",
Reporter: googleDNS,
SoftFail: true,
}
// Assert that rep1 is not already added with same name
err := collector.AddReporter(rep1)
assert.Nil(t, err)
time.Sleep(20 * time.Second) // let's wait for time ticker to run
collector.mu.RLock()
assert.True(t, collector.globalHealth)
// assert global JSON msg status
healthMsg, _ := json.Marshal(collector.reportersData)
collector.mu.RUnlock()
assert.JSONEq(t, `{"GoogleDNS":"OK: Healthy"}`, string(healthMsg))
}
func TestHealthForceCheck(t *testing.T) {
// Do not use NewCollector here, since datarace would occur
// between maunal vs ticker run
collector := &Collector{
reporters: make(map[string]*Config),
reportersData: make(map[string]string),
globalHealth: true,
}
googleDNS := &tcp{
address: "google.com:443",
}
rep1 := &Config{
Name: "GoogleDNS",
Reporter: googleDNS,
SoftFail: true,
}
// Assert that rep1 is not already added with same name
err := collector.AddReporter(rep1)
assert.Nil(t, err)
// immediately run checks
collector.runChecks()
// assert globalHealth
assert.True(t, collector.globalHealth)
// assert global JSON msg status
healthMsg, _ := json.Marshal(collector.reportersData)
assert.JSONEq(t, `{"GoogleDNS":"OK: Healthy"}`, string(healthMsg))
// Assert that adding rep2 with same name as rep1 will throw err
rep2 := &Config{
Name: "GoogleDNS",
Reporter: googleDNS,
SoftFail: true,
}
err = collector.AddReporter(rep2)
assert.NotNil(t, err)
// Assert rep3 check fails
googleFakePort := &tcp{
address: "google.com:12345",
}
rep3 := &Config{
Name: "GoogleFakePort",
Reporter: googleFakePort,
SoftFail: false,
}
err = collector.AddReporter(rep3)
assert.Nil(t, err)
collector.runChecks()
assert.False(t, collector.globalHealth)
healthMsg, _ = json.Marshal(collector.reportersData)
assert.Contains(t, string(healthMsg), `"GoogleFakePort":"KO: dial tcp`)
// TODO: some more testcases
// testcases := []struct {
// caseName string
// expected interface{}
// }{
// {
// caseName: "testX",
// expected: nil,
// },
// }
// for _, tc := range testcases {
// assert stuff
// }
}
func TestHealthComposeRoutePath(t *testing.T) {
testcases := []struct {
label string
basePath string
routePath string
result string
}{
{
label: "without basepath",
routePath: "/healthcheck",
result: "/healthcheck",
},
{
label: "without basepath and no route slash",
routePath: "healthcheck",
result: "/healthcheck",
},
{
label: "with basepath and slash",
basePath: "/admin",
routePath: "/healthcheck",
result: "/admin/healthcheck",
},
{
label: "with basepath and no slash",
basePath: "admin",
routePath: "/healthcheck",
result: "/admin/healthcheck",
},
{
label: "with basepath and no slash and no route slash",
basePath: "admin",
routePath: "healthcheck",
result: "/admin/healthcheck",
},
}
for _, tc := range testcases {
t.Run(tc.label, func(t *testing.T) {
result := composeRoutePath(tc.basePath, tc.routePath)
assert.Equal(t, tc.result, result)
})
}
}