forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goshim_test.go
77 lines (62 loc) · 1.52 KB
/
goshim_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
package shim
import (
"bufio"
"errors"
"io"
"log"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
)
func TestShimSetsUpLogger(t *testing.T) {
stderrReader, stderrWriter := io.Pipe()
stdinReader, stdinWriter := io.Pipe()
runErroringInputPlugin(t, 40*time.Second, stdinReader, nil, stderrWriter)
_, err := stdinWriter.Write([]byte("\n"))
require.NoError(t, err)
r := bufio.NewReader(stderrReader)
out, err := r.ReadString('\n')
require.NoError(t, err)
require.Contains(t, out, "Error in plugin: intentional")
err = stdinWriter.Close()
require.NoError(t, err)
}
func runErroringInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdout, stderr io.Writer) (metricProcessed chan bool, exited chan bool) {
metricProcessed = make(chan bool, 1)
exited = make(chan bool, 1)
inp := &erroringInput{}
shim := New()
if stdin != nil {
shim.stdin = stdin
}
if stdout != nil {
shim.stdout = stdout
}
if stderr != nil {
shim.stderr = stderr
log.SetOutput(stderr)
}
err := shim.AddInput(inp)
require.NoError(t, err)
go func() {
err := shim.Run(interval)
require.NoError(t, err)
exited <- true
}()
return metricProcessed, exited
}
type erroringInput struct {
}
func (i *erroringInput) SampleConfig() string {
return ""
}
func (i *erroringInput) Gather(acc telegraf.Accumulator) error {
acc.AddError(errors.New("intentional"))
return nil
}
func (i *erroringInput) Start(_ telegraf.Accumulator) error {
return nil
}
func (i *erroringInput) Stop() {
}