5
5
"context"
6
6
"encoding/json"
7
7
"fmt"
8
+ "io"
8
9
"io/ioutil"
9
10
"net/http"
10
11
"net/http/httptest"
@@ -18,6 +19,7 @@ import (
18
19
19
20
"github.com/go-kit/kit/log"
20
21
vault "github.com/hashicorp/vault/api"
22
+ "github.com/stretchr/testify/assert"
21
23
)
22
24
23
25
const (
@@ -545,6 +547,105 @@ func TestListWorkflows(t *testing.T) {
545
547
runTests (t , tests )
546
548
}
547
549
550
+ func TestHealthCheck (t * testing.T ) {
551
+ tests := []struct {
552
+ name string
553
+ endpoint string // Used to cause a connection error.
554
+ vaultStatusCode int
555
+ writeBadContentLength bool // Used to create response body error.
556
+ wantResponseBody string
557
+ wantStatusCode int
558
+ }{
559
+ {
560
+ name : "good_vault_200" ,
561
+ vaultStatusCode : http .StatusOK ,
562
+ wantResponseBody : "Health check succeeded\n " ,
563
+ wantStatusCode : http .StatusOK ,
564
+ },
565
+ {
566
+ name : "good_vault_429" ,
567
+ vaultStatusCode : http .StatusTooManyRequests ,
568
+ wantResponseBody : "Health check succeeded\n " ,
569
+ wantStatusCode : http .StatusOK ,
570
+ },
571
+ {
572
+ // We want successful health check in this vault error scenario.
573
+ name : "error_vault_read_response" ,
574
+ vaultStatusCode : http .StatusOK ,
575
+ writeBadContentLength : true ,
576
+ wantResponseBody : "Health check succeeded\n " ,
577
+ wantStatusCode : http .StatusOK ,
578
+ },
579
+ {
580
+ name : "error_vault_connection" ,
581
+ endpoint : string ('\f' ),
582
+ wantResponseBody : "Health check failed\n " ,
583
+ wantStatusCode : http .StatusServiceUnavailable ,
584
+ },
585
+ {
586
+ name : "error_vault_unhealthy_status_code" ,
587
+ vaultStatusCode : http .StatusInternalServerError ,
588
+ wantResponseBody : "Health check failed\n " ,
589
+ wantStatusCode : http .StatusServiceUnavailable ,
590
+ },
591
+ }
592
+
593
+ for _ , tt := range tests {
594
+ t .Run (tt .name , func (t * testing.T ) {
595
+ wantURL := "/v1/sys/health"
596
+
597
+ vaultSvc := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
598
+ if r .URL .Path != wantURL {
599
+ http .NotFound (w , r )
600
+ }
601
+
602
+ if r .Method != http .MethodGet {
603
+ w .WriteHeader (http .StatusMethodNotAllowed )
604
+ return
605
+ }
606
+
607
+ if tt .writeBadContentLength {
608
+ w .Header ().Set ("Content-Length" , "1" )
609
+ }
610
+
611
+ w .WriteHeader (tt .vaultStatusCode )
612
+ }))
613
+ defer vaultSvc .Close ()
614
+
615
+ vaultEndpoint := vaultSvc .URL
616
+ if tt .endpoint != "" {
617
+ vaultEndpoint = tt .endpoint
618
+ }
619
+
620
+ h := handler {
621
+ logger : log .NewNopLogger (),
622
+ env : env.Vars {
623
+ VaultAddress : vaultEndpoint ,
624
+ },
625
+ }
626
+
627
+ // Dummy request.
628
+ req , err := http .NewRequest ("" , "" , nil )
629
+ if err != nil {
630
+ assert .Nil (t , err )
631
+ }
632
+
633
+ resp := httptest .NewRecorder ()
634
+
635
+ h .healthCheck (resp , req )
636
+
637
+ respResult := resp .Result ()
638
+ defer respResult .Body .Close ()
639
+
640
+ body , err := io .ReadAll (respResult .Body )
641
+ assert .Nil (t , err )
642
+
643
+ assert .Equal (t , tt .wantStatusCode , respResult .StatusCode )
644
+ assert .Equal (t , tt .wantResponseBody , string (body ))
645
+ })
646
+ }
647
+ }
648
+
548
649
// Serialize a type to JSON-encoded byte buffer.
549
650
func serialize (toMarshal interface {}) * bytes.Buffer {
550
651
jsonStr , _ := json .Marshal (toMarshal )
0 commit comments