2015-07-04 01:41:36 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-07-06 20:01:10 +00:00
|
|
|
"bytes"
|
2015-07-04 01:41:36 +00:00
|
|
|
"os"
|
2015-07-06 20:01:10 +00:00
|
|
|
"reflect"
|
2015-07-04 01:41:36 +00:00
|
|
|
"testing"
|
2015-08-16 00:38:13 +00:00
|
|
|
"time"
|
2015-07-04 01:41:36 +00:00
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-07-04 01:41:36 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
)
|
|
|
|
|
2015-07-06 20:01:10 +00:00
|
|
|
type MockSink struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
cancel bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) ID() string {
|
|
|
|
return "Mock"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) Cancel() error {
|
|
|
|
m.cancel = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
func testStateStore(t *testing.T) *state.StateStore {
|
|
|
|
state, err := state.NewStateStore(os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if state == nil {
|
|
|
|
t.Fatalf("missing state")
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2015-07-04 01:41:36 +00:00
|
|
|
func testFSM(t *testing.T) *nomadFSM {
|
2015-07-24 05:30:08 +00:00
|
|
|
fsm, err := NewFSM(testBroker(t, 0), os.Stderr)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if fsm == nil {
|
|
|
|
t.Fatalf("missing fsm")
|
|
|
|
}
|
|
|
|
return fsm
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeLog(buf []byte) *raft.Log {
|
|
|
|
return &raft.Log{
|
|
|
|
Index: 1,
|
|
|
|
Term: 1,
|
|
|
|
Type: raft.LogCommand,
|
|
|
|
Data: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_RegisterNode(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
req := structs.NodeRegisterRequest{
|
2015-08-11 21:27:14 +00:00
|
|
|
Node: mock.Node(),
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
2015-07-07 16:51:42 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
node, err := fsm.State().GetNodeByID(req.Node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if node.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", node.CreateIndex)
|
|
|
|
}
|
2015-08-16 00:38:13 +00:00
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
index := tt.NearestIndex(time.Now().UTC())
|
|
|
|
if index != 1 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeregisterNode(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
req := structs.NodeRegisterRequest{
|
2015-07-04 01:41:36 +00:00
|
|
|
Node: node,
|
|
|
|
}
|
2015-07-07 16:51:42 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
req2 := structs.NodeDeregisterRequest{
|
2015-07-04 01:41:36 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
}
|
2015-07-07 16:51:42 +00:00
|
|
|
buf, err = structs.Encode(structs.NodeDeregisterRequestType, req2)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
node, err = fsm.State().GetNodeByID(req.Node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node != nil {
|
|
|
|
t.Fatalf("node found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_UpdateNodeStatus(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
req := structs.NodeRegisterRequest{
|
2015-07-04 01:41:36 +00:00
|
|
|
Node: node,
|
|
|
|
}
|
2015-07-07 16:51:42 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
req2 := structs.NodeUpdateStatusRequest{
|
2015-07-04 01:41:36 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
Status: structs.NodeStatusReady,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.NodeUpdateStatusRequestType, req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
node, err = fsm.State().GetNodeByID(req.Node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node.Status != structs.NodeStatusReady {
|
|
|
|
t.Fatalf("bad node: %#v", node)
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 20:01:10 +00:00
|
|
|
|
2015-07-07 16:55:47 +00:00
|
|
|
func TestFSM_RegisterJob(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
req := structs.JobRegisterRequest{
|
2015-08-11 21:27:14 +00:00
|
|
|
Job: mock.Job(),
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2015-07-23 22:15:48 +00:00
|
|
|
job, err := fsm.State().GetJobByID(req.Job.ID)
|
2015-07-07 16:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if job == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if job.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", job.CreateIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeregisterJob(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
job := mock.Job()
|
2015-07-07 16:55:47 +00:00
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req2 := structs.JobDeregisterRequest{
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID: job.ID,
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.JobDeregisterRequestType, req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
2015-07-23 22:15:48 +00:00
|
|
|
job, err = fsm.State().GetJobByID(req.Job.ID)
|
2015-07-07 16:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if job != nil {
|
|
|
|
t.Fatalf("job found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:52:38 +00:00
|
|
|
func TestFSM_UpdateEval(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
2015-08-06 18:32:42 +00:00
|
|
|
fsm.evalBroker.SetEnabled(true)
|
2015-07-23 22:52:38 +00:00
|
|
|
|
|
|
|
req := structs.EvalUpdateRequest{
|
2015-08-11 21:27:14 +00:00
|
|
|
Evals: []*structs.Evaluation{mock.Eval()},
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2015-08-06 21:51:15 +00:00
|
|
|
eval, err := fsm.State().GetEvalByID(req.Evals[0].ID)
|
2015-07-23 22:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if eval.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", eval.CreateIndex)
|
|
|
|
}
|
2015-08-06 18:32:42 +00:00
|
|
|
|
|
|
|
// Verify enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, eval)
|
|
|
|
}
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteEval(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-07-23 22:52:38 +00:00
|
|
|
req := structs.EvalUpdateRequest{
|
2015-08-06 21:51:15 +00:00
|
|
|
Evals: []*structs.Evaluation{eval},
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req2 := structs.EvalDeleteRequest{
|
2015-08-15 22:39:29 +00:00
|
|
|
Evals: []string{eval.ID},
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.EvalDeleteRequestType, req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
2015-08-06 21:51:15 +00:00
|
|
|
eval, err = fsm.State().GetEvalByID(req.Evals[0].ID)
|
2015-07-23 22:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval != nil {
|
|
|
|
t.Fatalf("eval found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:04:26 +00:00
|
|
|
func TestFSM_UpdateAllocations(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
alloc := mock.Alloc()
|
2015-08-04 21:04:26 +00:00
|
|
|
req := structs.AllocUpdateRequest{
|
|
|
|
Evict: nil,
|
|
|
|
Alloc: []*structs.Allocation{alloc},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.AllocUpdateRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
out, err := fsm.State().GetAllocByID(alloc.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
alloc.CreateIndex = out.CreateIndex
|
|
|
|
alloc.ModifyIndex = out.ModifyIndex
|
|
|
|
if !reflect.DeepEqual(alloc, out) {
|
|
|
|
t.Fatalf("bad: %#v %#v", alloc, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
req2 := structs.AllocUpdateRequest{
|
|
|
|
Evict: []string{alloc.ID},
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.AllocUpdateRequestType, req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-08-23 01:30:49 +00:00
|
|
|
// Verify we are evicted
|
2015-08-04 21:04:26 +00:00
|
|
|
out, err = fsm.State().GetAllocByID(alloc.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-23 01:30:49 +00:00
|
|
|
if out.Status != structs.AllocStatusEvict {
|
2015-08-04 21:04:26 +00:00
|
|
|
t.Fatalf("alloc found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:01:10 +00:00
|
|
|
func testSnapshotRestore(t *testing.T, fsm *nomadFSM) *nomadFSM {
|
|
|
|
// Snapshot
|
|
|
|
snap, err := fsm.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
|
|
|
|
// Persist
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
sink := &MockSink{buf, false}
|
|
|
|
if err := snap.Persist(sink); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to restore on a new FSM
|
|
|
|
fsm2 := testFSM(t)
|
|
|
|
|
|
|
|
// Do a restore
|
|
|
|
if err := fsm2.Restore(sink); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return fsm2
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_Nodes(t *testing.T) {
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
node1 := mock.Node()
|
2015-07-06 20:01:10 +00:00
|
|
|
state.RegisterNode(1000, node1)
|
2015-08-11 21:27:14 +00:00
|
|
|
node2 := mock.Node()
|
2015-07-06 20:01:10 +00:00
|
|
|
state.RegisterNode(1001, node2)
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
out1, _ := state2.GetNodeByID(node1.ID)
|
|
|
|
out2, _ := state2.GetNodeByID(node2.ID)
|
|
|
|
if !reflect.DeepEqual(node1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, node1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(node2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, node2)
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 21:51:01 +00:00
|
|
|
|
2015-07-07 16:55:47 +00:00
|
|
|
func TestFSM_SnapshotRestore_Jobs(t *testing.T) {
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
job1 := mock.Job()
|
2015-07-07 16:55:47 +00:00
|
|
|
state.RegisterJob(1000, job1)
|
2015-08-11 21:27:14 +00:00
|
|
|
job2 := mock.Job()
|
2015-07-07 16:55:47 +00:00
|
|
|
state.RegisterJob(1001, job2)
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2015-07-23 22:15:48 +00:00
|
|
|
out1, _ := state2.GetJobByID(job1.ID)
|
|
|
|
out2, _ := state2.GetJobByID(job2.ID)
|
2015-07-07 16:55:47 +00:00
|
|
|
if !reflect.DeepEqual(job1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, job1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(job2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, job2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:52:38 +00:00
|
|
|
func TestFSM_SnapshotRestore_Evals(t *testing.T) {
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
eval1 := mock.Eval()
|
2015-08-06 21:51:15 +00:00
|
|
|
state.UpsertEvals(1000, []*structs.Evaluation{eval1})
|
2015-08-11 21:27:14 +00:00
|
|
|
eval2 := mock.Eval()
|
2015-08-06 21:51:15 +00:00
|
|
|
state.UpsertEvals(1001, []*structs.Evaluation{eval2})
|
2015-07-23 22:52:38 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
out1, _ := state2.GetEvalByID(eval1.ID)
|
|
|
|
out2, _ := state2.GetEvalByID(eval2.ID)
|
|
|
|
if !reflect.DeepEqual(eval1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, eval1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(eval2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, eval2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:04:26 +00:00
|
|
|
func TestFSM_SnapshotRestore_Allocs(t *testing.T) {
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
alloc1 := mock.Alloc()
|
2015-08-04 21:04:26 +00:00
|
|
|
state.UpdateAllocations(1000, nil, []*structs.Allocation{alloc1})
|
2015-08-11 21:27:14 +00:00
|
|
|
alloc2 := mock.Alloc()
|
2015-08-04 21:04:26 +00:00
|
|
|
state.UpdateAllocations(1001, nil, []*structs.Allocation{alloc2})
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
out1, _ := state2.GetAllocByID(alloc1.ID)
|
|
|
|
out2, _ := state2.GetAllocByID(alloc2.ID)
|
|
|
|
if !reflect.DeepEqual(alloc1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, alloc1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(alloc2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, alloc2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:51:01 +00:00
|
|
|
func TestFSM_SnapshotRestore_Indexes(t *testing.T) {
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
node1 := mock.Node()
|
2015-07-06 21:51:01 +00:00
|
|
|
state.RegisterNode(1000, node1)
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
|
|
|
|
index, err := state2.GetIndex("nodes")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if index != 1000 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 00:38:13 +00:00
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_TimeTable(t *testing.T) {
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
start := time.Now().UTC()
|
|
|
|
tt.Witness(1000, start)
|
|
|
|
tt.Witness(2000, start.Add(10*time.Minute))
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
|
|
|
|
tt2 := fsm2.TimeTable()
|
|
|
|
if tt2.NearestTime(1500) != start {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if tt2.NearestIndex(start.Add(15*time.Minute)) != 2000 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|