2017-04-19 23:00:11 +00:00
|
|
|
package testrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2017-05-05 11:48:34 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2017-04-19 23:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type rpcFn func(string, interface{}, interface{}) error
|
|
|
|
|
|
|
|
func WaitForLeader(t *testing.T, rpc rpcFn, dc string) {
|
|
|
|
var out structs.IndexedNodes
|
2017-05-05 11:48:34 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-04-19 23:00:11 +00:00
|
|
|
// Ensure we have a leader and a node registration.
|
|
|
|
args := &structs.DCSpecificRequest{Datacenter: dc}
|
|
|
|
if err := rpc("Catalog.ListNodes", args, &out); err != nil {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatalf("Catalog.ListNodes failed: %v", err)
|
2017-04-19 23:00:11 +00:00
|
|
|
}
|
|
|
|
if !out.QueryMeta.KnownLeader {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatalf("No leader")
|
2017-04-19 23:00:11 +00:00
|
|
|
}
|
|
|
|
if out.Index == 0 {
|
2017-05-05 11:48:34 +00:00
|
|
|
r.Fatalf("Consul index is 0")
|
2017-04-19 23:00:11 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
})
|
2017-04-19 23:00:11 +00:00
|
|
|
}
|