-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
88 lines (75 loc) · 2.97 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
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"io"
"io/ioutil"
"testing"
"github.com/aws/aws-lambda-go/events"
)
func TestHandler(t *testing.T) {
t.Run("should parse and send an entry", func(t *testing.T) {
firehoseWriter := NewMockFirehoseWriter()
s3Reader := NewMockS3Reader(gz(`http 2018-09-18T21:38:37.519183Z app/test1/7f050ffab5373730 95.90.211.80:4254 172.31.7.183:80 0.001 0.000 0.000 200 200 461 654 "GET http://test1-332132803.eu-west-1.elb.amazonaws.com:80/ HTTP/1.1" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" - - arn:aws:elasticloadbalancing:eu-west-1:328305143014:targetgroup/target-group-1/76784dcf3348e51f "Root=1-5ba1705d-5617cf32286ecfb8172f73cc" "-" "-" 0 2018-09-18T21:38:37.518000Z "forward" "-" `))
inputJson := readJsonFromFile(t, "./testdata/s3-event.json")
var s3Event events.S3Event
if err := json.Unmarshal(inputJson, &s3Event); err != nil {
t.Errorf("could not unmarshal event: %v", err)
}
err := handler(s3Reader, firehoseWriter)(s3Event)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(firehoseWriter.records) == 0 {
t.Errorf("expected entries")
}
var entry AlbLogEntry
err = json.Unmarshal(firehoseWriter.records[0], &entry)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if entry.Request != "GET http://test1-332132803.eu-west-1.elb.amazonaws.com:80/ HTTP/1.1" {
t.Errorf("unexpected request: %v", entry.Request)
}
})
}
func gz(str string) io.Reader {
var buf bytes.Buffer
b := []byte(str)
gz := gzip.NewWriter(&buf)
gz.Write(b)
gz.Flush()
gz.Close()
return &buf
}
func TestReadLogEntries(t *testing.T) {
logEntries, err := readLogEntries(ioutil.NopCloser(gz(
`http 2018-09-18T21:38:37.519183Z app/test1/7f050ffab5373730 95.90.211.80:4254 172.31.7.183:80 0.001 0.000 0.000 200 200 461 654 "GET http://test1-332132803.eu-west-1.elb.amazonaws.com:80/ HTTP/1.1" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" - - arn:aws:elasticloadbalancing:eu-west-1:328305143014:targetgroup/target-group-1/76784dcf3348e51f "Root=1-5ba1705d-5617cf32286ecfb8172f73cc" "-" "-" 0 2018-09-18T21:38:37.518000Z "forward" "-" `)))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
entry := logEntries[0]
if entry.Type != "http" {
t.Errorf("invalid type")
}
if entry.Timestamp != "2018-09-18T21:38:37.519183Z" {
t.Errorf("invalid timestamp")
}
if entry.Client != "95.90.211.80" {
t.Errorf("invalid client")
}
if entry.Target != "172.31.7.183" {
t.Errorf("invalid target")
}
if entry.Request != "GET http://test1-332132803.eu-west-1.elb.amazonaws.com:80/ HTTP/1.1" {
t.Errorf("invalid request, %v", entry.Request)
}
}
func readJsonFromFile(t *testing.T, inputFile string) []byte {
inputJson, err := ioutil.ReadFile(inputFile)
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}
return inputJson
}