Skip to content

Commit

Permalink
func: add validation for update strategy at job level
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta committed Apr 3, 2023
1 parent 473135d commit af0c8b2
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 65 deletions.
6 changes: 6 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4629,6 +4629,12 @@ func (j *Job) Validate() error {
}
}

if !j.Update.IsEmpty() {
if err := j.Update.Validate(); err != nil {
mErr.Errors = append(mErr.Errors, err)
}
}

return mErr.ErrorOrNil()
}

Expand Down
174 changes: 109 additions & 65 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,125 @@ import (
func TestJob_Validate(t *testing.T) {
ci.Parallel(t)

j := &Job{}
err := j.Validate()
requireErrors(t, err,
"datacenters",
"job ID",
"job name",
"job region",
"job type",
"namespace",
"task groups",
)

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{
Enabled: true,
tests := []struct {
name string
job *Job
expErr []string
}{
{
name: "job is empty",
job: &Job{},
expErr: []string{
"datacenters",
"job ID",
"job name",
"job region",
"job type",
"namespace",
"task groups",
},
},
}
err = j.Validate()
require.Error(t, err, "Periodic")

j = &Job{
Region: "global",
ID: uuid.Generate(),
Namespace: "test",
Name: "my-job",
Type: JobTypeService,
Priority: JobDefaultPriority,
Datacenters: []string{"*"},
TaskGroups: []*TaskGroup{
{
Name: "web",
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
{
name: "job type is invalid",
job: &Job{
Type: "invalid-job-type",
},
expErr: []string{
`Invalid job type: "invalid-job-type"`,
},
},
{
name: "job periodic specification type is missing",
job: &Job{
Type: JobTypeService,
Periodic: &PeriodicConfig{
Enabled: true,
},
},
{
Name: "web",
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
expErr: []string{
`Unknown periodic specification type ""`,
"Must specify a spec",
},
},
{
name: "job datacenters is empty",
job: &Job{
Datacenters: []string{""},
},
expErr: []string{
"datacenter must be non-empty string",
},
},
{
name: "job update strategy is invalid",
job: &Job{
Update: UpdateStrategy{
HealthCheck: "foo",
HealthyDeadline: 10,
ProgressDeadline: 5,
MaxParallel: 2,
},
},
{
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
expErr: []string{
"Invalid health check given",
"Healthy deadline must be less than progress deadline",
},
},
{
name: "job task group is type invalid",
job: &Job{
Region: "global",
ID: uuid.Generate(),
Namespace: "test",
Name: "my-job",
Type: JobTypeService,
Priority: JobDefaultPriority,
Datacenters: []string{"*"},
TaskGroups: []*TaskGroup{
{
Name: "web",
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
},
},
{
Name: "web",
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
},
},
{
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
},
},
},
},
expErr: []string{
"2 redefines 'web' from group 1",
"group 3 missing name",
"Task group web validation failed",
"Missing tasks for task group",
"Unsupported restart mode",
"Task Group web should have a reschedule policy",
"Task Group web should have an ephemeral disk object",
},
},
}
err = j.Validate()
requireErrors(t, err,
"2 redefines 'web' from group 1",
"group 3 missing name",
"Task group web validation failed",
)
// test for invalid datacenters
j = &Job{
Datacenters: []string{""},
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := tc.job.Validate()
fmt.Println(err)
requireErrors(t, err, tc.expErr...)
})
}
err = j.Validate()
require.Error(t, err, "datacenter must be non-empty string")

}

func TestJob_ValidateScaling(t *testing.T) {
Expand Down

0 comments on commit af0c8b2

Please sign in to comment.