Validate job type

Fixes #2722
This commit is contained in:
Michael Schurter 2017-07-07 15:34:26 -07:00
parent e98e599419
commit 517c799439
2 changed files with 13 additions and 1 deletions

View file

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

View file

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