Skip to content

Commit

Permalink
Merge pull request #2805 from hashicorp/b-2722-invalid-type
Browse files Browse the repository at this point in the history
Validate job type
  • Loading branch information
schmichael authored Jul 7, 2017
2 parents faa4da7 + 2559ab7 commit 0ba7948
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1587,8 +1587,12 @@ func (j *Job) Validate() error {
if j.Name == "" {
mErr.Errors = append(mErr.Errors, errors.New("Missing job name"))
}
if j.Type == "" {
switch j.Type {
case JobTypeCore, JobTypeService, JobTypeBatch, JobTypeSystem:
case "":
mErr.Errors = append(mErr.Errors, errors.New("Missing job type"))
default:
mErr.Errors = append(mErr.Errors, fmt.Errorf("Invalid job type: %q", j.Type))
}
if j.Priority < JobMinPriority || j.Priority > JobMaxPriority {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Job priority must be between [%d, %d]", JobMinPriority, JobMaxPriority))
Expand Down
8 changes: 8 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestJob_Validate(t *testing.T) {
t.Fatalf("err: %s", err)
}

j = &Job{
Type: "invalid-job-type",
}
err = j.Validate()
if expected := `Invalid job type: "invalid-job-type"`; !strings.Contains(err.Error(), expected) {
t.Errorf("expected %s but found: %v", expected, err)
}

j = &Job{
Type: JobTypeService,
Periodic: &PeriodicConfig{
Expand Down

0 comments on commit 0ba7948

Please sign in to comment.