forked from TykTechnologies/tyk-pump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
378 lines (330 loc) · 9.62 KB
/
main_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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk-pump/pumps"
"github.com/stretchr/testify/assert"
)
type MockedPump struct {
CounterRequest int
TurnedOff bool
pumps.CommonPumpConfig
}
func (p *MockedPump) GetName() string {
return "Mocked Pump"
}
func (p *MockedPump) New() pumps.Pump {
return &MockedPump{}
}
func (p *MockedPump) Init(config interface{}) error {
return nil
}
func (p *MockedPump) WriteData(ctx context.Context, keys []interface{}) error {
for range keys {
p.CounterRequest++
}
return nil
}
func (p *MockedPump) Shutdown() error {
p.TurnedOff = true
return nil
}
func TestFilterData(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetFilters(
analytics.AnalyticsFilters{
APIIDs: []string{"api123"},
},
)
keys := make([]interface{}, 3)
keys[0] = analytics.AnalyticsRecord{APIID: "api111"}
keys[1] = analytics.AnalyticsRecord{APIID: "api123"}
keys[2] = analytics.AnalyticsRecord{APIID: "api321"}
filteredKeys := filterData(mockedPump, keys)
if len(keys) == len(filteredKeys) {
t.Fatal("keys and filtered keys have the same lenght")
}
}
// TestTrimData check the correct functionality of max_record_size
func TestTrimData(t *testing.T) {
loremIpsum := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
tcs := []struct {
testName string
maxRecordsSize int
expectedOutput int
}{
{
testName: "not set",
maxRecordsSize: 0,
expectedOutput: len(loremIpsum),
},
{
testName: "set smaller",
maxRecordsSize: 5,
expectedOutput: 5,
},
{
testName: "set bigger",
maxRecordsSize: len(loremIpsum) + 1,
expectedOutput: len(loremIpsum),
},
}
for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
mockedPump := &MockedPump{}
keys := make([]interface{}, 1)
keys[0] = analytics.AnalyticsRecord{
APIID: "api1",
RawResponse: loremIpsum,
RawRequest: loremIpsum,
}
t.Run("global config", func(t *testing.T) {
// first we test with global config
SystemConfig.MaxRecordSize = tc.maxRecordsSize
defer func() {
SystemConfig.MaxRecordSize = 0
}()
filteredKeys := filterData(mockedPump, keys)
decoded, ok := filteredKeys[0].(analytics.AnalyticsRecord)
assert.True(t, ok)
assert.Equal(t, len(decoded.RawRequest), tc.expectedOutput)
assert.Equal(t, len(decoded.RawResponse), tc.expectedOutput)
})
t.Run("pump config", func(t *testing.T) {
// we try setting pump directly
mockedPump.SetMaxRecordSize(tc.maxRecordsSize)
filteredKeys := filterData(mockedPump, keys)
decoded, ok := filteredKeys[0].(analytics.AnalyticsRecord)
assert.True(t, ok)
assert.Equal(t, len(decoded.RawRequest), tc.expectedOutput)
assert.Equal(t, len(decoded.RawResponse), tc.expectedOutput)
})
})
}
}
func TestOmitDetailsFilterData(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetOmitDetailedRecording(true)
keys := make([]interface{}, 1)
keys[0] = analytics.AnalyticsRecord{APIID: "api111", RawResponse: "test", RawRequest: "test"}
filteredKeys := filterData(mockedPump, keys)
if len(filteredKeys) == 0 {
t.Fatal("it shouldn't have filtered a key.")
}
record := filteredKeys[0].(analytics.AnalyticsRecord)
if record.RawRequest != "" || record.RawResponse != "" {
t.Fatal("raw_request and raw_response should be empty")
}
}
func TestWriteDataWithFilters(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetFilters(
analytics.AnalyticsFilters{
SkippedResponseCodes: []int{200},
},
)
mockedPump2 := &MockedPump{}
mockedPump3 := &MockedPump{}
mockedPump3.SetFilters(
analytics.AnalyticsFilters{
ResponseCodes: []int{200},
},
)
mockedPump4 := &MockedPump{}
mockedPump4.SetFilters(
analytics.AnalyticsFilters{
APIIDs: []string{"api123"},
OrgsIDs: []string{"123"},
ResponseCodes: []int{200},
},
)
mockedPump5 := &MockedPump{}
mockedPump5.SetFilters(
analytics.AnalyticsFilters{
APIIDs: []string{"api111"},
OrgsIDs: []string{"123"},
ResponseCodes: []int{200},
},
)
Pumps = []pumps.Pump{mockedPump, mockedPump2, mockedPump3, mockedPump4, mockedPump5}
assert.Len(t, Pumps, 5)
keys := make([]interface{}, 6)
keys[0] = analytics.AnalyticsRecord{APIID: "api111", ResponseCode: 400, OrgID: "321"}
keys[1] = analytics.AnalyticsRecord{APIID: "api123", ResponseCode: 200, OrgID: "123"}
keys[2] = analytics.AnalyticsRecord{APIID: "api123", ResponseCode: 500, OrgID: "123"}
keys[3] = analytics.AnalyticsRecord{APIID: "api123", ResponseCode: 200, OrgID: "321"}
keys[4] = analytics.AnalyticsRecord{APIID: "api111", ResponseCode: 404, OrgID: "321"}
keys[5] = analytics.AnalyticsRecord{APIID: "api111", ResponseCode: 500, OrgID: "321"}
job := instrument.NewJob("TestJob")
writeToPumps(keys, job, time.Now(), 2)
tcs := []struct {
testName string
mockedPump *MockedPump
expectedCounterRequest int
}{
{
testName: "skip_response_code 200",
mockedPump: Pumps[0].(*MockedPump),
expectedCounterRequest: 4,
},
{
testName: "no filter - all records",
mockedPump: Pumps[1].(*MockedPump),
expectedCounterRequest: 6,
},
{
testName: "response_codes 200",
mockedPump: Pumps[2].(*MockedPump),
expectedCounterRequest: 2,
},
{
testName: "api_ids api123 + org_ids 123 + responseCode 200 filters",
mockedPump: Pumps[3].(*MockedPump),
expectedCounterRequest: 1,
},
{
testName: "api_ids api111 + org_ids 123 + responseCode 200 filters",
mockedPump: Pumps[4].(*MockedPump),
expectedCounterRequest: 0,
},
}
for _, tc := range tcs {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expectedCounterRequest, tc.mockedPump.CounterRequest)
assert.Len(t, keys, 6)
})
}
}
func TestShutdown(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetFilters(
analytics.AnalyticsFilters{
APIIDs: []string{"api123"},
},
)
Pumps = []pumps.Pump{mockedPump}
wg := sync.WaitGroup{}
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
if checkShutdown(ctx, &wg) {
return
}
}
}()
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
termChan <- os.Interrupt
<-termChan
cancel()
wg.Wait()
mockedPump = Pumps[0].(*MockedPump)
if mockedPump.TurnedOff != true {
t.Fatal("MockedPump should have turned off")
}
}
func TestIgnoreFieldsFilterData(t *testing.T) {
keys := make([]interface{}, 1)
record := analytics.AnalyticsRecord{APIID: "api111", RawResponse: "test", RawRequest: "test", OrgID: "321", ResponseCode: 200, RequestTime: 123}
keys[0] = record
recordWithoutAPIID := record
recordWithoutAPIID.APIID = ""
recordWithoutAPIIDAndAPIName := record
recordWithoutAPIIDAndAPIName.APIID = ""
tcs := []struct {
expectedRecord analytics.AnalyticsRecord
testName string
ignoreFields []string
}{
{
testName: "ignore 1 field",
ignoreFields: []string{"api_id"},
expectedRecord: recordWithoutAPIID,
},
{
testName: "ignore 2 fields",
ignoreFields: []string{"api_id", "api_name"},
expectedRecord: recordWithoutAPIIDAndAPIName,
},
{
testName: "invalid field - log error must be shown",
ignoreFields: []string{"api_id", "api_name", "invalid_field"},
expectedRecord: recordWithoutAPIIDAndAPIName,
},
}
for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetIgnoreFields(tc.ignoreFields)
filteredKeys := filterData(mockedPump, keys)
for _, key := range filteredKeys {
record, ok := key.(analytics.AnalyticsRecord)
assert.True(t, ok)
assert.Equal(t, tc.expectedRecord, record)
}
})
}
}
func TestDecodedKey(t *testing.T) {
keys := make([]interface{}, 1)
record := analytics.AnalyticsRecord{APIID: "api111", RawResponse: "RGVjb2RlZFJlc3BvbnNl", RawRequest: "RGVjb2RlZFJlcXVlc3Q="}
keys[0] = record
tcs := []struct {
expectedRawResponse string
expectedRawRequest string
testName string
decodeResponse bool
decodeRequest bool
}{
{
testName: "Decode RESPONSE & REQUEST",
expectedRawResponse: "DecodedResponse",
expectedRawRequest: "DecodedRequest",
decodeResponse: true,
decodeRequest: true,
},
{
testName: "Decode RESPONSE",
expectedRawResponse: "DecodedResponse",
expectedRawRequest: "RGVjb2RlZFJlcXVlc3Q=",
decodeResponse: true,
decodeRequest: false,
},
{
testName: "Decode REQUEST",
expectedRawResponse: "RGVjb2RlZFJlc3BvbnNl",
expectedRawRequest: "DecodedRequest",
decodeResponse: false,
decodeRequest: true,
},
{
testName: "Decode NONE",
expectedRawResponse: "RGVjb2RlZFJlc3BvbnNl",
expectedRawRequest: "RGVjb2RlZFJlcXVlc3Q=",
decodeResponse: false,
decodeRequest: false,
},
}
for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
mockedPump := &MockedPump{}
mockedPump.SetDecodingRequest(tc.decodeRequest)
mockedPump.SetDecodingResponse(tc.decodeResponse)
filteredKeys := filterData(mockedPump, keys)
assert.Len(t, filteredKeys, 1)
record1, ok := filteredKeys[0].(analytics.AnalyticsRecord)
assert.True(t, ok)
assert.Equal(t, tc.expectedRawResponse, record1.RawResponse)
assert.Equal(t, tc.expectedRawRequest, record1.RawRequest)
})
}
}