2020package add_docker_metadata
2121
2222import (
23+ "errors"
2324 "fmt"
2425 "os"
2526 "runtime"
@@ -34,6 +35,7 @@ import (
3435 "github.com/elastic/elastic-agent-autodiscover/docker"
3536 "github.com/elastic/elastic-agent-libs/config"
3637 "github.com/elastic/elastic-agent-libs/logp"
38+ "github.com/elastic/elastic-agent-libs/logp/logptest"
3739 "github.com/elastic/elastic-agent-libs/mapstr"
3840 "github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup"
3941 "github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
@@ -114,7 +116,7 @@ func TestInitializationNoDocker(t *testing.T) {
114116func TestInitialization (t * testing.T ) {
115117 var testConfig = config .NewConfig ()
116118
117- p , err := buildDockerMetadataProcessor (logp .L (), testConfig , MockWatcherFactory (nil ))
119+ p , err := buildDockerMetadataProcessor (logp .L (), testConfig , MockWatcherFactory (nil , nil ))
118120 assert .NoError (t , err , "initializing add_docker_metadata processor" )
119121
120122 input := mapstr.M {}
@@ -130,7 +132,7 @@ func TestNoMatch(t *testing.T) {
130132 })
131133 assert .NoError (t , err )
132134
133- p , err := buildDockerMetadataProcessor (logp .L (), testConfig , MockWatcherFactory (nil ))
135+ p , err := buildDockerMetadataProcessor (logp .L (), testConfig , MockWatcherFactory (nil , nil ))
134136 assert .NoError (t , err , "initializing add_docker_metadata processor" )
135137
136138 input := mapstr.M {
@@ -148,7 +150,7 @@ func TestMatchNoContainer(t *testing.T) {
148150 })
149151 assert .NoError (t , err )
150152
151- p , err := buildDockerMetadataProcessor (logp .L (), testConfig , MockWatcherFactory (nil ))
153+ p , err := buildDockerMetadataProcessor (logp .L (), testConfig , MockWatcherFactory (nil , nil ))
152154 assert .NoError (t , err , "initializing add_docker_metadata processor" )
153155
154156 input := mapstr.M {
@@ -179,7 +181,7 @@ func TestMatchContainer(t *testing.T) {
179181 "b.foo" : "3" ,
180182 },
181183 },
182- }))
184+ }, nil ))
183185 assert .NoError (t , err , "initializing add_docker_metadata processor" )
184186
185187 input := mapstr.M {
@@ -227,7 +229,7 @@ func TestMatchContainerWithDedot(t *testing.T) {
227229 "b.foo" : "3" ,
228230 },
229231 },
230- }))
232+ }, nil ))
231233 assert .NoError (t , err , "initializing add_docker_metadata processor" )
232234
233235 input := mapstr.M {
@@ -269,7 +271,7 @@ func TestMatchSource(t *testing.T) {
269271 "b" : "2" ,
270272 },
271273 },
272- }))
274+ }, nil ))
273275 assert .NoError (t , err , "initializing add_docker_metadata processor" )
274276
275277 var inputSource string
@@ -328,7 +330,7 @@ func TestDisableSource(t *testing.T) {
328330 "b" : "2" ,
329331 },
330332 },
331- }))
333+ }, nil ))
332334 assert .NoError (t , err , "initializing add_docker_metadata processor" )
333335
334336 input := mapstr.M {
@@ -354,6 +356,7 @@ func TestMatchPIDs(t *testing.T) {
354356 },
355357 },
356358 },
359+ nil ,
357360 ))
358361 assert .NoError (t , err , "initializing add_docker_metadata processor" )
359362
@@ -436,22 +439,45 @@ func TestMatchPIDs(t *testing.T) {
436439 })
437440}
438441
442+ func TestWatcherError (t * testing.T ) {
443+ logger , observedLogs := logptest .NewTestingLoggerWithObserver (t , "" )
444+ testConfig , err := config .NewConfigFrom (map [string ]interface {}{
445+ "match_fields" : []string {"foo" },
446+ })
447+ assert .NoError (t , err )
448+
449+ p , err := buildDockerMetadataProcessor (logger , testConfig , MockWatcherFactory (nil , errors .New ("mock error" )))
450+ assert .NoError (t , err , "initializing add_docker_metadata processor" )
451+ assert .Len (t , observedLogs .FilterMessageSnippet ("unable to start the docker watcher" ).TakeAll (), 1 )
452+
453+ input := mapstr.M {
454+ "field" : "value" ,
455+ }
456+ result , err := p .Run (& beat.Event {Fields : input })
457+ assert .NoError (t , err , "processing an event" )
458+ assert .Equal (t , mapstr.M {"field" : "value" }, result .Fields )
459+ }
460+
439461// Mock container watcher
440462
441- func MockWatcherFactory (containers map [string ]* docker.Container ) docker.WatcherConstructor {
463+ func MockWatcherFactory (containers map [string ]* docker.Container , startErr error ) docker.WatcherConstructor {
442464 if containers == nil {
443465 containers = make (map [string ]* docker.Container )
444466 }
445467 return func (_ * logp.Logger , host string , tls * docker.TLSConfig , shortID bool ) (docker.Watcher , error ) {
446- return & mockWatcher {containers : containers }, nil
468+ return & mockWatcher {containers : containers , startErr : startErr }, nil
447469 }
448470}
449471
450472type mockWatcher struct {
451473 containers map [string ]* docker.Container
474+ startErr error
452475}
453476
454477func (m * mockWatcher ) Start () error {
478+ if m .startErr != nil {
479+ return m .startErr
480+ }
455481 return nil
456482}
457483
0 commit comments