Skip to content

Commit

Permalink
backport of commit f589810
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Apr 24, 2023
1 parent ac59964 commit f9ae03f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,15 @@ func (j *Job) Scale(args *structs.JobScaleRequest, reply *structs.JobRegisterRes
return err
}

// Perform validation on the job to ensure we have something that can
// actually be scaled. This logic can only exist here, as we need access
// to the job object.
if job == nil {
return structs.NewErrRPCCoded(404, fmt.Sprintf("job %q not found", args.JobID))
}
if job.Type == structs.JobTypeSystem {
return structs.NewErrRPCCoded(http.StatusBadRequest, `cannot scale jobs of type "system"`)
}

// Since job is going to be mutated we must copy it since state store methods
// return a shared pointer.
Expand Down
28 changes: 28 additions & 0 deletions nomad/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7635,6 +7635,34 @@ func TestJobEndpoint_Scale_Priority(t *testing.T) {
requireAssertion.NotZero(eval.ModifyTime)
}

func TestJobEndpoint_Scale_SystemJob(t *testing.T) {
ci.Parallel(t)

testServer, testServerCleanup := TestServer(t, nil)
defer testServerCleanup()
codec := rpcClient(t, testServer)
testutil.WaitForLeader(t, testServer.RPC)
state := testServer.fsm.State()

mockSystemJob := mock.SystemJob()
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 10, nil, mockSystemJob))

scaleReq := &structs.JobScaleRequest{
JobID: mockSystemJob.ID,
Target: map[string]string{
structs.ScalingTargetGroup: mockSystemJob.TaskGroups[0].Name,
},
Count: pointer.Of(int64(13)),
WriteRequest: structs.WriteRequest{
Region: DefaultRegion,
Namespace: mockSystemJob.Namespace,
},
}
var resp structs.JobRegisterResponse
must.ErrorContains(t, msgpackrpc.CallWithCodec(codec, "Job.Scale", scaleReq, &resp),
`400,cannot scale jobs of type "system"`)
}

func TestJobEndpoint_InvalidCount(t *testing.T) {
ci.Parallel(t)
require := require.New(t)
Expand Down

0 comments on commit f9ae03f

Please sign in to comment.