...

Source file src/edge-infra.dev/pkg/lib/server/liveoption_test.go

Documentation: edge-infra.dev/pkg/lib/server

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"edge-infra.dev/pkg/lib/server/inmemorylistener"
     9  )
    10  
    11  func TestLiveFuncOption(t *testing.T) {
    12  	var l = inmemorylistener.New()
    13  	var httpc = l.HTTPClient()
    14  	var s, setLivenessCode, err = NewLivenessServer(OptionListener(l))
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	go s.Run()                             //nolint:errcheck
    19  	defer s.Shutdown(context.Background()) //nolint:errcheck
    20  
    21  	// Check that the live function is serving HTTP status 503 (aka http.StatusServiceUnavailable)
    22  	for i := 0; i < 5; i++ {
    23  		if resp, err := httpc.Get("http://localhost/livez"); err != nil {
    24  			t.Fatal(err)
    25  		} else if resp.StatusCode != http.StatusServiceUnavailable {
    26  			const msg = "Want http.StatusServiceUnavailable status code %d before the live func has been called. Got status code %d"
    27  			t.Fatalf(msg, http.StatusServiceUnavailable, resp.StatusCode)
    28  		}
    29  	}
    30  	t.Logf("Yay, got status %d before live function was called", http.StatusServiceUnavailable)
    31  
    32  	// Call the live function so the server serves 200 OK.
    33  	t.Logf("Setting live function using code 200 and text 'LIVE'")
    34  	setLivenessCode(http.StatusOK)
    35  	t.Logf("Live function called.")
    36  
    37  	// Check that the live function is serving 200 OK
    38  	for i := 0; i < 5; i++ {
    39  		if resp, err := httpc.Get("http://localhost/livez"); err != nil {
    40  			t.Fatal(err)
    41  		} else if resp.StatusCode != http.StatusOK {
    42  			const msg = "Want http.StatusOk status code %d after the live func has been called. Got status code %d"
    43  			t.Fatalf(msg, http.StatusOK, resp.StatusCode)
    44  		}
    45  	}
    46  	t.Logf("Yay, got status %d after live function was called", http.StatusOK)
    47  
    48  	// Call the live function so the server serves 500.
    49  	t.Logf("Setting live function using code 500")
    50  	setLivenessCode(http.StatusInternalServerError)
    51  	t.Logf("Live function called.")
    52  
    53  	// Check that the live function is serving 500.
    54  	for i := 0; i < 5; i++ {
    55  		if resp, err := httpc.Get("http://localhost/livez"); err != nil {
    56  			t.Fatal(err)
    57  		} else if resp.StatusCode != http.StatusInternalServerError {
    58  			const msg = "Want http.StatusInternalServerError status code %d after the live func has been called. Got status code %d"
    59  			t.Fatalf(msg, http.StatusInternalServerError, resp.StatusCode)
    60  		}
    61  	}
    62  	t.Logf("Yay, got status %d after live function was called", http.StatusInternalServerError)
    63  }
    64  

View as plain text