@@ -47,6 +47,7 @@ import (
4747 "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
4848 taskresources "github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
4949 th "github.com/tektoncd/pipeline/pkg/reconciler/testing"
50+ ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
5051 "github.com/tektoncd/pipeline/pkg/reconciler/volumeclaim"
5152 resolutioncommon "github.com/tektoncd/pipeline/pkg/resolution/common"
5253 remoteresource "github.com/tektoncd/pipeline/pkg/resolution/resource"
@@ -18222,3 +18223,89 @@ spec:
1822218223 })
1822318224 }
1822418225}
18226+
18227+ func TestReconcile_ManagedBy(t *testing.T) {
18228+ namespace := "foo"
18229+ prManagedByTektonName := "test-pr-managed-by-tekton"
18230+ prManagedByOtherName := "test-pr-managed-by-other"
18231+
18232+ ps := []*v1.Pipeline{simpleHelloWorldPipeline}
18233+ ts := []*v1.Task{simpleHelloWorldTask}
18234+
18235+ prs := []*v1.PipelineRun{
18236+ parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
18237+ metadata:
18238+ name: %s
18239+ namespace: %s
18240+ spec:
18241+ pipelineRef:
18242+ name: test-pipeline
18243+ managedBy: tekton.dev/pipeline
18244+ `, prManagedByTektonName, namespace)),
18245+ parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
18246+ metadata:
18247+ name: %s
18248+ namespace: %s
18249+ spec:
18250+ pipelineRef:
18251+ name: test-pipeline
18252+ managedBy: other-controller
18253+ `, prManagedByOtherName, namespace)),
18254+ }
18255+
18256+ // This data is filtered and simulates what the informer would pass to the reconciler.
18257+ // It only contains the PipelineRun that should be reconciled.
18258+ filteredData := test.Data{
18259+ PipelineRuns: []*v1.PipelineRun{prs[0]}, // Only the one managed by Tekton
18260+ Pipelines: ps,
18261+ Tasks: ts,
18262+ ConfigMaps: th.NewFeatureFlagsConfigMapInSlice(),
18263+ }
18264+
18265+ // Initialize controller with filtered data for the listers
18266+ prt := newPipelineRunTest(t, filteredData)
18267+ defer prt.Cancel()
18268+
18269+ // Create clients with all the data for verification
18270+ ctx, _ := ttesting.SetupFakeContext(t)
18271+ clients, _ := test.SeedTestData(t, ctx, test.Data{
18272+ PipelineRuns: prs,
18273+ Pipelines: ps,
18274+ Tasks: ts,
18275+ ConfigMaps: th.NewFeatureFlagsConfigMapInSlice(),
18276+ })
18277+
18278+ // Verify no TaskRuns were created for the externally managed PipelineRun
18279+ taskRunsOther := getTaskRunsForPipelineRun(prt.TestAssets.Ctx, t, clients, namespace, prManagedByOtherName)
18280+ if len(taskRunsOther) != 0 {
18281+ t.Errorf("Expected no TaskRuns for externally managed PipelineRun, but found %d", len(taskRunsOther))
18282+ }
18283+
18284+ // Verify its status is unchanged
18285+ reconciledOther, err := clients.Pipeline.TektonV1().PipelineRuns(namespace).Get(prt.TestAssets.Ctx, prManagedByOtherName, metav1.GetOptions{})
18286+ if err != nil {
18287+ t.Fatalf("Failed to get externally managed PipelineRun: %v", err)
18288+ }
18289+ if len(reconciledOther.Status.Conditions) != 0 {
18290+ t.Errorf("Expected externally managed PipelineRun to have no conditions, but it did")
18291+ }
18292+
18293+ // 2. Reconcile the PipelineRun managed by "tekton.dev/pipeline"
18294+ // We expect this one to be processed normally.
18295+ wantEvents := []string{
18296+ "Normal Started",
18297+ "Normal Running Tasks Completed: 0",
18298+ }
18299+ reconciledTekton, clients := prt.reconcileRun(namespace, prManagedByTektonName, wantEvents, false)
18300+
18301+ // Verify a TaskRun was created for the Tekton-managed PipelineRun
18302+ taskRunsTekton := getTaskRunsForPipelineRun(prt.TestAssets.Ctx, t, clients, namespace, prManagedByTektonName)
18303+ if len(taskRunsTekton) != 1 {
18304+ t.Errorf("Expected 1 TaskRun for Tekton-managed PipelineRun, but found %d", len(taskRunsTekton))
18305+ }
18306+
18307+ // Verify its status was updated
18308+ if !reconciledTekton.Status.GetCondition(apis.ConditionSucceeded).IsUnknown() {
18309+ t.Errorf("Expected Tekton-managed PipelineRun to be running, but it was not")
18310+ }
18311+ }
0 commit comments