Skip to content

Commit

Permalink
Backport of api: prevent panic on job plan into release/1.4.x (#17710)
Browse files Browse the repository at this point in the history
This pull request was automerged via backport-assistant
  • Loading branch information
hc-github-team-nomad-core committed Jun 23, 2023
1 parent 9a4e761 commit f7975c4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/17689.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: Fixed a bug that caused a panic when calling the `Jobs().Plan()` function with a job missing an ID
```
3 changes: 3 additions & 0 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ func (j *Jobs) PlanOpts(job *Job, opts *PlanOptions, q *WriteOptions) (*JobPlanR
if job == nil {
return nil, nil, errors.New("must pass non-nil job")
}
if job.ID == nil {
return nil, nil, errors.New("job is missing ID")
}

// Setup the request
req := &JobPlanRequest{
Expand Down
6 changes: 6 additions & 0 deletions api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,12 @@ func TestJobs_Plan(t *testing.T) {
_, _, err = jobs.Plan(nil, true, nil)
must.Error(t, err)

// Check that passing a nil job ID fails
invalidJob := testJob()
invalidJob.ID = nil
_, _, err = jobs.Plan(invalidJob, true, nil)
must.Error(t, err)

// Make a plan request
planResp, wm, err := jobs.Plan(job, true, nil)
must.NoError(t, err)
Expand Down

0 comments on commit f7975c4

Please sign in to comment.