From f56bdbe03aefb1a7fe251382dd4e6a43b7ba0d51 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 16 May 2016 12:49:18 -0700 Subject: [PATCH] Switch to using the harness --- api/jobs.go | 8 ++--- api/jobs_test.go | 8 ++--- command/agent/job_endpoint_test.go | 2 +- nomad/job_endpoint.go | 11 +++++-- scheduler/inmem_planner.go | 36 --------------------- scheduler/{scheduler_test.go => testing.go} | 13 ++------ scheduler/util_test.go | 7 ++++ 7 files changed, 27 insertions(+), 58 deletions(-) delete mode 100644 scheduler/inmem_planner.go rename scheduler/{scheduler_test.go => testing.go} (92%) diff --git a/api/jobs.go b/api/jobs.go index 91fc2aa9b..71c761caa 100644 --- a/api/jobs.go +++ b/api/jobs.go @@ -282,10 +282,10 @@ type JobPlanRequest struct { } type JobPlanResponse struct { - Cas uint64 - CreatedEvals []*Evaluation - Diff *JobDiff - Annotations *PlanAnnotations + JobModifyIndex uint64 + CreatedEvals []*Evaluation + Diff *JobDiff + Annotations *PlanAnnotations } type JobDiff struct { diff --git a/api/jobs_test.go b/api/jobs_test.go index 99c744082..8ca0b55fc 100644 --- a/api/jobs_test.go +++ b/api/jobs_test.go @@ -380,8 +380,8 @@ func TestJobs_Plan(t *testing.T) { t.Fatalf("nil response") } - if planResp.Cas == 0 { - t.Fatalf("bad Cas value: %#v", planResp) + if planResp.JobModifyIndex == 0 { + t.Fatalf("bad JobModifyIndex value: %#v", planResp) } if planResp.Diff == nil { t.Fatalf("got nil diff: %#v", planResp) @@ -405,8 +405,8 @@ func TestJobs_Plan(t *testing.T) { t.Fatalf("nil response") } - if planResp.Cas == 0 { - t.Fatalf("bad Cas value: %d", planResp.Cas) + if planResp.JobModifyIndex == 0 { + t.Fatalf("bad JobModifyIndex value: %d", planResp.JobModifyIndex) } if planResp.Diff != nil { t.Fatalf("got non-nil diff: %#v", planResp) diff --git a/command/agent/job_endpoint_test.go b/command/agent/job_endpoint_test.go index 22c448525..c0de9ee3e 100644 --- a/command/agent/job_endpoint_test.go +++ b/command/agent/job_endpoint_test.go @@ -510,7 +510,7 @@ func TestHTTP_JobPlan(t *testing.T) { // Check the response plan := obj.(structs.JobPlanResponse) - if plan.Plan == nil { + if plan.Annotations == nil { t.Fatalf("bad: %v", plan) } diff --git a/nomad/job_endpoint.go b/nomad/job_endpoint.go index ec9186f8c..13a8c9e44 100644 --- a/nomad/job_endpoint.go +++ b/nomad/job_endpoint.go @@ -443,7 +443,9 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse) // Create an in-memory Planner that returns no errors and stores the // submitted plan and created evals. - planner := scheduler.NewInMemoryPlanner() + planner := &scheduler.Harness{ + State: &snap.StateStore, + } // Create the scheduler and run it sched, err := scheduler.NewScheduler(eval.Type, j.srv.logger, snap, planner) @@ -456,7 +458,10 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse) } // Annotate and store the diff - annotations := planner.Plan.Annotations + if plans := len(planner.Plans); plans != 1 { + return fmt.Errorf("scheduler resulted in an unexpected number of plans: %d", plans) + } + annotations := planner.Plans[0].Annotations if args.Diff { jobDiff, err := oldJob.Diff(args.Job, true) if err != nil { @@ -471,7 +476,7 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse) reply.JobModifyIndex = index reply.Annotations = annotations - reply.CreatedEvals = planner.CreatedEvals + reply.CreatedEvals = planner.CreateEvals reply.Index = index return nil } diff --git a/scheduler/inmem_planner.go b/scheduler/inmem_planner.go deleted file mode 100644 index 1c9ccbbd6..000000000 --- a/scheduler/inmem_planner.go +++ /dev/null @@ -1,36 +0,0 @@ -package scheduler - -import "github.com/hashicorp/nomad/nomad/structs" - -// InMemPlanner is an in-memory Planner that can be used to invoke the scheduler -// without side-effects. -type InMemPlanner struct { - CreatedEvals []*structs.Evaluation - Plan *structs.Plan -} - -// NewInMemoryPlanner returns a new in-memory planner. -func NewInMemoryPlanner() *InMemPlanner { - return &InMemPlanner{} -} - -func (i *InMemPlanner) SubmitPlan(plan *structs.Plan) (*structs.PlanResult, State, error) { - i.Plan = plan - - // Create a fully committed plan result. - result := &structs.PlanResult{ - NodeUpdate: plan.NodeUpdate, - NodeAllocation: plan.NodeAllocation, - } - - return result, nil, nil -} - -func (i *InMemPlanner) UpdateEval(eval *structs.Evaluation) error { - return nil -} - -func (i *InMemPlanner) CreateEval(eval *structs.Evaluation) error { - i.CreatedEvals = append(i.CreatedEvals, eval) - return nil -} diff --git a/scheduler/scheduler_test.go b/scheduler/testing.go similarity index 92% rename from scheduler/scheduler_test.go rename to scheduler/testing.go index 526ac2d90..fbd1aeda7 100644 --- a/scheduler/scheduler_test.go +++ b/scheduler/testing.go @@ -29,9 +29,9 @@ func (r *RejectPlan) CreateEval(*structs.Evaluation) error { return nil } -// Harness is a lightweight testing harness for schedulers. -// It manages a state store copy and provides the planner -// interface. It can be extended for various testing uses. +// Harness is a lightweight testing harness for schedulers. It manages a state +// store copy and provides the planner interface. It can be extended for various +// testing uses or for invoking the scheduler without side effects. type Harness struct { State *state.StateStore @@ -178,10 +178,3 @@ func (h *Harness) AssertEvalStatus(t *testing.T, state string) { t.Fatalf("bad: %#v", update) } } - -// noErr is used to assert there are no errors -func noErr(t *testing.T, err error) { - if err != nil { - t.Fatalf("err: %v", err) - } -} diff --git a/scheduler/util_test.go b/scheduler/util_test.go index b2f518e57..0d5cc915b 100644 --- a/scheduler/util_test.go +++ b/scheduler/util_test.go @@ -12,6 +12,13 @@ import ( "github.com/hashicorp/nomad/nomad/structs" ) +// noErr is used to assert there are no errors +func noErr(t *testing.T, err error) { + if err != nil { + t.Fatalf("err: %v", err) + } +} + func TestMaterializeTaskGroups(t *testing.T) { job := mock.Job() index := materializeTaskGroups(job)