Add more tests

This commit is contained in:
Derek Chiang 2015-04-28 22:07:10 -04:00 committed by James Phillips
parent 139c9240ea
commit 88550714ca
2 changed files with 43 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package consul
import (
"math/rand"
"os"
"reflect"
"testing"
"time"
@ -33,12 +34,7 @@ func getRandomCoordinate() *coordinate.Coordinate {
}
func coordinatesEqual(a, b *coordinate.Coordinate) bool {
config := coordinate.DefaultConfig()
dist, err := a.DistanceTo(b, config)
if err != nil {
panic(err)
}
return dist < 0.1
return reflect.DeepEqual(a, b)
}
func TestCoordinateUpdate(t *testing.T) {

View File

@ -728,6 +728,47 @@ func TestFSM_KVSCheckAndSet(t *testing.T) {
}
}
func TestFSM_CoordinateUpdate(t *testing.T) {
path, err := ioutil.TempDir("", "fsm")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(path)
fsm, err := NewFSM(nil, path, os.Stderr)
if err != nil {
t.Fatalf("err: %v", err)
}
defer fsm.Close()
nodeName := "Node1"
req := structs.CoordinateUpdateRequest{
Datacenter: "dc1",
Node: nodeName,
Op: structs.CoordinateSet,
Coord: getRandomCoordinate(),
}
buf, err := structs.Encode(structs.CoordinateRequestType, req)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := fsm.Apply(makeLog(buf))
if resp != nil {
t.Fatalf("resp: %v", resp)
}
// Verify key is set
_, d, err := fsm.state.CoordinateGet(nodeName)
if err != nil {
t.Fatalf("err: %v", err)
}
if d == nil {
t.Fatalf("missing")
}
if !coordinatesEqual(req.Coord, d.Coord) {
t.Fatalf("wrong coordinate")
}
}
func TestFSM_SessionCreate_Destroy(t *testing.T) {
fsm, err := NewFSM(nil, os.Stderr)
if err != nil {