Skip to content

Commit

Permalink
validate TaskRun retries in TestRunSpec is greater than or equal to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma-YuXin committed Apr 5, 2024
1 parent 5222fbe commit d96567e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/apis/pipeline/v1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func (ts *TaskRunSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("statusMessage should not be set if status is not set, but it is currently set to %s", ts.StatusMessage), "statusMessage"))
}
}

if ts.Retries < 0 {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%d should be >= 0", ts.Retries), "retries"))
}
if ts.Timeout != nil {
// timeout should be a valid duration of at least 0.
if ts.Timeout.Duration < 0 {
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/pipeline/v1/taskrun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,15 @@ func TestTaskRunSpec_Invalidate(t *testing.T) {
Timeout: &metav1.Duration{Duration: -48 * time.Hour},
},
wantErr: apis.ErrInvalidValue("-48h0m0s should be >= 0", "timeout"),
}, {
name: "negative pipeline retries",
spec: v1.TaskRunSpec{
TaskRef: &v1.TaskRef{
Name: "taskrefname",
},
Retries: -3,
},
wantErr: apis.ErrInvalidValue("-3 should be >= 0", "retries"),
}, {
name: "wrong taskrun cancel",
spec: v1.TaskRunSpec{
Expand Down Expand Up @@ -828,6 +837,18 @@ func TestTaskRunSpec_Validate(t *testing.T) {
}},
},
},
}, {
name: "with positive retries",
spec: v1.TaskRunSpec{
Timeout: &metav1.Duration{Duration: 0},
Retries: 3,
TaskSpec: &v1.TaskSpec{
Steps: []v1.Step{{
Name: "mystep",
Image: "myimage",
}},
},
},
}, {
name: "parameters",
spec: v1.TaskRunSpec{
Expand Down

0 comments on commit d96567e

Please sign in to comment.