Skip to content
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
122 changes: 122 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
eventstest "github.com/tektoncd/pipeline/test/events"
"github.com/tektoncd/pipeline/test/names"
"github.com/tektoncd/pipeline/test/parse"
resolutioncommon "github.com/tektoncd/resolution/pkg/common"
"gomodules.xyz/jsonpatch/v2"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -7178,6 +7179,127 @@ spec:
checkPipelineRunConditionStatusAndReason(t, updatedPipelineRun, corev1.ConditionUnknown, v1beta1.PipelineRunReasonRunning.String())
}

// TestReconcileWithFailingResolver checks that a PipelineRun with a failing Resolver
// field creates a ResolutionRequest object for that Resolver's type, and
// that when the request fails, the PipelineRun fails.
func TestReconcileWithFailingResolver(t *testing.T) {
resolverName := "does-not-exist"
pr := parse.MustParsePipelineRun(t, `
metadata:
name: pr
namespace: default
spec:
pipelineRef:
resolver: does-not-exist
serviceAccountName: default
`)

cms := []*corev1.ConfigMap{withEnabledAlphaAPIFields(newFeatureFlagsConfigMap())}

d := test.Data{
ConfigMaps: cms,
PipelineRuns: []*v1beta1.PipelineRun{pr},
ServiceAccounts: []*corev1.ServiceAccount{{
ObjectMeta: metav1.ObjectMeta{Name: pr.Spec.ServiceAccountName, Namespace: "foo"},
}},
}

prt := newPipelineRunTest(d, t)
defer prt.Cancel()

wantEvents := []string(nil)
pipelinerun, _ := prt.reconcileRun(pr.Namespace, pr.Name, wantEvents, false)
checkPipelineRunConditionStatusAndReason(t, pipelinerun, corev1.ConditionUnknown, ReasonResolvingPipelineRef)

client := prt.TestAssets.Clients.ResolutionRequests.ResolutionV1alpha1().ResolutionRequests("default")
resolutionrequests, err := client.List(prt.TestAssets.Ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("unexpected error listing resource requests: %v", err)
}
numResolutionRequests := len(resolutionrequests.Items)
if numResolutionRequests != 1 {
t.Fatalf("expected exactly 1 resource request but found %d", numResolutionRequests)
}

resreq := &resolutionrequests.Items[0]
resolutionRequestType := resreq.ObjectMeta.Labels["resolution.tekton.dev/type"]
if resolutionRequestType != resolverName {
t.Fatalf("expected resource request type %q but saw %q", resolutionRequestType, resolverName)
}

resreq.Status.MarkFailed(resolutioncommon.ReasonResolutionTimedOut, "resolution took longer than global timeout of 1 minute")
resreq, err = client.UpdateStatus(prt.TestAssets.Ctx, resreq, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("unexpected error updating resource request with resolved pipeline data: %v", err)
}

// Check that the pipeline fails with the appropriate reason.
updatedPipelineRun, _ := prt.reconcileRun("default", "pr", nil, true)
checkPipelineRunConditionStatusAndReason(t, updatedPipelineRun, corev1.ConditionFalse, ReasonCouldntGetPipeline)
}

// TestReconcileWithFailingTaskResolver checks that a PipelineRun with a failing Resolver
// field for a Task creates a ResolutionRequest object for that Resolver's type, and
// that when the request fails, the PipelineRun fails.
func TestReconcileWithFailingTaskResolver(t *testing.T) {
resolverName := "foobar"
pr := parse.MustParsePipelineRun(t, `
metadata:
name: pr
namespace: default
spec:
pipelineSpec:
tasks:
- name: some-task
taskRef:
resolver: foobar
serviceAccountName: default
`)

cms := []*corev1.ConfigMap{withEnabledAlphaAPIFields(newFeatureFlagsConfigMap())}

d := test.Data{
ConfigMaps: cms,
PipelineRuns: []*v1beta1.PipelineRun{pr},
ServiceAccounts: []*corev1.ServiceAccount{{
ObjectMeta: metav1.ObjectMeta{Name: pr.Spec.ServiceAccountName, Namespace: "foo"},
}},
}

prt := newPipelineRunTest(d, t)
defer prt.Cancel()

wantEvents := []string(nil)
pipelinerun, _ := prt.reconcileRun(pr.Namespace, pr.Name, wantEvents, false)
checkPipelineRunConditionStatusAndReason(t, pipelinerun, corev1.ConditionUnknown, v1beta1.TaskRunReasonResolvingTaskRef)

client := prt.TestAssets.Clients.ResolutionRequests.ResolutionV1alpha1().ResolutionRequests("default")
resolutionrequests, err := client.List(prt.TestAssets.Ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("unexpected error listing resource requests: %v", err)
}
numResolutionRequests := len(resolutionrequests.Items)
if numResolutionRequests != 1 {
t.Fatalf("expected exactly 1 resource request but found %d", numResolutionRequests)
}

resreq := &resolutionrequests.Items[0]
resolutionRequestType := resreq.ObjectMeta.Labels["resolution.tekton.dev/type"]
if resolutionRequestType != resolverName {
t.Fatalf("expected resource request type %q but saw %q", resolutionRequestType, resolverName)
}

resreq.Status.MarkFailed(resolutioncommon.ReasonResolutionTimedOut, "resolution took longer than global timeout of 1 minute")
resreq, err = client.UpdateStatus(prt.TestAssets.Ctx, resreq, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("unexpected error updating resource request with resolved pipeline data: %v", err)
}

// Check that the pipeline fails.
updatedPipelineRun, _ := prt.reconcileRun("default", "pr", nil, true)
checkPipelineRunConditionStatusAndReason(t, updatedPipelineRun, corev1.ConditionFalse, ReasonCouldntGetTask)
}

// TestReconcileWithTaskResolver checks that a PipelineRun with a populated Resolver
// field for a Task creates a ResolutionRequest object for that Resolver's type, and
// that when the request is successfully resolved the PipelineRun begins running.
Expand Down
91 changes: 91 additions & 0 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
eventstest "github.com/tektoncd/pipeline/test/events"
"github.com/tektoncd/pipeline/test/names"
"github.com/tektoncd/pipeline/test/parse"
resolutioncommon "github.com/tektoncd/resolution/pkg/common"
corev1 "k8s.io/api/core/v1"
k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -1346,6 +1347,96 @@ spec:
}
}

// TestReconcileWithFailingResolver checks that a TaskRun with a failing Resolver
// field creates a ResolutionRequest object for that Resolver's type, and
// that when the request fails, the TaskRun fails.
func TestReconcileWithFailingResolver(t *testing.T) {
resolverName := "foobar"
tr := parse.MustParseTaskRun(t, `
metadata:
name: tr
namespace: default
spec:
taskRef:
resolver: foobar
serviceAccountName: default
`)

cms := []*corev1.ConfigMap{{
ObjectMeta: metav1.ObjectMeta{Namespace: system.Namespace(), Name: config.GetFeatureFlagsConfigName()},
Data: map[string]string{
"enable-api-fields": config.AlphaAPIFields,
},
}}

d := test.Data{
ConfigMaps: cms,
TaskRuns: []*v1beta1.TaskRun{tr},
ServiceAccounts: []*corev1.ServiceAccount{{
ObjectMeta: metav1.ObjectMeta{Name: tr.Spec.ServiceAccountName, Namespace: "foo"},
}},
}

testAssets, cancel := getTaskRunController(t, d)
defer cancel()
c := testAssets.Controller
clients := testAssets.Clients
saName := "default"
if _, err := clients.Kube.CoreV1().ServiceAccounts(tr.Namespace).Create(testAssets.Ctx, &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: saName,
Namespace: tr.Namespace,
},
}, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}

if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(tr)); err == nil {
t.Error("Wanted a resource request in progress error, but got nil.")
} else if controller.IsPermanentError(err) {
t.Errorf("expected no error. Got error %v", err)
}

client := testAssets.Clients.ResolutionRequests.ResolutionV1alpha1().ResolutionRequests("default")
resolutionrequests, err := client.List(testAssets.Ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("unexpected error listing resource requests: %v", err)
}
numResolutionRequests := len(resolutionrequests.Items)
if numResolutionRequests != 1 {
t.Fatalf("expected exactly 1 resource request but found %d", numResolutionRequests)
}

resreq := &resolutionrequests.Items[0]
resolutionRequestType := resreq.ObjectMeta.Labels["resolution.tekton.dev/type"]
if resolutionRequestType != resolverName {
t.Fatalf("expected resource request type %q but saw %q", resolutionRequestType, resolverName)
}

resreq.Status.MarkFailed(resolutioncommon.ReasonResolutionTimedOut, "resolution took longer than global timeout of 1 minute")
resreq, err = client.UpdateStatus(testAssets.Ctx, resreq, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("unexpected error updating resource request with resolved pipeline data: %v", err)
}

// Check that the TaskRun fails.
if err := c.Reconciler.Reconcile(testAssets.Ctx, getRunName(tr)); err == nil {
t.Fatalf("expected an error")
}

updatedTR, err := clients.Pipeline.TektonV1beta1().TaskRuns(tr.Namespace).Get(testAssets.Ctx, tr.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("getting updated taskrun: %v", err)
}
condition := updatedTR.Status.GetCondition(apis.ConditionSucceeded)
if condition == nil || condition.Status != corev1.ConditionFalse {
t.Errorf("Expected fresh TaskRun to have failed, but had %v", condition)
}
if condition != nil && condition.Reason != podconvert.ReasonFailedResolution {
t.Errorf("Expected reason %q but was %s", podconvert.ReasonFailedResolution, condition.Reason)
}
}

func TestReconcile_SetsStartTime(t *testing.T) {
taskRun := parse.MustParseTaskRun(t, `
metadata:
Expand Down