119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package consul
|
|
|
|
import (
|
|
"math/rand"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
"github.com/hashicorp/consul/testutil"
|
|
"github.com/hashicorp/serf/coordinate"
|
|
)
|
|
|
|
// getRandomCoordinate generates a random coordinate.
|
|
func getRandomCoordinate() *coordinate.Coordinate {
|
|
config := coordinate.DefaultConfig()
|
|
// Randomly apply updates between n clients
|
|
n := 5
|
|
clients := make([]*coordinate.Client, n)
|
|
for i := 0; i < n; i++ {
|
|
clients[i], _ = coordinate.NewClient(config)
|
|
}
|
|
|
|
for i := 0; i < n*100; i++ {
|
|
k1 := rand.Intn(n)
|
|
k2 := rand.Intn(n)
|
|
if k1 == k2 {
|
|
continue
|
|
}
|
|
clients[k1].Update(clients[k2].GetCoordinate(), time.Duration(rand.Int63())*time.Microsecond)
|
|
}
|
|
return clients[rand.Intn(n)].GetCoordinate()
|
|
}
|
|
|
|
func coordinatesEqual(a, b *coordinate.Coordinate) bool {
|
|
return reflect.DeepEqual(a, b)
|
|
}
|
|
|
|
func TestCoordinateUpdate(t *testing.T) {
|
|
dir1, s1 := testServer(t)
|
|
defer os.RemoveAll(dir1)
|
|
defer s1.Shutdown()
|
|
client := rpcClient(t, s1)
|
|
defer client.Close()
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
arg := structs.CoordinateUpdateRequest{
|
|
Datacenter: "dc1",
|
|
Node: "node1",
|
|
Op: structs.CoordinateSet,
|
|
Coord: getRandomCoordinate(),
|
|
}
|
|
|
|
var out struct{}
|
|
if err := client.Call("Coordinate.Update", &arg, &out); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
|
|
// Verify
|
|
state := s1.fsm.State()
|
|
_, d, err := state.CoordinateGet("node1")
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if !coordinatesEqual(d.Coord, arg.Coord) {
|
|
t.Fatalf("should be equal\n%v\n%v", d.Coord, arg.Coord)
|
|
}
|
|
}
|
|
|
|
func TestCoordinateGet(t *testing.T) {
|
|
dir1, s1 := testServer(t)
|
|
defer os.RemoveAll(dir1)
|
|
defer s1.Shutdown()
|
|
client := rpcClient(t, s1)
|
|
defer client.Close()
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
arg := structs.CoordinateUpdateRequest{
|
|
Datacenter: "dc1",
|
|
Node: "node1",
|
|
Op: structs.CoordinateSet,
|
|
Coord: getRandomCoordinate(),
|
|
}
|
|
|
|
var out struct{}
|
|
if err := client.Call("Coordinate.Update", &arg, &out); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
|
|
// Get via RPC
|
|
var out2 *structs.IndexedCoordinate
|
|
arg2 := structs.CoordinateGetRequest{
|
|
Datacenter: "dc1",
|
|
Node: "node1",
|
|
}
|
|
if err := client.Call("Coordinate.Get", &arg2, &out2); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if !coordinatesEqual(out2.Coord, arg.Coord) {
|
|
t.Fatalf("should be equal\n%v\n%v", out2.Coord, arg.Coord)
|
|
}
|
|
|
|
// Now let's override the original coordinate; Coordinate.Get should return
|
|
// the latest coordinate
|
|
arg.Coord = getRandomCoordinate()
|
|
if err := client.Call("Coordinate.Update", &arg, &out); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if err := client.Call("Coordinate.Get", &arg2, &out2); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if !coordinatesEqual(out2.Coord, arg.Coord) {
|
|
t.Fatalf("should be equal\n%v\n%v", out2.Coord, arg.Coord)
|
|
}
|
|
}
|