open-nomad/nomad/state/state_store_test.go

1094 lines
20 KiB
Go
Raw Normal View History

package state
2015-07-04 01:19:43 +00:00
import (
"os"
"reflect"
"sort"
2015-07-04 01:19:43 +00:00
"testing"
"github.com/hashicorp/nomad/nomad/mock"
2015-07-04 01:19:43 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
func testStateStore(t *testing.T) *StateStore {
state, err := NewStateStore(os.Stderr)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if state == nil {
t.Fatalf("missing state")
}
return state
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpsertNode_Node(t *testing.T) {
2015-07-04 01:19:43 +00:00
state := testStateStore(t)
node := mock.Node()
2015-07-04 01:19:43 +00:00
err := state.UpsertNode(1000, node)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.NodeByID(node.ID)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(node, out) {
t.Fatalf("bad: %#v %#v", node, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("nodes")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1000 {
t.Fatalf("bad: %d", index)
}
2015-07-04 01:19:43 +00:00
}
2015-09-07 03:51:01 +00:00
func TestStateStore_DeleteNode_Node(t *testing.T) {
2015-07-04 01:19:43 +00:00
state := testStateStore(t)
node := mock.Node()
2015-07-04 01:19:43 +00:00
err := state.UpsertNode(1000, node)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
err = state.DeleteNode(1001, node.ID)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.NodeByID(node.ID)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %#v %#v", node, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("nodes")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
2015-07-04 01:19:43 +00:00
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpdateNodeStatus_Node(t *testing.T) {
2015-07-04 01:19:43 +00:00
state := testStateStore(t)
node := mock.Node()
2015-07-04 01:19:43 +00:00
err := state.UpsertNode(1000, node)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
err = state.UpdateNodeStatus(1001, node.ID, structs.NodeStatusReady)
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.NodeByID(node.ID)
2015-07-04 01:19:43 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if out.Status != structs.NodeStatusReady {
t.Fatalf("bad: %#v", out)
}
if out.ModifyIndex != 1001 {
t.Fatalf("bad: %#v", out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("nodes")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
2015-09-07 02:51:50 +00:00
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpdateNodeDrain_Node(t *testing.T) {
2015-09-07 02:51:50 +00:00
state := testStateStore(t)
node := mock.Node()
err := state.UpsertNode(1000, node)
2015-09-07 02:51:50 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
err = state.UpdateNodeDrain(1001, node.ID, true)
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.NodeByID(node.ID)
2015-09-07 02:51:50 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !out.Drain {
t.Fatalf("bad: %#v", out)
}
if out.ModifyIndex != 1001 {
t.Fatalf("bad: %#v", out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("nodes")
2015-09-07 02:51:50 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
2015-07-04 01:19:43 +00:00
}
func TestStateStore_Nodes(t *testing.T) {
state := testStateStore(t)
var nodes []*structs.Node
for i := 0; i < 10; i++ {
node := mock.Node()
nodes = append(nodes, node)
err := state.UpsertNode(1000+uint64(i), node)
if err != nil {
t.Fatalf("err: %v", err)
}
}
iter, err := state.Nodes()
if err != nil {
t.Fatalf("err: %v", err)
}
var out []*structs.Node
for {
raw := iter.Next()
if raw == nil {
break
}
out = append(out, raw.(*structs.Node))
}
sort.Sort(NodeIDSort(nodes))
sort.Sort(NodeIDSort(out))
if !reflect.DeepEqual(nodes, out) {
t.Fatalf("bad: %#v %#v", nodes, out)
}
}
2015-07-07 16:41:05 +00:00
func TestStateStore_RestoreNode(t *testing.T) {
state := testStateStore(t)
restore, err := state.Restore()
if err != nil {
t.Fatalf("err: %v", err)
}
node := mock.Node()
2015-07-07 16:41:05 +00:00
err = restore.NodeRestore(node)
if err != nil {
t.Fatalf("err: %v", err)
}
restore.Commit()
2015-09-07 03:51:01 +00:00
out, err := state.NodeByID(node.ID)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(out, node) {
t.Fatalf("Bad: %#v %#v", out, node)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpsertJob_Job(t *testing.T) {
2015-07-07 16:41:05 +00:00
state := testStateStore(t)
job := mock.Job()
2015-07-07 16:41:05 +00:00
err := state.UpsertJob(1000, job)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.JobByID(job.ID)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(job, out) {
t.Fatalf("bad: %#v %#v", job, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("jobs")
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1000 {
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpdateUpsertJob_Job(t *testing.T) {
2015-07-07 16:41:05 +00:00
state := testStateStore(t)
job := mock.Job()
2015-07-07 16:41:05 +00:00
err := state.UpsertJob(1000, job)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
job2 := mock.Job()
job2.ID = job.ID
err = state.UpsertJob(1001, job2)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.JobByID(job.ID)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(job2, out) {
t.Fatalf("bad: %#v %#v", job2, out)
}
if out.CreateIndex != 1000 {
t.Fatalf("bad: %#v", out)
}
if out.ModifyIndex != 1001 {
t.Fatalf("bad: %#v", out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("jobs")
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_DeleteJob_Job(t *testing.T) {
2015-07-07 16:41:05 +00:00
state := testStateStore(t)
job := mock.Job()
2015-07-07 16:41:05 +00:00
err := state.UpsertJob(1000, job)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
err = state.DeleteJob(1001, job.ID)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.JobByID(job.ID)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %#v %#v", job, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("jobs")
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
}
func TestStateStore_Jobs(t *testing.T) {
state := testStateStore(t)
var jobs []*structs.Job
for i := 0; i < 10; i++ {
job := mock.Job()
2015-07-07 16:41:05 +00:00
jobs = append(jobs, job)
err := state.UpsertJob(1000+uint64(i), job)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
}
iter, err := state.Jobs()
if err != nil {
t.Fatalf("err: %v", err)
}
var out []*structs.Job
for {
raw := iter.Next()
if raw == nil {
break
}
out = append(out, raw.(*structs.Job))
}
sort.Sort(JobIDSort(jobs))
sort.Sort(JobIDSort(out))
2015-07-07 16:41:05 +00:00
if !reflect.DeepEqual(jobs, out) {
t.Fatalf("bad: %#v %#v", jobs, out)
}
}
func TestStateStore_JobsByScheduler(t *testing.T) {
state := testStateStore(t)
var serviceJobs []*structs.Job
var sysJobs []*structs.Job
for i := 0; i < 10; i++ {
job := mock.Job()
serviceJobs = append(serviceJobs, job)
err := state.UpsertJob(1000+uint64(i), job)
if err != nil {
t.Fatalf("err: %v", err)
}
}
for i := 0; i < 10; i++ {
job := mock.SystemJob()
sysJobs = append(sysJobs, job)
err := state.UpsertJob(2000+uint64(i), job)
if err != nil {
t.Fatalf("err: %v", err)
}
}
iter, err := state.JobsByScheduler("service")
if err != nil {
t.Fatalf("err: %v", err)
}
var outService []*structs.Job
for {
raw := iter.Next()
if raw == nil {
break
}
outService = append(outService, raw.(*structs.Job))
}
iter, err = state.JobsByScheduler("system")
if err != nil {
t.Fatalf("err: %v", err)
}
var outSystem []*structs.Job
for {
raw := iter.Next()
if raw == nil {
break
}
outSystem = append(outSystem, raw.(*structs.Job))
}
sort.Sort(JobIDSort(serviceJobs))
sort.Sort(JobIDSort(sysJobs))
sort.Sort(JobIDSort(outService))
sort.Sort(JobIDSort(outSystem))
if !reflect.DeepEqual(serviceJobs, outService) {
t.Fatalf("bad: %#v %#v", serviceJobs, outService)
}
if !reflect.DeepEqual(sysJobs, outSystem) {
t.Fatalf("bad: %#v %#v", sysJobs, outSystem)
}
}
2015-07-07 16:41:05 +00:00
func TestStateStore_RestoreJob(t *testing.T) {
state := testStateStore(t)
restore, err := state.Restore()
if err != nil {
t.Fatalf("err: %v", err)
}
job := mock.Job()
2015-07-07 16:41:05 +00:00
err = restore.JobRestore(job)
if err != nil {
t.Fatalf("err: %v", err)
}
restore.Commit()
2015-09-07 03:51:01 +00:00
out, err := state.JobByID(job.ID)
2015-07-07 16:41:05 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(out, job) {
t.Fatalf("Bad: %#v %#v", out, job)
}
}
func TestStateStore_Indexes(t *testing.T) {
state := testStateStore(t)
node := mock.Node()
err := state.UpsertNode(1000, node)
if err != nil {
t.Fatalf("err: %v", err)
}
iter, err := state.Indexes()
if err != nil {
t.Fatalf("err: %v", err)
}
var out []*IndexEntry
for {
raw := iter.Next()
if raw == nil {
break
}
out = append(out, raw.(*IndexEntry))
}
expect := []*IndexEntry{
&IndexEntry{"nodes", 1000},
}
if !reflect.DeepEqual(expect, out) {
t.Fatalf("bad: %#v %#v", expect, out)
}
}
2015-07-07 16:41:05 +00:00
func TestStateStore_RestoreIndex(t *testing.T) {
state := testStateStore(t)
restore, err := state.Restore()
if err != nil {
t.Fatalf("err: %v", err)
}
2015-07-07 16:41:05 +00:00
index := &IndexEntry{"jobs", 1000}
err = restore.IndexRestore(index)
if err != nil {
t.Fatalf("err: %v", err)
}
restore.Commit()
2015-09-07 03:51:01 +00:00
out, err := state.Index("jobs")
if err != nil {
t.Fatalf("err: %v", err)
}
2015-07-07 16:41:05 +00:00
if out != 1000 {
t.Fatalf("Bad: %#v %#v", out, 1000)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpsertEvals_Eval(t *testing.T) {
2015-07-23 22:43:06 +00:00
state := testStateStore(t)
eval := mock.Eval()
2015-07-23 22:43:06 +00:00
err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.EvalByID(eval.ID)
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(eval, out) {
t.Fatalf("bad: %#v %#v", eval, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("evals")
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1000 {
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_Update_UpsertEvals_Eval(t *testing.T) {
2015-07-23 22:43:06 +00:00
state := testStateStore(t)
eval := mock.Eval()
2015-07-23 22:43:06 +00:00
err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
eval2 := mock.Eval()
2015-07-23 22:43:06 +00:00
eval2.ID = eval.ID
err = state.UpsertEvals(1001, []*structs.Evaluation{eval2})
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.EvalByID(eval.ID)
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(eval2, out) {
t.Fatalf("bad: %#v %#v", eval2, out)
}
if out.CreateIndex != 1000 {
t.Fatalf("bad: %#v", out)
}
if out.ModifyIndex != 1001 {
t.Fatalf("bad: %#v", out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("evals")
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_DeleteEval_Eval(t *testing.T) {
2015-07-23 22:43:06 +00:00
state := testStateStore(t)
eval := mock.Eval()
eval2 := mock.Eval()
alloc := mock.Alloc()
alloc2 := mock.Alloc()
2015-07-23 22:43:06 +00:00
err := state.UpsertEvals(1000, []*structs.Evaluation{eval, eval2})
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc, alloc2})
if err != nil {
t.Fatalf("err: %v", err)
}
notify1 := make(chan struct{}, 1)
state.WatchAllocNode(alloc.NodeID, notify1)
err = state.DeleteEval(1002, []string{eval.ID, eval2.ID}, []string{alloc.ID, alloc2.ID})
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.EvalByID(eval.ID)
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %#v %#v", eval, out)
2015-07-23 22:43:06 +00:00
}
2015-09-07 03:51:01 +00:00
out, err = state.EvalByID(eval2.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %#v %#v", eval, out)
}
2015-09-07 03:51:01 +00:00
outA, err := state.AllocByID(alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %#v %#v", alloc, outA)
}
2015-09-07 03:51:01 +00:00
outA, err = state.AllocByID(alloc2.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if out != nil {
t.Fatalf("bad: %#v %#v", alloc, outA)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("evals")
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1002 {
t.Fatalf("bad: %d", index)
}
2015-09-07 03:51:01 +00:00
index, err = state.Index("allocs")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1002 {
2015-07-23 22:43:06 +00:00
t.Fatalf("bad: %d", index)
}
select {
case <-notify1:
default:
t.Fatalf("should be notified")
}
2015-07-23 22:43:06 +00:00
}
2015-09-06 19:10:24 +00:00
func TestStateStore_EvalsByJob(t *testing.T) {
state := testStateStore(t)
eval1 := mock.Eval()
eval2 := mock.Eval()
eval2.JobID = eval1.JobID
eval3 := mock.Eval()
evals := []*structs.Evaluation{eval1, eval2}
err := state.UpsertEvals(1000, evals)
if err != nil {
t.Fatalf("err: %v", err)
}
err = state.UpsertEvals(1001, []*structs.Evaluation{eval3})
if err != nil {
t.Fatalf("err: %v", err)
}
out, err := state.EvalsByJob(eval1.JobID)
if err != nil {
t.Fatalf("err: %v", err)
}
sort.Sort(EvalIDSort(evals))
sort.Sort(EvalIDSort(out))
if !reflect.DeepEqual(evals, out) {
t.Fatalf("bad: %#v %#v", evals, out)
}
}
2015-07-23 22:43:06 +00:00
func TestStateStore_Evals(t *testing.T) {
state := testStateStore(t)
var evals []*structs.Evaluation
for i := 0; i < 10; i++ {
eval := mock.Eval()
2015-07-23 22:43:06 +00:00
evals = append(evals, eval)
err := state.UpsertEvals(1000+uint64(i), []*structs.Evaluation{eval})
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
}
iter, err := state.Evals()
if err != nil {
t.Fatalf("err: %v", err)
}
var out []*structs.Evaluation
for {
raw := iter.Next()
if raw == nil {
break
}
out = append(out, raw.(*structs.Evaluation))
}
sort.Sort(EvalIDSort(evals))
sort.Sort(EvalIDSort(out))
if !reflect.DeepEqual(evals, out) {
t.Fatalf("bad: %#v %#v", evals, out)
}
}
func TestStateStore_RestoreEval(t *testing.T) {
state := testStateStore(t)
restore, err := state.Restore()
if err != nil {
t.Fatalf("err: %v", err)
}
job := mock.Eval()
2015-07-23 22:43:06 +00:00
err = restore.EvalRestore(job)
if err != nil {
t.Fatalf("err: %v", err)
}
restore.Commit()
2015-09-07 03:51:01 +00:00
out, err := state.EvalByID(job.ID)
2015-07-23 22:43:06 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(out, job) {
t.Fatalf("Bad: %#v %#v", out, job)
}
}
func TestStateStore_UpdateAllocFromClient(t *testing.T) {
state := testStateStore(t)
alloc := mock.Alloc()
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
if err != nil {
t.Fatalf("err: %v", err)
}
update := new(structs.Allocation)
*update = *alloc
update.ClientStatus = structs.AllocClientStatusFailed
err = state.UpdateAllocFromClient(1001, update)
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.AllocByID(alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
update.ModifyIndex = 1001
if !reflect.DeepEqual(update, out) {
t.Fatalf("bad: %#v %#v", update, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("allocs")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpsertAlloc_Alloc(t *testing.T) {
state := testStateStore(t)
alloc := mock.Alloc()
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.AllocByID(alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(alloc, out) {
t.Fatalf("bad: %#v %#v", alloc, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("allocs")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1000 {
t.Fatalf("bad: %d", index)
}
}
func TestStateStore_WatchAllocNode(t *testing.T) {
state := testStateStore(t)
notify1 := make(chan struct{}, 1)
notify2 := make(chan struct{}, 1)
state.WatchAllocNode("foo", notify1)
state.WatchAllocNode("foo", notify2)
state.StopWatchAllocNode("foo", notify2)
alloc := mock.Alloc()
alloc.NodeID = "foo"
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
if err != nil {
t.Fatalf("err: %v", err)
}
select {
case <-notify1:
default:
t.Fatalf("should be notified")
}
select {
case <-notify2:
t.Fatalf("should not be notified")
default:
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_UpdateAlloc_Alloc(t *testing.T) {
state := testStateStore(t)
alloc := mock.Alloc()
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
if err != nil {
t.Fatalf("err: %v", err)
}
alloc2 := mock.Alloc()
alloc2.ID = alloc.ID
alloc2.NodeID = alloc.NodeID + ".new"
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc2})
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.AllocByID(alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(alloc2, out) {
t.Fatalf("bad: %#v %#v", alloc2, out)
}
if out.CreateIndex != 1000 {
t.Fatalf("bad: %#v", out)
}
if out.ModifyIndex != 1001 {
t.Fatalf("bad: %#v", out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("allocs")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
}
2015-09-07 03:51:01 +00:00
func TestStateStore_EvictAlloc_Alloc(t *testing.T) {
state := testStateStore(t)
alloc := mock.Alloc()
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
if err != nil {
t.Fatalf("err: %v", err)
}
2015-08-25 23:26:34 +00:00
alloc2 := new(structs.Allocation)
*alloc2 = *alloc
alloc2.DesiredStatus = structs.AllocDesiredStatusEvict
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc2})
if err != nil {
t.Fatalf("err: %v", err)
}
2015-09-07 03:51:01 +00:00
out, err := state.AllocByID(alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
2015-08-25 23:26:34 +00:00
if out.DesiredStatus != structs.AllocDesiredStatusEvict {
t.Fatalf("bad: %#v %#v", alloc, out)
}
2015-09-07 03:51:01 +00:00
index, err := state.Index("allocs")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1001 {
t.Fatalf("bad: %d", index)
}
}
2015-08-05 01:32:50 +00:00
func TestStateStore_AllocsByNode(t *testing.T) {
state := testStateStore(t)
var allocs []*structs.Allocation
for i := 0; i < 10; i++ {
alloc := mock.Alloc()
2015-08-05 01:32:50 +00:00
alloc.NodeID = "foo"
allocs = append(allocs, alloc)
}
err := state.UpsertAllocs(1000, allocs)
2015-08-05 01:32:50 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
out, err := state.AllocsByNode("foo")
if err != nil {
t.Fatalf("err: %v", err)
}
sort.Sort(AllocIDSort(allocs))
sort.Sort(AllocIDSort(out))
if !reflect.DeepEqual(allocs, out) {
t.Fatalf("bad: %#v %#v", allocs, out)
}
}
2015-08-07 00:36:10 +00:00
func TestStateStore_AllocsByJob(t *testing.T) {
state := testStateStore(t)
var allocs []*structs.Allocation
for i := 0; i < 10; i++ {
alloc := mock.Alloc()
2015-08-07 00:36:10 +00:00
alloc.JobID = "foo"
allocs = append(allocs, alloc)
}
err := state.UpsertAllocs(1000, allocs)
2015-08-07 00:36:10 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
out, err := state.AllocsByJob("foo")
if err != nil {
t.Fatalf("err: %v", err)
}
sort.Sort(AllocIDSort(allocs))
sort.Sort(AllocIDSort(out))
if !reflect.DeepEqual(allocs, out) {
t.Fatalf("bad: %#v %#v", allocs, out)
}
}
func TestStateStore_Allocs(t *testing.T) {
state := testStateStore(t)
var allocs []*structs.Allocation
for i := 0; i < 10; i++ {
alloc := mock.Alloc()
allocs = append(allocs, alloc)
}
err := state.UpsertAllocs(1000, allocs)
if err != nil {
t.Fatalf("err: %v", err)
}
iter, err := state.Allocs()
if err != nil {
t.Fatalf("err: %v", err)
}
var out []*structs.Allocation
for {
raw := iter.Next()
if raw == nil {
break
}
out = append(out, raw.(*structs.Allocation))
}
sort.Sort(AllocIDSort(allocs))
sort.Sort(AllocIDSort(out))
if !reflect.DeepEqual(allocs, out) {
t.Fatalf("bad: %#v %#v", allocs, out)
}
}
func TestStateStore_RestoreAlloc(t *testing.T) {
state := testStateStore(t)
restore, err := state.Restore()
if err != nil {
t.Fatalf("err: %v", err)
}
alloc := mock.Alloc()
err = restore.AllocRestore(alloc)
if err != nil {
t.Fatalf("err: %v", err)
}
restore.Commit()
2015-09-07 03:51:01 +00:00
out, err := state.AllocByID(alloc.ID)
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(out, alloc) {
t.Fatalf("Bad: %#v %#v", out, alloc)
}
}
// NodeIDSort is used to sort nodes by ID
type NodeIDSort []*structs.Node
func (n NodeIDSort) Len() int {
return len(n)
}
func (n NodeIDSort) Less(i, j int) bool {
return n[i].ID < n[j].ID
}
func (n NodeIDSort) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
2015-07-07 16:41:05 +00:00
// JobIDis used to sort jobs by id
type JobIDSort []*structs.Job
2015-07-07 16:41:05 +00:00
func (n JobIDSort) Len() int {
2015-07-07 16:41:05 +00:00
return len(n)
}
func (n JobIDSort) Less(i, j int) bool {
return n[i].ID < n[j].ID
2015-07-07 16:41:05 +00:00
}
func (n JobIDSort) Swap(i, j int) {
2015-07-07 16:41:05 +00:00
n[i], n[j] = n[j], n[i]
}
2015-07-23 22:43:06 +00:00
// EvalIDis used to sort evals by id
type EvalIDSort []*structs.Evaluation
func (n EvalIDSort) Len() int {
return len(n)
}
func (n EvalIDSort) Less(i, j int) bool {
return n[i].ID < n[j].ID
}
func (n EvalIDSort) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
// AllocIDsort used to sort allocations by id
type AllocIDSort []*structs.Allocation
func (n AllocIDSort) Len() int {
return len(n)
}
func (n AllocIDSort) Less(i, j int) bool {
return n[i].ID < n[j].ID
}
func (n AllocIDSort) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}