diff --git a/nomad/job_endpoint.go b/nomad/job_endpoint.go index 30ce58098..76f566393 100644 --- a/nomad/job_endpoint.go +++ b/nomad/job_endpoint.go @@ -332,6 +332,18 @@ func (j *Job) Revert(args *structs.JobRevertRequest, reply *structs.JobRegisterR return fmt.Errorf("job %q at version %d not found", args.JobID, args.JobVersion) } + cur, err := snap.JobByID(ws, args.JobID) + if err != nil { + return err + } + if cur == nil { + return fmt.Errorf("job %q not found", args.JobID) + } + + if args.JobVersion == cur.Version { + return fmt.Errorf("can't revert to current version") + } + // Build the register request reg := &structs.JobRegisterRequest{ Job: jobV.Copy(), @@ -340,11 +352,6 @@ func (j *Job) Revert(args *structs.JobRevertRequest, reply *structs.JobRegisterR // If the request is enforcing the existing version do a check. if args.EnforcePriorVersion != nil { - cur, err := snap.JobByID(ws, args.JobID) - if err != nil { - return err - } - if cur.Version != *args.EnforcePriorVersion { return fmt.Errorf("Current job has version %d; enforcing version %d", cur.Version, *args.EnforcePriorVersion) } diff --git a/nomad/job_endpoint_test.go b/nomad/job_endpoint_test.go index e0843bdcf..271d1b22a 100644 --- a/nomad/job_endpoint_test.go +++ b/nomad/job_endpoint_test.go @@ -743,6 +743,19 @@ func TestJobEndpoint_Revert(t *testing.T) { t.Fatalf("expected enforcement error") } + // Create revert request and enforcing it be at the current version + revertReq = &structs.JobRevertRequest{ + JobID: job.ID, + JobVersion: 1, + WriteRequest: structs.WriteRequest{Region: "global"}, + } + + // Fetch the response + err = msgpackrpc.CallWithCodec(codec, "Job.Revert", revertReq, &resp) + if err == nil || !strings.Contains(err.Error(), "current version") { + t.Fatalf("expected current version err: %v", err) + } + // Create revert request and enforcing it be at version 1 revertReq = &structs.JobRevertRequest{ JobID: job.ID,