forked from apiaryio/heroku-datadog-drain-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
210 lines (179 loc) · 5.68 KB
/
server_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
package main
import (
"bytes"
"encoding/base64"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
)
var fullTests = []struct {
cnt int
Req string
Expected []string
}{
{
cnt: 2,
Req: `255 <158>1 2015-04-02T11:52:34.520012+00:00 host heroku router - at=info method=POST path="/users" host=myapp.com request_id=c1806361-2081-42e7-a8aa-92b6808eac8e fwd="24.76.242.18" dyno=web.1 connect=1ms service=37ms status=201 bytes=828`,
Expected: []string{
"heroku.router.request.connect:1.000000|h|#at:info,dyno:web.1,host:myapp.com,method:POST,path:/users,status:201,statusFamily:2xx",
"heroku.router.request.service:37.000000|h|#at:info,dyno:web.1,host:myapp.com,method:POST,path:/users,status:201,statusFamily:2xx",
},
},
{
cnt: 1,
Req: `229 <45>1 2015-04-02T11:48:16.839257+00:00 host heroku web.1 - source=web.1 dyno=heroku.35930502.b9de5fce-44b7-4287-99a7-504519070cba sample#load_avg_1m=0.01`,
Expected: []string{
"heroku.dyno.load.avg.1m:0.010000|g|#source:web.1,type:web",
},
},
{
cnt: 3,
Req: `222 <134>1 2015-04-07T16:01:43.517062+00:00 host app api - Scaled to web@3:Performance-L mailer@1:Standard-2X by user [email protected]`,
Expected: []string{
"_e{16,77}:heroku/api: test|Scaled to web@3:Performance-L mailer@1:Standard-2X by user [email protected]",
"heroku.dyno.mailer:1.000000|g",
"heroku.dyno.web:3.000000|g",
},
},
{
cnt: 1,
Req: `222 <134>1 2015-04-07T16:01:43.517062+00:00 host app api - Release v1 created by foo@bar`,
Expected: []string{
"_e{13,29}:app/api: test|Release v1 created by foo@bar",
},
},
{
cnt: 9,
Req: `452 <134>1 2015-04-07T16:01:43.517062+00:00 host app web.1 - info: responseLogger: metric#tag#route=/parser metric#request_id=11747467-f4ce-4b06-8c99-92be968a02e3 metric#request_length=541 metric#response_length=5163 metric#parser_time=5ms metric#eventLoop.count=606 metric#eventLoop.avg_ms=515.503300330033 metric#eventLoop.p50_ms=0.8805309734513275 metric#eventLoop.p95_ms=3457.206896551724 metric#eventLoop.p99_ms=3457.206896551724 metric#eventLoop.max_ms=5008`,
Expected: []string{
"app.metric.request.length:541.000000|g|#source:web.1,type:web,route:/parser",
"app.metric.response.length:5163.000000|g|#source:web.1,type:web,route:/parser",
"app.metric.parser.time:5.000000|g|#source:web.1,type:web,route:/parser",
"app.metric.eventLoop.count:606.000000|g|#source:web.1,type:web,route:/parser",
"app.metric.eventLoop.avg.ms:515.503300|g|#source:web.1,type:web,route:/parser",
"app.metric.eventLoop.p50.ms:0.880531|g|#source:web.1,type:web,route:/parser",
"app.metric.eventLoop.p95.ms:3457.206897|g|#source:web.1,type:web,route:/parser",
"app.metric.eventLoop.p99.ms:3457.206897|g|#source:web.1,type:web,route:/parser",
"app.metric.eventLoop.max.ms:5008.000000|g|#source:web.1,type:web,route:/parser",
},
},
}
func TestStatusRequest(t *testing.T) {
r := gin.New()
r.GET("/status", func(c *gin.Context) {
c.String(200, "OK")
})
req, _ := http.NewRequest("GET", "/status", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
if string(body) != "OK" {
t.Error("resp body should match")
}
if resp.Code != 200 {
t.Error("should get a 200")
}
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func TestLogRequest(t *testing.T) {
s := loadServerCtx()
s.in = make(chan *logData)
defer close(s.in)
s.out = make(chan *logMetrics)
defer close(s.out)
go logProcess(s.in, s.out)
r := gin.New()
accounts := map[string]string{}
accounts["test"] = "pass"
auth := r.Group("/", gin.BasicAuth(accounts))
auth.POST("/", s.processLogs)
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer([]byte("LINE of text\nAnother line\n")))
req.SetBasicAuth("test", "pass")
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
if string(body) != "OK" {
t.Error("resp body should match")
}
if resp.Code != 200 {
t.Error("should get a 200")
}
}
func TestFull(t *testing.T) {
s := loadServerCtx()
s.in = make(chan *logData)
defer close(s.in)
s.out = make(chan *logMetrics)
defer close(s.out)
addr := "localhost:1201"
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
t.Fatal(err)
}
server, err := net.ListenUDP("udp", udpAddr)
if err != nil {
t.Fatal(err)
}
defer server.Close()
c, err := statsdClient(addr)
if err != nil {
t.Fatal(err)
}
go logProcess(s.in, s.out)
go c.sendToStatsd(s.out)
r := gin.New()
accounts := map[string]string{}
accounts["test"] = "pass"
auth := r.Group("/", gin.BasicAuth(accounts))
auth.POST("/", s.processLogs)
data := make([]byte, 1024)
for _, tt := range fullTests {
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer([]byte(tt.Req)))
req.SetBasicAuth("test", "pass")
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
if string(body) != "OK" {
t.Error("resp body should match")
}
if resp.Code != 200 {
t.Error("should get a 200")
}
if tt.cnt != len(tt.Expected) {
t.Error("Count of expected results isn't equal to inputs")
}
for i := 0; i < tt.cnt; i++ {
n, err := server.Read(data)
if err != nil {
t.Fatal(err)
}
message := data[:n]
findEqual := false
for j := 0; j < len(tt.Expected); j++ {
if string(message) == tt.Expected[j] {
findEqual = true
}
}
if findEqual == false {
t.Errorf("Expected: %s. Actual: %s", tt.Expected[i], string(message))
}
}
}
time.Sleep(1 * time.Second)
}