forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execd_test.go
195 lines (162 loc) · 4.61 KB
/
execd_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
package execd
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/parsers/influx"
influxSerializer "github.com/influxdata/telegraf/plugins/serializers/influx"
"github.com/influxdata/telegraf/testutil"
)
var now = time.Date(2020, 6, 30, 16, 16, 0, 0, time.UTC)
func TestExternalOutputWorks(t *testing.T) {
serializer := &influxSerializer.Serializer{}
require.NoError(t, serializer.Init())
exe, err := os.Executable()
require.NoError(t, err)
e := &Execd{
Command: []string{exe, "-testoutput"},
Environment: []string{"PLUGINS_OUTPUTS_EXECD_MODE=application", "METRIC_NAME=cpu"},
RestartDelay: config.Duration(5 * time.Second),
serializer: serializer,
Log: testutil.Logger{},
}
require.NoError(t, e.Init())
wg := &sync.WaitGroup{}
wg.Add(1)
e.process.ReadStderrFn = func(rstderr io.Reader) {
scanner := bufio.NewScanner(rstderr)
for scanner.Scan() {
t.Errorf("stderr: %q", scanner.Text())
}
if err := scanner.Err(); err != nil {
if !strings.HasSuffix(err.Error(), "already closed") {
t.Errorf("error reading stderr: %v", err)
}
}
wg.Done()
}
m := metric.New(
"cpu",
map[string]string{"name": "cpu1"},
map[string]interface{}{"idle": 50, "sys": 30},
now,
)
require.NoError(t, e.Connect())
require.NoError(t, e.Write([]telegraf.Metric{m}))
require.NoError(t, e.Close())
wg.Wait()
}
func TestPartiallyUnserializableThrowError(t *testing.T) {
serializer := &influxSerializer.Serializer{}
require.NoError(t, serializer.Init())
exe, err := os.Executable()
require.NoError(t, err)
e := &Execd{
Command: []string{exe, "-testoutput"},
Environment: []string{"PLUGINS_OUTPUTS_EXECD_MODE=application", "METRIC_NAME=cpu"},
RestartDelay: config.Duration(5 * time.Second),
IgnoreSerializationError: false,
serializer: serializer,
Log: testutil.Logger{},
}
require.NoError(t, e.Init())
m1 := metric.New(
"cpu",
map[string]string{"name": "cpu1"},
map[string]interface{}{"idle": 50, "sys": 30},
now,
)
m2 := metric.New(
"cpu",
map[string]string{"name": "cpu2"},
map[string]interface{}{},
now,
)
require.NoError(t, e.Connect())
require.Error(t, e.Write([]telegraf.Metric{m1, m2}))
require.NoError(t, e.Close())
}
func TestPartiallyUnserializableCanBeSkipped(t *testing.T) {
serializer := &influxSerializer.Serializer{}
require.NoError(t, serializer.Init())
exe, err := os.Executable()
require.NoError(t, err)
e := &Execd{
Command: []string{exe, "-testoutput"},
Environment: []string{"PLUGINS_OUTPUTS_EXECD_MODE=application", "METRIC_NAME=cpu"},
RestartDelay: config.Duration(5 * time.Second),
IgnoreSerializationError: true,
serializer: serializer,
Log: testutil.Logger{},
}
require.NoError(t, e.Init())
m1 := metric.New(
"cpu",
map[string]string{"name": "cpu1"},
map[string]interface{}{"idle": 50, "sys": 30},
now,
)
m2 := metric.New(
"cpu",
map[string]string{"name": "cpu2"},
map[string]interface{}{},
now,
)
require.NoError(t, e.Connect())
require.NoError(t, e.Write([]telegraf.Metric{m1, m2}))
require.NoError(t, e.Close())
}
var testoutput = flag.Bool("testoutput", false,
"if true, act like line input program instead of test")
func TestMain(m *testing.M) {
flag.Parse()
runMode := os.Getenv("PLUGINS_OUTPUTS_EXECD_MODE")
if *testoutput && runMode == "application" {
runOutputConsumerProgram()
os.Exit(0)
}
code := m.Run()
os.Exit(code)
}
func runOutputConsumerProgram() {
metricName := os.Getenv("METRIC_NAME")
parser := influx.NewStreamParser(os.Stdin)
for {
m, err := parser.Next()
if err != nil {
if errors.Is(err, influx.EOF) {
return // stream ended
}
var parseErr *influx.ParseError
if errors.As(err, &parseErr) {
fmt.Fprintf(os.Stderr, "parse ERR %v\n", parseErr)
//nolint:revive // error code is important for this "test"
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "ERR %v\n", err)
//nolint:revive // error code is important for this "test"
os.Exit(1)
}
expected := testutil.MustMetric(metricName,
map[string]string{"name": "cpu1"},
map[string]interface{}{"idle": 50, "sys": 30},
now,
)
if !testutil.MetricEqual(expected, m) {
fmt.Fprintf(os.Stderr, "metric doesn't match expected\n")
//nolint:revive // error code is important for this "test"
os.Exit(1)
}
}
}