diff --git a/test/pipelinerun_test.go b/test/pipelinerun_test.go index 50d2248ada2..9fd0d6b6166 100644 --- a/test/pipelinerun_test.go +++ b/test/pipelinerun_test.go @@ -41,47 +41,50 @@ import ( "k8s.io/client-go/kubernetes" "knative.dev/pkg/apis" knativetest "knative.dev/pkg/test" + "knative.dev/pkg/test/helpers" ) var ( - pipelineName = "pipeline" - pipelineRunName = "pipelinerun" - secretName = "secret" - saName = "service-account" - taskName = "task" - task1Name = "task1" - cond1Name = "cond-1" + secretName = "secret" + saName = "service-account" + task1Name = "task1" + cond1Name = "cond-1" ) func TestPipelineRun(t *testing.T) { t.Parallel() type tests struct { name string - testSetup func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) + testSetup func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1beta1.Pipeline) expectedTaskRuns []string expectedNumberOfEvents int - pipelineRunFunc func(*testing.T, int, string) *v1beta1.PipelineRun + pipelineRunFunc func(*testing.T, int, string, string, map[string]*v1alpha1.PipelineResource) *v1beta1.PipelineRun } tds := []tests{{ name: "fan-in and fan-out", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, _ int) (map[string]*v1alpha1.PipelineResource, *v1beta1.Pipeline) { t.Helper() - for _, task := range getFanInFanOutTasks(t, namespace) { + tasks := getFanInFanOutTasks(t, namespace) + for _, task := range tasks { if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } } - for _, res := range getFanInFanOutGitResources(t) { + resources := getFanInFanOutGitResources(t) + for _, res := range resources { if _, err := c.PipelineResourceClient.Create(ctx, res, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", res.Name, err) } } - if _, err := c.PipelineClient.Create(ctx, getFanInFanOutPipeline(t, index, namespace), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + p := getFanInFanOutPipeline(t, namespace, tasks) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return resources, p }, pipelineRunFunc: getFanInFanOutPipelineRun, expectedTaskRuns: []string{"create-file-kritis", "create-fan-out-1", "create-fan-out-2", "check-fan-in"}, @@ -89,7 +92,7 @@ func TestPipelineRun(t *testing.T) { expectedNumberOfEvents: 5, }, { name: "service account propagation and pipeline param", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1beta1.Pipeline) { t.Helper() t.Skip("build-crd-testing project got removed, the secret-sauce doesn't exist anymore, skipping") if _, err := c.KubeClient.CoreV1().Secrets(namespace).Create(ctx, getPipelineRunSecret(index, namespace), metav1.CreateOptions{}); err != nil { @@ -115,14 +118,17 @@ spec: image: gcr.io/tekton-releases/dogfooding/skopeo:latest command: ['skopeo'] args: ['copy', '$(params["the.path"])', '$(params["the.dest"])'] -`, getName(taskName, index), namespace)) +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) + t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - if _, err := c.PipelineClient.Create(ctx, getHelloWorldPipelineWithSingularTask(t, index, namespace), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + p := getHelloWorldPipelineWithSingularTask(t, namespace, task.Name) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return nil, p }, expectedTaskRuns: []string{task1Name}, // 1 from PipelineRun and 1 from Tasks defined in pipelinerun @@ -130,11 +136,11 @@ spec: pipelineRunFunc: getHelloWorldPipelineRun, }, { name: "pipeline succeeds when task skipped due to failed condition", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, _ int) (map[string]*v1alpha1.PipelineResource, *v1beta1.Pipeline) { t.Helper() - cond := getFailingCondition() + cond := getFailingCondition(t) if _, err := c.ConditionClient.Create(ctx, cond, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Condition `%s`: %s", cond1Name, err) + t.Fatalf("Failed to create Condition `%s`: %s", cond.Name, err) } task := parse.MustParseTask(t, fmt.Sprintf(` @@ -146,13 +152,16 @@ spec: - image: ubuntu command: ['/bin/bash'] args: ['-c', 'echo hello, world'] -`, getName(taskName, index), namespace)) +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) + t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - if _, err := c.PipelineClient.Create(ctx, getPipelineWithFailingCondition(t, index, namespace), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + p := getPipelineWithFailingCondition(t, namespace, task.Name, cond.Name) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return nil, p }, expectedTaskRuns: []string{}, // 1 from PipelineRun; 0 from taskrun since it should not be executed due to condition failing @@ -160,7 +169,7 @@ spec: pipelineRunFunc: getConditionalPipelineRun, }, { name: "pipelinerun succeeds with LimitRange minimum in namespace", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1beta1.Pipeline) { t.Helper() t.Skip("build-crd-testing project got removed, the secret-sauce doesn't exist anymore, skipping") if _, err := c.KubeClient.CoreV1().LimitRanges(namespace).Create(ctx, getLimitRange("prlimitrange", namespace, "100m", "99Mi", "100m"), metav1.CreateOptions{}); err != nil { @@ -190,13 +199,17 @@ spec: image: gcr.io/tekton-releases/dogfooding/skopeo:latest command: ['skopeo'] args: ['copy', '$(params["the.path"])', '$(params["the.dest"])'] -`, getName(taskName, index), namespace)) +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", fmt.Sprint("task", index), err) } - if _, err := c.PipelineClient.Create(ctx, getHelloWorldPipelineWithSingularTask(t, index, namespace), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + + p := getHelloWorldPipelineWithSingularTask(t, namespace, task.Name) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return nil, p }, expectedTaskRuns: []string{task1Name}, // 1 from PipelineRun and 1 from Tasks defined in pipelinerun @@ -218,10 +231,11 @@ spec: defer tearDown(ctx, t, c, namespace) t.Logf("Setting up test resources for %q test in namespace %s", td.name, namespace) - td.testSetup(ctx, t, c, namespace, i) + resources, p := td.testSetup(ctx, t, c, namespace, i) - prName := fmt.Sprintf("%s%d", pipelineRunName, i) - pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(t, i, namespace), metav1.CreateOptions{}) + pipelineRun := td.pipelineRunFunc(t, i, namespace, p.Name, resources) + prName := pipelineRun.Name + _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -300,7 +314,7 @@ spec: } } -func getHelloWorldPipelineWithSingularTask(t *testing.T, suffix int, namespace string) *v1beta1.Pipeline { +func getHelloWorldPipelineWithSingularTask(t *testing.T, namespace string, taskName string) *v1beta1.Pipeline { return parse.MustParsePipeline(t, fmt.Sprintf(` metadata: name: %s @@ -320,7 +334,7 @@ spec: value: $(params.dest) taskRef: name: %s -`, getName(pipelineName, suffix), namespace, task1Name, getName(taskName, suffix))) +`, helpers.ObjectNameForTest(t), namespace, task1Name, taskName)) } // TestPipelineRunRefDeleted tests that a running PipelineRun doesn't fail when the Pipeline @@ -334,12 +348,13 @@ func TestPipelineRunRefDeleted(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - prName := "pipelinerun-referencing-deleted" + pipelineName := helpers.ObjectNameForTest(t) + prName := helpers.ObjectNameForTest(t) t.Logf("Creating Pipeline, and PipelineRun %s in namespace %s", prName, namespace) - pipeline := parse.MustParsePipeline(t, ` + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` metadata: - name: pipeline-to-be-deleted + name: %s spec: tasks: - name: step1 @@ -361,18 +376,18 @@ spec: #!/usr/bin/env bash # Sleep for another 10s sleep 10 -`) +`, pipelineName)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", prName, err) + t.Fatalf("Failed to create Pipeline `%s`: %s", pipelineName, err) } - pipelinerun := parse.MustParsePipelineRun(t, ` + pipelinerun := parse.MustParsePipelineRun(t, fmt.Sprintf(` metadata: - name: pipelinerun-referencing-deleted + name: %s spec: pipelineRef: - name: pipeline-to-be-deleted -`) + name: %s +`, prName, pipelineName)) _, err := c.PipelineRunClient.Create(ctx, pipelinerun, metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) @@ -407,7 +422,9 @@ func TestPipelineRunPending(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - prName := "pending-pipelinerun-test" + taskName := helpers.ObjectNameForTest(t) + pipelineName := helpers.ObjectNameForTest(t) + prName := helpers.ObjectNameForTest(t) t.Logf("Creating Task, Pipeline, and Pending PipelineRun %s in namespace %s", prName, namespace) @@ -420,8 +437,8 @@ spec: - image: ubuntu command: ['/bin/bash'] args: ['-c', 'echo hello, world'] -`, prName, namespace)), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", prName, err) +`, taskName, namespace)), metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Task `%s`: %s", taskName, err) } if _, err := c.PipelineClient.Create(ctx, parse.MustParsePipeline(t, fmt.Sprintf(` @@ -433,8 +450,8 @@ spec: - name: task taskRef: name: %s -`, prName, namespace, prName)), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", prName, err) +`, pipelineName, namespace, taskName)), metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", pipelineName, err) } pipelineRun, err := c.PipelineRunClient.Create(ctx, parse.MustParsePipelineRun(t, fmt.Sprintf(` @@ -445,7 +462,7 @@ spec: pipelineRef: name: %s status: PipelineRunPending -`, prName, namespace, prName)), metav1.CreateOptions{}) +`, prName, namespace, pipelineName)), metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -478,10 +495,11 @@ spec: } } -func getFanInFanOutTasks(t *testing.T, namespace string) []*v1beta1.Task { - return []*v1beta1.Task{parse.MustParseTask(t, fmt.Sprintf(` +func getFanInFanOutTasks(t *testing.T, namespace string) map[string]*v1beta1.Task { + return map[string]*v1beta1.Task{ + "create-file": parse.MustParseTask(t, fmt.Sprintf(` metadata: - name: create-file + name: %s namespace: %s spec: resources: @@ -501,10 +519,10 @@ spec: command: ['/bin/bash'] image: ubuntu name: write-data-task-0-step-1 -`, namespace)), - parse.MustParseTask(t, fmt.Sprintf(` +`, helpers.ObjectNameForTest(t), namespace)), + "check-create-files-exists": parse.MustParseTask(t, fmt.Sprintf(` metadata: - name: check-create-files-exists + name: %s namespace: %s spec: resources: @@ -523,10 +541,10 @@ spec: command: ['/bin/bash'] image: ubuntu name: write-data-task-1 -`, namespace)), - parse.MustParseTask(t, fmt.Sprintf(` +`, helpers.ObjectNameForTest(t), namespace)), + "check-create-files-exists-2": parse.MustParseTask(t, fmt.Sprintf(` metadata: - name: check-create-files-exists-2 + name: %s namespace: %s spec: resources: @@ -545,10 +563,10 @@ spec: command: ['/bin/bash'] image: ubuntu name: write-data-task-1 -`, namespace)), - parse.MustParseTask(t, fmt.Sprintf(` +`, helpers.ObjectNameForTest(t), namespace)), + "read-files": parse.MustParseTask(t, fmt.Sprintf(` metadata: - name: read-files + name: %s namespace: %s spec: resources: @@ -565,11 +583,11 @@ spec: command: ['/bin/bash'] image: ubuntu name: read-from-task-1 -`, namespace)), +`, helpers.ObjectNameForTest(t), namespace)), } } -func getFanInFanOutPipeline(t *testing.T, suffix int, namespace string) *v1beta1.Pipeline { +func getFanInFanOutPipeline(t *testing.T, namespace string, tasks map[string]*v1beta1.Task) *v1beta1.Pipeline { return parse.MustParsePipeline(t, fmt.Sprintf(` metadata: name: %s @@ -588,7 +606,7 @@ spec: - name: workspace resource: git-repo taskRef: - name: create-file + name: %s - name: create-fan-out-1 resources: inputs: @@ -600,7 +618,7 @@ spec: - name: workspace resource: git-repo taskRef: - name: check-create-files-exists + name: %s - name: create-fan-out-2 resources: inputs: @@ -612,7 +630,7 @@ spec: - name: workspace resource: git-repo taskRef: - name: check-create-files-exists-2 + name: %s - name: check-fan-in resources: inputs: @@ -622,14 +640,16 @@ spec: name: workspace resource: git-repo taskRef: - name: read-files -`, getName(pipelineName, suffix), namespace)) + name: %s +`, helpers.ObjectNameForTest(t), namespace, tasks["create-file"].Name, tasks["check-create-files-exists"].Name, + tasks["check-create-files-exists-2"].Name, tasks["read-files"].Name)) } -func getFanInFanOutGitResources(t *testing.T) []*v1alpha1.PipelineResource { - return []*v1alpha1.PipelineResource{parse.MustParsePipelineResource(t, ` +func getFanInFanOutGitResources(t *testing.T) map[string]*v1alpha1.PipelineResource { + return map[string]*v1alpha1.PipelineResource{ + "kritis-resource-git": parse.MustParsePipelineResource(t, fmt.Sprintf(` metadata: - name: kritis-resource-git + name: %s spec: type: git params: @@ -637,7 +657,7 @@ spec: value: https://github.com/grafeas/kritis - name: Revision value: master -`)} +`, helpers.ObjectNameForTest(t)))} } func getPipelineRunServiceAccount(suffix int, namespace string) *corev1.ServiceAccount { @@ -651,7 +671,7 @@ func getPipelineRunServiceAccount(suffix int, namespace string) *corev1.ServiceA }}, } } -func getFanInFanOutPipelineRun(t *testing.T, suffix int, namespace string) *v1beta1.PipelineRun { +func getFanInFanOutPipelineRun(t *testing.T, _ int, namespace string, pipelineName string, resources map[string]*v1alpha1.PipelineResource) *v1beta1.PipelineRun { return parse.MustParsePipelineRun(t, fmt.Sprintf(` metadata: name: %s @@ -662,8 +682,8 @@ spec: resources: - name: git-repo resourceRef: - name: kritis-resource-git -`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) + name: %s +`, helpers.ObjectNameForTest(t), namespace, pipelineName, resources["kritis-resource-git"].Name)) } func getPipelineRunSecret(suffix int, namespace string) *corev1.Secret { @@ -695,7 +715,7 @@ func getPipelineRunSecret(suffix int, namespace string) *corev1.Secret { } } -func getHelloWorldPipelineRun(t *testing.T, suffix int, namespace string) *v1beta1.PipelineRun { +func getHelloWorldPipelineRun(t *testing.T, suffix int, namespace string, pipelineName string, _ map[string]*v1alpha1.PipelineResource) *v1beta1.PipelineRun { return parse.MustParsePipelineRun(t, fmt.Sprintf(` metadata: labels: @@ -711,7 +731,7 @@ spec: pipelineRef: name: %s serviceAccountName: %s%d -`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix), saName, suffix)) +`, helpers.ObjectNameForTest(t), namespace, pipelineName, saName, suffix)) } func getName(namespace string, suffix int) string { @@ -873,7 +893,7 @@ func assertAnnotationsMatch(t *testing.T, expectedAnnotations, actualAnnotations } } -func getPipelineWithFailingCondition(t *testing.T, suffix int, namespace string) *v1beta1.Pipeline { +func getPipelineWithFailingCondition(t *testing.T, namespace string, taskName, condName string) *v1beta1.Pipeline { return parse.MustParsePipeline(t, fmt.Sprintf(` metadata: name: %s @@ -889,27 +909,22 @@ spec: taskRef: name: %s runAfter: ['%s'] -`, getName(pipelineName, suffix), namespace, task1Name, getName(taskName, suffix), cond1Name, getName(taskName, suffix), task1Name)) +`, helpers.ObjectNameForTest(t), namespace, task1Name, taskName, condName, taskName, task1Name)) } -func getFailingCondition() *v1alpha1.Condition { - return &v1alpha1.Condition{ - ObjectMeta: metav1.ObjectMeta{ - Name: cond1Name, - }, - Spec: v1alpha1.ConditionSpec{ - Check: v1alpha1.Step{ - Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"exit 1"}, - }, - }, - }, - } +func getFailingCondition(t *testing.T) *v1alpha1.Condition { + return parse.MustParseCondition(t, fmt.Sprintf(` +metadata: + name: %s +spec: + check: + image: ubuntu + command: ['/bin/bash'] + args: ['exit 1'] +`, helpers.ObjectNameForTest(t))) } -func getConditionalPipelineRun(t *testing.T, suffix int, namespace string) *v1beta1.PipelineRun { +func getConditionalPipelineRun(t *testing.T, _ int, namespace string, pipelineName string, _ map[string]*v1alpha1.PipelineResource) *v1beta1.PipelineRun { return parse.MustParsePipelineRun(t, fmt.Sprintf(` metadata: name: %s @@ -919,7 +934,7 @@ metadata: spec: pipelineRef: name: %s -`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) +`, helpers.ObjectNameForTest(t), namespace, pipelineName)) } func getLimitRange(name, namespace, resourceCPU, resourceMemory, resourceEphemeralStorage string) *corev1.LimitRange { diff --git a/test/v1alpha1/pipelinerun_test.go b/test/v1alpha1/pipelinerun_test.go index 08ad27cfe71..494836cbb18 100644 --- a/test/v1alpha1/pipelinerun_test.go +++ b/test/v1alpha1/pipelinerun_test.go @@ -39,6 +39,7 @@ import ( "k8s.io/client-go/kubernetes" "knative.dev/pkg/apis" knativetest "knative.dev/pkg/test" + "knative.dev/pkg/test/helpers" ) var ( @@ -48,7 +49,6 @@ var ( saName = "service-account" taskName = "task" task1Name = "task1" - cond1Name = "cond-1" pipelineRunTimeout = 10 * time.Minute ) @@ -56,31 +56,35 @@ func TestPipelineRun(t *testing.T) { t.Parallel() type tests struct { name string - testSetup func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) + testSetup func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1alpha1.Pipeline) expectedTaskRuns []string expectedNumberOfEvents int - pipelineRunFunc func(*testing.T, int, string) *v1alpha1.PipelineRun + pipelineRunFunc func(*testing.T, int, string, string, map[string]*v1alpha1.PipelineResource) *v1alpha1.PipelineRun } tds := []tests{{ name: "fan-in and fan-out", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1alpha1.Pipeline) { t.Helper() - for _, task := range getFanInFanOutTasks(t) { + tasks := getFanInFanOutTasks(t) + for _, task := range tasks { if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } } - - for _, res := range getFanInFanOutGitResources(t) { + resources := getFanInFanOutGitResources(t) + for _, res := range resources { if _, err := c.PipelineResourceClient.Create(ctx, res, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", res.Name, err) } } - if _, err := c.PipelineClient.Create(ctx, getFanInFanOutPipeline(t, index), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + p := getFanInFanOutPipeline(t, tasks) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return resources, p }, pipelineRunFunc: getFanInFanOutPipelineRun, expectedTaskRuns: []string{"create-file-kritis", "create-fan-out-1", "create-fan-out-2", "check-fan-in"}, @@ -88,7 +92,7 @@ func TestPipelineRun(t *testing.T) { expectedNumberOfEvents: 5, }, { name: "service account propagation and pipeline param", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1alpha1.Pipeline) { t.Helper() t.Skip("build-crd-testing project got removed, the secret-sauce doesn't exist anymore, skipping") if _, err := c.KubeClient.CoreV1().Secrets(namespace).Create(ctx, getPipelineRunSecret(index), metav1.CreateOptions{}); err != nil { @@ -113,14 +117,17 @@ spec: image: gcr.io/tekton-releases/dogfooding/skopeo:latest command: ['skopeo'] args: ['copy', '$(inputs.params.path)', '$(inputs.params.dest)'] -`, getName(taskName, index))) +`, helpers.ObjectNameForTest(t))) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) + t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - if _, err := c.PipelineClient.Create(ctx, getHelloWorldPipelineWithSingularTask(t, index), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + p := getHelloWorldPipelineWithSingularTask(t, task.Name) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return nil, p }, expectedTaskRuns: []string{task1Name}, // 1 from PipelineRun and 1 from Tasks defined in pipelinerun @@ -128,11 +135,11 @@ spec: pipelineRunFunc: getHelloWorldPipelineRun, }, { name: "pipeline succeeds when task skipped due to failed condition", - testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { + testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) (map[string]*v1alpha1.PipelineResource, *v1alpha1.Pipeline) { t.Helper() - cond := getFailingCondition(namespace) + cond := getFailingCondition(t, namespace) if _, err := c.ConditionClient.Create(ctx, cond, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Condition `%s`: %s", cond1Name, err) + t.Fatalf("Failed to create Condition `%s`: %s", cond.Name, err) } task := parse.MustParseAlphaTask(t, fmt.Sprintf(` @@ -143,14 +150,17 @@ spec: - image: ubuntu command: ['/bin/bash'] args: ['-c', 'echo hello, world'] -`, getName(taskName, index))) +`, helpers.ObjectNameForTest(t))) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) + t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - if _, err := c.PipelineClient.Create(ctx, getPipelineWithFailingCondition(t, index), metav1.CreateOptions{}); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", getName(pipelineName, index), err) + p := getPipelineWithFailingCondition(t, task.Name, cond.Name) + if _, err := c.PipelineClient.Create(ctx, p, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Pipeline `%s`: %s", p.Name, err) } + + return nil, p }, expectedTaskRuns: []string{}, // 1 from PipelineRun; 0 from taskrun since it should not be executed due to condition failing @@ -172,10 +182,10 @@ spec: defer tearDown(ctx, t, c, namespace) t.Logf("Setting up test resources for %q test in namespace %s", td.name, namespace) - td.testSetup(ctx, t, c, namespace, i) + resources, p := td.testSetup(ctx, t, c, namespace, i) prName := fmt.Sprintf("%s%d", pipelineRunName, i) - pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(t, i, namespace), metav1.CreateOptions{}) + pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(t, i, namespace, p.Name, resources), metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -254,7 +264,7 @@ spec: } } -func getHelloWorldPipelineWithSingularTask(t *testing.T, suffix int) *v1alpha1.Pipeline { +func getHelloWorldPipelineWithSingularTask(t *testing.T, taskName string) *v1alpha1.Pipeline { return parse.MustParseAlphaPipeline(t, fmt.Sprintf(` metadata: name: %s @@ -273,22 +283,21 @@ spec: value: $(params.dest) taskRef: name: %s -`, getName(pipelineName, suffix), task1Name, getName(taskName, suffix))) +`, helpers.ObjectNameForTest(t), task1Name, taskName)) } -func getFanInFanOutTasks(t *testing.T) []*v1alpha1.Task { - return []*v1alpha1.Task{ - parse.MustParseAlphaTask(t, ` +func getFanInFanOutTasks(t *testing.T) map[string]*v1alpha1.Task { + return map[string]*v1alpha1.Task{ + "create-file": parse.MustParseAlphaTask(t, fmt.Sprintf(` metadata: - name: create-file + name: %s spec: - inputs: - resources: + resources: + inputs: - name: workspace targetPath: brandnewspace type: git - outputs: - resources: + outputs: - name: workspace type: git steps: @@ -300,17 +309,16 @@ spec: command: ['/bin/bash'] image: ubuntu name: write-data-task-0-step-1 -`), - parse.MustParseAlphaTask(t, ` +`, helpers.ObjectNameForTest(t))), + "check-create-files-exists": parse.MustParseAlphaTask(t, fmt.Sprintf(` metadata: - name: check-create-files-exists + name: %s spec: - inputs: - resources: + resources: + inputs: - name: workspace type: git - outputs: - resources: + outputs: - name: workspace type: git steps: @@ -322,17 +330,16 @@ spec: command: ['/bin/bash'] image: ubuntu name: write-data-task-1 -`), - parse.MustParseAlphaTask(t, ` +`, helpers.ObjectNameForTest(t))), + "check-create-files-exists-2": parse.MustParseAlphaTask(t, fmt.Sprintf(` metadata: - name: check-create-files-exists-2 + name: %s spec: - inputs: - resources: + resources: + inputs: - name: workspace type: git - outputs: - resources: + outputs: - name: workspace type: git steps: @@ -344,13 +351,13 @@ spec: command: ['/bin/bash'] image: ubuntu name: write-data-task-1 -`), - parse.MustParseAlphaTask(t, ` +`, helpers.ObjectNameForTest(t))), + "read-files": parse.MustParseAlphaTask(t, fmt.Sprintf(` metadata: - name: read-files + name: %s spec: - inputs: - resources: + resources: + inputs: - name: workspace type: git targetPath: readingspace @@ -363,11 +370,11 @@ spec: command: ['/bin/bash'] image: ubuntu name: read-from-task-1 -`), +`, helpers.ObjectNameForTest(t))), } } -func getFanInFanOutPipeline(t *testing.T, suffix int) *v1alpha1.Pipeline { +func getFanInFanOutPipeline(t *testing.T, tasks map[string]*v1alpha1.Task) *v1alpha1.Pipeline { return parse.MustParseAlphaPipeline(t, fmt.Sprintf(` metadata: name: %s @@ -385,7 +392,7 @@ spec: - name: workspace resource: git-repo taskRef: - name: create-file + name: %s - name: create-fan-out-1 resources: inputs: @@ -397,7 +404,7 @@ spec: - name: workspace resource: git-repo taskRef: - name: check-create-files-exists + name: %s - name: create-fan-out-2 resources: inputs: @@ -409,7 +416,7 @@ spec: - name: workspace resource: git-repo taskRef: - name: check-create-files-exists-2 + name: %s - name: check-fan-in resources: inputs: @@ -419,14 +426,16 @@ spec: name: workspace resource: git-repo taskRef: - name: read-files -`, getName(pipelineName, suffix))) + name: %s +`, helpers.ObjectNameForTest(t), tasks["create-file"].Name, tasks["check-create-files-exists"].Name, + tasks["check-create-files-exists-2"].Name, tasks["read-files"].Name)) } -func getFanInFanOutGitResources(t *testing.T) []*v1alpha1.PipelineResource { - return []*v1alpha1.PipelineResource{parse.MustParsePipelineResource(t, ` +func getFanInFanOutGitResources(t *testing.T) map[string]*v1alpha1.PipelineResource { + return map[string]*v1alpha1.PipelineResource{ + "kritis-resource-git": parse.MustParsePipelineResource(t, fmt.Sprintf(` metadata: - name: kritis-resource-git + name: %s spec: type: git params: @@ -434,7 +443,7 @@ spec: value: https://github.com/grafeas/kritis - name: Revision value: master -`)} +`, helpers.ObjectNameForTest(t)))} } func getPipelineRunServiceAccount(suffix int) *corev1.ServiceAccount { @@ -447,7 +456,7 @@ func getPipelineRunServiceAccount(suffix int) *corev1.ServiceAccount { }}, } } -func getFanInFanOutPipelineRun(t *testing.T, suffix int, namespace string) *v1alpha1.PipelineRun { +func getFanInFanOutPipelineRun(t *testing.T, _ int, namespace string, pipelineName string, resources map[string]*v1alpha1.PipelineResource) *v1alpha1.PipelineRun { return parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` metadata: name: %s @@ -458,8 +467,8 @@ spec: resources: - name: git-repo resourceRef: - name: kritis-resource-git -`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) + name: %s +`, helpers.ObjectNameForTest(t), namespace, pipelineName, resources["kritis-resource-git"].Name)) } func getPipelineRunSecret(suffix int) *corev1.Secret { @@ -490,7 +499,7 @@ func getPipelineRunSecret(suffix int) *corev1.Secret { } } -func getHelloWorldPipelineRun(t *testing.T, suffix int, namespace string) *v1alpha1.PipelineRun { +func getHelloWorldPipelineRun(t *testing.T, suffix int, namespace string, pipelineName string, _ map[string]*v1alpha1.PipelineResource) *v1alpha1.PipelineRun { return parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` metadata: labels: @@ -506,7 +515,7 @@ spec: pipelineRef: name: %s serviceAccountName: %s%d -`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix), saName, suffix)) +`, helpers.ObjectNameForTest(t), namespace, pipelineName, saName, suffix)) } func getName(namespace string, suffix int) string { @@ -668,7 +677,7 @@ func assertAnnotationsMatch(t *testing.T, expectedAnnotations, actualAnnotations } } -func getPipelineWithFailingCondition(t *testing.T, suffix int) *v1alpha1.Pipeline { +func getPipelineWithFailingCondition(t *testing.T, taskName, condName string) *v1alpha1.Pipeline { return parse.MustParseAlphaPipeline(t, fmt.Sprintf(` metadata: name: %s @@ -683,28 +692,23 @@ spec: taskRef: name: %s runAfter: ['%s'] -`, getName(pipelineName, suffix), task1Name, getName(taskName, suffix), cond1Name, getName(taskName, suffix), task1Name)) +`, helpers.ObjectNameForTest(t), task1Name, taskName, condName, taskName, task1Name)) } -func getFailingCondition(namespace string) *v1alpha1.Condition { - return &v1alpha1.Condition{ - ObjectMeta: metav1.ObjectMeta{ - Name: cond1Name, - Namespace: namespace, - }, - Spec: v1alpha1.ConditionSpec{ - Check: v1alpha1.Step{ - Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"exit 1"}, - }, - }, - }, - } +func getFailingCondition(t *testing.T, namespace string) *v1alpha1.Condition { + return parse.MustParseCondition(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + check: + image: ubuntu + command: ['/bin/bash'] + args: ['exit 1'] +`, helpers.ObjectNameForTest(t), namespace)) } -func getConditionalPipelineRun(t *testing.T, suffix int, namespace string) *v1alpha1.PipelineRun { +func getConditionalPipelineRun(t *testing.T, _ int, namespace string, pipelineName string, _ map[string]*v1alpha1.PipelineResource) *v1alpha1.PipelineRun { return parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` metadata: name: %s @@ -714,5 +718,5 @@ metadata: spec: pipelineRef: name: %s -`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) +`, helpers.ObjectNameForTest(t), namespace, pipelineName)) }