From ffc6b88a366bda126e10ec7b9f195e451fadbd2c Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 13 Aug 2015 22:11:32 -0700 Subject: [PATCH] scheduler: test node drain behavior --- scheduler/service_sched_test.go | 73 ++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/scheduler/service_sched_test.go b/scheduler/service_sched_test.go index 5d6061934..040532f9f 100644 --- a/scheduler/service_sched_test.go +++ b/scheduler/service_sched_test.go @@ -40,7 +40,7 @@ func TestServiceSched_JobRegister(t *testing.T) { } plan := h.Plans[0] - // Ensure the plan evicted all nodes + // Ensure the plan allocated var planned []*structs.Allocation for _, allocList := range plan.NodeAllocation { planned = append(planned, allocList...) @@ -114,5 +114,74 @@ func TestServiceSched_JobDeregister(t *testing.T) { } func TestServiceSched_NodeDrain(t *testing.T) { - // TODO + h := NewHarness(t) + + // Register a draining node + node := mock.Node() + node.Status = structs.NodeStatusDrain + noErr(t, h.State.RegisterNode(h.NextIndex(), node)) + + // Create some nodes + for i := 0; i < 10; i++ { + node := mock.Node() + noErr(t, h.State.RegisterNode(h.NextIndex(), node)) + } + + // Generate a fake job with allocations + job := mock.Job() + noErr(t, h.State.RegisterJob(h.NextIndex(), job)) + + var allocs []*structs.Allocation + for i := 0; i < 10; i++ { + alloc := mock.Alloc() + alloc.Job = job + alloc.JobID = job.ID + alloc.NodeID = node.ID + allocs = append(allocs, alloc) + } + noErr(t, h.State.UpdateAllocations(h.NextIndex(), nil, allocs)) + + // Create a mock evaluation to deal with drain + eval := &structs.Evaluation{ + ID: mock.GenerateUUID(), + Priority: 50, + TriggeredBy: structs.EvalTriggerNodeUpdate, + JobID: job.ID, + NodeID: node.ID, + } + + // Process the evaluation + err := h.Process(NewServiceScheduler, eval) + if err != nil { + t.Fatalf("err: %v", err) + } + + // Ensure a single plan + if len(h.Plans) != 1 { + t.Fatalf("bad: %#v", h.Plans) + } + plan := h.Plans[0] + + // Ensure the plan evicted all allocs + if len(plan.NodeEvict[node.ID]) != len(allocs) { + t.Fatalf("bad: %#v", plan) + } + + // Ensure the plan allocated + var planned []*structs.Allocation + for _, allocList := range plan.NodeAllocation { + planned = append(planned, allocList...) + } + if len(planned) != 10 { + t.Fatalf("bad: %#v", plan) + } + + // Lookup the allocations by JobID + out, err := h.State.AllocsByJob(job.ID) + noErr(t, err) + + // Ensure all allocations placed + if len(out) != 10 { + t.Fatalf("bad: %#v", out) + } }