forked from dcos/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcos_statsd_test.go
300 lines (250 loc) · 8.4 KB
/
dcos_statsd_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
package dcos_statsd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs/dcos_statsd/containers"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)
func TestStart(t *testing.T) {
t.Run("Server with no saved state", func(t *testing.T) {
ds := DCOSStatsd{}
// startTestServer runs a /health request test
addr := startTestServer(t, &ds)
defer ds.Stop()
// Check that no containers were created
resp, err := http.Get(addr + "/containers")
assertResponseWas(t, resp, err, "[]")
})
t.Run("Server with a single container saved", func(t *testing.T) {
// Create a temp dir:
dir, err := ioutil.TempDir("", "containers")
if err != nil {
assert.Fail(t, fmt.Sprintf("Could not create temp dir: %s", err))
}
defer os.RemoveAll(dir)
// Create JSON in memory:
ctrport := findFreePort()
ctrjson := fmt.Sprintf(
`{"container_id":"abc123","statsd_host":"127.0.0.1","statsd_port":%d}`,
ctrport)
// Write JSON to disk:
err = ioutil.WriteFile(dir+"/abc123", []byte(ctrjson), 0666)
if err != nil {
assert.Fail(t, fmt.Sprintf("Could not write container state: %s", err))
}
// Finally run DCOSStatsd.Start():
ds := DCOSStatsd{ContainersDir: dir}
addr := startTestServer(t, &ds)
defer ds.Stop()
// Ensure that container shows up in output:
resp, err := http.Get(addr + "/containers")
// encoding/json respects alphabetical order, so this is safe
assertResponseWas(t, resp, err, fmt.Sprintf("[%s]", ctrjson))
})
}
func TestStop(t *testing.T) {
t.Run("Server with no containers", func(t *testing.T) {
ds := DCOSStatsd{}
addr := startTestServer(t, &ds)
ds.Stop()
// Test that the server has stopped
resp, err := http.Get(addr + "/health")
assert.NotNil(t, err)
assert.Nil(t, resp)
})
t.Run("Server with a container", func(t *testing.T) {
ds := DCOSStatsd{}
addr := startTestServer(t, &ds)
port := findFreePort()
ctrjson := fmt.Sprintf(`{"container_id": "abc123","statsd_host": "127.0.0.1","statsd_port":%d}`, port)
http.Post(addr+"/container", "application/json", bytes.NewBuffer([]byte(ctrjson)))
ds.Stop()
// Test that the server has stopped
resp, err := http.Get(addr + "/health")
assert.NotNil(t, err)
assert.Nil(t, resp)
// Test that the statsd server has stopped by listening on the same port
statsdAddr, _ := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", port))
ln, err := net.ListenUDP("udp", statsdAddr)
assert.Nil(t, err)
if err == nil { // this test is necesasry to avoid segfaults
ln.Close()
}
})
}
func TestGather(t *testing.T) {
var acc testutil.Accumulator
dir, err := ioutil.TempDir("", "containers")
if err != nil {
assert.Fail(t, fmt.Sprintf("Could not create temp dir: %s", err))
}
defer os.RemoveAll(dir)
ds := DCOSStatsd{StatsdHost: "127.0.0.1", ContainersDir: dir, AllowedPendingMessages: 321}
addr := startTestServer(t, &ds)
defer ds.Stop()
// Test that the command API works as expected:
t.Log("A container on a random port")
abcjson := `{"container_id": "abc123"}`
resp, err := http.Post(addr+"/container", "application/json", bytes.NewBuffer([]byte(abcjson)))
assert.Nil(t, err)
abc := parseContainer(t, resp.Body)
assert.Equal(t, "abc123", abc.Id)
assert.NotEmpty(t, abc.StatsdHost)
assert.NotEmpty(t, abc.StatsdPort)
// the container json object does not serialize its internals, hence we
// retrieve it directly from the DCOSStatsd object
ctr := ds.containers["abc123"]
assert.NotNil(t, ctr)
assert.Equal(t, 321, ctr.Server.AllowedPendingMessages)
t.Log("A container on a known port")
xyzport := findFreePort()
xyzjson := fmt.Sprintf(`{"container_id":"xyz123","statsd_host":"127.0.0.1","statsd_port":%d}`, xyzport)
resp, err = http.Post(addr+"/container", "application/json", bytes.NewBuffer([]byte(xyzjson)))
assert.Nil(t, err)
xyz := parseContainer(t, resp.Body)
assert.Equal(t, "xyz123", xyz.Id)
assert.Equal(t, "127.0.0.1", xyz.StatsdHost)
assert.Equal(t, xyzport, xyz.StatsdPort)
t.Log("A container with an ID that already exists")
resp, err = http.Post(addr+"/container", "application/json", bytes.NewBuffer([]byte(abcjson)))
assert.Nil(t, err)
abc2 := parseContainer(t, resp.Body)
// Should have been redirected to the original abc123
assert.Equal(t, abc, abc2)
// no new containers should have been created
assert.Equal(t, 2, len(ds.containers))
t.Log("A container on an occupied port")
qqqjson := fmt.Sprintf(`{"container_id":"qqq123","statsd_host":"127.0.0.1","statsd_port":%d}`, xyzport)
_, err = http.Post(addr+"/container", "application/json", bytes.NewBuffer([]byte(qqqjson)))
assert.Nil(t, err)
// no new containers should have been created
assert.Equal(t, 2, len(ds.containers))
t.Log("Sending statsd to containers")
abcconn := dialUDPPort(t, abc.StatsdPort)
xyzconn := dialUDPPort(t, xyz.StatsdPort)
// Send each count ten times to each server
for i := 0; i < 10; i++ {
abcconn.Write([]byte("foo:123|c"))
xyzconn.Write([]byte("foo:123|c"))
}
abcconn.Close()
xyzconn.Close()
err = acc.GatherError(ds.Gather)
assert.Nil(t, err)
// Tests for the existence of these stats are run in TestGatherUDP
// as they do not regularly pass on CI. Invoke them via
// go test -tags udp
t.Log("Containers are persisted to disk")
files, err := ioutil.ReadDir(ds.ContainersDir)
assert.Nil(t, err)
assert.Equal(t, 2, len(files))
t.Log("Removing a container")
_, err = httpDelete(t, addr+"/container/abc123")
assert.Nil(t, err)
// The container was removed
assert.Equal(t, len(ds.containers), 1)
// abc123 no longer shows up in /containers
resp, err = http.Get(addr + "/containers")
assertResponseWas(t, resp, err, fmt.Sprintf("[%s]", xyzjson))
files, err = ioutil.ReadDir(ds.ContainersDir)
assert.Nil(t, err)
assert.Equal(t, 1, len(files))
}
// startTestServer starts a server on the specified DCOSStatsd on a randomly
// selected port and returns the address on which it will be served. It also
// runs a test against the /health endpoint to ensure that the command API is
// ready.
func startTestServer(t *testing.T, ds *DCOSStatsd) string {
port := findFreePort()
ds.Listen = fmt.Sprintf(":%d", port)
addr := fmt.Sprintf("http://localhost:%d", port)
var acc telegraf.Accumulator
acc = &testutil.Accumulator{}
err := ds.Start(acc)
assert.Nil(t, err)
// Ensure that the command API is ready
err = waitFor(func() bool {
_, err := http.Get(addr + "/health")
return err == nil
})
assert.Nil(t, err)
return addr
}
// waitFor waits five seconds for a condition to be true
func waitFor(cond func() bool) error {
done := make(chan bool)
go func() {
for {
if cond() {
done <- true
break
}
time.Sleep(100 * time.Millisecond)
}
}()
select {
case <-done:
return nil
case <-time.After(30 * time.Second):
return errors.New("timed out waiting for condition")
}
}
// findFreePort momentarily listens on :0, then closes the connection and
// returns the port assigned
func findFreePort() int {
ln, _ := net.Listen("tcp", ":0")
ln.Close()
addr := ln.Addr().(*net.TCPAddr)
return addr.Port
}
// assertResponseWas is a convenience method for testing http request responses
func assertResponseWas(t *testing.T, r *http.Response, err error, expected string) {
assert.Nil(t, err)
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err)
assert.Equal(t, expected, string(body))
}
// parseContainer is a convenience method for getting a Container from an
// http response
func parseContainer(t *testing.T, body io.Reader) containers.Container {
var ctr containers.Container
decoder := json.NewDecoder(body)
if err := decoder.Decode(&ctr); err != nil {
assert.Fail(t, "JSON could not be decoded to container: %q", err)
}
return ctr
}
// dialUDPPort dials localhost:port and returns the connection
func dialUDPPort(t *testing.T, port int) net.Conn {
straddr := fmt.Sprintf(":%d", port)
addr, err := net.ResolveUDPAddr("udp", straddr)
if err != nil {
assert.Fail(t, "Could not resolve address ", straddr)
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
assert.Fail(t, "Could not dial UDP ", straddr)
}
return conn
}
// httpDelete acts like http.Get, but for delete
func httpDelete(t *testing.T, addr string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest("DELETE", addr, nil)
if err != nil {
assert.Fail(t, "Could not perform delete")
}
return client.Do(req)
}