Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PipelineSpec validation. #152

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}