2020-05-04 18:09:10 +00:00
|
|
|
package shim
|
|
|
|
|
|
|
|
import (
|
2020-05-05 14:14:57 +00:00
|
|
|
"bufio"
|
2020-05-04 18:09:10 +00:00
|
|
|
"bytes"
|
2020-05-05 14:14:57 +00:00
|
|
|
"io"
|
2020-05-04 18:09:10 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShimWorks(t *testing.T) {
|
|
|
|
stdoutBytes := bytes.NewBufferString("")
|
|
|
|
stdout = stdoutBytes
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
stdin, _ = io.Pipe() // hold the stdin pipe open
|
|
|
|
|
2020-05-04 18:09:10 +00:00
|
|
|
timeout := time.NewTimer(10 * time.Second)
|
2020-05-05 14:14:57 +00:00
|
|
|
metricProcessed, _ := runInputPlugin(t, 10*time.Millisecond)
|
2020-05-04 18:09:10 +00:00
|
|
|
|
|
|
|
select {
|
2020-05-05 14:14:57 +00:00
|
|
|
case <-metricProcessed:
|
2020-05-04 18:09:10 +00:00
|
|
|
case <-timeout.C:
|
|
|
|
require.Fail(t, "Timeout waiting for metric to arrive")
|
|
|
|
}
|
|
|
|
for stdoutBytes.Len() == 0 {
|
|
|
|
select {
|
|
|
|
case <-timeout.C:
|
|
|
|
require.Fail(t, "Timeout waiting to read metric from stdout")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out := string(stdoutBytes.Bytes())
|
|
|
|
require.Contains(t, out, "\n")
|
|
|
|
metricLine := strings.Split(out, "\n")[0]
|
|
|
|
require.Equal(t, "measurement,tag=tag field=1i 1234000005678", metricLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShimStdinSignalingWorks(t *testing.T) {
|
2020-05-05 14:14:57 +00:00
|
|
|
stdinReader, stdinWriter := io.Pipe()
|
|
|
|
stdoutReader, stdoutWriter := io.Pipe()
|
|
|
|
|
|
|
|
stdin = stdinReader
|
|
|
|
stdout = stdoutWriter
|
2020-05-04 18:09:10 +00:00
|
|
|
|
|
|
|
timeout := time.NewTimer(10 * time.Second)
|
2020-05-05 14:14:57 +00:00
|
|
|
metricProcessed, exited := runInputPlugin(t, 40*time.Second)
|
2020-05-04 18:09:10 +00:00
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
stdinWriter.Write([]byte("\n"))
|
2020-05-04 18:09:10 +00:00
|
|
|
|
|
|
|
select {
|
2020-05-05 14:14:57 +00:00
|
|
|
case <-metricProcessed:
|
2020-05-04 18:09:10 +00:00
|
|
|
case <-timeout.C:
|
|
|
|
require.Fail(t, "Timeout waiting for metric to arrive")
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
r := bufio.NewReader(stdoutReader)
|
|
|
|
out, err := r.ReadString('\n')
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "measurement,tag=tag field=1i 1234000005678\n", out)
|
2020-05-04 18:09:10 +00:00
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
stdinWriter.Close()
|
|
|
|
// check that it exits cleanly
|
|
|
|
<-exited
|
2020-05-04 18:09:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 14:14:57 +00:00
|
|
|
func runInputPlugin(t *testing.T, interval time.Duration) (metricProcessed chan bool, exited chan bool) {
|
|
|
|
metricProcessed = make(chan bool)
|
|
|
|
exited = make(chan bool)
|
2020-05-04 18:09:10 +00:00
|
|
|
inp := &testInput{
|
2020-05-05 14:14:57 +00:00
|
|
|
metricProcessed: metricProcessed,
|
2020-05-04 18:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shim := New()
|
|
|
|
shim.AddInput(inp)
|
|
|
|
go func() {
|
2020-05-05 14:14:57 +00:00
|
|
|
err := shim.Run(interval)
|
2020-05-04 18:09:10 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-05 14:14:57 +00:00
|
|
|
exited <- true
|
2020-05-04 18:09:10 +00:00
|
|
|
}()
|
2020-05-05 14:14:57 +00:00
|
|
|
return metricProcessed, exited
|
2020-05-04 18:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testInput struct {
|
2020-05-05 14:14:57 +00:00
|
|
|
metricProcessed chan bool
|
2020-05-04 18:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInput) SampleConfig() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInput) Description() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInput) Gather(acc telegraf.Accumulator) error {
|
|
|
|
acc.AddFields("measurement",
|
|
|
|
map[string]interface{}{
|
|
|
|
"field": 1,
|
|
|
|
},
|
|
|
|
map[string]string{
|
|
|
|
"tag": "tag",
|
|
|
|
}, time.Unix(1234, 5678))
|
2020-05-05 14:14:57 +00:00
|
|
|
i.metricProcessed <- true
|
2020-05-04 18:09:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInput) Start(acc telegraf.Accumulator) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInput) Stop() {
|
|
|
|
}
|