Skip to content

Commit

Permalink
ARMADA-2849 Add Validation for Jobset ID (#213) (#3904)
Browse files Browse the repository at this point in the history
Co-authored-by: Eleanor Pratt <Eleanor.Pratt@gresearch.co.uk>
  • Loading branch information
d80tb7 and Eleanor Pratt authored Sep 5, 2024
1 parent cb2b95e commit bdf9f4e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/server/submit/validation/submit_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
requestValidators = []requestValidator{
validateHasQueue,
validateGangs,
validateHasJobSetId,
validateJobSetIdLength,
}
itemValidators = []itemValidator{
validateHasNamespace,
Expand Down Expand Up @@ -100,6 +102,23 @@ func validateIngresses(j *api.JobSubmitRequestItem, _ configuration.SubmissionCo
return nil
}

// Ensures that the request has non-empty job set id field.
func validateHasJobSetId(j *api.JobSubmitRequest, _ configuration.SubmissionConfig) error {
if len(j.JobSetId) == 0 {
return fmt.Errorf("job set id is a required field")
}
return nil
}

// Ensures that the request has job set id field isn't too long.
func validateJobSetIdLength(j *api.JobSubmitRequest, _ configuration.SubmissionConfig) error {
const maxJobSetIdChars = 1024
if len(j.GetJobSetId()) >= maxJobSetIdChars {
return fmt.Errorf("job set id of length %d must be less than max character length %d", len(j.GetJobSetId()), maxJobSetIdChars)
}
return nil
}

// Ensures that the request has non-empty namespace field.
func validateHasNamespace(j *api.JobSubmitRequestItem, _ configuration.SubmissionConfig) error {
if len(j.Namespace) == 0 {
Expand Down
58 changes: 58 additions & 0 deletions internal/server/submit/validation/submit_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,64 @@ func TestValidateNamespace(t *testing.T) {
}
}

func TestValidateHasJobSetID(t *testing.T) {
tests := map[string]struct {
req *api.JobSubmitRequest
expectSuccess bool
}{
"no job set id": {
req: &api.JobSubmitRequest{},
expectSuccess: false,
},
"has any job set id": {
req: &api.JobSubmitRequest{
JobSetId: "job_set_id",
},
expectSuccess: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := validateHasJobSetId(tc.req, configuration.SubmissionConfig{})
if tc.expectSuccess {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}

func TestValidateJobSetIdLength(t *testing.T) {
tests := map[string]struct {
req *api.JobSubmitRequest
expectSuccess bool
}{
"job set id of 1023 chars valid": {
req: &api.JobSubmitRequest{
JobSetId: strings.Repeat("a", 1023),
},
expectSuccess: true,
},
"job set id of 1024 chars invalid": {
req: &api.JobSubmitRequest{
JobSetId: strings.Repeat("a", 1024),
},
expectSuccess: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := validateJobSetIdLength(tc.req, configuration.SubmissionConfig{})
if tc.expectSuccess {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}

func TestValidatePodSpecSize(t *testing.T) {
defaultPodSpec := &v1.PodSpec{
Volumes: []v1.Volume{
Expand Down

0 comments on commit bdf9f4e

Please sign in to comment.