github.com/argoproj/argo-cd@v1.8.7/util/healthz/healthz_test.go (about) 1 package healthz 2 3 import ( 4 "fmt" 5 "net" 6 "net/http" 7 "testing" 8 ) 9 10 func TestHealthCheck(t *testing.T) { 11 sentinel := false 12 13 serve := func(c chan<- string) { 14 // listen on first available dynamic (unprivileged) port 15 listener, err := net.Listen("tcp", ":0") 16 if err != nil { 17 panic(err) 18 } 19 20 // send back the address so that it can be used 21 c <- listener.Addr().String() 22 23 mux := http.NewServeMux() 24 ServeHealthCheck(mux, func(r *http.Request) error { 25 if sentinel { 26 return fmt.Errorf("This is a dummy error") 27 } 28 return nil 29 }) 30 panic(http.Serve(listener, mux)) 31 } 32 33 c := make(chan string, 1) 34 35 // run a local webserver to test data retrieval 36 go serve(c) 37 38 address := <-c 39 t.Logf("Listening at address: %s", address) 40 41 server := "http://" + address 42 43 resp, err := http.Get(server + "/healthz") 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 if resp.StatusCode != 200 { 49 t.Fatalf("Was expecting status code 200 from health check, but got %d instead", resp.StatusCode) 50 } 51 52 sentinel = true 53 54 resp, _ = http.Get(server + "/healthz") 55 if resp.StatusCode != 503 { 56 t.Fatalf("Was expecting status code 503 from health check, but got %d instead", resp.StatusCode) 57 } 58 59 }