forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_test.go
105 lines (93 loc) · 2.03 KB
/
exec_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
package exec
import (
"bytes"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/influxdata/telegraf/testutil"
)
func TestExec(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test due to OS/executable dependencies")
}
tests := []struct {
name string
command []string
err bool
metrics []telegraf.Metric
}{
{
name: "test success",
command: []string{"tee"},
err: false,
metrics: testutil.MockMetrics(),
},
{
name: "test doesn't accept stdin",
command: []string{"sleep", "5s"},
err: true,
metrics: testutil.MockMetrics(),
},
{
name: "test command not found",
command: []string{"/no/exist", "-h"},
err: true,
metrics: testutil.MockMetrics(),
},
{
name: "test no metrics output",
command: []string{"tee"},
err: false,
metrics: []telegraf.Metric{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &Exec{
Command: tt.command,
Timeout: internal.Duration{Duration: time.Second},
runner: &CommandRunner{},
}
s, _ := serializers.NewInfluxSerializer()
e.SetSerializer(s)
e.Connect()
require.Equal(t, tt.err, e.Write(tt.metrics) != nil)
})
}
}
func TestTruncate(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
len int
}{
{
name: "long out",
buf: bytes.NewBufferString(strings.Repeat("a", maxStderrBytes+100)),
len: maxStderrBytes + len("..."),
},
{
name: "multiline out",
buf: bytes.NewBufferString("hola\ngato\n"),
len: len("hola") + len("..."),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := truncate(*tt.buf)
require.Equal(t, tt.len, len(s))
})
}
}
func TestExecDocs(t *testing.T) {
e := &Exec{}
e.Description()
e.SampleConfig()
require.NoError(t, e.Close())
e = &Exec{runner: &CommandRunner{}}
require.NoError(t, e.Close())
}