...

Source file src/github.com/docker/distribution/health/health_test.go

Documentation: github.com/docker/distribution/health

     1  package health
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  )
    10  
    11  // TestReturns200IfThereAreNoChecks ensures that the result code of the health
    12  // endpoint is 200 if there are not currently registered checks.
    13  func TestReturns200IfThereAreNoChecks(t *testing.T) {
    14  	recorder := httptest.NewRecorder()
    15  
    16  	req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health", nil)
    17  	if err != nil {
    18  		t.Errorf("Failed to create request.")
    19  	}
    20  
    21  	StatusHandler(recorder, req)
    22  
    23  	if recorder.Code != 200 {
    24  		t.Errorf("Did not get a 200.")
    25  	}
    26  }
    27  
    28  // TestReturns503IfThereAreErrorChecks ensures that the result code of the
    29  // health endpoint is 503 if there are health checks with errors.
    30  func TestReturns503IfThereAreErrorChecks(t *testing.T) {
    31  	recorder := httptest.NewRecorder()
    32  
    33  	req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health", nil)
    34  	if err != nil {
    35  		t.Errorf("Failed to create request.")
    36  	}
    37  
    38  	// Create a manual error
    39  	Register("some_check", CheckFunc(func() error {
    40  		return errors.New("This Check did not succeed")
    41  	}))
    42  
    43  	StatusHandler(recorder, req)
    44  
    45  	if recorder.Code != 503 {
    46  		t.Errorf("Did not get a 503.")
    47  	}
    48  }
    49  
    50  // TestHealthHandler ensures that our handler implementation correct protects
    51  // the web application when things aren't so healthy.
    52  func TestHealthHandler(t *testing.T) {
    53  	// clear out existing checks.
    54  	DefaultRegistry = NewRegistry()
    55  
    56  	// protect an http server
    57  	handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    58  		w.WriteHeader(http.StatusNoContent)
    59  	}))
    60  
    61  	// wrap it in our health handler
    62  	handler = Handler(handler)
    63  
    64  	// use this swap check status
    65  	updater := NewStatusUpdater()
    66  	Register("test_check", updater)
    67  
    68  	// now, create a test server
    69  	server := httptest.NewServer(handler)
    70  
    71  	checkUp := func(t *testing.T, message string) {
    72  		resp, err := http.Get(server.URL)
    73  		if err != nil {
    74  			t.Fatalf("error getting success status: %v", err)
    75  		}
    76  		defer resp.Body.Close()
    77  
    78  		if resp.StatusCode != http.StatusNoContent {
    79  			t.Fatalf("unexpected response code from server when %s: %d != %d", message, resp.StatusCode, http.StatusNoContent)
    80  		}
    81  		// NOTE(stevvooe): we really don't care about the body -- the format is
    82  		// not standardized or supported, yet.
    83  	}
    84  
    85  	checkDown := func(t *testing.T, message string) {
    86  		resp, err := http.Get(server.URL)
    87  		if err != nil {
    88  			t.Fatalf("error getting down status: %v", err)
    89  		}
    90  		defer resp.Body.Close()
    91  
    92  		if resp.StatusCode != http.StatusServiceUnavailable {
    93  			t.Fatalf("unexpected response code from server when %s: %d != %d", message, resp.StatusCode, http.StatusServiceUnavailable)
    94  		}
    95  	}
    96  
    97  	// server should be up
    98  	checkUp(t, "initial health check")
    99  
   100  	// now, we fail the health check
   101  	updater.Update(fmt.Errorf("the server is now out of commission"))
   102  	checkDown(t, "server should be down") // should be down
   103  
   104  	// bring server back up
   105  	updater.Update(nil)
   106  	checkUp(t, "when server is back up") // now we should be back up.
   107  }
   108  

View as plain text