2016-09-07 02:01:27 -07:00
|
|
|
package health
|
|
|
|
|
|
2017-11-13 09:52:40 +00:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
2018-01-18 10:40:09 +00:00
|
|
|
"time"
|
2017-11-13 09:52:40 +00:00
|
|
|
)
|
|
|
|
|
|
2016-09-07 02:01:27 -07:00
|
|
|
func TestHealth(t *testing.T) {
|
2018-01-18 10:40:09 +00:00
|
|
|
h := newHealth(":0")
|
2017-08-27 21:33:38 +01:00
|
|
|
|
2018-01-10 11:41:22 +00:00
|
|
|
if err := h.OnStartup(); err != nil {
|
2016-09-07 02:01:27 -07:00
|
|
|
t.Fatalf("Unable to startup the health server: %v", err)
|
|
|
|
|
}
|
2018-04-21 17:43:02 +01:00
|
|
|
defer h.OnFinalShutdown()
|
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)
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
}
|
2017-08-27 21:33:38 +01:00
|
|
|
response.Body.Close()
|
|
|
|
|
|
|
|
|
|
if string(content) != ok {
|
2016-09-07 02:01:27 -07:00
|
|
|
t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-18 10:40:09 +00:00
|
|
|
|
|
|
|
|
func TestHealthLameduck(t *testing.T) {
|
|
|
|
|
h := newHealth(":0")
|
|
|
|
|
h.lameduck = 250 * time.Millisecond
|
|
|
|
|
|
|
|
|
|
if err := h.OnStartup(); err != nil {
|
|
|
|
|
t.Fatalf("Unable to startup the health server: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 17:43:02 +01:00
|
|
|
h.OnFinalShutdown()
|
2018-01-18 10:40:09 +00:00
|
|
|
}
|