2016-09-07 02:01:27 -07:00
|
|
|
package health
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestHealth(t *testing.T) {
|
|
|
|
|
// We use a random port instead of a fixed port like 8080 that may have been
|
|
|
|
|
// occupied by some other process.
|
2016-09-23 09:14:12 +01:00
|
|
|
h := health{Addr: ":0"}
|
|
|
|
|
if err := h.Startup(); err != nil {
|
2016-09-07 02:01:27 -07:00
|
|
|
t.Fatalf("Unable to startup the health server: %v", err)
|
|
|
|
|
}
|
2016-09-23 09:14:12 +01:00
|
|
|
defer h.Shutdown()
|
2016-09-07 02:01:27 -07:00
|
|
|
|
|
|
|
|
// Reconstruct the http address based on the port allocated by operating system.
|
2016-09-23 09:14:12 +01:00
|
|
|
address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
|
2016-09-07 02:01:27 -07:00
|
|
|
|
|
|
|
|
response, err := http.Get(address)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unable to query %s: %v", address, err)
|
|
|
|
|
}
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
|
t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
content, err := ioutil.ReadAll(response.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unable to get response body from %s: %v", address, err)
|
|
|
|
|
}
|
|
|
|
|
if string(content) != "OK" {
|
|
|
|
|
t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
|
|
|
|
|
}
|
|
|
|
|
}
|