forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_test.go
More file actions
126 lines (108 loc) · 3 KB
/
csv_test.go
File metadata and controls
126 lines (108 loc) · 3 KB
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
package s3
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stripe/veneur/samplers"
)
type CSVTestCase struct {
Name string
DDMetric samplers.DDMetric
Row io.Reader
}
func CSVTestCases() []CSVTestCase {
partition := time.Now().UTC().Format("20060102")
return []CSVTestCase{
{
Name: "BasicDDMetric",
DDMetric: samplers.DDMetric{
Name: "a.b.c.max",
Value: [1][2]float64{[2]float64{1476119058,
100}},
Tags: []string{"foo:bar",
"baz:quz"},
MetricType: "gauge",
Hostname: "globalstats",
DeviceName: "food",
Interval: 0,
},
Row: strings.NewReader(fmt.Sprintf("a.b.c.max\t{foo:bar,baz:quz}\tgauge\tglobalstats\ttestbox-c3eac9\tfood\t0\t2016-10-10 05:04:18\t100\t%s\n", partition)),
},
{
// Test that we are able to handle a missing field (DeviceName)
Name: "MissingDeviceName",
DDMetric: samplers.DDMetric{
Name: "a.b.c.max",
Value: [1][2]float64{[2]float64{1476119058,
100}},
Tags: []string{"foo:bar",
"baz:quz"},
MetricType: "rate",
Hostname: "localhost",
DeviceName: "",
Interval: 10,
},
Row: strings.NewReader(fmt.Sprintf("a.b.c.max\t{foo:bar,baz:quz}\trate\tlocalhost\ttestbox-c3eac9\t\t10\t2016-10-10 05:04:18\t100\t%s\n", partition)),
},
{
// Test that we are able to handle tags which have tab characters in them
// by quoting the entire field
// (tags shouldn't do this, but we should handle them properly anyway)
Name: "TabTag",
DDMetric: samplers.DDMetric{
Name: "a.b.c.max",
Value: [1][2]float64{[2]float64{1476119058,
100}},
Tags: []string{"foo:b\tar",
"baz:quz"},
MetricType: "rate",
Hostname: "localhost",
DeviceName: "eniac",
Interval: 10,
},
Row: strings.NewReader(fmt.Sprintf("a.b.c.max\t\"{foo:b\tar,baz:quz}\"\trate\tlocalhost\ttestbox-c3eac9\teniac\t10\t2016-10-10 05:04:18\t100\t%s\n", partition)),
},
}
}
func TestEncodeCSV(t *testing.T) {
testCases := CSVTestCases()
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
b := &bytes.Buffer{}
w := csv.NewWriter(b)
w.Comma = '\t'
tm := time.Now()
err := EncodeDDMetricCSV(tc.DDMetric, w, &tm, "testbox-c3eac9")
assert.NoError(t, err)
// We need to flush or there won't actually be any data there
w.Flush()
assert.NoError(t, err)
assertReadersEqual(t, tc.Row, b)
})
}
}
// Helper function for determining that two readers are equal
func assertReadersEqual(t *testing.T, expected io.Reader, actual io.Reader) {
// If we can seek, ensure that we're starting at the beginning
for _, reader := range []io.Reader{expected, actual} {
if readerSeeker, ok := reader.(io.ReadSeeker); ok {
readerSeeker.Seek(0, io.SeekStart)
}
}
// do the lazy thing for now
bts, err := ioutil.ReadAll(expected)
if err != nil {
t.Fatal(err)
}
bts2, err := ioutil.ReadAll(actual)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(bts), string(bts2))
}