forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdcos_metrics_test.go
268 lines (230 loc) · 5.27 KB
/
dcos_metrics_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
// +build !windows
package dcos_metrics
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net"
"net/http"
"testing"
"time"
"github.com/dcos/dcos-metrics/producers"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/metric"
)
func TestSplitHostPort(t *testing.T) {
type testCase struct {
hostPort string
host string
port int
}
testCases := []testCase{
{
hostPort: "localhost:8000",
host: "localhost",
port: 8000,
},
{
hostPort: "10.10.0.1:10",
host: "10.10.0.1",
port: 10,
},
{
hostPort: "host.name.com:60000",
host: "host.name.com",
port: 60000,
},
{
hostPort: ":80",
host: "",
port: 80,
},
}
for _, tc := range testCases {
host, port, err := splitHostPort(tc.hostPort)
if err != nil {
t.Fatal(err)
}
if host != tc.host {
t.Fatalf("expected host %s for hostport %s, got %s", tc.host, tc.hostPort, host)
}
if port != tc.port {
t.Fatalf("expected port %d for hostport %s, got %d", tc.port, tc.hostPort, port)
}
}
badPortCases := []string{
"localhost:foo",
"localhost:",
}
for _, hostPort := range badPortCases {
_, _, err := splitHostPort(hostPort)
if err == nil {
t.Fatalf("expected error for hostport %s", hostPort)
}
}
}
func TestDCOSMetricsNaNValue(t *testing.T) {
// Assert that the server returns a 200 status for container app metrics after the HTTP producer receives a NaN value.
containerID := "cid"
dcosMetrics, url, err := setupDCOSMetrics()
if err != nil {
t.Fatal(err)
}
defer dcosMetrics.Close()
err = waitFor(func() bool {
_, err := http.Get(url + "/health")
return err == nil
})
if err != nil {
t.Fatal(err)
}
m, err := metric.New(
"prefix.foo",
map[string]string{
"container_id": containerID,
"service_name": "sname",
"task_name": "tname",
"executor_name": "ename",
"label_name": "label_value",
"metric_type": "gauge",
},
map[string]interface{}{
"metric1": math.NaN(),
},
time.Now(),
)
if err != nil {
t.Fatal(err)
}
err = dcosMetrics.Write([]telegraf.Metric{m})
if err != nil {
t.Fatal(err)
}
resp, err := http.Get(url + "/v0/containers/" + containerID + "/app")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("expected status code 200, got %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var metrics producers.MetricsMessage
json.Unmarshal(body, &metrics)
found := false
for _, dp := range metrics.Datapoints {
if dp.Name == "prefix.foo.metric1" {
if dp.Value != "" {
t.Fatalf("expected datapoint value to be empty string, got %v", dp.Value)
}
found = true
break
}
}
if !found {
t.Fatal("datapoint missing in response")
}
}
func TestDCOSMetricsNilValue(t *testing.T) {
// Assert that the server returns node metrics, even when supplied with multiple metrics messages,
// some of which have missing fields
dcosMetrics, url, err := setupDCOSMetrics()
if err != nil {
t.Fatal(err)
}
defer dcosMetrics.Close()
err = waitFor(func() bool {
_, err := http.Get(url + "/health")
return err == nil
})
if err != nil {
t.Fatal(err)
}
m1, err := metric.New(
"dcos.metrics.node.system",
map[string]string{},
map[string]interface{}{"load1": uint64(123), "load5": uint64(1234), "load15": uint64(12345)},
time.Now(),
)
if err != nil {
t.Fatal(err)
}
m2, err := metric.New(
"dcos.metrics.node.system",
map[string]string{"mesos_id": "fake mesos id"},
map[string]interface{}{"uptime": uint64(12345)},
time.Now(),
)
if err != nil {
t.Fatal(err)
}
err = dcosMetrics.Write([]telegraf.Metric{m1, m2})
if err != nil {
t.Fatal(err)
}
resp, err := http.Get(url + "/v0/node")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("expected status code 200, got %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var metrics producers.MetricsMessage
json.Unmarshal(body, &metrics)
results := map[string]interface{}{}
for _, dp := range metrics.Datapoints {
results[dp.Name] = dp.Value
}
if len(results) != 4 {
t.Fatal("datapoint missing in response")
}
}
func setupDCOSMetrics() (DCOSMetrics, string, error) {
serverHostPort := fmt.Sprintf("localhost:%d", findFreePort())
serverURL := fmt.Sprintf("http://%s", serverHostPort)
dm := DCOSMetrics{
Listen: serverHostPort,
CacheExpiry: internal.Duration{Duration: time.Second},
MesosID: "fake-mesos-id",
DCOSNodeRole: "agent",
DCOSClusterID: "fake-cluster-id",
DCOSNodePrivateIP: "10.0.0.1",
}
return dm, serverURL, dm.Connect()
}
// 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
}
// 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(5 * time.Second):
return errors.New("timed out waiting for condition")
}
}