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 checking for duplicate declared resources #2266

Merged
merged 1 commit into from
Mar 23, 2020
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
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func (p *Pipeline) Validate(ctx context.Context) *apis.FieldError {
}

func validateDeclaredResources(ps *PipelineSpec) error {
encountered := map[string]struct{}{}
for _, r := range ps.Resources {
if _, ok := encountered[r.Name]; ok {
return fmt.Errorf("resource with name %q appears more than once", r.Name)
}
encountered[r.Name] = struct{}{}
}
required := []string{}
for _, t := range ps.Tasks {
if t.Resources != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ func TestPipeline_Validate(t *testing.T) {
tb.PipelineTaskInputResource("the-resource", "great-resource", tb.From("bar"))),
)),
failureExpected: true,
}, {
name: "duplicate resource declaration",
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
tb.PipelineDeclaredResource("duplicate-resource", v1alpha1.PipelineResourceTypeGit),
tb.PipelineDeclaredResource("duplicate-resource", v1alpha1.PipelineResourceTypeGit),
tb.PipelineTask("foo", "foo-task",
tb.PipelineTaskInputResource("the-resource", "duplicate-resource")),
)),
failureExpected: true,
}, {
name: "output resources missing from declaration",
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func (p *Pipeline) Validate(ctx context.Context) *apis.FieldError {
}

func validateDeclaredResources(ps *PipelineSpec) error {
encountered := map[string]struct{}{}
for _, r := range ps.Resources {
if _, ok := encountered[r.Name]; ok {
return fmt.Errorf("resource with name %q appears more than once", r.Name)
}
encountered[r.Name] = struct{}{}
}
required := []string{}
for _, t := range ps.Tasks {
if t.Resources != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,28 @@ func TestPipeline_Validate(t *testing.T) {
},
},
failureExpected: true,
}, {
name: "duplicate resource declaration",
p: &v1beta1.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: "pipeline"},
Spec: v1beta1.PipelineSpec{
Resources: []v1beta1.PipelineDeclaredResource{{
Name: "duplicate-resource", Type: v1beta1.PipelineResourceTypeGit,
}, {
Name: "duplicate-resource", Type: v1beta1.PipelineResourceTypeGit,
}},
Tasks: []v1beta1.PipelineTask{{
Name: "foo",
TaskRef: &v1beta1.TaskRef{Name: "foo-task"},
Resources: &v1beta1.PipelineTaskResources{
Inputs: []v1beta1.PipelineTaskInputResource{{
Name: "the-resource", Resource: "duplicate-resource",
}},
},
}},
},
},
failureExpected: true,
}, {
name: "output resources missing from declaration",
p: &v1beta1.Pipeline{
Expand Down