@@ -33,6 +33,7 @@ import (
33
33
"github.com/elastic/elastic-agent/pkg/component"
34
34
"github.com/elastic/elastic-agent/pkg/component/runtime"
35
35
agentclient "github.com/elastic/elastic-agent/pkg/control/v2/client"
36
+ "github.com/elastic/elastic-agent/pkg/core/logger"
36
37
"github.com/elastic/elastic-agent/pkg/utils/broadcaster"
37
38
)
38
39
@@ -331,22 +332,31 @@ func TestCoordinatorReportsInvalidPolicy(t *testing.T) {
331
332
// does let's report a failure instead of timing out the test runner.
332
333
ctx , cancel := context .WithTimeout (context .Background (), time .Second )
333
334
defer cancel ()
334
- logger := logp .NewLogger ("testing" )
335
+
336
+ log , obs := logger .NewTesting ("" )
337
+ defer func () {
338
+ if t .Failed () {
339
+ t .Log ("test failed, coordinator logs below:" )
340
+ for _ , l := range obs .TakeAll () {
341
+ t .Log (l )
342
+ }
343
+ }
344
+ }()
335
345
336
346
upgradeMgr , err := upgrade .NewUpgrader (
337
- logger ,
347
+ log ,
338
348
& artifact.Config {},
339
349
& info.AgentInfo {},
340
350
)
341
- require .NoError (t , err )
351
+ require .NoError (t , err , "errored when creating a new upgrader" )
342
352
343
353
// Channels have buffer length 1, so we don't have to run on multiple
344
354
// goroutines.
345
355
stateChan := make (chan State , 1 )
346
356
configChan := make (chan ConfigChange , 1 )
347
357
varsChan := make (chan []* transpiler.Vars , 1 )
348
358
coord := & Coordinator {
349
- logger : logger ,
359
+ logger : log ,
350
360
state : State {
351
361
CoordinatorState : agentclient .Healthy ,
352
362
CoordinatorMessage : "Running" ,
@@ -390,19 +400,18 @@ agent.download.sourceURI:
390
400
"failed to reload upgrade manager configuration" ,
391
401
"configErr should match policy failure, got %v" , coord .configErr )
392
402
403
+ stateChangeTimeout := 2 * time .Second
393
404
select {
394
405
case state := <- stateChan :
395
406
assert .Equal (t , agentclient .Failed , state .State , "Failed policy change should cause Failed coordinator state" )
396
407
assert .Contains (t , state .Message , cfgChange .err .Error (), "Coordinator state should report failed policy change" )
397
408
398
409
// The component model update happens on a goroutine, thus the new state
399
410
// might not have been sent yet. Therefore, a timeout is required.
400
- case <- time .After (time . Second ):
401
- assert . Fail ( t , " Coordinator's state didn't change" )
411
+ case <- time .After (stateChangeTimeout ):
412
+ t . Fatalf ( "timedout after %s waiting Coordinator's state to change", stateChangeTimeout )
402
413
}
403
414
404
- // isn't this another test?
405
-
406
415
// Send an empty vars update. This should regenerate the component model
407
416
// based on the last good (empty) policy, producing a "successful" update,
408
417
// but the overall reported state should still be Failed because the last
@@ -420,8 +429,9 @@ agent.download.sourceURI:
420
429
421
430
// The component model update happens on a goroutine, thus the new state
422
431
// might not have been sent yet. Therefore, a timeout is required.
423
- case <- time .After (time .Second ):
424
- assert .Fail (t , "Vars change should cause state update" )
432
+ case <- time .After (stateChangeTimeout ):
433
+ t .Fatalf ("timedout after %s waiting Vars change to cause a state update" ,
434
+ stateChangeTimeout )
425
435
}
426
436
427
437
// Finally, send an empty (valid) policy update and confirm that it
@@ -439,8 +449,9 @@ agent.download.sourceURI:
439
449
440
450
// The component model update happens on a goroutine, thus the new state
441
451
// might not have been sent yet. Therefore, a timeout is required.
442
- case <- time .After (time .Second ):
443
- assert .Fail (t , "Policy change should cause state update" )
452
+ case <- time .After (stateChangeTimeout ):
453
+ t .Fatalf ("timedout after %s waiting Policy change to cause a state update" ,
454
+ stateChangeTimeout )
444
455
}
445
456
}
446
457
0 commit comments