forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multifile_test.go
76 lines (62 loc) · 1.76 KB
/
multifile_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
package multifile
import (
"os"
"path"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFileTypes(t *testing.T) {
wd, _ := os.Getwd()
m := MultiFile{
BaseDir: path.Join(wd, `testdata`),
FailEarly: true,
Files: []File{
{Name: `bool.txt`, Dest: `examplebool`, Conversion: `bool`},
{Name: `float.txt`, Dest: `examplefloat`, Conversion: `float`},
{Name: `int.txt`, Dest: `examplefloatX`, Conversion: `float(3)`},
{Name: `int.txt`, Dest: `exampleint`, Conversion: `int`},
{Name: `string.txt`, Dest: `examplestring`},
{Name: `tag.txt`, Dest: `exampletag`, Conversion: `tag`},
{Name: `int.txt`, Conversion: `int`},
},
}
var acc testutil.Accumulator
err := m.Gather(&acc)
require.NoError(t, err)
assert.Equal(t, map[string]string{"exampletag": "test"}, acc.Metrics[0].Tags)
assert.Equal(t, map[string]interface{}{
"examplebool": true,
"examplestring": "hello world",
"exampleint": int64(123456),
"int.txt": int64(123456),
"examplefloat": 123.456,
"examplefloatX": 123.456,
}, acc.Metrics[0].Fields)
}
func FailEarly(failEarly bool, t *testing.T) error {
wd, _ := os.Getwd()
m := MultiFile{
BaseDir: path.Join(wd, `testdata`),
FailEarly: failEarly,
Files: []File{
{Name: `int.txt`, Dest: `exampleint`, Conversion: `int`},
{Name: `int.txt`, Dest: `exampleerror`, Conversion: `bool`},
},
}
var acc testutil.Accumulator
err := m.Gather(&acc)
if err == nil {
assert.Equal(t, map[string]interface{}{
"exampleint": int64(123456),
}, acc.Metrics[0].Fields)
}
return err
}
func TestFailEarly(t *testing.T) {
err := FailEarly(false, t)
require.NoError(t, err)
err = FailEarly(true, t)
require.Error(t, err)
}