diff --git a/command/agent/health_endpoint_test.go b/command/agent/health_endpoint_test.go index be8a99368..03706de87 100644 --- a/command/agent/health_endpoint_test.go +++ b/command/agent/health_endpoint_test.go @@ -11,32 +11,30 @@ import ( ) func TestHealthChecksInState(t *testing.T) { - dir, srv := makeHTTPServer(t) - defer os.RemoveAll(dir) - defer srv.Shutdown() - defer srv.agent.Shutdown() + httpTest(t, func(srv *HTTPServer) { + req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil) + if err != nil { + t.Fatalf("err: %v", err) + } - testutil.WaitForLeader(t, srv.agent.RPC, "dc1") + testutil.WaitForResult(func() (bool, error) { + resp := httptest.NewRecorder() + obj, err := srv.HealthChecksInState(resp, req) + if err != nil { + return false, err + } + if err := checkIndex(resp); err != nil { + return false, err + } - req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil) - if err != nil { - t.Fatalf("err: %v", err) - } - - resp := httptest.NewRecorder() - obj, err := srv.HealthChecksInState(resp, req) - if err != nil { - t.Fatalf("err: %v", err) - } - - // TODO: Failing - assertIndex(t, resp) - - // Should be 1 health check for the server - nodes := obj.(structs.HealthChecks) - if len(nodes) != 1 { - t.Fatalf("bad: %v", obj) - } + // Should be 1 health check for the server + nodes := obj.(structs.HealthChecks) + if len(nodes) != 1 { + return false, fmt.Errorf("bad: %v", obj) + } + return true, nil + }, func(err error) { t.Fatalf("err: %v", err) }) + }) } func TestHealthNodeChecks(t *testing.T) { diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 6b57693f4..7d680536a 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -3,6 +3,7 @@ package agent import ( "bytes" "encoding/json" + "fmt" "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/testutil" "io" @@ -244,6 +245,15 @@ func assertIndex(t *testing.T, resp *httptest.ResponseRecorder) { } } +// checkIndex is like assertIndex but returns an error +func checkIndex(resp *httptest.ResponseRecorder) error { + header := resp.Header().Get("X-Consul-Index") + if header == "" || header == "0" { + return fmt.Errorf("Bad: %v", header) + } + return nil +} + // getIndex parses X-Consul-Index func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 { header := resp.Header().Get("X-Consul-Index")