forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecd_test.go
More file actions
85 lines (66 loc) · 1.82 KB
/
Copy pathexecd_test.go
File metadata and controls
85 lines (66 loc) · 1.82 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
// +build !windows
package execd
import (
"testing"
"time"
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/models"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf"
)
func TestExternalInputWorks(t *testing.T) {
jsonParser, err := parsers.NewInfluxParser()
require.NoError(t, err)
e := &Execd{
Command: []string{shell(), fileShellScriptPath()},
RestartDelay: config.Duration(5 * time.Second),
parser: jsonParser,
Signal: "STDIN",
}
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))
e.Stop()
// grab a metric and make sure it's a thing
m := readChanWithTimeout(t, metrics, 10*time.Second)
require.Equal(t, "counter_bash", m.Name())
val, ok := m.GetField("count")
require.True(t, ok)
require.Equal(t, float64(0), val)
// test that a later gather will not panic
e.Gather(acc)
}
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
}
func fileShellScriptPath() string {
return "./examples/count.sh"
}
func shell() string {
return "sh"
}
type TestMetricMaker struct{}
func (tm *TestMetricMaker) Name() string {
return "TestPlugin"
}
func (tm *TestMetricMaker) LogName() string {
return tm.Name()
}
func (tm *TestMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric {
return metric
}
func (tm *TestMetricMaker) Log() telegraf.Logger {
return models.NewLogger("TestPlugin", "test", "")
}