Skip to content

Commit

Permalink
Add PipelineSpec validation.
Browse files Browse the repository at this point in the history
This adds validation for the following cases:
- duplicate PipelineTask Names
- invalid passedConstraint Names
  • Loading branch information
dlorenc authored and knative-prow-robot committed Oct 15, 2018
1 parent 2789caa commit 5d286dd
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/apis/pipeline/v1alpha1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ type SourceBinding struct {
Key string `json:"key"`
// The Resource this binding is referring to
ResourceRef PipelineResourceRef `json:"resourceRef"`
// TODO: validate the passedConstraints values match previous Task names
// PassedConstraints is the list of Task names that the resource has to pass through.
// +optional
PassedConstraints []string `json:"passedConstraints,omitempty"`
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"fmt"

"github.com/knative/pkg/apis"
"k8s.io/apimachinery/pkg/api/equality"
)
Expand All @@ -33,5 +35,24 @@ func (ps *PipelineSpec) Validate() *apis.FieldError {
return apis.ErrMissingField(apis.CurrentField)
}

// Names cannot be duplicated
taskNames := map[string]struct{}{}
for _, t := range ps.Tasks {
if _, ok := taskNames[t.Name]; ok {
return apis.ErrMultipleOneOf("spec.tasks.name")
}
taskNames[t.Name] = struct{}{}
}

// passedConstraints should match other tasks.
for _, t := range ps.Tasks {
for _, isb := range t.InputSourceBindings {
for _, pc := range isb.PassedConstraints {
if _, ok := taskNames[pc]; !ok {
return apis.ErrInvalidKeyName(pc, fmt.Sprintf("spec.tasks.inputSourceBindings.%s", pc))
}
}
}
}
return nil
}
128 changes: 128 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"testing"
)

func TestPipelineSpec_Validate_Error(t *testing.T) {
type fields struct {
Tasks []PipelineTask
Generation int64
}
tests := []struct {
name string
fields fields
}{
{
name: "duplicate tasks",
fields: fields{
Tasks: []PipelineTask{
{
Name: "foo",
},
{
Name: "foo",
},
},
},
},
{
name: "invalid constraint tasks",
fields: fields{
Tasks: []PipelineTask{
{
Name: "foo",
InputSourceBindings: []SourceBinding{
{
Name: "binding",
PassedConstraints: []string{"bar"},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ps := &PipelineSpec{
Tasks: tt.fields.Tasks,
Generation: tt.fields.Generation,
}
if err := ps.Validate(); err == nil {
t.Error("PipelineSpec.Validate() did not return error, wanted error")
}
})
}
}

func TestPipelineSpec_Validate_Valid(t *testing.T) {
type fields struct {
Tasks []PipelineTask
Generation int64
}
tests := []struct {
name string
fields fields
}{
{
name: "no duplicate tasks",
fields: fields{
Tasks: []PipelineTask{
{
Name: "foo",
},
{
Name: "baz",
},
},
},
},
{
name: "valid constraint tasks",
fields: fields{
Tasks: []PipelineTask{
{
Name: "foo",
InputSourceBindings: []SourceBinding{
{
Name: "binding",
PassedConstraints: []string{"bar"},
},
},
},
{
Name: "bar",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ps := &PipelineSpec{
Tasks: tt.fields.Tasks,
Generation: tt.fields.Generation,
}
if err := ps.Validate(); err != nil {
t.Errorf("PipelineSpec.Validate() returned error: %v", err)
}
})
}
}

0 comments on commit 5d286dd

Please sign in to comment.