github.com/cilium/cilium@v1.16.2/operator/api/health_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package api
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/cilium/hive/cell"
    13  	"github.com/cilium/hive/hivetest"
    14  	"github.com/go-openapi/runtime"
    15  	"go.uber.org/goleak"
    16  
    17  	"github.com/cilium/cilium/api/v1/operator/server/restapi/operator"
    18  	"github.com/cilium/cilium/pkg/hive"
    19  	k8sClient "github.com/cilium/cilium/pkg/k8s/client"
    20  	"github.com/cilium/cilium/pkg/safeio"
    21  )
    22  
    23  func TestHealthHandlerK8sDisabled(t *testing.T) {
    24  	defer goleak.VerifyNone(
    25  		t,
    26  		// ignore goroutine started from sigs.k8s.io/controller-runtime/pkg/log.go init function
    27  		goleak.IgnoreTopFunction("time.Sleep"),
    28  	)
    29  
    30  	rr := httptest.NewRecorder()
    31  
    32  	hive := hive.New(
    33  		k8sClient.FakeClientCell,
    34  		cell.Invoke(func(cs *k8sClient.FakeClientset) {
    35  			cs.Disable()
    36  		}),
    37  
    38  		HealthHandlerCell(
    39  			func() bool {
    40  				return false
    41  			},
    42  			func() bool {
    43  				return false
    44  			},
    45  		),
    46  
    47  		// transform GetHealthzHandler in a http.HandlerFunc to use
    48  		// the http package testing facilities
    49  		cell.Provide(func(h operator.GetHealthzHandler) http.HandlerFunc {
    50  			return func(w http.ResponseWriter, r *http.Request) {
    51  				res := h.Handle(operator.GetHealthzParams{})
    52  				res.WriteResponse(w, runtime.TextProducer())
    53  			}
    54  		}),
    55  
    56  		cell.Invoke(func(hf http.HandlerFunc) {
    57  			req := httptest.NewRequest(http.MethodGet, "http://localhost/healthz", nil)
    58  			hf.ServeHTTP(rr, req)
    59  		}),
    60  	)
    61  
    62  	tlog := hivetest.Logger(t)
    63  	if err := hive.Start(tlog, context.Background()); err != nil {
    64  		t.Fatalf("failed to start: %s", err)
    65  	}
    66  
    67  	if rr.Result().StatusCode != http.StatusNotImplemented {
    68  		t.Fatalf("expected http status code %d, got %d", http.StatusNotImplemented, rr.Result().StatusCode)
    69  	}
    70  
    71  	if err := hive.Stop(tlog, context.Background()); err != nil {
    72  		t.Fatalf("failed to stop: %s", err)
    73  	}
    74  }
    75  
    76  func TestHealthHandlerK8sEnabled(t *testing.T) {
    77  	defer goleak.VerifyNone(
    78  		t,
    79  		// ignore goroutine started from sigs.k8s.io/controller-runtime/pkg/log.go init function
    80  		goleak.IgnoreTopFunction("time.Sleep"),
    81  	)
    82  
    83  	rr := httptest.NewRecorder()
    84  
    85  	hive := hive.New(
    86  		k8sClient.FakeClientCell,
    87  
    88  		HealthHandlerCell(
    89  			func() bool {
    90  				return false
    91  			},
    92  			func() bool {
    93  				return false
    94  			},
    95  		),
    96  
    97  		// transform GetHealthzHandler in a http.HandlerFunc to use
    98  		// the http package testing facilities
    99  		cell.Provide(func(h operator.GetHealthzHandler) http.HandlerFunc {
   100  			return func(w http.ResponseWriter, r *http.Request) {
   101  				res := h.Handle(operator.GetHealthzParams{})
   102  				res.WriteResponse(w, runtime.TextProducer())
   103  			}
   104  		}),
   105  
   106  		cell.Invoke(func(hf http.HandlerFunc) {
   107  			req := httptest.NewRequest(http.MethodGet, "http://localhost/healthz", nil)
   108  			hf.ServeHTTP(rr, req)
   109  		}),
   110  	)
   111  
   112  	tlog := hivetest.Logger(t)
   113  	if err := hive.Start(tlog, context.Background()); err != nil {
   114  		t.Fatalf("failed to start: %s", err)
   115  	}
   116  
   117  	if rr.Result().StatusCode != http.StatusOK {
   118  		t.Fatalf("expected http status code %d, got %d", http.StatusOK, rr.Result().StatusCode)
   119  	}
   120  
   121  	body, err := safeio.ReadAllLimit(rr.Result().Body, safeio.KB)
   122  	if err != nil {
   123  		t.Fatalf("error while reading response body: %s", err)
   124  	}
   125  	rr.Result().Body.Close()
   126  
   127  	if string(body) != "ok" {
   128  		t.Fatalf("expected response body %q, got: %q", "ok", string(body))
   129  	}
   130  
   131  	if err := hive.Stop(tlog, context.Background()); err != nil {
   132  		t.Fatalf("failed to stop: %s", err)
   133  	}
   134  }