Merge pull request #2678 from hashicorp/sethvargo/utilfixes

testutil updates
This commit is contained in:
James Phillips 2017-01-25 21:36:22 -08:00 committed by GitHub
commit 7e8212489d
2 changed files with 24 additions and 3 deletions

View File

@ -292,6 +292,7 @@ func (s *TestServer) waitForAPI() {
// waitForLeader waits for the Consul server's HTTP API to become // waitForLeader waits for the Consul server's HTTP API to become
// available, and then waits for a known leader and an index of // available, and then waits for a known leader and an index of
// 1 or more to be observed to confirm leader election is done. // 1 or more to be observed to confirm leader election is done.
// It then waits to ensure the anti-entropy checks have completed.
func (s *TestServer) waitForLeader() { func (s *TestServer) waitForLeader() {
WaitForResult(func() (bool, error) { WaitForResult(func() (bool, error) {
// Query the API and check the status code // Query the API and check the status code
@ -312,6 +313,25 @@ func (s *TestServer) waitForLeader() {
if resp.Header.Get("X-Consul-Index") == "0" { if resp.Header.Get("X-Consul-Index") == "0" {
return false, fmt.Errorf("Consul index is 0") return false, fmt.Errorf("Consul index is 0")
} }
var parsed []map[string]interface{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&parsed); err != nil {
return false, err
}
if len(parsed) < 1 {
return false, fmt.Errorf("No nodes")
}
taggedAddresses, ok := parsed[0]["TaggedAddresses"].(map[string]interface{})
if !ok {
return false, fmt.Errorf("Missing tagged addresses")
}
if _, ok := taggedAddresses["lan"]; !ok {
return false, fmt.Errorf("No lan tagged addresses")
}
return true, nil return true, nil
}, func(err error) { }, func(err error) {
defer s.Stop() defer s.Stop()

View File

@ -1,19 +1,20 @@
package testutil package testutil
import ( import (
"github.com/hashicorp/consul/consul/structs"
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/consul/structs"
) )
type testFn func() (bool, error) type testFn func() (bool, error)
type errorFn func(error) type errorFn func(error)
func WaitForResult(test testFn, error errorFn) { func WaitForResult(test testFn, error errorFn) {
retries := 1000 retries := 100
for retries > 0 { for retries > 0 {
time.Sleep(10 * time.Millisecond) time.Sleep(100 * time.Millisecond)
retries-- retries--
success, err := test() success, err := test()