@@ -444,9 +444,16 @@ type StatusItem struct {
444444 Duration time.Duration
445445}
446446
447+ type instanceUpInfo struct {
448+ Version string
449+ Role string
450+ Mode string
451+ }
452+
447453// TestInstance checks the `up` status of an arangod server instance.
448- func (s * Service ) TestInstance (ctx context.Context , address string , port int , statusChanged chan StatusItem ) (up bool , version string , statusTrail []int , cancelled bool ) {
449- instanceUp := make (chan string )
454+ func (s * Service ) TestInstance (ctx context.Context , address string , port int ,
455+ expectedRole , expectedMode string , statusChanged chan StatusItem ) (up , correctRole bool , version , role , mode string , statusTrail []int , cancelled bool ) {
456+ instanceUp := make (chan instanceUpInfo )
450457 statusCodes := make (chan int )
451458 if statusChanged != nil {
452459 defer close (statusChanged )
@@ -464,7 +471,7 @@ func (s *Service) TestInstance(ctx context.Context, address string, port int, st
464471 },
465472 }
466473 }
467- makeRequest := func () (string , int , error ) {
474+ makeVersionRequest := func () (string , int , error ) {
468475 addr := net .JoinHostPort (address , strconv .Itoa (port ))
469476 url := fmt .Sprintf ("%s://%s/_api/version" , scheme , addr )
470477 req , err := http .NewRequest ("GET" , url , nil )
@@ -491,24 +498,63 @@ func (s *Service) TestInstance(ctx context.Context, address string, port int, st
491498 }
492499 return versionResponse .Version , resp .StatusCode , nil
493500 }
501+ makeRoleRequest := func () (string , string , int , error ) {
502+ addr := net .JoinHostPort (address , strconv .Itoa (port ))
503+ url := fmt .Sprintf ("%s://%s/_admin/server/role" , scheme , addr )
504+ req , err := http .NewRequest ("GET" , url , nil )
505+ if err != nil {
506+ return "" , "" , - 1 , maskAny (err )
507+ }
508+ if err := addJwtHeader (req , s .jwtSecret ); err != nil {
509+ return "" , "" , - 2 , maskAny (err )
510+ }
511+ resp , err := client .Do (req )
512+ if err != nil {
513+ return "" , "" , - 3 , maskAny (err )
514+ }
515+ if resp .StatusCode != 200 {
516+ return "" , "" , resp .StatusCode , maskAny (fmt .Errorf ("Invalid status %d" , resp .StatusCode ))
517+ }
518+ roleResponse := struct {
519+ Role string `json:"role,omitempty"`
520+ Mode string `json:"mode,omitempty"`
521+ }{}
522+ defer resp .Body .Close ()
523+ decoder := json .NewDecoder (resp .Body )
524+ if err := decoder .Decode (& roleResponse ); err != nil {
525+ return "" , "" , - 4 , maskAny (fmt .Errorf ("Unexpected role response: %#v" , err ))
526+ }
527+ return roleResponse .Role , roleResponse .Mode , resp .StatusCode , nil
528+ }
494529
495530 for i := 0 ; i < 300 ; i ++ {
496- if version , statusCode , err := makeRequest (); err == nil {
497- instanceUp <- version
531+ if version , statusCode , err := makeVersionRequest (); err == nil {
532+ if role , mode , statusCode , err := makeRoleRequest (); err == nil {
533+ instanceUp <- instanceUpInfo {
534+ Version : version ,
535+ Role : role ,
536+ Mode : mode ,
537+ }
538+ } else {
539+ statusCodes <- statusCode
540+ }
498541 break
499542 } else {
500543 statusCodes <- statusCode
501544 }
502545 time .Sleep (time .Millisecond * 500 )
503546 }
504- instanceUp <- ""
547+ instanceUp <- instanceUpInfo {}
505548 }()
506549 statusTrail = make ([]int , 0 , 16 )
507550 startTime := time .Now ()
508551 for {
509552 select {
510- case version := <- instanceUp :
511- return version != "" , version , statusTrail , false
553+ case instanceInfo := <- instanceUp :
554+ up = instanceInfo .Version != ""
555+ correctRole = instanceInfo .Role == expectedRole || expectedRole == ""
556+ correctMode := instanceInfo .Mode == expectedMode || expectedMode == ""
557+ return up , correctRole && correctMode , instanceInfo .Version , instanceInfo .Role , instanceInfo .Mode , statusTrail , false
512558 case statusCode := <- statusCodes :
513559 lastStatusCode := math .MinInt32
514560 if len (statusTrail ) > 0 {
@@ -525,7 +571,7 @@ func (s *Service) TestInstance(ctx context.Context, address string, port int, st
525571 }
526572 }
527573 case <- ctx .Done ():
528- return false , "" , statusTrail , true
574+ return false , false , "" , "" , "" , statusTrail , true
529575 }
530576 }
531577}
0 commit comments