github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/scheduler/context_test.go (about) 1 package scheduler 2 3 import ( 4 "log" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/nomad/nomad/state" 9 "github.com/hashicorp/nomad/nomad/structs" 10 ) 11 12 func testContext(t testing.TB) (*state.StateStore, *EvalContext) { 13 state, err := state.NewStateStore(os.Stderr) 14 if err != nil { 15 t.Fatalf("err: %v", err) 16 } 17 plan := &structs.Plan{ 18 NodeUpdate: make(map[string][]*structs.Allocation), 19 NodeAllocation: make(map[string][]*structs.Allocation), 20 } 21 22 logger := log.New(os.Stderr, "", log.LstdFlags) 23 24 ctx := NewEvalContext(state, plan, logger) 25 return state, ctx 26 } 27 28 func TestEvalContext_ProposedAlloc(t *testing.T) { 29 state, ctx := testContext(t) 30 nodes := []*RankedNode{ 31 &RankedNode{ 32 Node: &structs.Node{ 33 // Perfect fit 34 ID: structs.GenerateUUID(), 35 Resources: &structs.Resources{ 36 CPU: 2048, 37 MemoryMB: 2048, 38 }, 39 }, 40 }, 41 &RankedNode{ 42 Node: &structs.Node{ 43 // Perfect fit 44 ID: structs.GenerateUUID(), 45 Resources: &structs.Resources{ 46 CPU: 2048, 47 MemoryMB: 2048, 48 }, 49 }, 50 }, 51 } 52 53 // Add existing allocations 54 alloc1 := &structs.Allocation{ 55 ID: structs.GenerateUUID(), 56 EvalID: structs.GenerateUUID(), 57 NodeID: nodes[0].Node.ID, 58 JobID: structs.GenerateUUID(), 59 Resources: &structs.Resources{ 60 CPU: 2048, 61 MemoryMB: 2048, 62 }, 63 DesiredStatus: structs.AllocDesiredStatusRun, 64 } 65 alloc2 := &structs.Allocation{ 66 ID: structs.GenerateUUID(), 67 EvalID: structs.GenerateUUID(), 68 NodeID: nodes[1].Node.ID, 69 JobID: structs.GenerateUUID(), 70 Resources: &structs.Resources{ 71 CPU: 1024, 72 MemoryMB: 1024, 73 }, 74 DesiredStatus: structs.AllocDesiredStatusRun, 75 } 76 noErr(t, state.UpsertAllocs(1000, []*structs.Allocation{alloc1, alloc2})) 77 78 // Add a planned eviction to alloc1 79 plan := ctx.Plan() 80 plan.NodeUpdate[nodes[0].Node.ID] = []*structs.Allocation{alloc1} 81 82 // Add a planned placement to node1 83 plan.NodeAllocation[nodes[1].Node.ID] = []*structs.Allocation{ 84 &structs.Allocation{ 85 Resources: &structs.Resources{ 86 CPU: 1024, 87 MemoryMB: 1024, 88 }, 89 }, 90 } 91 92 proposed, err := ctx.ProposedAllocs(nodes[0].Node.ID) 93 if err != nil { 94 t.Fatalf("err: %v", err) 95 } 96 if len(proposed) != 0 { 97 t.Fatalf("bad: %#v", proposed) 98 } 99 100 proposed, err = ctx.ProposedAllocs(nodes[1].Node.ID) 101 if err != nil { 102 t.Fatalf("err: %v", err) 103 } 104 if len(proposed) != 2 { 105 t.Fatalf("bad: %#v", proposed) 106 } 107 }