Skip to content

Commit

Permalink
Support Condition only resources in PipelineTask
Browse files Browse the repository at this point in the history
This commit fixes the Pipeline validation logic to account
for resources that are declared in a `PipelineTaskCondition` but
not in a `PipelineTask`. Previously, each resource used in a
`PipelineTaskCondition` also had to be declared in the `PipelineTask`.

Fixes #1263

Signed-off-by: Dibyo Mukherjee <[email protected]>
  • Loading branch information
dibyom authored and tekton-robot committed Sep 4, 2019
1 parent 454ddec commit d8a2fa5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func validateDeclaredResources(ps *PipelineSpec) error {
required = append(required, output.Resource)
}
}

for _, condition := range t.Conditions {
for _, cr := range condition.Resources {
required = append(required, cr.Resource)
}
}
}

provided := make([]string, 0, len(ps.Resources))
Expand Down
21 changes: 20 additions & 1 deletion pkg/apis/pipeline/v1alpha1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,22 @@ func TestPipelineSpec_Validate(t *testing.T) {
tb.PipelineDeclaredResource("wonderful-resource", v1alpha1.PipelineResourceTypeImage),
tb.PipelineTask("bar", "bar-task",
tb.PipelineTaskInputResource("some-workspace", "great-resource"),
tb.PipelineTaskOutputResource("some-image", "wonderful-resource")),
tb.PipelineTaskOutputResource("some-image", "wonderful-resource"),
tb.PipelineTaskCondition("some-condition",
tb.PipelineTaskConditionResource("some-workspace", "great-resource"))),
tb.PipelineTask("foo", "foo-task",
tb.PipelineTaskInputResource("wow-image", "wonderful-resource", tb.From("bar"))),
)),
failureExpected: false,
}, {
name: "valid condition only resource",
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
tb.PipelineDeclaredResource("great-resource", v1alpha1.PipelineResourceTypeGit),
tb.PipelineTask("bar", "bar-task",
tb.PipelineTaskCondition("some-condition",
tb.PipelineTaskConditionResource("some-workspace", "great-resource"))),
)),
failureExpected: false,
}, {
name: "valid parameter variables",
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
Expand Down Expand Up @@ -221,6 +232,14 @@ func TestPipelineSpec_Validate(t *testing.T) {
tb.PipelineTaskOutputResource("the-magic-resource", "great-resource")),
)),
failureExpected: true,
}, {
name: "invalid condition only resource",
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
tb.PipelineTask("bar", "bar-task",
tb.PipelineTaskCondition("some-condition",
tb.PipelineTaskConditionResource("some-workspace", "missing-resource"))),
)),
failureExpected: true,
}, {
name: "from resource isn't output by task",
p: tb.Pipeline("pipeline", "namespace", tb.PipelineSpec(
Expand Down
15 changes: 14 additions & 1 deletion test/builder/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func PipelineTaskCondition(conditionRef string, ops ...PipelineTaskConditionOp)
}
}

// PipelineTaskCondition adds a parameter to a PipelineTaskCondition
// PipelineTaskConditionParam adds a parameter to a PipelineTaskCondition
func PipelineTaskConditionParam(name, val string) PipelineTaskConditionOp {
return func(condition *v1alpha1.PipelineTaskCondition) {
if condition.Params == nil {
Expand All @@ -245,6 +245,19 @@ func PipelineTaskConditionParam(name, val string) PipelineTaskConditionOp {
}
}

// PipelineTaskConditionResource adds a resource to a PipelineTaskCondition
func PipelineTaskConditionResource(name, resource string) PipelineTaskConditionOp {
return func(condition *v1alpha1.PipelineTaskCondition) {
if condition.Resources == nil {
condition.Resources = []v1alpha1.PipelineConditionResource{}
}
condition.Resources = append(condition.Resources, v1alpha1.PipelineConditionResource{
Name: name,
Resource: resource,
})
}
}

// PipelineRun creates a PipelineRun with default values.
// Any number of PipelineRun modifier can be passed to transform it.
func PipelineRun(name, namespace string, ops ...PipelineRunOp) *v1alpha1.PipelineRun {
Expand Down
5 changes: 5 additions & 0 deletions test/builder/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestPipeline(t *testing.T) {
tb.PipelineTaskParam("arrayparam", "array", "value"),
tb.PipelineTaskCondition("some-condition-ref",
tb.PipelineTaskConditionParam("param-name", "param-value"),
tb.PipelineTaskConditionResource("some-resource", "my-only-git-resource"),
),
),
tb.PipelineTask("bar", "chocolate",
Expand Down Expand Up @@ -92,6 +93,10 @@ func TestPipeline(t *testing.T) {
StringVal: "param-value",
},
}},
Resources: []v1alpha1.PipelineConditionResource{{
Name: "some-resource",
Resource: "my-only-git-resource",
}},
}},
}, {
Name: "bar",
Expand Down

0 comments on commit d8a2fa5

Please sign in to comment.