forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_checker_test.go
216 lines (183 loc) · 5.12 KB
/
host_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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bytes"
"net/http/httptest"
"net/url"
"sync"
"testing"
"text/template"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/storage"
)
const sampleUptimeTestAPI = `{
"api_id": "test",
"use_keyless": true,
"version_data": {
"not_versioned": true,
"versions": {
"v1": {"name": "v1"}
}
},
"uptime_tests": {
"check_list": [
{
"url": "{{.Host1}}/get",
"method": "GET"
},
{
"url": "{{.Host2}}/get",
"method": "GET"
}
]
},
"proxy": {
"listen_path": "/",
"enable_load_balancing": true,
"check_host_against_uptime_tests": true,
"target_list": [
"{{.Host1}}",
"{{.Host2}}"
]
}
}`
type testEventHandler struct {
cb func(config.EventMessage)
}
func (w *testEventHandler) Init(handlerConf interface{}) error {
return nil
}
func (w *testEventHandler) HandleEvent(em config.EventMessage) {
w.cb(em)
}
func TestHostChecker(t *testing.T) {
specTmpl := template.Must(template.New("spec").Parse(sampleUptimeTestAPI))
tmplData := struct {
Host1, Host2 string
}{
testHttpAny,
testHttpFailureAny,
}
specBuf := &bytes.Buffer{}
specTmpl.ExecuteTemplate(specBuf, specTmpl.Name(), &tmplData)
spec := createDefinitionFromString(specBuf.String())
// From api_loader.go#processSpec
sl := apidef.NewHostListFromList(spec.Proxy.Targets)
spec.Proxy.StructuredTargetList = sl
var eventWG sync.WaitGroup
// Should receive one HostDown event
eventWG.Add(1)
cb := func(em config.EventMessage) {
eventWG.Done()
}
spec.EventPaths = map[apidef.TykEvent][]config.TykEventHandler{
"HostDown": {&testEventHandler{cb}},
}
apisMu.Lock()
apisByID = map[string]*APISpec{spec.APIID: spec}
apisMu.Unlock()
GlobalHostChecker.checkerMu.Lock()
GlobalHostChecker.checker.sampleTriggerLimit = 1
GlobalHostChecker.checkerMu.Unlock()
defer func() {
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
GlobalHostChecker.checkerMu.Lock()
GlobalHostChecker.checker.sampleTriggerLimit = defaultSampletTriggerLimit
GlobalHostChecker.checkerMu.Unlock()
}()
SetCheckerHostList()
GlobalHostChecker.checkerMu.Lock()
if len(GlobalHostChecker.currentHostList) != 2 {
t.Error("Should update hosts manager check list", GlobalHostChecker.currentHostList)
}
if len(GlobalHostChecker.checker.newList) != 2 {
t.Error("Should update host checker check list")
}
GlobalHostChecker.checkerMu.Unlock()
hostCheckTicker <- struct{}{}
eventWG.Wait()
if GlobalHostChecker.HostDown(testHttpAny) {
t.Error("Should not mark as down")
}
if !GlobalHostChecker.HostDown(testHttpFailureAny) {
t.Error("Should mark as down")
}
// Test it many times concurrently, to simulate concurrent and
// parallel requests to the API. This will catch bugs in those
// scenarios, like data races.
var targetWG sync.WaitGroup
for i := 0; i < 10; i++ {
targetWG.Add(1)
go func() {
host, err := nextTarget(spec.Proxy.StructuredTargetList, spec)
if err != nil {
t.Error("Should return nil error, got", err)
}
if host != testHttpAny {
t.Error("Should return only active host, got", host)
}
targetWG.Done()
}()
}
targetWG.Wait()
GlobalHostChecker.checkerMu.Lock()
if GlobalHostChecker.checker.checkTimeout != defaultTimeout {
t.Error("Should set defaults", GlobalHostChecker.checker.checkTimeout)
}
redisStore := GlobalHostChecker.store.(storage.RedisCluster)
if ttl, _ := redisStore.GetKeyTTL(PoolerHostSentinelKeyPrefix + testHttpFailure); int(ttl) != GlobalHostChecker.checker.checkTimeout+1 {
t.Error("HostDown expiration key should be checkTimeout + 1", ttl)
}
GlobalHostChecker.checkerMu.Unlock()
}
func TestReverseProxyAllDown(t *testing.T) {
specTmpl := template.Must(template.New("spec").Parse(sampleUptimeTestAPI))
tmplData := struct {
Host1, Host2 string
}{
testHttpFailureAny,
testHttpFailureAny,
}
specBuf := &bytes.Buffer{}
specTmpl.ExecuteTemplate(specBuf, specTmpl.Name(), &tmplData)
spec := createDefinitionFromString(specBuf.String())
// From api_loader.go#processSpec
sl := apidef.NewHostListFromList(spec.Proxy.Targets)
spec.Proxy.StructuredTargetList = sl
var eventWG sync.WaitGroup
// Should receive one HostDown event
eventWG.Add(1)
cb := func(em config.EventMessage) {
eventWG.Done()
}
spec.EventPaths = map[apidef.TykEvent][]config.TykEventHandler{
"HostDown": {&testEventHandler{cb}},
}
apisMu.Lock()
apisByID = map[string]*APISpec{spec.APIID: spec}
apisMu.Unlock()
GlobalHostChecker.checkerMu.Lock()
GlobalHostChecker.checker.sampleTriggerLimit = 1
GlobalHostChecker.checkerMu.Unlock()
defer func() {
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
GlobalHostChecker.checkerMu.Lock()
GlobalHostChecker.checker.sampleTriggerLimit = defaultSampletTriggerLimit
GlobalHostChecker.checkerMu.Unlock()
}()
SetCheckerHostList()
hostCheckTicker <- struct{}{}
eventWG.Wait()
remote, _ := url.Parse(testHttpAny)
proxy := TykNewSingleHostReverseProxy(remote, spec)
req := testReq(t, "GET", "/", nil)
rec := httptest.NewRecorder()
proxy.ServeHTTP(rec, req)
if rec.Code != 503 {
t.Fatalf("wanted code to be 503, was %d", rec.Code)
}
}