-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfeed_test.go
379 lines (345 loc) · 10.4 KB
/
feed_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
379
package sparkypmtatracking_test
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"testing"
"time"
"github.com/go-redis/redis"
spmta "github.com/tuck1s/sparkypmtatracking"
)
const mockAPIKey = "xyzzy"
const testAction = "c" // click
const testURL = "http://example.com/this/is/a/test"
const testUserAgent = "Some Lovely Browser User Agent String"
const testIPAddress = "12.34.56.78"
const testTime = 1 * time.Second // delay we have to wait for batches to show up in the logfile
const testSubaccountID = 3
const testMockBatchResponse = "mock test passed"
// Capture the usual log output into a memory buffer, for later verification
func captureLog() *bytes.Buffer {
var buf bytes.Buffer
log.SetOutput(&buf)
log.SetFlags(0) // disable date/time prefix as it makes hard to compare
return &buf
}
// Retrieve log output as a string
func retrieveLog(bptr *bytes.Buffer) string {
return (*bptr).String()
}
// Wait for processes to log something for a defined number of seconds, look for a number of occurrences of an expected string
func checkLog(t *testing.T, waitfor int, myLogp *bytes.Buffer, expected string, times int) {
res := ""
count := 0
for res == "" {
time.Sleep(1 * time.Second)
count++
if count > waitfor {
t.Errorf("Waited %v seconds and no response - exiting", waitfor)
break
}
res = retrieveLog(myLogp)
}
time.Sleep(testTime)
res = retrieveLog(myLogp)
t.Log(res)
if strings.Count(res, expected) != times {
t.Error(res)
}
}
// checkHeaders walks through the headers and checks against expected values
func checkHeaders(h http.Header, chk map[string]string) error {
for k, v := range chk {
if len(h[k]) != 1 {
s := fmt.Errorf("Header %s has length %d", k, len(h[k]))
return s
}
// Case-insensitive comparison
if strings.ToLower(h[k][0]) != strings.ToLower(v) {
s := fmt.Errorf("Header %s has value %s, was expecting value %s", k, h[k][0], v)
return s
}
}
return nil
}
// checkResponse compares the mock SparkPost request with what was expected
func checkResponse(r *http.Request) error {
if r.Method != "POST" {
return fmt.Errorf("Unexpected Method found: %s", r.Method)
}
if r.RequestURI != "/api/v1/ingest/events" {
return fmt.Errorf("Unexpected RequestURI: %s", r.RequestURI)
}
expectedHeaders := map[string]string{
"Authorization": mockAPIKey,
"Content-Encoding": "gzip",
"Content-Type": "application/x-ndjson",
"Accept-Encoding": "gzip",
}
if err := checkHeaders(r.Header, expectedHeaders); err != nil {
return err
}
// Pass the body through the Gzip reader
zr, err := gzip.NewReader(r.Body)
if err != nil {
return err
}
// Scan for newline-delimited content
s := bufio.NewScanner(zr)
for s.Scan() {
ln := s.Bytes()
// Decode back into struct
var event spmta.SparkPostEvent
if err := json.Unmarshal(ln, &event); err != nil {
return err
}
// Check contents - allow for some missing fields during tests
e := event.EventWrapper.EventGrouping
if e.IPAddress != testIPAddress {
return fmt.Errorf("Event has a suspect IPAddress %v", e.IPAddress)
}
if len(e.TimeStamp) < 10 || e.UserAgent != testUserAgent {
return fmt.Errorf("Event has a suspect UserAgent %v", e.UserAgent)
}
}
return nil
}
// Mock SparkPost endpoint
func ingestServer(w http.ResponseWriter, r *http.Request) {
s := checkResponse(r)
if s != nil {
w.WriteHeader(http.StatusBadRequest)
eJSON := fmt.Sprintf(`{"errors": [ {"message": "%s"} ]}`, s.Error())
w.Write([]byte(eJSON))
return
}
// Success
eJSON := fmt.Sprintf(`{"results": {"id": "%s"} }`, testMockBatchResponse)
w.Write([]byte(eJSON))
}
// Run the mock SparkPost endpoint
func startMockIngest(t *testing.T, addrPort string) {
http.HandleFunc("/", ingestServer)
server := &http.Server{
Addr: addrPort,
}
err := server.ListenAndServe()
if err != nil {
t.Errorf("Error starting mock SparkPost ingest endpoint %v", err)
}
}
// Test the feeder function by creating a mockup endpoint that will receive data that we push to it
func TestFeedForever(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
p := rand.Intn(1000) + 8000 // use a random port unmber as workaround for resource hogging during debug
mockIngestAddrPort := ":" + strconv.Itoa(p)
// Start the mock SparkPost endpoint server concurrently
go startMockIngest(t, mockIngestAddrPort)
client := spmta.MyRedis()
// Start the feeder process concurrently. We don't have to wait the usual time
go spmta.FeedForever(client, "http://"+mockIngestAddrPort, mockAPIKey, testTime)
t.Log("One event")
myLogp := captureLog()
emptyRedisQueue(client)
mockEvents(t, 1, client, true)
checkLog(t, 10, myLogp, testMockBatchResponse, 1)
t.Log("Many events")
myLogp = captureLog()
emptyRedisQueue(client)
mockEvents(t, 12000, client, true)
checkLog(t, 10, myLogp, testMockBatchResponse, 2) // two batches
t.Log("One event with no message_id in redis")
myLogp = captureLog()
emptyRedisQueue(client)
mockEvents(t, 1, client, false)
checkLog(t, 10, myLogp, testMockBatchResponse, 1)
}
func wrongTypeErr(err error) bool {
s := err.Error()
return len(s) >= 9 && s[0:9] == "WRONGTYPE"
}
func emptyRedisQueue(client *redis.Client) {
// Make sure redis queue is empty
for {
_, err := client.LPop(spmta.RedisQueue).Result()
if err == nil {
continue // actual value read .. keep going
}
// Check for empty, or queue error
if err == redis.Nil || wrongTypeErr(err) {
break
}
}
}
const ttl = time.Duration(testTime * 20) // expires fairly quickly after test run
func mockEvents(t *testing.T, nEvents int, client *redis.Client, augment bool) {
for i := 0; i < nEvents; i++ {
msgID := spmta.UniqMessageID()
recip := RandomRecipient()
e := testEvent(msgID)
// Build the composite info ready to push into the Redis queue
eBytes, err := json.Marshal(e)
if err != nil {
t.Errorf("Error %v", err)
}
if augment {
// Create a fake acct_etl augmentation record in Redis
augmentData := map[string]string{
"header_x-sp-subaccount-id": strconv.Itoa(testSubaccountID),
"rcpt": recip,
}
augmentJSON, err := json.Marshal(augmentData)
if err != nil {
t.Errorf("Error %v", err)
}
msgIDKey := spmta.TrackingPrefix + msgID
_, err = client.Set(msgIDKey, augmentJSON, ttl).Result()
if err != nil {
t.Errorf("Error %v", err)
}
}
if _, err = client.RPush(spmta.RedisQueue, eBytes).Result(); err != nil {
t.Errorf("Error %v", err)
}
}
}
// Build a test event
func testEvent(msgID string) spmta.TrackEvent {
var e spmta.TrackEvent
e.WD = spmta.WrapperData{
Action: testAction,
TargetLinkURL: testURL,
MessageID: msgID,
}
e.UserAgent = testUserAgent
e.IPAddress = testIPAddress
e.TimeStamp = strconv.FormatInt(time.Now().Unix(), 10)
return e
}
// Detailed unit tests
func TestFeedEventsErrorCases(t *testing.T) {
client := spmta.MyRedis()
client.Close() // deliberately close the connection before using
err := spmta.FeedEvents(client, "http://example.com", "", testTime)
if err.Error() != "redis: client is closed" {
t.Errorf("Error %v", err)
}
}
func TestAgedContent(t *testing.T) {
// no content, not aged
tBuf := spmta.TimedBuffer{
Content: []byte(""),
TimeStarted: time.Now(),
MaxAge: 10 * time.Second,
}
res := tBuf.AgedContent()
if res {
t.Errorf("Unexpected value")
}
// content, not aged
tBuf = spmta.TimedBuffer{
Content: []byte("Some content"),
TimeStarted: time.Now(),
MaxAge: 10 * time.Second,
}
res = tBuf.AgedContent()
if res {
t.Errorf("Unexpected value")
}
// no content, aged
tBuf = spmta.TimedBuffer{
Content: []byte(""),
// have to add a negative duration, see https://golang.org/pkg/time/#example_Time_Sub
TimeStarted: time.Now().Add(-30 * time.Second),
MaxAge: 10 * time.Second,
}
res = tBuf.AgedContent()
if res {
t.Errorf("Unexpected value")
}
// content, aged
tBuf = spmta.TimedBuffer{
Content: []byte("Some content"),
TimeStarted: time.Now().Add(-2 * time.Second),
MaxAge: 1 * time.Second,
}
res = tBuf.AgedContent()
if !res {
t.Errorf("Unexpected value")
}
}
func TestSparkPostEventNDJSONFaultyInputs(t *testing.T) {
client := spmta.MyRedis()
// Invalid input string, i.e. not properly constructed JSON
const eStrFaulty = `{"WD":{"act":"c`
_, err := spmta.SparkPostEventNDJSON(eStrFaulty, client)
if !strings.Contains(err.Error(), "end of JSON") {
t.Error(err)
}
// Message ID that can't be found, so event will succeed, not be augmented, and warning logged
const notFoundMsgID = "0000123456789abcdef1"
const redisKeyNotFoundMsgID = "msgID_" + notFoundMsgID
client.Del(redisKeyNotFoundMsgID)
myLogp := captureLog()
e := testEvent(notFoundMsgID)
eBytes, err := json.Marshal(e)
if err != nil {
t.Errorf("Error %v", err)
}
_, err = spmta.SparkPostEventNDJSON(string(eBytes), client)
if err != nil {
t.Error(err)
}
checkLog(t, 1, myLogp, "Warning: redis key "+redisKeyNotFoundMsgID+" not found", 1)
// MessageID Redis record corrupt
const augmentFaulty = `{"header_x-sp-subaccount-id"`
const augmentFaultyMsgID = "0000123456789abcdef2"
e = testEvent(augmentFaultyMsgID)
eBytes, err = json.Marshal(e)
if err != nil {
t.Errorf("Error %v", err)
}
client.Set("msgID_"+augmentFaultyMsgID, augmentFaulty, ttl)
_, err = spmta.SparkPostEventNDJSON(string(eBytes), client)
if !strings.Contains(err.Error(), "end of JSON") {
t.Error(err)
}
}
func TestSparkPostIngestFaultyInputs(t *testing.T) {
client := spmta.MyRedis()
var ingestData []byte // empty
// provoke an error in the host address
host := "http://api.sparkpost.com/not_an_api"
apiKey := "junk"
err := spmta.SparkPostIngest(ingestData, client, host, apiKey)
if !strings.Contains(err.Error(), "Only https is allowed") {
t.Error(err)
}
// provoke an error in JSON response
host = "https://example.com/"
err = spmta.SparkPostIngest(ingestData, client, host, apiKey)
if !strings.Contains(err.Error(), "invalid character") {
t.Error(err)
}
}
func TestFeedEventsFaultyInputs(t *testing.T) {
client := spmta.MyRedis()
// Invalid input string, i.e. not properly constructed JSON, pushed into Redis queue
eBytesFaulty := []byte(`{"WD":{"act":"c`)
if _, err := client.RPush(spmta.RedisQueue, eBytesFaulty).Result(); err != nil {
t.Errorf("Error %v", err)
}
host := "http://api.sparkpost.com/not_an_api"
apiKey := "junk"
err := spmta.FeedEvents(client, host, apiKey, testTime)
if !strings.Contains(err.Error(), "end of JSON") {
t.Error(err)
}
}