forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execd_test.go
233 lines (195 loc) · 5.55 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package execd
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/plugins/parsers/influx"
"github.com/influxdata/telegraf/plugins/parsers/prometheus"
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/influxdata/telegraf/testutil"
)
func TestSettingConfigWorks(t *testing.T) {
cfg := `
[[inputs.execd]]
command = ["a", "b", "c"]
environment = ["d=e", "f=1"]
restart_delay = "1m"
signal = "SIGHUP"
`
conf := config.NewConfig()
require.NoError(t, conf.LoadConfigData([]byte(cfg)))
require.Len(t, conf.Inputs, 1)
inp, ok := conf.Inputs[0].Input.(*Execd)
require.True(t, ok)
require.EqualValues(t, []string{"a", "b", "c"}, inp.Command)
require.EqualValues(t, []string{"d=e", "f=1"}, inp.Environment)
require.EqualValues(t, 1*time.Minute, inp.RestartDelay)
require.EqualValues(t, "SIGHUP", inp.Signal)
}
func TestExternalInputWorks(t *testing.T) {
influxParser := models.NewRunningParser(&influx.Parser{}, &models.ParserConfig{})
require.NoError(t, influxParser.Init())
exe, err := os.Executable()
require.NoError(t, err)
e := &Execd{
Command: []string{exe, "-counter"},
Environment: []string{"PLUGINS_INPUTS_EXECD_MODE=application", "METRIC_NAME=counter"},
RestartDelay: config.Duration(5 * time.Second),
Signal: "STDIN",
Log: testutil.Logger{},
}
e.SetParser(influxParser)
metrics := make(chan telegraf.Metric, 10)
defer close(metrics)
acc := agent.NewAccumulator(&TestMetricMaker{}, metrics)
require.NoError(t, e.Start(acc))
require.NoError(t, e.Gather(acc))
// grab a metric and make sure it's a thing
m := readChanWithTimeout(t, metrics, 10*time.Second)
e.Stop()
require.Equal(t, "counter", m.Name())
val, ok := m.GetField("count")
require.True(t, ok)
require.EqualValues(t, 0, val)
}
func TestParsesLinesContainingNewline(t *testing.T) {
parser := models.NewRunningParser(&influx.Parser{}, &models.ParserConfig{})
require.NoError(t, parser.Init())
metrics := make(chan telegraf.Metric, 10)
defer close(metrics)
acc := agent.NewAccumulator(&TestMetricMaker{}, metrics)
e := &Execd{
RestartDelay: config.Duration(5 * time.Second),
Signal: "STDIN",
acc: acc,
Log: testutil.Logger{},
}
e.SetParser(parser)
cases := []struct {
Name string
Value string
}{
{
Name: "no-newline",
Value: "my message",
}, {
Name: "newline",
Value: "my\nmessage",
},
}
for _, test := range cases {
t.Run(test.Name, func(t *testing.T) {
line := fmt.Sprintf("event message=\"%v\" 1587128639239000000", test.Value)
e.outputReader(strings.NewReader(line))
m := readChanWithTimeout(t, metrics, 1*time.Second)
require.Equal(t, "event", m.Name())
val, ok := m.GetField("message")
require.True(t, ok)
require.Equal(t, test.Value, val)
})
}
}
func TestParsesPrometheus(t *testing.T) {
parser := models.NewRunningParser(&prometheus.Parser{}, &models.ParserConfig{})
require.NoError(t, parser.Init())
metrics := make(chan telegraf.Metric, 10)
defer close(metrics)
var acc testutil.Accumulator
e := &Execd{
RestartDelay: config.Duration(5 * time.Second),
Signal: "STDIN",
acc: &acc,
Log: testutil.Logger{},
}
e.SetParser(parser)
lines := `# HELP This is just a test metric.
# TYPE test summary
test{handler="execd",quantile="0.5"} 42.0
`
expected := []telegraf.Metric{
testutil.MustMetric(
"prometheus",
map[string]string{"handler": "execd", "quantile": "0.5"},
map[string]interface{}{"test": float64(42.0)},
time.Unix(0, 0),
),
}
e.outputReader(strings.NewReader(lines))
check := func() bool { return acc.NMetrics() == uint64(len(expected)) }
require.Eventually(t, check, 1*time.Second, 100*time.Millisecond)
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}
func readChanWithTimeout(t *testing.T, metrics chan telegraf.Metric, timeout time.Duration) telegraf.Metric {
to := time.NewTimer(timeout)
defer to.Stop()
select {
case m := <-metrics:
return m
case <-to.C:
require.FailNow(t, "timeout waiting for metric")
}
return nil
}
type TestMetricMaker struct{}
func (tm *TestMetricMaker) Name() string {
return "TestPlugin"
}
func (tm *TestMetricMaker) LogName() string {
return tm.Name()
}
func (tm *TestMetricMaker) MakeMetric(aMetric telegraf.Metric) telegraf.Metric {
return aMetric
}
func (tm *TestMetricMaker) Log() telegraf.Logger {
return models.NewLogger("TestPlugin", "test", "")
}
var counter = flag.Bool("counter", false,
"if true, act like line input program instead of test")
func TestMain(m *testing.M) {
flag.Parse()
runMode := os.Getenv("PLUGINS_INPUTS_EXECD_MODE")
if *counter && runMode == "application" {
if err := runCounterProgram(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
code := m.Run()
os.Exit(code)
}
func runCounterProgram() error {
envMetricName := os.Getenv("METRIC_NAME")
i := 0
serializer := serializers.NewInfluxSerializer()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
m := metric.New(envMetricName,
map[string]string{},
map[string]interface{}{
"count": i,
},
time.Now(),
)
i++
b, err := serializer.Serialize(m)
if err != nil {
fmt.Fprintf(os.Stderr, "ERR %v\n", err)
return err
}
if _, err := fmt.Fprint(os.Stdout, string(b)); err != nil {
return err
}
}
return nil
}