Skip to content

Commit

Permalink
Refactor test cases for remote TaskRef
Browse files Browse the repository at this point in the history
This commit refactors the test cases for taskRef_Test by:
- separate the TestGetTaskFunc for local, resolution and bundle tests
  since part of the syntax has deprecated in v1beta1 and diverged when
  we are going to move to v1.
- creats a separated test class TestGetTaskFunc_Bundle since there would
  be no `taskref.name` field in bundle resolvers
- remove the test cases regarding remote tasks ie. remote task without
  defaults in TestGetTaskFunc to TestGetTaskFunc_Remoteresolution and
  renames it to TestGetTaskFunc_Local
- change the test cases where bundle resolver is used for the remote
  clusterTask, which does not make sense as a functionality given it is valid
  • Loading branch information
JeromeJu committed Jun 6, 2023
1 parent c20c200 commit d6677c7
Showing 1 changed file with 83 additions and 83 deletions.
166 changes: 83 additions & 83 deletions pkg/reconciler/taskrun/resources/taskref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ func TestLocalTaskRef(t *testing.T) {
}
}

func TestGetTaskFunc(t *testing.T) {
// TestGetTaskFunc_Bundle tests the deprecated v1beta1 bundle syntax, this
// can be removed when support for the bundle syntax is removed in the v1 PR.
func TestGetTaskFunc_Bundle(t *testing.T) {
// Set up a fake registry to push an image to.
s := httptest.NewServer(registry.New())
defer s.Close()
Expand Down Expand Up @@ -289,7 +291,7 @@ func TestGetTaskFunc(t *testing.T) {
expectedKind v1beta1.TaskKind
}{
{
name: "remote-task",
name: "remote-task-bundle",
localTasks: []runtime.Object{simpleNamespacedTask},
remoteTasks: []runtime.Object{
&v1beta1.Task{
Expand All @@ -313,7 +315,7 @@ func TestGetTaskFunc(t *testing.T) {
},
ref: &v1beta1.TaskRef{
Name: "simple",
Bundle: u.Host + "/remote-task",
Bundle: u.Host + "/remote-task-bundle",
},
expected: &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -325,53 +327,60 @@ func TestGetTaskFunc(t *testing.T) {
},
},
expectedKind: v1beta1.NamespacedTaskKind,
}, {
name: "remote-task-without-defaults",
localTasks: []runtime.Object{},
remoteTasks: []runtime.Object{
&v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{
Name: "simple",
Namespace: "default",
},
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
Spec: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Image: "something",
}},
Params: []v1beta1.ParamSpec{{
Name: "foo",
},
},
},
}},
ref: &v1beta1.TaskRef{
Name: "simple",
Bundle: u.Host + "/remote-task-without-defaults",
},
expected: &v1beta1.Task{
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
tektonclient := fake.NewSimpleClientset(tc.localTasks...)
kubeclient := fakek8s.NewSimpleClientset(&v1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "simple",
Namespace: "default",
Name: "default",
},
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
Spec: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Image: "something",
}},
Params: []v1beta1.ParamSpec{{
Name: "foo",
}},
})

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

tr := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
Spec: v1beta1.TaskRunSpec{
TaskRef: tc.ref,
ServiceAccountName: "default",
},
},
expectedKind: v1beta1.NamespacedTaskKind,
}, {
}
fn := resources.GetTaskFunc(ctx, kubeclient, tektonclient, nil, tr, tc.ref, "", "default", "default", nil /*VerificationPolicies*/)

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

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

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

func TestGetTaskFunc_Local(t *testing.T) {
ctx := context.Background()

testcases := []struct {
name string
localTasks []runtime.Object
remoteTasks []runtime.Object
ref *v1beta1.TaskRef
expected runtime.Object
expectedKind v1beta1.TaskKind
}{
{
name: "local-task",
localTasks: []runtime.Object{simpleNamespacedTask},
remoteTasks: []runtime.Object{
Expand Down Expand Up @@ -399,38 +408,6 @@ func TestGetTaskFunc(t *testing.T) {
},
expected: simpleNamespacedTask,
expectedKind: v1beta1.NamespacedTaskKind,
}, {
name: "remote-cluster-task",
localTasks: []runtime.Object{
&v1beta1.ClusterTask{
TypeMeta: metav1.TypeMeta{APIVersion: "v1beta1", Kind: "ClusterTask"},
ObjectMeta: metav1.ObjectMeta{Name: "simple"},
Spec: v1beta1.TaskSpec{Params: []v1beta1.ParamSpec{{Name: "foo"}}},
},
},
remoteTasks: []runtime.Object{
&v1beta1.ClusterTask{
TypeMeta: metav1.TypeMeta{APIVersion: "tekton.dev/v1beta1", Kind: "ClusterTask"},
ObjectMeta: metav1.ObjectMeta{Name: "simple"},
},
&v1beta1.ClusterTask{
TypeMeta: metav1.TypeMeta{APIVersion: "tekton.dev/v1beta1", Kind: "ClusterTask"},
ObjectMeta: metav1.ObjectMeta{Name: "dummy"},
},
},
ref: &v1beta1.TaskRef{
Name: "simple",
Kind: v1beta1.ClusterTaskKind,
Bundle: u.Host + "/remote-cluster-task",
},
expected: &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: "simple"},
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
},
expectedKind: v1beta1.ClusterTaskKind,
}, {
name: "local-cluster-task",
localTasks: []runtime.Object{simpleClusterTask},
Expand Down Expand Up @@ -476,11 +453,6 @@ func TestGetTaskFunc(t *testing.T) {
},
})

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

trForFunc := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Name: "some-tr"},
Spec: v1beta1.TaskRunSpec{
Expand Down Expand Up @@ -597,6 +569,14 @@ func TestGetTaskFunc_RemoteResolution(t *testing.T) {
taskYAMLStringWithBetaFeatures,
}, "\n"),
wantTask: parse.MustParseV1beta1Task(t, taskYAMLStringWithBetaFeatures),
}, {
name: "v1beta1 cluster task",
taskYAML: strings.Join([]string{
"kind: ClusterTask",
"apiVersion: tekton.dev/v1beta1",
taskYAMLString,
}, "\n"),
wantTask: parse.MustParseV1beta1Task(t, taskYAMLString),
}, {
name: "v1 task",
taskYAML: strings.Join([]string{
Expand All @@ -613,6 +593,14 @@ func TestGetTaskFunc_RemoteResolution(t *testing.T) {
taskYAMLStringWithBetaFeatures,
}, "\n"),
wantErr: true,
}, {
name: "v1 task without defaults",
taskYAML: strings.Join([]string{
"kind: Task",
"apiVersion: tekton.dev/v1",
remoteTaskYamlWithoutDefaults,
}, "\n"),
wantTask: parse.MustParseV1beta1Task(t, remoteTaskYamlWithoutDefaults),
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -1384,6 +1372,18 @@ spec:
type: array
`

var remoteTaskYamlWithoutDefaults = `
metadata:
name: simple
namespace: default
spec:
steps:
- image: something
params:
- name: foo
type: string
`

func bytesToRequester(data []byte, source *v1beta1.RefSource) *test.Requester {
resolved := test.NewResolvedResource(data, nil, source, nil)
requester := test.NewRequester(resolved, nil)
Expand Down

0 comments on commit d6677c7

Please sign in to comment.