Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongxuanzhang committed Nov 1, 2023
1 parent ad975cc commit e68b7f4
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions pkg/reconciler/taskrun/resources/taskref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e68b7f4

Please sign in to comment.