Browse Source

api: improve test reliability

pull/581/head
Ryan Uber 10 years ago
parent
commit
6c4ff58d2a
  1. 23
      api/.gitignore
  2. 19
      api/api_test.go
  3. 54
      api/health_test.go

23
api/.gitignore vendored

@ -1,23 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test

19
api/api_test.go

@ -10,6 +10,8 @@ import (
"strings"
"testing"
"time"
"github.com/hashicorp/consul/testutil"
)
var consulConfig = `{
@ -78,25 +80,22 @@ func newTestServer(t *testing.T) *testServer {
// Allow the server some time to start, and verify we have a leader.
client := new(http.Client)
for i := 0; ; i++ {
if i >= 20 {
t.Fatal("Server failed to start")
}
time.Sleep(100 * time.Millisecond)
testutil.WaitForResult(func() (bool, error) {
resp, err := client.Get("http://127.0.0.1:18800/v1/status/leader")
if err != nil {
continue
return false, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil || !strings.Contains(string(body), "18000") {
continue
return false, fmt.Errorf("No leader")
}
break
}
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
return &testServer{
pid: cmd.Process.Pid,

54
api/health_test.go

@ -75,18 +75,22 @@ func TestHealth_Service(t *testing.T) {
health := c.Health()
// consul service should always exist...
checks, meta, err := health.Service("consul", "", true, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
if meta.LastIndex == 0 {
t.Fatalf("bad: %v", meta)
}
if len(checks) == 0 {
t.Fatalf("Bad: %v", checks)
}
testutil.WaitForResult(func() (bool, error) {
// consul service should always exist...
checks, meta, err := health.Service("consul", "", true, nil)
if err != nil {
return false, err
}
if meta.LastIndex == 0 {
return false, fmt.Errorf("bad: %v", meta)
}
if len(checks) == 0 {
return false, fmt.Errorf("Bad: %v", checks)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
}
func TestHealth_State(t *testing.T) {
@ -95,15 +99,19 @@ func TestHealth_State(t *testing.T) {
health := c.Health()
checks, meta, err := health.State("any", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
if meta.LastIndex == 0 {
t.Fatalf("bad: %v", meta)
}
if len(checks) == 0 {
t.Fatalf("Bad: %v", checks)
}
testutil.WaitForResult(func() (bool, error) {
checks, meta, err := health.State("any", nil)
if err != nil {
return false, err
}
if meta.LastIndex == 0 {
return false, fmt.Errorf("bad: %v", meta)
}
if len(checks) == 0 {
return false, fmt.Errorf("Bad: %v", checks)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
}

Loading…
Cancel
Save