-
Notifications
You must be signed in to change notification settings - Fork 4
/
encoder_test.go
184 lines (156 loc) · 4.29 KB
/
encoder_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
package fluxline
// DCSO fluxline
// Copyright (c) 2017, 2018, DCSO GmbH
import (
"bytes"
"io"
"math"
"regexp"
"strings"
"testing"
"time"
)
var testStruct = struct {
TestVal uint64 `influx:"testval"`
TestVal2 uint64 `influx:"testvalue"`
TestVal3 int64 `influx:"testvalue2"`
TestVal4 string `influx:"testvalue3"`
TestDate time.Time `influx:"testvaluetime"`
TestBool bool `influx:"testvaluebool"`
TestFloat float64 `influx:"testvalueflt64"`
TestFloat32 float32 `influx:"testvalueflt32"`
}{
TestVal: 1,
TestVal2: 2,
TestVal3: -3,
TestVal4: `foobar"baz`,
TestDate: time.Now(),
TestFloat32: math.Pi,
TestFloat: 1.29e-24,
}
var testStructInvalidType = struct {
TestVal uint64 `influx:"testval"`
TestVal2 uint64 `influx:"testvalue"`
TestVal3 int64 `influx:"testvalue2"`
Foo io.Writer `influx:"testinval"`
}{
TestVal: 1,
TestVal2: 2,
TestVal3: -3,
}
var testStructStringLong = struct {
TestStr string `influx:"testval"`
}{
TestStr: strings.Repeat("#", 70000),
}
var testStructPartUntagged = struct {
TestVal uint64 `influx:"testval"`
TestVal2 uint64 `influx:"testvalue"`
TestVal3 int64
}{
TestVal: 1,
TestVal2: 2,
TestVal3: -3,
}
var testStructAllUntagged = struct {
TestVal uint64
TestVal2 uint64
TestVal3 int64
}{
TestVal: 1,
TestVal2: 2,
TestVal3: -3,
}
func TestEncoderEncoder(t *testing.T) {
var b bytes.Buffer
ile := NewEncoder(&b)
tags := make(map[string]string)
tags["foo"] = "bar"
tags["baaz gogo"] = "gu,gu"
err := ile.Encode("mytool", testStruct, tags)
if err != nil {
t.Fatal(err)
}
out := b.String()
if len(out) == 0 {
t.Fatalf("unexpected result length: %d == 0", len(out))
}
if match, _ := regexp.Match(`^mytool,host=[^,]+,baaz\\ gogo=gu\\,gu,foo=bar testval=1i,testvalue=2i,testvalue2=-3i,testvalue3=\"foobar\\\"baz\",testvaluebool=false,testvalueflt32=3.1415927,testvalueflt64=1.29e-24,testvaluetime=`, []byte(out)); !match {
t.Fatalf("unexpected match content: %s", out)
}
}
func TestEncoderEncoderWithHostname(t *testing.T) {
var b bytes.Buffer
ile := NewEncoderWithHostname(&b, "testhost")
tags := make(map[string]string)
tags["foo"] = "bar"
tags["baaz gogo"] = "gu,gu"
err := ile.Encode("mytool", testStruct, tags)
if err != nil {
t.Fatal(err)
}
out := b.String()
if len(out) == 0 {
t.Fatalf("unexpected result length: %d == 0", len(out))
}
if match, _ := regexp.Match(`^mytool,host=testhost,baaz\\ gogo=gu\\,gu,foo=bar testval=1i,testvalue=2i,testvalue2=-3i,testvalue3=\"foobar\\\"baz\",testvaluebool=false,testvalueflt32=3.1415927,testvalueflt64=1.29e-24,testvaluetime=`, []byte(out)); !match {
t.Fatalf("unexpected match content: %s", out)
}
}
func TestEncoderTypeFail(t *testing.T) {
var b bytes.Buffer
ile := NewEncoder(&b)
tags := make(map[string]string)
err := ile.Encode("mytool", testStructInvalidType, tags)
if err == nil {
t.Fatal(err)
}
}
func TestEncoderNoTypes(t *testing.T) {
var b bytes.Buffer
ile := NewEncoder(&b)
tags := make(map[string]string)
err := ile.EncodeWithoutTypes("mytool", testStruct, tags)
if err != nil {
t.Fatal(err)
}
out := b.String()
if match, _ := regexp.Match(`^mytool,host=[^,]+ testval=1,testvalue=2,testvalue2=-3,testvalue3=\"foobar\\\"baz\",testvaluebool=false,testvalueflt32=3.1415927,testvalueflt64=1.29e-24,testvaluetime=`, []byte(out)); !match {
t.Fatalf("unexpected match content: %s", out)
}
}
func TestEncoderStringTooLongFail(t *testing.T) {
var b bytes.Buffer
ile := NewEncoder(&b)
tags := make(map[string]string)
err := ile.Encode("mytool", testStructStringLong, tags)
if err == nil {
t.Fatal(err)
}
}
func TestEncoderPartUntagged(t *testing.T) {
var b bytes.Buffer
ile := NewEncoder(&b)
tags := make(map[string]string)
err := ile.Encode("mytool", testStructPartUntagged, tags)
if err != nil {
t.Fatal(err)
}
out := b.String()
if match, _ := regexp.Match(`^mytool,host=[^,]+ testval=1i,testvalue=2i`, []byte(out)); !match {
t.Fatalf("unexpected match content: %s", out)
}
}
func TestEncoderAllUntagged(t *testing.T) {
var b bytes.Buffer
ile := NewEncoder(&b)
tags := make(map[string]string)
err := ile.Encode("mytool", testStructAllUntagged, tags)
if err != nil {
t.Fatal(err)
}
out := b.String()
if len(out) != 0 {
t.Fatalf("unexpected result length: %d != 0", len(out))
}
}