forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.go
56 lines (46 loc) · 1.32 KB
/
testing.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
package module
import (
"encoding/json"
"errors"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/testing"
)
// receiveOneEvent receives one event from the events channel then closes the
// returned done channel. If no events are received it will close the returned
// done channel after the timeout period elapses.
func receiveOneEvent(d testing.Driver, events <-chan beat.Event, timeout time.Duration) <-chan struct{} {
done := make(chan struct{})
go func() {
defer close(done)
select {
case <-time.Tick(timeout):
d.Error("error", errors.New("timeout waiting for an event"))
case event, ok := <-events:
if !ok {
return
}
// At this point in the pipeline the error has been converted to a
// string and written to error.message.
if v, err := event.Fields.GetValue("error.message"); err == nil {
if errMsg, ok := v.(string); ok {
d.Error("error", errors.New(errMsg))
return
}
}
outputJSON(d, &event)
}
}()
return done
}
func outputJSON(d testing.Driver, event *beat.Event) {
out := event.Fields.Clone()
out.Put("@timestamp", common.Time(event.Timestamp))
jsonData, err := json.MarshalIndent(out, "", " ")
if err != nil {
d.Error("convert error", err)
return
}
d.Result(string(jsonData))
}