forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
288 lines (257 loc) · 7.67 KB
/
storage_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package dnscache
import (
"net"
"reflect"
"testing"
"time"
"github.com/miekg/dns"
"github.com/TykTechnologies/tyk/test"
)
var (
expiration = 10
checkInterval = 5
)
const (
host = "orig-host.com."
singleRecordHost = "single.orig-host.com."
host2 = "orig-host2.com."
host3 = "some.orig-host3.com"
host4 = "some.orig-host4.com"
hostErrorable = "unknown.orig-host.com."
wsHost = "ws.orig-host.com."
)
var (
etcHostsMap = map[string][]string{
singleRecordHost: {"10.0.2.10"},
host: {"127.0.0.1", "127.0.0.2", "127.0.0.3"},
host2: {"10.0.2.0", "10.0.2.1", "10.0.2.2"},
host3: {"10.0.2.15", "10.0.2.16"},
host4: {"10.0.2.11", "10.0.2.10"},
wsHost: {"127.0.0.1", "127.0.0.1"},
}
etcHostsErrorMap = map[string]int{
hostErrorable: dns.RcodeServerFailure,
}
)
type configTestStorageFetchItem struct {
*testing.T
etcHostsMap map[string][]string
etcHostsErrorsMap map[string]int
}
func setupTestStorageFetchItem(cfg *configTestStorageFetchItem) func() {
handle, err := test.InitDNSMock(cfg.etcHostsMap, cfg.etcHostsErrorsMap)
if err != nil {
cfg.T.Error(err.Error())
}
return func() {
if err := handle.ShutdownDnsMock(); err != nil {
cfg.T.Error(err.Error())
}
}
}
func TestStorageFetchItem(t *testing.T) {
dnsCache := NewDnsCacheStorage(time.Duration(expiration)*time.Second, time.Duration(checkInterval)*time.Second)
tearDownTestStorageFetchItem := setupTestStorageFetchItem(&configTestStorageFetchItem{t, etcHostsMap, etcHostsErrorMap})
defer func() {
tearDownTestStorageFetchItem()
dnsCache.Clear()
dnsCache = nil
}()
cases := []struct {
name string
Host string
ExpectedIPs []string
expectedErrorType reflect.Type
shouldExistInCache bool
shouldBeAddedToCache bool
}{
{
"Should cache first dns record first fetch",
host, etcHostsMap[host],
nil, false, true,
},
{
"Should cache second dns record first fetch",
host2, etcHostsMap[host2],
nil, false, true,
},
{
"Should populate from cache first dns record second fetch",
host, etcHostsMap[host],
nil, true, false,
},
{
"Should populate from cache first dns record third fetch",
host, etcHostsMap[host],
nil, true, false,
},
{
"Should populate from cache second dns record second fetch",
host2, etcHostsMap[host2],
nil, true, false,
},
{
"Shouldn't cache dns record fetch in case error",
hostErrorable, nil,
reflect.TypeOf(&net.DNSError{}), false, false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := dnsCache.FetchItem(tc.Host)
if tc.expectedErrorType != nil {
if err == nil || tc.expectedErrorType != reflect.TypeOf(err) {
t.Fatalf("wanted FetchItem error type %v, got %v. Error=%#v", tc.expectedErrorType, reflect.TypeOf(err), err)
}
if _, ok := dnsCache.Get(tc.Host); got != nil || ok {
t.Fatalf("wanted FetchItem error to omit cache. got %#v, ok=%t", got, ok)
}
return
}
if err != nil || !reflect.DeepEqual(got, tc.ExpectedIPs) {
t.Fatalf("wanted ips %q, got %q. Error: %v", tc.ExpectedIPs, got, err)
}
if tc.shouldExistInCache || tc.shouldBeAddedToCache {
record, ok := dnsCache.Get(tc.Host)
if !ok {
t.Fatalf("Host addresses weren't found in cache; host %q", tc.Host)
}
if !test.IsDnsRecordsAddrsEqualsTo(record.Addrs, tc.ExpectedIPs) {
t.Fatalf("wanted cached ips %v, got record %v", tc.ExpectedIPs, record)
}
} else {
if got, ok := dnsCache.Get(tc.Host); !test.IsDnsRecordsAddrsEqualsTo(got.Addrs, nil) || ok {
t.Fatalf("wanted FetchItem to omit write to cache. got %#v, ok=%t", got, ok)
}
}
})
}
}
func TestStorageRecordExpiration(t *testing.T) {
var (
expiration = 2000
checkInterval = 1500
)
type testRecord struct {
dns string
addrs []string
addDelay time.Duration
}
cases := []struct {
name string
records []testRecord
sleepBeforeCleanup time.Duration
notExpiredAfterDelay []testRecord
checkInterval int
}{
{
"Shouldn't remove dns record when ttl/expiration < 1",
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
},
time.Duration(checkInterval+10) * time.Millisecond,
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
},
checkInterval,
},
{
"Should remove single dns record after expiration",
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
},
time.Duration(expiration+10) * time.Millisecond,
[]testRecord{},
checkInterval,
},
{
"Should leave as expired dns records if check_interval=-1",
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
{dns: host2, addrs: etcHostsMap[host2]},
{dns: wsHost, addrs: etcHostsMap[wsHost]},
},
time.Duration(checkInterval+10) * time.Millisecond,
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
{dns: host2, addrs: etcHostsMap[host2]},
{dns: wsHost, addrs: etcHostsMap[wsHost]},
},
-1,
},
{
"Should remove all(>1) dns records after expiration",
[]testRecord{
{dns: host2, addrs: etcHostsMap[host]},
{dns: host2, addrs: etcHostsMap[host2]},
{dns: host2, addrs: etcHostsMap[wsHost]},
},
time.Duration(expiration+10) * time.Millisecond,
[]testRecord{},
checkInterval,
},
{
"Should remove only expired record after expiration",
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
{dns: host2, addrs: etcHostsMap[host2], addDelay: 500 * time.Millisecond},
{dns: wsHost, addrs: etcHostsMap[wsHost]},
},
time.Duration(expiration-400) * time.Millisecond,
[]testRecord{
{dns: host2, addrs: etcHostsMap[host2]},
{dns: wsHost, addrs: etcHostsMap[wsHost]},
},
checkInterval,
},
{
"Should remove only expired records after expiration",
[]testRecord{
{dns: host, addrs: etcHostsMap[host]},
{dns: host2, addrs: etcHostsMap[host2], addDelay: 250 * time.Millisecond},
{dns: host3, addrs: etcHostsMap[host3], addDelay: 500 * time.Millisecond},
{dns: host4, addrs: etcHostsMap[host4], addDelay: 100 * time.Millisecond},
{dns: wsHost, addrs: etcHostsMap[wsHost]},
},
time.Duration(expiration-350) * time.Millisecond,
[]testRecord{
{dns: host3, addrs: etcHostsMap[host3]},
{dns: host4, addrs: etcHostsMap[host4]},
{dns: wsHost, addrs: etcHostsMap[wsHost]},
},
checkInterval,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
dnsCache := NewDnsCacheStorage(time.Duration(expiration)*time.Millisecond, time.Duration(tc.checkInterval)*time.Millisecond)
for _, r := range tc.records {
if r.addDelay > 0 {
time.Sleep(r.addDelay)
}
dnsCache.Set(r.dns, r.addrs)
}
if tc.sleepBeforeCleanup > 0 {
time.Sleep(tc.sleepBeforeCleanup)
}
if lenNonExpired, lenCurrent := len(tc.notExpiredAfterDelay), len(dnsCache.Items(tc.checkInterval == -1)); lenNonExpired != lenCurrent {
t.Fatalf("wanted len(nonExpiredItems) %d, got %d. items=%+v", lenNonExpired, lenCurrent, dnsCache.Items(tc.checkInterval == -1))
}
if tc.checkInterval == -1 {
for _, r := range tc.records {
if item, ok := dnsCache.Items(true)[r.dns]; !ok || !test.IsDnsRecordsAddrsEqualsTo(item.Addrs, r.addrs) {
t.Fatalf("wanted expired cached ips %v, got item %#v. items=%+v, ok=%t", r.addrs, item, dnsCache.Items(true), ok)
}
}
} else {
for _, r := range tc.notExpiredAfterDelay {
if item, ok := dnsCache.Get(r.dns); !ok || !test.IsDnsRecordsAddrsEqualsTo(item.Addrs, r.addrs) {
t.Fatalf("wanted cached ips %v, got item %#v. items=%+v, ok=%t", r.addrs, item, dnsCache.Items(false), ok)
}
}
}
dnsCache.Clear()
dnsCache = nil
})
}
}