Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEP-0091] add more no error test cases for taskrun and pipelinerun #6754

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 80 additions & 33 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/google/go-containerregistry/pkg/registry"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resolutionv1beta1 "github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1"
resolutionutil "github.com/tektoncd/pipeline/pkg/internal/resolution"
Expand Down Expand Up @@ -11580,6 +11581,25 @@ spec:
t.Fatal("fail to marshal task", err)
}

noMatchPolicy := []*v1alpha1.VerificationPolicy{{
ObjectMeta: metav1.ObjectMeta{
Name: "no-match",
Namespace: ts.Namespace,
},
Spec: v1alpha1.VerificationPolicySpec{
Resources: []v1alpha1.ResourcePattern{{Pattern: "no-match"}},
}}}
// warnPolicy doesn't contain keys so it will fail verification but doesn't fail the run
warnPolicy := []*v1alpha1.VerificationPolicy{{
ObjectMeta: metav1.ObjectMeta{
Name: "warn-policy",
Namespace: ts.Namespace,
},
Spec: v1alpha1.VerificationPolicySpec{
Resources: []v1alpha1.ResourcePattern{{Pattern: ".*"}},
Mode: v1alpha1.ModeWarn,
}}}

prs := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: test-pipelinerun
Expand All @@ -11590,30 +11610,57 @@ spec:
resolver: %s
`, resolverName))

cms := []*corev1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{
"trusted-resources-verification-no-match-policy": config.FailNoMatchPolicy,
},
},
testCases := []struct {
name string
task []*v1beta1.Task
noMatchPolicy string
verificationPolicies []*v1alpha1.VerificationPolicy
}{{
name: "ignore no match policy",
noMatchPolicy: config.IgnoreNoMatchPolicy,
verificationPolicies: noMatchPolicy,
}, {
name: "warn no match policy",
noMatchPolicy: config.WarnNoMatchPolicy,
verificationPolicies: noMatchPolicy,
}, {
name: "pass enforce policy",
noMatchPolicy: config.FailNoMatchPolicy,
verificationPolicies: vps,
}, {
name: "only fail warn policy",
noMatchPolicy: config.FailNoMatchPolicy,
verificationPolicies: warnPolicy,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cms := []*corev1.ConfigMap{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code block is just move the previous test code into the for loop, and use the noMatchPolicy and verificationPolicies from the tests

{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{
"trusted-resources-verification-no-match-policy": tc.noMatchPolicy,
},
},
}

pipelineReq := getResolvedResolutionRequest(t, resolverName, signedPipelineBytes, prs.Namespace, prs.Name)
taskReq := getResolvedResolutionRequest(t, resolverName, signedTaskBytes, prs.Namespace, prs.Name+"-"+ps.Spec.Tasks[0].Name)
pipelineReq := getResolvedResolutionRequest(t, resolverName, signedPipelineBytes, prs.Namespace, prs.Name)
taskReq := getResolvedResolutionRequest(t, resolverName, signedTaskBytes, prs.Namespace, prs.Name+"-"+ps.Spec.Tasks[0].Name)

d := test.Data{
PipelineRuns: []*v1beta1.PipelineRun{prs},
VerificationPolicies: vps,
ConfigMaps: cms,
ResolutionRequests: []*resolutionv1beta1.ResolutionRequest{&pipelineReq, &taskReq},
}
prt := newPipelineRunTest(t, d)
defer prt.Cancel()
d := test.Data{
PipelineRuns: []*v1beta1.PipelineRun{prs},
VerificationPolicies: tc.verificationPolicies,
ConfigMaps: cms,
ResolutionRequests: []*resolutionv1beta1.ResolutionRequest{&pipelineReq, &taskReq},
}
prt := newPipelineRunTest(t, d)
defer prt.Cancel()

reconciledRun, _ := prt.reconcileRun("foo", "test-pipelinerun", []string{}, false)
reconciledRun, _ := prt.reconcileRun("foo", "test-pipelinerun", []string{}, false)

checkPipelineRunConditionStatusAndReason(t, reconciledRun, corev1.ConditionUnknown, v1beta1.PipelineRunReasonRunning.String())
checkPipelineRunConditionStatusAndReason(t, reconciledRun, corev1.ConditionUnknown, v1beta1.PipelineRunReasonRunning.String())
})
}
}

func TestReconcile_verifyResolvedPipeline_Error(t *testing.T) {
Expand Down Expand Up @@ -11737,6 +11784,17 @@ spec:
},
}

pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: test-pipelinerun
namespace: foo
selfLink: /pipeline/1234
spec:
pipelineRef:
resolver: %s
serviceAccountName: default
`, resolverName))

testCases := []struct {
name string
pipelinerun []*v1beta1.PipelineRun
Expand Down Expand Up @@ -11766,22 +11824,11 @@ spec:
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
prs := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: test-pipelinerun
namespace: foo
selfLink: /pipeline/1234
spec:
pipelineRef:
resolver: %s
serviceAccountName: default
`, resolverName))

pipelineReq := getResolvedResolutionRequest(t, resolverName, tc.pipelineBytes, prs.Namespace, prs.Name)
taskReq := getResolvedResolutionRequest(t, resolverName, tc.taskBytes, prs.Namespace, prs.Name+"-"+ps.Spec.Tasks[0].Name)
pipelineReq := getResolvedResolutionRequest(t, resolverName, tc.pipelineBytes, pr.Namespace, pr.Name)
taskReq := getResolvedResolutionRequest(t, resolverName, tc.taskBytes, pr.Namespace, pr.Name+"-"+ps.Spec.Tasks[0].Name)

d := test.Data{
PipelineRuns: []*v1beta1.PipelineRun{prs},
PipelineRuns: []*v1beta1.PipelineRun{pr},
ConfigMaps: cms,
VerificationPolicies: vps,
ResolutionRequests: []*resolutionv1beta1.ResolutionRequest{&pipelineReq, &taskReq},
Expand Down
142 changes: 93 additions & 49 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resolutionv1beta1 "github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1"
resolutionutil "github.com/tektoncd/pipeline/pkg/internal/resolution"
Expand Down Expand Up @@ -4925,6 +4926,24 @@ spec:
t.Fatal("fail to marshal task", err)
}

noMatchPolicy := []*v1alpha1.VerificationPolicy{{
ObjectMeta: metav1.ObjectMeta{
Name: "no-match",
Namespace: ts.Namespace,
},
Spec: v1alpha1.VerificationPolicySpec{
Resources: []v1alpha1.ResourcePattern{{Pattern: "no-match"}},
}}}
// warnPolicy doesn't contain keys so it will fail verification but doesn't fail the run
warnPolicy := []*v1alpha1.VerificationPolicy{{
ObjectMeta: metav1.ObjectMeta{
Name: "warn-policy",
Namespace: ts.Namespace,
},
Spec: v1alpha1.VerificationPolicySpec{
Resources: []v1alpha1.ResourcePattern{{Pattern: ".*"}},
Mode: v1alpha1.ModeWarn,
}}}
tr := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(`
metadata:
name: test-taskrun
Expand All @@ -4939,41 +4958,67 @@ spec:
status:
podName: the-pod
`, resolverName))

cms := []*corev1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{
"trusted-resources-verification-no-match-policy": config.FailNoMatchPolicy,
},
},
}
rr := getResolvedResolutionRequest(t, resolverName, signedTaskBytes, tr.Namespace, tr.Name)
d := test.Data{
TaskRuns: []*v1beta1.TaskRun{tr},
ConfigMaps: cms,
VerificationPolicies: vps,
ResolutionRequests: []*resolutionv1beta1.ResolutionRequest{&rr},
testCases := []struct {
name string
task []*v1beta1.Task
noMatchPolicy string
verificationPolicies []*v1alpha1.VerificationPolicy
}{{
name: "ignore no match policy",
noMatchPolicy: config.IgnoreNoMatchPolicy,
verificationPolicies: noMatchPolicy,
}, {
name: "warn no match policy",
noMatchPolicy: config.WarnNoMatchPolicy,
verificationPolicies: noMatchPolicy,
}, {
name: "pass enforce policy",
noMatchPolicy: config.FailNoMatchPolicy,
verificationPolicies: vps,
}, {
name: "only fail warn policy",
noMatchPolicy: config.FailNoMatchPolicy,
verificationPolicies: warnPolicy,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cms := []*corev1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{
"trusted-resources-verification-no-match-policy": tc.noMatchPolicy,
},
},
}
rr := getResolvedResolutionRequest(t, resolverName, signedTaskBytes, tr.Namespace, tr.Name)
d := test.Data{
TaskRuns: []*v1beta1.TaskRun{tr},
ConfigMaps: cms,
VerificationPolicies: tc.verificationPolicies,
ResolutionRequests: []*resolutionv1beta1.ResolutionRequest{&rr},
}

testAssets, cancel := getTaskRunController(t, d)
defer cancel()
createServiceAccount(t, testAssets, tr.Spec.ServiceAccountName, tr.Namespace)
err = testAssets.Controller.Reconciler.Reconcile(testAssets.Ctx, getRunName(tr))
testAssets, cancel := getTaskRunController(t, d)
defer cancel()
createServiceAccount(t, testAssets, tr.Spec.ServiceAccountName, tr.Namespace)
err = testAssets.Controller.Reconciler.Reconcile(testAssets.Ctx, getRunName(tr))

if ok, _ := controller.IsRequeueKey(err); !ok {
t.Errorf("Error reconciling TaskRun. Got error %v", err)
}
tr, err = testAssets.Clients.Pipeline.TektonV1beta1().TaskRuns(tr.Namespace).Get(testAssets.Ctx, tr.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("getting updated taskrun: %v", err)
}
condition := tr.Status.GetCondition(apis.ConditionSucceeded)
if condition == nil || condition.Status != corev1.ConditionUnknown {
t.Errorf("Expected fresh TaskRun to have in progress status, but had %v", condition)
}
if condition != nil && condition.Reason != v1beta1.TaskRunReasonRunning.String() {
t.Errorf("Expected reason %q but was %s", v1beta1.TaskRunReasonRunning.String(), condition.Reason)
if ok, _ := controller.IsRequeueKey(err); !ok {
t.Errorf("Error reconciling TaskRun. Got error %v", err)
}
reconciledRun, err := testAssets.Clients.Pipeline.TektonV1beta1().TaskRuns(tr.Namespace).Get(testAssets.Ctx, tr.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("getting updated taskrun: %v", err)
}
condition := reconciledRun.Status.GetCondition(apis.ConditionSucceeded)
if condition == nil || condition.Status != corev1.ConditionUnknown {
t.Errorf("Expected fresh TaskRun to have in progress status, but had %v", condition)
}
if condition != nil && condition.Reason != v1beta1.TaskRunReasonRunning.String() {
t.Errorf("Expected reason %q but was %s", v1beta1.TaskRunReasonRunning.String(), condition.Reason)
}
})
}
}

Expand Down Expand Up @@ -5023,7 +5068,20 @@ spec:
},
},
}

tr := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(`
metadata:
name: test-taskrun
namespace: foo
spec:
params:
- name: myarg
value: foo
taskRef:
resolver: %s
name: test-task
status:
podName: the-pod
`, resolverName))
testCases := []struct {
name string
taskBytes []byte
Expand All @@ -5040,20 +5098,6 @@ spec:

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tr := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(`
metadata:
name: test-taskrun
namespace: foo
spec:
params:
- name: myarg
value: foo
taskRef:
resolver: %s
name: test-task
status:
podName: the-pod
`, resolverName))
rr := getResolvedResolutionRequest(t, resolverName, tc.taskBytes, tr.Namespace, tr.Name)
d := test.Data{
TaskRuns: []*v1beta1.TaskRun{tr},
Expand All @@ -5070,11 +5114,11 @@ status:
if !errors.Is(err, trustedresources.ErrResourceVerificationFailed) {
t.Errorf("Reconcile got %v but want %v", err, trustedresources.ErrResourceVerificationFailed)
}
tr, err = testAssets.Clients.Pipeline.TektonV1beta1().TaskRuns(tr.Namespace).Get(testAssets.Ctx, tr.Name, metav1.GetOptions{})
reconciledRun, err := testAssets.Clients.Pipeline.TektonV1beta1().TaskRuns(tr.Namespace).Get(testAssets.Ctx, tr.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("getting updated taskrun: %v", err)
}
condition := tr.Status.GetCondition(apis.ConditionSucceeded)
condition := reconciledRun.Status.GetCondition(apis.ConditionSucceeded)
if condition.Type != apis.ConditionSucceeded || condition.Status != corev1.ConditionFalse || condition.Reason != podconvert.ReasonResourceVerificationFailed {
t.Errorf("Expected TaskRun to fail with reason \"%s\" but it did not. Final conditions were:\n%#v", podconvert.ReasonResourceVerificationFailed, tr.Status.Conditions)
}
Expand Down