2017-04-19 23:00:11 +00:00
|
|
|
package testrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/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
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
if out.Index < 2 {
|
|
|
|
r.Fatalf("Consul index should be at least 2")
|
2017-04-19 23:00:11 +00:00
|
|
|
}
|
2017-05-05 11:48:34 +00:00
|
|
|
})
|
2017-04-19 23:00:11 +00:00
|
|
|
}
|