Skip to content

Commit

Permalink
Fix bundle conversion for pipelineRef
Browse files Browse the repository at this point in the history
This commit fixes the bundle conversion for PipelineRef which should
be converting to the bundle Resolver with a Pipeline kind. It also adds
the conversion integration tests for the bundle logics to prevent such
error. It extracts the setup for bundle and makes it into a helper function.
  • Loading branch information
JeromeJu committed Jun 8, 2023
1 parent b5c2135 commit d60af7d
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 125 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/pipelineref_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (pr PipelineRef) convertBundleToResolver(sink *v1.PipelineRef) {
Value: v1.ParamValue{StringVal: pr.Name, Type: v1.ParamTypeString},
}, {
Name: "kind",
Value: v1.ParamValue{StringVal: "Task", Type: v1.ParamTypeString},
Value: v1.ParamValue{StringVal: "Pipeline", Type: v1.ParamTypeString},
}},
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/pipelinerun_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func TestPipelineRunConversionFromDeprecated(t *testing.T) {
Params: v1beta1.Params{
{Name: "bundle", Value: v1beta1.ParamValue{StringVal: "test-bundle", Type: "string"}},
{Name: "name", Value: v1beta1.ParamValue{StringVal: "test-bundle-name", Type: "string"}},
{Name: "kind", Value: v1beta1.ParamValue{StringVal: "Task", Type: "string"}},
{Name: "kind", Value: v1beta1.ParamValue{StringVal: "Pipeline", Type: "string"}},
},
},
},
Expand Down
301 changes: 301 additions & 0 deletions test/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (
)

var (
requireBundleFeatureFlags = requireAnyGate(map[string]string{
"enable-tekton-oci-bundles": "true",
"enable-api-fields": "alpha",
})

filterLabels = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "Labels")
filterAnnotations = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "Annotations")
filterV1TaskRunStatus = cmpopts.IgnoreFields(v1.TaskRunStatusFields{}, "StartTime", "CompletionTime")
Expand Down Expand Up @@ -649,6 +654,210 @@ status:
name: %s-hello-task
pipelineTaskName: hello-task
`

v1beta1TaskWithBundleYaml = `
metadata:
name: %s
namespace: %s
spec:
steps:
- name: hello
image: alpine
script: 'echo Hello'
`

v1beta1PipelineWithBundleYaml = `
metadata:
name: %s
namespace: %s
spec:
tasks:
- name: hello-world
taskRef:
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
`

v1beta1TaskRunWithBundleYaml = `
metadata:
name: %s
namespace: %s
spec:
taskRef:
name: %s
bundle: %s
`

v1beta1PipelineRunWithBundleYaml = `
metadata:
name: %s
namespace: %s
spec:
pipelineRef:
name: %s
bundle: %s
`

v1TaskRunWithBundleExpectedYaml = `
metadata:
name: %s
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Task
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
podName: %s-pod
taskSpec:
steps:
- computeResources: {}
image: alpine
name: hello
script: 'echo Hello'
steps:
- image: alpine
name: hello
script: 'echo Hello'
terminated:
reason: Completed
`

v1PipelineRunWithBundleExpectedYaml = `
metadata:
name: %s
namespace: %s
spec:
taskRunTemplate:
serviceAccountName: default
timeouts:
pipeline: 1h
pipelineRef:
kind: Pipeline
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Pipeline
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
pipelineSpec:
tasks:
- name: hello-world
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
childReferences:
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: %s-hello-world
pipelineTaskName: hello-world
`

v1beta1TaskRunWithBundleRoundTripYaml = `
metadata:
name: %s
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Task
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
podName: %s-pod
taskSpec:
steps:
- computeResources: {}
image: alpine
name: hello
script: 'echo Hello'
steps:
- image: alpine
name: hello
script: 'echo Hello'
terminated:
reason: Completed
`

v1beta1PipelineRunWithBundleRoundTripYaml = `
metadata:
name: %s
namespace: %s
spec:
serviceAccountName: default
timeouts:
pipeline: 1h
pipelineRef:
kind: Pipeline
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: Pipeline
status:
conditions:
- type: Succeeded
status: "True"
reason: "Succeeded"
pipelineSpec:
tasks:
- name: hello-world
taskRef:
kind: Task
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
childReferences:
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: %s-hello-world
pipelineTaskName: hello-world
`
)

// TestTaskCRDConversion first creates a v1beta1 Task CRD using v1beta1Clients and
Expand Down Expand Up @@ -947,3 +1156,95 @@ func TestPipelineRunCRDConversion(t *testing.T) {
t.Errorf("-want, +got: %v", d)
}
}

// TestBundleConversion tests v1beta1 bundle syntax converted into v1 since it has
// been deprecated in v1 and it would be converted into bundle resolver in pipelineRef and
// taskRef.
func TestBundleConversion(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

t.Parallel()

c, namespace := setup(ctx, t, withRegistry, requireBundleFeatureFlags)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

repo := fmt.Sprintf("%s:5000/tektonbundlessimple", getRegistryServiceIP(ctx, t, c, namespace))
taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
task := parse.MustParseV1beta1Task(t, fmt.Sprintf(v1beta1TaskWithBundleYaml, taskName, namespace))
pipeline := parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(v1beta1PipelineWithBundleYaml, pipelineName, namespace, repo, taskName))
bundleSetup(ctx, t, c, namespace, repo, task, pipeline)

v1beta1TaskRunName := helpers.ObjectNameForTest(t)
v1beta1TaskRun := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(v1beta1TaskRunWithBundleYaml, v1beta1TaskRunName, namespace, taskName, repo))
v1TaskRunExpected := parse.MustParseV1TaskRun(t, fmt.Sprintf(v1TaskRunWithBundleExpectedYaml, v1beta1TaskRunName, namespace, repo, taskName, v1beta1TaskRunName))
v1beta1TaskRunRoundTripExpected := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(v1beta1TaskRunWithBundleRoundTripYaml, v1beta1TaskRunName, namespace, repo, taskName, v1beta1TaskRunName))

v1TaskRunExpected.Status.Provenance = &v1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}
v1beta1TaskRunRoundTripExpected.Status.Provenance = &v1beta1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}

if _, err := c.V1beta1TaskRunClient.Create(ctx, v1beta1TaskRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create v1beta1 TaskRun: %s", err)
}
if err := WaitForTaskRunState(ctx, c, v1beta1TaskRunName, Succeed(v1beta1TaskRunName), v1beta1TaskRunName, "v1beta1"); err != nil {
t.Fatalf("Failed waiting for v1beta1 TaskRun done: %v", err)
}

v1TaskRunGot, err := c.V1TaskRunClient.Get(ctx, v1beta1TaskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected v1 TaskRun for %s: %s", v1beta1TaskRunName, err)
}
if d := cmp.Diff(v1TaskRunExpected, v1TaskRunGot, filterV1TaskRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}

v1beta1TaskRunRoundTrip := &v1beta1.TaskRun{}
if err := v1beta1TaskRunRoundTrip.ConvertFrom(context.Background(), v1TaskRunGot); err != nil {
t.Fatalf("Failed to convert roundtrip v1beta1TaskRunGot ConvertFrom v1 = %v", err)
}
if d := cmp.Diff(v1beta1TaskRunRoundTripExpected, v1beta1TaskRunRoundTrip, filterV1beta1TaskRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}

v1beta1ToV1PipelineRunName := helpers.ObjectNameForTest(t)
v1beta1PipelineRun := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(v1beta1PipelineRunWithBundleYaml, v1beta1ToV1PipelineRunName, namespace, pipelineName, repo))
v1PipelineRunExpected := parse.MustParseV1PipelineRun(t, fmt.Sprintf(v1PipelineRunWithBundleExpectedYaml, v1beta1ToV1PipelineRunName, namespace, repo, pipelineName, repo, taskName, v1beta1ToV1PipelineRunName))
v1beta1PRRoundTripExpected := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(v1beta1PipelineRunWithBundleRoundTripYaml, v1beta1ToV1PipelineRunName, namespace, repo, pipelineName, repo, taskName, v1beta1ToV1PipelineRunName))

v1PipelineRunExpected.Status.Provenance = &v1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}
v1beta1PRRoundTripExpected.Status.Provenance = &v1beta1.Provenance{
FeatureFlags: getFeatureFlagsBaseOnAPIFlag(t),
}

if _, err := c.V1beta1PipelineRunClient.Create(ctx, v1beta1PipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create v1beta1 PipelineRun: %s", err)
}
if err := WaitForPipelineRunState(ctx, c, v1beta1ToV1PipelineRunName, timeout, Succeed(v1beta1ToV1PipelineRunName), v1beta1ToV1PipelineRunName, "v1beta1"); err != nil {
t.Fatalf("Failed waiting for v1beta1 PipelineRun done: %v", err)
}

v1PipelineRunGot, err := c.V1PipelineRunClient.Get(ctx, v1beta1ToV1PipelineRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected v1 PipelineRun for %s: %s", v1beta1ToV1PipelineRunName, err)
}
if d := cmp.Diff(v1PipelineRunExpected, v1PipelineRunGot, filterV1PipelineRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}

v1beta1PRRoundTrip := &v1beta1.PipelineRun{}
if err := v1beta1PRRoundTrip.ConvertFrom(context.Background(), v1PipelineRunGot); err != nil {
t.Fatalf("Error roundtrip v1beta1PipelineRun ConvertFrom v1PipelineRunGot = %v", err)
}
if d := cmp.Diff(v1beta1PRRoundTripExpected, v1beta1PRRoundTrip, filterV1beta1PipelineRunFields...); d != "" {
t.Errorf("-want, +got: %v", d)
}
}
Loading

0 comments on commit d60af7d

Please sign in to comment.