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) req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil)
defer srv.Shutdown() if err != nil {
defer srv.agent.Shutdown() 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) // Should be 1 health check for the server
if err != nil { nodes := obj.(structs.HealthChecks)
t.Fatalf("err: %v", err) if len(nodes) != 1 {
} return false, fmt.Errorf("bad: %v", obj)
}
resp := httptest.NewRecorder() return true, nil
obj, err := srv.HealthChecksInState(resp, req) }, func(err error) { t.Fatalf("err: %v", err) })
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)
}
} }
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")