@@ -8,13 +8,17 @@ package policy
8
8
9
9
import (
10
10
"context"
11
+ "encoding/json"
12
+ "errors"
11
13
"testing"
12
14
13
15
"github.com/stretchr/testify/assert"
14
16
"github.com/stretchr/testify/mock"
15
17
"github.com/stretchr/testify/require"
16
18
19
+ "github.com/elastic/elastic-agent-client/v7/pkg/client"
17
20
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
21
+ "github.com/elastic/fleet-server/v7/internal/pkg/dl"
18
22
"github.com/elastic/fleet-server/v7/internal/pkg/model"
19
23
ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
20
24
testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log"
@@ -462,6 +466,14 @@ func TestPolicyRemoteESOutputPrepare(t *testing.T) {
462
466
mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).
463
467
Return (& apiKey , nil ).Once ()
464
468
bulker .On ("CreateAndGetBulker" , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).Return (outputBulker , false ).Once ()
469
+ bulker .On ("Create" , mock .Anything , dl .FleetOutputHealth , mock .Anything , mock .MatchedBy (func (body []byte ) bool {
470
+ var doc model.OutputHealth
471
+ err := json .Unmarshal (body , & doc )
472
+ if err != nil {
473
+ t .Fatal (err )
474
+ }
475
+ return doc .Message == "" && doc .State == client .UnitStateHealthy .String ()
476
+ }), mock .Anything ).Return ("" , nil )
465
477
466
478
output := Output {
467
479
Type : OutputTypeRemoteElasticsearch ,
@@ -500,4 +512,48 @@ func TestPolicyRemoteESOutputPrepare(t *testing.T) {
500
512
501
513
bulker .AssertExpectations (t )
502
514
})
515
+
516
+ t .Run ("Report degraded output health on API key create failure" , func (t * testing.T ) {
517
+ logger := testlog .SetLogger (t )
518
+ bulker := ftesting .NewMockBulk ()
519
+ var apiKey * bulk.APIKey = nil
520
+ var err error = errors .New ("error connecting" )
521
+
522
+ outputBulker := ftesting .NewMockBulk ()
523
+ outputBulker .On ("APIKeyCreate" ,
524
+ mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).
525
+ Return (apiKey , err ).Once ()
526
+ bulker .On ("CreateAndGetBulker" , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).Return (outputBulker , false ).Once ()
527
+ bulker .On ("Create" , mock .Anything , dl .FleetOutputHealth , mock .Anything , mock .MatchedBy (func (body []byte ) bool {
528
+ var doc model.OutputHealth
529
+ err := json .Unmarshal (body , & doc )
530
+ if err != nil {
531
+ t .Fatal (err )
532
+ }
533
+ return doc .Message == "remote ES could not create API key due to error: error connecting" && doc .State == client .UnitStateDegraded .String ()
534
+ }), mock .Anything ).Return ("" , nil )
535
+
536
+ output := Output {
537
+ Type : OutputTypeRemoteElasticsearch ,
538
+ Name : "test output" ,
539
+ Role : & RoleT {
540
+ Sha2 : "new-hash" ,
541
+ Raw : TestPayload ,
542
+ },
543
+ }
544
+
545
+ policyMap := map [string ]map [string ]interface {}{
546
+ "test output" : map [string ]interface {}{
547
+ "hosts" : []interface {}{"http://localhost" },
548
+ "service_token" : "serviceToken1" ,
549
+ "type" : OutputTypeRemoteElasticsearch ,
550
+ },
551
+ }
552
+ testAgent := & model.Agent {Outputs : map [string ]* model.PolicyOutput {}}
553
+
554
+ err = output .Prepare (context .Background (), logger , bulker , testAgent , policyMap )
555
+ require .NoError (t , err , "expected prepare to pass" )
556
+
557
+ bulker .AssertExpectations (t )
558
+ })
503
559
}
0 commit comments