forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
opentsdb_test.go
203 lines (183 loc) · 4.34 KB
/
opentsdb_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
package opentsdb
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
)
func TestCleanTags(t *testing.T) {
var tagtests = []struct {
ptIn map[string]string
outTags map[string]string
}{
{
map[string]string{"one": "two", "three": "four"},
map[string]string{"one": "two", "three": "four"},
},
{
map[string]string{"aaa": "bbb"},
map[string]string{"aaa": "bbb"},
},
{
map[string]string{"Sp%ci@l Chars[": "g$t repl#ce)d"},
map[string]string{"Sp-ci-l_Chars_": "g-t_repl-ce_d"},
},
{
map[string]string{"μnicodε_letters": "okαy"},
map[string]string{"μnicodε_letters": "okαy"},
},
{
map[string]string{"n☺": "emojies☠"},
map[string]string{"n_": "emojies_"},
},
{
map[string]string{},
map[string]string{},
},
}
for _, tt := range tagtests {
tags := cleanTags(tt.ptIn)
if !reflect.DeepEqual(tags, tt.outTags) {
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
}
}
}
func TestBuildTagsTelnet(t *testing.T) {
var tagtests = []struct {
ptIn map[string]string
outTags string
}{
{
map[string]string{"one": "two", "three": "four"},
"one=two three=four",
},
{
map[string]string{"aaa": "bbb"},
"aaa=bbb",
},
{
map[string]string{"one": "two", "aaa": "bbb"},
"aaa=bbb one=two",
},
{
map[string]string{},
"",
},
}
for _, tt := range tagtests {
tags := ToLineFormat(tt.ptIn)
if !reflect.DeepEqual(tags, tt.outTags) {
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
}
}
}
func TestSanitize(t *testing.T) {
tests := []struct {
name string
value string
expected string
}{
{
name: "Ascii letters and numbers allowed",
value: "ascii 123",
expected: "ascii_123",
},
{
name: "Allowed punct",
value: "-_./",
expected: "-_./",
},
{
name: "Special conversions to hyphen",
value: "@*%#$!",
expected: "-----_",
},
{
name: "Unicode Letters allowed",
value: "μnicodε_letters",
expected: "μnicodε_letters",
},
{
name: "Other Unicode not allowed",
value: "“☢”",
expected: "___",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := sanitize(tt.value)
require.Equal(t, tt.expected, actual)
})
}
}
func BenchmarkHttpSend(b *testing.B) {
const BatchSize = 50
const MetricsCount = 4 * BatchSize
metrics := make([]telegraf.Metric, MetricsCount)
for i := 0; i < MetricsCount; i++ {
metrics[i] = testutil.TestMetric(1.0)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, "{}")
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
panic(err)
}
_, p, _ := net.SplitHostPort(u.Host)
port, err := strconv.Atoi(p)
if err != nil {
panic(err)
}
o := &OpenTSDB{
Host: ts.URL,
Port: port,
Prefix: "",
HttpBatchSize: BatchSize,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
o.Write(metrics)
}
}
// func TestWrite(t *testing.T) {
// if testing.Short() {
// t.Skip("Skipping integration test in short mode")
// }
// o := &OpenTSDB{
// Host: testutil.GetLocalHost(),
// Port: 4242,
// Prefix: "prefix.test.",
// }
// // Verify that we can connect to the OpenTSDB instance
// err := o.Connect()
// require.NoError(t, err)
// // Verify that we can successfully write data to OpenTSDB
// err = o.Write(testutil.MockMetrics())
// require.NoError(t, err)
// // Verify positive and negative test cases of writing data
// metrics := testutil.MockMetrics()
// metrics = append(metrics, testutil.TestMetric(float64(1.0),
// "justametric.float"))
// metrics = append(metrics, testutil.TestMetric(int64(123456789),
// "justametric.int"))
// metrics = append(metrics, testutil.TestMetric(uint64(123456789012345),
// "justametric.uint"))
// metrics = append(metrics, testutil.TestMetric("Lorem Ipsum",
// "justametric.string"))
// metrics = append(metrics, testutil.TestMetric(float64(42.0),
// "justametric.anotherfloat"))
// metrics = append(metrics, testutil.TestMetric(float64(42.0),
// "metric w/ specialchars"))
// err = o.Write(metrics)
// require.NoError(t, err)
// }