forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_test.go
117 lines (97 loc) · 2.37 KB
/
processor_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
package shim
import (
"bufio"
"io"
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/parsers/influx"
serializers_influx "github.com/influxdata/telegraf/plugins/serializers/influx"
)
func TestProcessorShim(t *testing.T) {
testSendAndReceive(t, "f1", "fv1")
}
func TestProcessorShimWithLargerThanDefaultScannerBufferSize(t *testing.T) {
letters := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, 0, bufio.MaxScanTokenSize*2)
for i := 0; i < bufio.MaxScanTokenSize*2; i++ {
b = append(b, letters[rand.Intn(len(letters))])
}
testSendAndReceive(t, "f1", string(b))
}
func testSendAndReceive(t *testing.T, fieldKey, fieldValue string) {
p := &testProcessor{"hi", "mom"}
stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()
s := New()
// inject test into shim
s.stdin = stdinReader
s.stdout = stdoutWriter
err := s.AddProcessor(p)
require.NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
if err := s.RunProcessor(); err != nil {
t.Error(err)
}
wg.Done()
}()
serializer := &serializers_influx.Serializer{}
require.NoError(t, serializer.Init())
parser := influx.Parser{}
require.NoError(t, parser.Init())
m := metric.New("thing",
map[string]string{
"a": "b",
},
map[string]interface{}{
"v": 1,
fieldKey: fieldValue,
},
time.Now(),
)
b, err := serializer.Serialize(m)
require.NoError(t, err)
_, err = stdinWriter.Write(b)
require.NoError(t, err)
err = stdinWriter.Close()
require.NoError(t, err)
r := bufio.NewReader(stdoutReader)
out, err := r.ReadString('\n')
require.NoError(t, err)
mOut, err := parser.ParseLine(out)
require.NoError(t, err)
val, ok := mOut.GetTag(p.tagName)
require.True(t, ok)
require.Equal(t, p.tagValue, val)
val2, ok := mOut.Fields()[fieldKey]
require.True(t, ok)
require.Equal(t, fieldValue, val2)
go func() {
if _, err = io.ReadAll(r); err != nil {
t.Error(err)
}
}()
wg.Wait()
}
type testProcessor struct {
tagName string
tagValue string
}
func (p *testProcessor) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, m := range in {
m.AddTag(p.tagName, p.tagValue)
}
return in
}
func (p *testProcessor) SampleConfig() string {
return ""
}
func (p *testProcessor) Description() string {
return ""
}