Skip to content

Commit

Permalink
Refactor test cases for remote PipelineRef
Browse files Browse the repository at this point in the history
This commit refactors the test cases related with remote pipelineRef,
which separates the original TestGetTaskFunc to local, bundle and
remote due to the syntax of v1beta1 bundle has been deprecated in v1
and replaced with the bundle resolver.

This is similar to the cleanup for TaskRef in tektoncd#6778
  • Loading branch information
JeromeJu committed Jun 9, 2023
1 parent 06d2499 commit 34999b8
Showing 1 changed file with 69 additions and 43 deletions.
112 changes: 69 additions & 43 deletions pkg/reconciler/pipelinerun/resources/pipelineref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
fakek8s "k8s.io/client-go/kubernetes/fake"
"knative.dev/pkg/logging"
logtesting "knative.dev/pkg/logging/testing"
)

var (
Expand Down Expand Up @@ -152,7 +151,9 @@ func TestLocalPipelineRef(t *testing.T) {
}
}

func TestGetPipelineFunc(t *testing.T) {
// TestGetPipelineFunc_Bundle tests the deprecated v1beta1 bundle syntax, this
// can be removed when support for the bundle syntax is removed
func TestGetPipelineFunc_Bundle(t *testing.T) {
// Set up a fake registry to push an image to.
s := httptest.NewServer(registry.New())
defer s.Close()
Expand All @@ -162,14 +163,6 @@ func TestGetPipelineFunc(t *testing.T) {
}

ctx := context.Background()
cfg := config.NewStore(logtesting.TestLogger(t))
cfg.OnConfigChanged(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName()},
Data: map[string]string{
"enable-tekton-oci-bundles": "true",
},
})
ctx = cfg.ToContext(ctx)

testcases := []struct {
name string
Expand All @@ -178,18 +171,81 @@ func TestGetPipelineFunc(t *testing.T) {
ref *v1beta1.PipelineRef
expected runtime.Object
}{{
name: "remote-pipeline",
name: "remote-pipeline-bundle",
localPipelines: []runtime.Object{
simplePipelineWithBaseSpec(),
dummyPipeline,
},
remotePipelines: []runtime.Object{simplePipeline(), dummyPipeline},
ref: &v1beta1.PipelineRef{
Name: "simple",
Bundle: u.Host + "/remote-pipeline",
Bundle: u.Host + "/remote-pipeline-bundle",
},
expected: simplePipeline(),
}, {
}}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
tektonclient := fake.NewSimpleClientset(tc.localPipelines...)
kubeclient := fakek8s.NewSimpleClientset(&v1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "default",
},
})

_, err := test.CreateImage(u.Host+"/"+tc.name, tc.remotePipelines...)
if err != nil {
t.Fatalf("failed to upload test image: %s", err.Error())
}

pr := &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: tc.ref,
ServiceAccountName: "default",
},
}

fn := resources.GetPipelineFunc(ctx, kubeclient, tektonclient, nil, pr, nil /*VerificationPolicies*/)
if err != nil {
t.Fatalf("failed to get pipeline fn: %s", err.Error())
}

pipeline, refSource, _, err := fn(ctx, tc.ref.Name)
if err != nil {
t.Fatalf("failed to call pipelinefn: %s", err.Error())
}

if diff := cmp.Diff(pipeline, tc.expected); tc.expected != nil && diff != "" {
t.Error(diff)
}

if refSource != nil {
t.Errorf("expected refSource is nil, but got %v", refSource)
}
})
}
}

func TestGetPipelineFunc_Local(t *testing.T) {
// Set up a fake registry to push an image to.
s := httptest.NewServer(registry.New())
defer s.Close()
u, err := url.Parse(s.URL)
if err != nil {
t.Fatal(err)
}

ctx := context.Background()

testcases := []struct {
name string
localPipelines []runtime.Object
remotePipelines []runtime.Object
ref *v1beta1.PipelineRef
expected runtime.Object
}{{
name: "local-pipeline",
localPipelines: []runtime.Object{
simplePipelineWithBaseSpec(),
Expand All @@ -200,17 +256,6 @@ func TestGetPipelineFunc(t *testing.T) {
Name: "simple",
},
expected: simplePipelineWithBaseSpec(),
}, {
name: "remote-pipeline-without-defaults",
localPipelines: []runtime.Object{simplePipeline()},
remotePipelines: []runtime.Object{
simplePipelineWithSpecAndParam(""),
dummyPipeline},
ref: &v1beta1.PipelineRef{
Name: "simple",
Bundle: u.Host + "/remote-pipeline-without-defaults",
},
expected: simplePipelineWithSpecParamAndKind(),
}}

for _, tc := range testcases {
Expand Down Expand Up @@ -1253,25 +1298,6 @@ func simplePipelineWithBaseSpec() *v1beta1.Pipeline {
return p
}

func simplePipelineWithSpecAndParam(pt v1beta1.ParamType) *v1beta1.Pipeline {
p := simplePipelineWithBaseSpec()
p.Spec.Params = []v1beta1.ParamSpec{{
Name: "foo",
Type: pt,
}}

return p
}

func simplePipelineWithSpecParamAndKind() *v1beta1.Pipeline {
p := simplePipelineWithBaseSpec()
p.Spec.Params = []v1beta1.ParamSpec{{
Name: "foo",
}}

return p
}

// This is missing the kind and apiVersion because those are added by
// the MustParse helpers from the test package.
var pipelineYAMLString = `
Expand Down

0 comments on commit 34999b8

Please sign in to comment.