From e68b7f4465d3b2cd93d9a36ecf9bbcce1aa8946c Mon Sep 17 00:00:00 2001 From: Yongxuan Zhang Date: Wed, 1 Nov 2023 19:03:58 +0000 Subject: [PATCH] unit tests --- .../taskrun/resources/taskref_test.go | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/pkg/reconciler/taskrun/resources/taskref_test.go b/pkg/reconciler/taskrun/resources/taskref_test.go index 60412c62808..2ccd01e3d9b 100644 --- a/pkg/reconciler/taskrun/resources/taskref_test.go +++ b/pkg/reconciler/taskrun/resources/taskref_test.go @@ -607,6 +607,109 @@ func TestGetStepActionFunc_Local(t *testing.T) { }) } } + +func TestGetStepActionFunc_RemoteResolution(t *testing.T) { + ctx := context.Background() + stepRef := &v1.Ref{ResolverRef: v1.ResolverRef{Resolver: "git"}} + + testcases := []struct { + name string + stepActionYAML string + wantStepAction *v1alpha1.StepAction + wantErr bool + }{{ + name: "remote StepAction", + stepActionYAML: strings.Join([]string{ + "kind: StepAction", + "apiVersion: tekton.dev/v1alpha1", + stepActionYAMLString, + }, "\n"), + wantStepAction: parse.MustParseV1alpha1StepAction(t, stepActionYAMLString), + }} + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + resolved := test.NewResolvedResource([]byte(tc.stepActionYAML), nil /* annotations */, sampleRefSource.DeepCopy(), nil /* data error */) + requester := test.NewRequester(resolved, nil) + tr := &v1.TaskRun{ + ObjectMeta: metav1.ObjectMeta{Namespace: "default"}, + Spec: v1.TaskRunSpec{ + TaskSpec: &v1.TaskSpec{ + Steps: []v1.Step{{ + Ref: stepRef, + }}, + }, + ServiceAccountName: "default", + }, + } + tektonclient := fake.NewSimpleClientset() + fn := resources.GetStepActionFunc(tektonclient, nil, requester, tr, &tr.Spec.TaskSpec.Steps[0]) + + resolvedStepAction, resolvedRefSource, err := fn(ctx, tr.Spec.TaskSpec.Steps[0].Ref) + if tc.wantErr { + if err == nil { + t.Fatalf("expected an error when calling GetStepActionFunc but got none") + } + } else { + if err != nil { + t.Fatalf("failed to call fn: %s", err.Error()) + } + + if d := cmp.Diff(sampleRefSource, resolvedRefSource); d != "" { + t.Errorf("refSources did not match: %s", diff.PrintWantGot(d)) + } + + if d := cmp.Diff(tc.wantStepAction, resolvedStepAction); d != "" { + t.Errorf("resolvedStepActions did not match: %s", diff.PrintWantGot(d)) + } + } + }) + } +} + +func TestGetStepActionFunc_RemoteResolutionInvalidData(t *testing.T) { + ctx := context.Background() + stepRef := &v1.Ref{ResolverRef: v1.ResolverRef{Resolver: "git"}} + + testcases := []struct { + name string + resolvesTo []byte + }{{ + name: "invalid data", + resolvesTo: []byte("INVALID YAML"), + }, { + name: "resolved not StepAction", + resolvesTo: []byte(strings.Join([]string{ + "kind: Task", + "apiVersion: tekton.dev/v1beta1", + taskYAMLString, + }, "\n")), + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + resource := test.NewResolvedResource(tc.resolvesTo, nil, nil, nil) + requester := test.NewRequester(resource, nil) + tr := &v1.TaskRun{ + ObjectMeta: metav1.ObjectMeta{Namespace: "default"}, + Spec: v1.TaskRunSpec{ + TaskSpec: &v1.TaskSpec{ + Steps: []v1.Step{{ + Ref: stepRef, + }}, + }, + ServiceAccountName: "default", + }, + } + tektonclient := fake.NewSimpleClientset() + fn := resources.GetStepActionFunc(tektonclient, nil, requester, tr, &tr.Spec.TaskSpec.Steps[0]) + if _, _, err := fn(ctx, tr.Spec.TaskSpec.Steps[0].Ref); err == nil { + t.Fatalf("expected error due to invalid pipeline data but saw none") + } + }) + } +} + func TestGetTaskFuncFromTaskRunSpecAlreadyFetched(t *testing.T) { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) @@ -1526,6 +1629,15 @@ spec: echo "hello world!" ` +var stepActionYAMLString = ` +metadata: + name: foo + namespace: default +spec: + image: myImage + command: ["ls"] +` + var remoteTaskYamlWithoutDefaults = ` metadata: name: simple