@@ -32,6 +32,7 @@ import (
3232 v2 "github.com/elastic/beats/v7/filebeat/input/v2"
3333 "github.com/elastic/beats/v7/libbeat/beat"
3434 "github.com/elastic/beats/v7/libbeat/common/acker"
35+ "github.com/elastic/beats/v7/libbeat/management/status"
3536 "github.com/elastic/beats/v7/libbeat/statestore"
3637 "github.com/elastic/beats/v7/libbeat/statestore/storetest"
3738 conf "github.com/elastic/elastic-agent-libs/config"
@@ -40,10 +41,11 @@ import (
4041)
4142
4243type inputTestingEnvironment struct {
43- t * testing.T
44- workingDir string
45- stateStore * testInputStore
46- pipeline * mockPipelineConnector
44+ t * testing.T
45+ workingDir string
46+ stateStore * testInputStore
47+ pipeline * mockPipelineConnector
48+ statusReporter * mockStatusReporter
4749
4850 pluginInitOnce sync.Once
4951 plugin v2.Plugin
@@ -54,10 +56,11 @@ type inputTestingEnvironment struct {
5456
5557func newInputTestingEnvironment (t * testing.T ) * inputTestingEnvironment {
5658 return & inputTestingEnvironment {
57- t : t ,
58- workingDir : t .TempDir (),
59- stateStore : openTestStatestore (),
60- pipeline : & mockPipelineConnector {},
59+ t : t ,
60+ workingDir : t .TempDir (),
61+ stateStore : openTestStatestore (),
62+ pipeline : & mockPipelineConnector {},
63+ statusReporter : & mockStatusReporter {},
6164 }
6265}
6366
@@ -95,7 +98,7 @@ func (e *inputTestingEnvironment) startInput(ctx context.Context, inp v2.Input)
9598 }
9699 }()
97100
98- inputCtx := v2.Context {Logger : logp .L (), Cancelation : ctx }
101+ inputCtx := v2.Context {Logger : logp .L (), Cancelation : ctx , StatusReporter : e . statusReporter }
99102 if err := inp .Run (inputCtx , e .pipeline ); err != nil {
100103 e .t .Errorf ("input 'Run' method returned an error: %s" , err )
101104 }
@@ -125,6 +128,25 @@ func (e *inputTestingEnvironment) waitUntilEventCount(count int) {
125128 }, 5 * time .Second , 10 * time .Millisecond , & msg )
126129}
127130
131+ func (e * inputTestingEnvironment ) RequireStatuses (expected []statusUpdate ) {
132+ t := e .t
133+ t .Helper ()
134+ got := e .statusReporter .GetUpdates ()
135+ if len (got ) != len (expected ) {
136+ t .Fatalf ("expecting %d updates, got %d" , len (expected ), len (got ))
137+ }
138+
139+ for i := range expected {
140+ g , e := got [i ], expected [i ]
141+ if g != e {
142+ t .Errorf (
143+ "expecting [%d] status update to be {state:%s, msg:%s}, got {state:%s, msg:%s}" ,
144+ i , e .state .String (), e .msg , g .state .String (), g .msg ,
145+ )
146+ }
147+ }
148+ }
149+
128150type testInputStore struct {
129151 registry * statestore.Registry
130152}
@@ -251,3 +273,25 @@ func blockingACKer(starter context.Context) beat.EventListener {
251273 }
252274 })
253275}
276+
277+ type statusUpdate struct {
278+ state status.Status
279+ msg string
280+ }
281+
282+ type mockStatusReporter struct {
283+ mutex sync.RWMutex
284+ updates []statusUpdate
285+ }
286+
287+ func (m * mockStatusReporter ) UpdateStatus (status status.Status , msg string ) {
288+ m .mutex .Lock ()
289+ m .updates = append (m .updates , statusUpdate {status , msg })
290+ m .mutex .Unlock ()
291+ }
292+
293+ func (m * mockStatusReporter ) GetUpdates () []statusUpdate {
294+ m .mutex .RLock ()
295+ defer m .mutex .RUnlock ()
296+ return append ([]statusUpdate {}, m .updates ... )
297+ }
0 commit comments