agent: Improving test reliability

This commit is contained in:
Armon Dadgar 2014-05-21 12:31:22 -07:00
parent b4311679d8
commit 999e7abed4
2 changed files with 32 additions and 24 deletions

View file

@ -11,32 +11,30 @@ import (
) )
func TestHealthChecksInState(t *testing.T) { func TestHealthChecksInState(t *testing.T) {
dir, srv := makeHTTPServer(t) httpTest(t, func(srv *HTTPServer) {
defer os.RemoveAll(dir)
defer srv.Shutdown()
defer srv.agent.Shutdown()
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")
req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil) req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
testutil.WaitForResult(func() (bool, error) {
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := srv.HealthChecksInState(resp, req) obj, err := srv.HealthChecksInState(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) return false, err
}
if err := checkIndex(resp); err != nil {
return false, err
} }
// TODO: Failing
assertIndex(t, resp)
// Should be 1 health check for the server // Should be 1 health check for the server
nodes := obj.(structs.HealthChecks) nodes := obj.(structs.HealthChecks)
if len(nodes) != 1 { if len(nodes) != 1 {
t.Fatalf("bad: %v", obj) return false, fmt.Errorf("bad: %v", obj)
} }
return true, nil
}, func(err error) { t.Fatalf("err: %v", err) })
})
} }
func TestHealthNodeChecks(t *testing.T) { func TestHealthNodeChecks(t *testing.T) {

View file

@ -3,6 +3,7 @@ package agent
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"io" "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 // getIndex parses X-Consul-Index
func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 { func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 {
header := resp.Header().Get("X-Consul-Index") header := resp.Header().Get("X-Consul-Index")