Skip to content

Commit

Permalink
Add validation for "cencel" of "taskrun" and "pipelinerun"
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pli authored and tekton-robot committed Apr 16, 2020
1 parent d85678c commit 6c4f6b0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (ps *PipelineRunSpec) Validate(ctx context.Context) *apis.FieldError {
}
}

if ps.Status != "" {
if ps.Status != PipelineRunSpecStatusCancelled {
return apis.ErrInvalidValue(fmt.Sprintf("%s should be %s", ps.Status, PipelineRunSpecStatusCancelled), "spec.status")
}
}

if ps.Workspaces != nil {
wsNames := make(map[string]int)
for idx, ws := range ps.Workspaces {
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ func TestPipelineRun_Invalidate(t *testing.T) {
},
},
want: apis.ErrInvalidValue("-48h0m0s should be >= 0", "spec.timeout"),
}, {
name: "wrong pipelinerun cancel",
pr: v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Name: "pipelinelineName",
},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: &v1beta1.PipelineRef{
Name: "prname",
},
Status: "PipelineRunCancell",
},
},
want: apis.ErrInvalidValue("PipelineRunCancell should be PipelineRunCancelled", "spec.status"),
},
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func (ts *TaskRunSpec) Validate(ctx context.Context) *apis.FieldError {
return err
}

if ts.Status != "" {
if ts.Status != TaskRunSpecStatusCancelled {
return apis.ErrInvalidValue(fmt.Sprintf("%s should be %s", ts.Status, TaskRunSpecStatusCancelled), "spec.status")
}
}

if ts.Timeout != nil {
// timeout should be a valid duration of at least 0.
if ts.Timeout.Duration < 0 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ func TestTaskRunSpec_Invalidate(t *testing.T) {
Timeout: &metav1.Duration{Duration: -48 * time.Hour},
},
wantErr: apis.ErrInvalidValue("-48h0m0s should be >= 0", "spec.timeout"),
}, {
name: "wrong taskrun cancel",
spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "taskrefname",
},
Status: "TaskRunCancell",
},
wantErr: apis.ErrInvalidValue("TaskRunCancell should be TaskRunCancelled", "spec.status"),
}, {
name: "invalid taskspec",
spec: v1beta1.TaskRunSpec{
Expand Down

0 comments on commit 6c4f6b0

Please sign in to comment.