2015-08-15 23:07:50 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-08-16 00:42:51 +00:00
|
|
|
"time"
|
2015-08-15 23:07:50 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCoreScheduler_EvalGC(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Insert "dead" eval
|
|
|
|
state := s1.fsm.State()
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.Status = structs.EvalStatusFailed
|
|
|
|
err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert "dead" alloc
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.EvalID = eval.ID
|
2015-08-26 00:36:52 +00:00
|
|
|
alloc.DesiredStatus = structs.AllocDesiredStatusFailed
|
2015-09-07 03:47:42 +00:00
|
|
|
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc})
|
2015-08-15 23:07:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-16 00:42:51 +00:00
|
|
|
// Update the time tables to make this work
|
|
|
|
tt := s1.fsm.TimeTable()
|
|
|
|
tt.Witness(2000, time.Now().UTC().Add(-1*s1.config.EvalGCThreshold))
|
|
|
|
|
2015-08-15 23:07:50 +00:00
|
|
|
// Create a core scheduler
|
|
|
|
snap, err := state.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
core := NewCoreScheduler(s1, snap)
|
|
|
|
|
|
|
|
// Attempt the GC
|
|
|
|
gc := s1.coreJobEval(structs.CoreJobEvalGC)
|
|
|
|
gc.ModifyIndex = 2000
|
|
|
|
err = core.Process(gc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be gone
|
2015-09-07 03:56:38 +00:00
|
|
|
out, err := state.EvalByID(eval.ID)
|
2015-08-15 23:07:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2015-09-07 03:56:38 +00:00
|
|
|
outA, err := state.AllocByID(alloc.ID)
|
2015-08-15 23:07:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if outA != nil {
|
|
|
|
t.Fatalf("bad: %v", outA)
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 18:01:29 +00:00
|
|
|
|
|
|
|
func TestCoreScheduler_NodeGC(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Insert "dead" node
|
|
|
|
state := s1.fsm.State()
|
|
|
|
node := mock.Node()
|
|
|
|
node.Status = structs.NodeStatusDown
|
|
|
|
err := state.UpsertNode(1000, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the time tables to make this work
|
|
|
|
tt := s1.fsm.TimeTable()
|
|
|
|
tt.Witness(2000, time.Now().UTC().Add(-1*s1.config.NodeGCThreshold))
|
|
|
|
|
|
|
|
// Create a core scheduler
|
|
|
|
snap, err := state.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
core := NewCoreScheduler(s1, snap)
|
|
|
|
|
|
|
|
// Attempt the GC
|
|
|
|
gc := s1.coreJobEval(structs.CoreJobNodeGC)
|
|
|
|
gc.ModifyIndex = 2000
|
|
|
|
err = core.Process(gc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be gone
|
|
|
|
out, err := state.NodeByID(node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|