Don't allow revert to current version

This commit is contained in:
Alex Dadgar 2017-04-19 13:28:29 -07:00
parent 65945a09fa
commit 321e01988a
2 changed files with 25 additions and 5 deletions

View File

@ -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)
}

View File

@ -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,