github.com/thanos-io/thanos@v0.32.5/pkg/prober/http_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package prober
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"net"
    10  	"net/http"
    11  	"path"
    12  	"testing"
    13  
    14  	"github.com/go-kit/log"
    15  	"github.com/oklog/run"
    16  	"github.com/pkg/errors"
    17  
    18  	"github.com/efficientgo/core/testutil"
    19  )
    20  
    21  func TestHTTPProberHealthInitialState(t *testing.T) {
    22  	p := NewHTTP()
    23  
    24  	testutil.Assert(t, !p.isHealthy(), "initially should not be healthy")
    25  }
    26  
    27  func TestHTTPProberReadinessInitialState(t *testing.T) {
    28  	p := NewHTTP()
    29  
    30  	testutil.Assert(t, !p.IsReady(), "initially should not be ready")
    31  }
    32  
    33  func TestHTTPProberHealthyStatusSetting(t *testing.T) {
    34  	testError := errors.New("test error")
    35  	p := NewHTTP()
    36  
    37  	p.Healthy()
    38  
    39  	testutil.Assert(t, p.isHealthy(), "should be healthy")
    40  
    41  	p.NotHealthy(testError)
    42  
    43  	testutil.Assert(t, !p.isHealthy(), "should not be healthy")
    44  }
    45  
    46  func TestHTTPProberReadyStatusSetting(t *testing.T) {
    47  	testError := errors.New("test error")
    48  	p := NewHTTP()
    49  
    50  	p.Ready()
    51  
    52  	testutil.Assert(t, p.IsReady(), "should be ready")
    53  
    54  	p.NotReady(testError)
    55  
    56  	testutil.Assert(t, !p.IsReady(), "should not be ready")
    57  }
    58  
    59  func TestHTTPProberMuxRegistering(t *testing.T) {
    60  	serverAddress := fmt.Sprintf("localhost:%d", 8081)
    61  
    62  	l, err := net.Listen("tcp", serverAddress)
    63  	testutil.Ok(t, err)
    64  
    65  	p := NewHTTP()
    66  
    67  	healthyEndpointPath := "/-/healthy"
    68  	readyEndpointPath := "/-/ready"
    69  
    70  	mux := http.NewServeMux()
    71  	mux.HandleFunc(healthyEndpointPath, p.HealthyHandler(log.NewNopLogger()))
    72  	mux.HandleFunc(readyEndpointPath, p.ReadyHandler(log.NewNopLogger()))
    73  
    74  	var g run.Group
    75  	g.Add(func() error {
    76  		return errors.Wrap(http.Serve(l, mux), "serve probes")
    77  	}, func(err error) {
    78  		t.Fatalf("server failed: %v", err)
    79  	})
    80  
    81  	go func() { _ = g.Run() }()
    82  
    83  	{
    84  		ctx, cancel := context.WithCancel(context.Background())
    85  		defer cancel()
    86  
    87  		resp, err := doGet(ctx, path.Join(serverAddress, healthyEndpointPath))
    88  		testutil.Ok(t, err)
    89  		defer resp.Body.Close()
    90  
    91  		testutil.Equals(t, resp.StatusCode, http.StatusServiceUnavailable, "should not be healthy, response code: %d", resp.StatusCode)
    92  	}
    93  	{
    94  		ctx, cancel := context.WithCancel(context.Background())
    95  		defer cancel()
    96  
    97  		resp, err := doGet(ctx, path.Join(serverAddress, readyEndpointPath))
    98  		testutil.Ok(t, err)
    99  		defer resp.Body.Close()
   100  
   101  		testutil.Equals(t, resp.StatusCode, http.StatusServiceUnavailable, "should not be ready, response code: %d", resp.StatusCode)
   102  	}
   103  	{
   104  		p.Healthy()
   105  
   106  		ctx, cancel := context.WithCancel(context.Background())
   107  		defer cancel()
   108  
   109  		resp, err := doGet(ctx, path.Join(serverAddress, healthyEndpointPath))
   110  		testutil.Ok(t, err)
   111  		defer resp.Body.Close()
   112  
   113  		testutil.Equals(t, resp.StatusCode, http.StatusOK, "should be healthy, response code: %d", resp.StatusCode)
   114  	}
   115  	{
   116  		p.Ready()
   117  
   118  		ctx, cancel := context.WithCancel(context.Background())
   119  		defer cancel()
   120  
   121  		resp, err := doGet(ctx, path.Join(serverAddress, readyEndpointPath))
   122  		testutil.Ok(t, err)
   123  		defer resp.Body.Close()
   124  
   125  		testutil.Equals(t, resp.StatusCode, http.StatusOK, "should be ready, response code: %d", resp.StatusCode)
   126  	}
   127  }
   128  
   129  func doGet(ctx context.Context, url string) (*http.Response, error) {
   130  	req, err := http.NewRequest("GET", fmt.Sprintf("http://%s", url), nil)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	return http.DefaultClient.Do(req.WithContext(ctx))
   136  }