diff --git a/test/artifact_bucket_test.go b/test/artifact_bucket_test.go index 4e9802617bd..539d5e185bd 100644 --- a/test/artifact_bucket_test.go +++ b/test/artifact_bucket_test.go @@ -26,10 +26,10 @@ import ( "testing" "time" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/config" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -73,44 +73,41 @@ func TestStorageBucketPipelineRun(t *testing.T) { defer deleteBucketSecret(ctx, c, t, namespace) t.Logf("Creating GCS bucket %s", bucketName) - createbuckettask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: "createbuckettask", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "step1", - Image: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine", - Command: []string{"/bin/bash"}, - Args: []string{"-c", fmt.Sprintf("gcloud auth activate-service-account --key-file /var/secret/bucket-secret/bucket-secret-key && gsutil mb gs://%s", bucketName)}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "bucket-secret-volume", - MountPath: fmt.Sprintf("/var/secret/%s", bucketSecretName), - }}, - Env: []corev1.EnvVar{{ - Name: "CREDENTIALS", Value: fmt.Sprintf("/var/secret/%s/%s", bucketSecretName, bucketSecretKey), - }}, - }}}, - Volumes: []corev1.Volume{{ - Name: "bucket-secret-volume", - VolumeSource: corev1.VolumeSource{ - Secret: &corev1.SecretVolumeSource{ - SecretName: bucketSecretName, - }, - }, - }}, - }, - } + createbuckettask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: createbuckettask + namespace: %s +spec: + steps: + - name: step1 + image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine + command: ['/bin/bash'] + args: ['-c', 'gcloud auth activate-service-account --key-file /var/secret/bucket-secret/bucket-secret-key && gsutil mb gs://%s'] + volumeMounts: + - name: bucket-secret-volume + mountPath: /var/secret/%s + env: + - name: CREDENTIALS + value: /var/secret/%s/%s + volumes: + - name: bucket-secret-volume + secret: + secretName: %s +`, namespace, bucketName, bucketSecretName, bucketSecretName, bucketSecretKey, bucketSecretName)) t.Logf("Creating Task %s", "createbuckettask") if _, err := c.TaskClient.Create(ctx, createbuckettask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", "createbuckettask", err) } - createbuckettaskrun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: "createbuckettaskrun", Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: "createbuckettask"}, - }, - } + createbuckettaskrun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: createbuckettaskrun + namespace: %s +spec: + taskRef: + name: createbuckettask +`, namespace)) t.Logf("Creating TaskRun %s", "createbuckettaskrun") if _, err := c.TaskRunClient.Create(ctx, createbuckettaskrun, metav1.CreateOptions{}); err != nil { @@ -141,120 +138,112 @@ func TestStorageBucketPipelineRun(t *testing.T) { defer resetConfigMap(ctx, t, c, systemNamespace, config.GetArtifactBucketConfigName(), originalConfigMapData) t.Logf("Creating Git PipelineResource %s", helloworldResourceName) - helloworldResource := &resourcev1alpha1.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: helloworldResourceName, - }, - Spec: resourcev1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeGit, - Params: []resourcev1alpha1.ResourceParam{ - { - Name: "Url", - Value: "https://github.com/pivotal-nader-ziada/gohelloworld", - }, - { - Name: "Revision", - Value: "master", - }, - }, - }, - } + helloworldResource := parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + params: + - name: Url + value: https://github.com/pivotal-nader-ziada/gohelloworld + - name: Revision + value: master + type: git +`, helloworldResourceName)) if _, err := c.PipelineResourceClient.Create(ctx, helloworldResource, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", helloworldResourceName, err) } t.Logf("Creating Task %s", addFileTaskName) - addFileTask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: addFileTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Name: "addfile", Image: "ubuntu", - }, - Script: "echo '#!/bin/bash\necho hello' > /workspace/helloworldgit/newfile", - }, { - Container: corev1.Container{ - Name: "make-executable", Image: "ubuntu", - }, - Script: "chmod +x /workspace/helloworldgit/newfile", - }}, - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: helloworldResourceName, Type: resourcev1alpha1.PipelineResourceTypeGit, - }}}, - Outputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: helloworldResourceName, Type: resourcev1alpha1.PipelineResourceTypeGit, - }}}, - }, - }, - } + addFileTask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: %s + type: git + outputs: + - name: %s + type: git + steps: + - image: ubuntu + name: addfile + script: |- + echo '#!/bin/bash + echo hello' > /workspace/helloworldgit/newfile + - image: ubuntu + name: make-executable + script: chmod +x /workspace/helloworldgit/newfile +`, addFileTaskName, namespace, helloworldResourceName, helloworldResourceName)) if _, err := c.TaskClient.Create(ctx, addFileTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", addFileTaskName, err) } t.Logf("Creating Task %s", runFileTaskName) - readFileTask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: runFileTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "runfile", Image: "ubuntu", - Command: []string{"/workspace/hellowrld/newfile"}, - }}}, - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: helloworldResourceName, Type: resourcev1alpha1.PipelineResourceTypeGit, - }}}, - }, - }, - } + readFileTask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: %s + type: git + steps: + - command: ['/workspace/hellowrld/newfile'] + image: ubuntu + name: runfile +`, runFileTaskName, namespace, helloworldResourceName)) if _, err := c.TaskClient.Create(ctx, readFileTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", runFileTaskName, err) } t.Logf("Creating Pipeline %s", bucketTestPipelineName) - bucketTestPipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: bucketTestPipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "source-repo", Type: resourcev1alpha1.PipelineResourceTypeGit, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "addfile", - TaskRef: &v1beta1.TaskRef{Name: addFileTaskName}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "helloworldgit", Resource: "source-repo", - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "helloworldgit", Resource: "source-repo", - }}, - }, - }, { - Name: "runfile", - TaskRef: &v1beta1.TaskRef{Name: runFileTaskName}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "helloworldgit", Resource: "source-repo", - }}, - }, - }}, - }, - } + bucketTestPipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + - name: source-repo + type: git + tasks: + - name: addfile + resources: + inputs: + - name: helloworldgit + resource: source-repo + outputs: + - name: helloworldgit + resource: source-repo + taskRef: + name: %s + - name: runfile + resources: + inputs: + - name: helloworldgit + resource: source-repo + taskRef: + name: %s +`, bucketTestPipelineName, namespace, addFileTaskName, runFileTaskName)) if _, err := c.PipelineClient.Create(ctx, bucketTestPipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", bucketTestPipelineName, err) } t.Logf("Creating PipelineRun %s", bucketTestPipelineRunName) - bucketTestPipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: bucketTestPipelineRunName, Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: bucketTestPipelineName}, - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "source-repo", - ResourceRef: &v1beta1.PipelineResourceRef{Name: helloworldResourceName}, - }}, - }, - } + bucketTestPipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + resources: + - name: source-repo + resourceRef: + name: %s +`, bucketTestPipelineRunName, namespace, bucketTestPipelineName, helloworldResourceName)) if _, err := c.PipelineRunClient.Create(ctx, bucketTestPipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", bucketTestPipelineRunName, err) } @@ -316,44 +305,42 @@ func resetConfigMap(ctx context.Context, t *testing.T, c *clients, namespace, co } func runTaskToDeleteBucket(ctx context.Context, c *clients, t *testing.T, namespace, bucketName, bucketSecretName, bucketSecretKey string) { - deletelbuckettask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: "deletelbuckettask", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "step1", - Image: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine", - Command: []string{"/bin/bash"}, - Args: []string{"-c", fmt.Sprintf("gcloud auth activate-service-account --key-file /var/secret/bucket-secret/bucket-secret-key && gsutil rm -r gs://%s", bucketName)}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "bucket-secret-volume", - MountPath: fmt.Sprintf("/var/secret/%s", bucketSecretName), - }}, - Env: []corev1.EnvVar{{ - Name: "CREDENTIALS", Value: fmt.Sprintf("/var/secret/%s/%s", bucketSecretName, bucketSecretKey), - }}, - }}}, - Volumes: []corev1.Volume{{ - Name: "bucket-secret-volume", - VolumeSource: corev1.VolumeSource{ - Secret: &corev1.SecretVolumeSource{ - SecretName: bucketSecretName, - }, - }, - }}, - }, - } + deletelbuckettask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: deletelbuckettask + namespace: %s +spec: + steps: + - args: ['-c', 'gcloud auth activate-service-account --key-file /var/secret/bucket-secret/bucket-secret-key && gsutil rm -r gs://%s'] + command: ['/bin/bash'] + env: + - name: CREDENTIALS + value: /var/secret/%s/%s + image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine + name: step1 + resources: {} + volumeMounts: + - mountPath: /var/secret/%s + name: bucket-secret-volume + volumes: + - name: bucket-secret-volume + secret: + secretName: %s +`, namespace, bucketName, bucketSecretName, bucketSecretKey, bucketSecretName, bucketSecretName)) t.Logf("Creating Task %s", "deletelbuckettask") if _, err := c.TaskClient.Create(ctx, deletelbuckettask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", "deletelbuckettask", err) } - deletelbuckettaskrun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: "deletelbuckettaskrun", Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: "deletelbuckettask"}, - }, - } + deletelbuckettaskrun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: deletelbuckettaskrun + namespace: %s +spec: + taskRef: + name: deletelbuckettask +`, namespace)) t.Logf("Creating TaskRun %s", "deletelbuckettaskrun") if _, err := c.TaskRunClient.Create(ctx, deletelbuckettaskrun, metav1.CreateOptions{}); err != nil { diff --git a/test/cancel_test.go b/test/cancel_test.go index 0b2a857bb38..1069aa47b32 100644 --- a/test/cancel_test.go +++ b/test/cancel_test.go @@ -26,9 +26,10 @@ import ( "sync" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" jsonpatch "gomodules.xyz/jsonpatch/v2" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" knativetest "knative.dev/pkg/test" @@ -60,25 +61,20 @@ func TestTaskRunPipelineRunCancel(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineSpec: &v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "task", - Retries: numRetries, - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "busybox", - }, - Script: "sleep 5000", - }}, - }}, - }}, - }, - }, - } + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineSpec: + tasks: + - name: task + retries: %d + taskSpec: + steps: + - image: busybox + script: 'sleep 5000' +`, helpers.ObjectNameForTest(t), namespace, numRetries)) t.Logf("Creating PipelineRun in namespace %s", namespace) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { diff --git a/test/cluster_resource_test.go b/test/cluster_resource_test.go index 3b99e4683c1..286f28c74fb 100644 --- a/test/cluster_resource_test.go +++ b/test/cluster_resource_test.go @@ -23,6 +23,8 @@ import ( "fmt" "testing" + "github.com/tektoncd/pipeline/test/parse" + resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" @@ -80,7 +82,7 @@ func TestClusterResource(t *testing.T) { } func getClusterResource(t *testing.T, name, sname string) *resourcev1alpha1.PipelineResource { - return mustParsePipelineResource(t, fmt.Sprintf(` + return parse.MustParsePipelineResource(t, fmt.Sprintf(` metadata: name: %s spec: @@ -102,7 +104,6 @@ spec: secretKey: tokenkey secretName: %s `, name, sname, sname)) - } func getClusterResourceTaskSecret(namespace, name string) *corev1.Secret { @@ -119,7 +120,7 @@ func getClusterResourceTaskSecret(namespace, name string) *corev1.Secret { } func getClusterResourceTask(t *testing.T, namespace, name, configName string) *v1beta1.Task { - return mustParseTask(t, fmt.Sprintf(` + return parse.MustParseTask(t, fmt.Sprintf(` metadata: name: %s namespace: %s @@ -155,7 +156,7 @@ spec: } func getClusterResourceTaskRun(t *testing.T, namespace, name, taskName, resName string) *v1beta1.TaskRun { - return mustParseTaskRun(t, fmt.Sprintf(` + return parse.MustParseTaskRun(t, fmt.Sprintf(` metadata: name: %s namespace: %s diff --git a/test/conformance_test.go b/test/conformance_test.go index 742c0e4a315..051df35b5e3 100644 --- a/test/conformance_test.go +++ b/test/conformance_test.go @@ -20,9 +20,12 @@ package test import ( "context" + "fmt" "strings" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" @@ -55,17 +58,16 @@ func TestTaskRun(t *testing.T) { }{{ name: "successful-task-run", trName: "echo-hello-task-run", - tr: &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: "echo-hello-task-run", Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: fqImageName, - Command: []string{"echo", "\"hello\""}, - }}}, - }, - }, - }, + tr: parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: echo-hello-task-run + namespace: %s +spec: + taskSpec: + steps: + - image: %s + command: ['echo', '"hello"'] +`, namespace, fqImageName)), fn: TaskRunSucceed, expectedConditionStatus: corev1.ConditionTrue, expectedStepState: []v1beta1.StepState{{ @@ -79,26 +81,23 @@ func TestTaskRun(t *testing.T) { }, { name: "failed-task-run", trName: "failed-echo-hello-task-run", - tr: &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: "failed-echo-hello-task-run", Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: fqImageName, - Command: []string{"/bin/sh"}, - Args: []string{"-c", "echo hello"}, - }}, {Container: corev1.Container{ - Image: fqImageName, - Command: []string{"/bin/sh"}, - Args: []string{"-c", "exit 1"}, - }}, {Container: corev1.Container{ - Image: fqImageName, - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 30s"}, - }}}, - }, - }, - }, + tr: parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: failed-echo-hello-task-run + namespace: %s +spec: + taskSpec: + steps: + - image: %s + command: ['/bin/sh'] + args: ['-c', 'echo hello'] + - image: %s + command: ['/bin/sh'] + args: ['-c', 'exit 1'] + - image: %s + command: ['/bin/sh'] + args: ['-c', 'sleep 30s'] +`, namespace, fqImageName, fqImageName, fqImageName)), fn: TaskRunFailed, expectedConditionStatus: corev1.ConditionFalse, expectedStepState: []v1beta1.StepState{{ diff --git a/test/custom_task_test.go b/test/custom_task_test.go index bec8207ca7d..71d8f026a53 100644 --- a/test/custom_task_test.go +++ b/test/custom_task_test.go @@ -26,6 +26,8 @@ import ( "testing" "time" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" @@ -33,7 +35,6 @@ import ( "go.opencensus.io/trace" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" knativetest "knative.dev/pkg/test" @@ -63,58 +64,46 @@ func TestCustomTask(t *testing.T) { pipelineRunName := "custom-task-pipeline" if _, err := c.PipelineRunClient.Create( ctx, - &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - PipelineSpec: &v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "custom-task-ref", - TaskRef: &v1beta1.TaskRef{ - APIVersion: apiVersion, - Kind: kind, - }, - }, { - Name: "custom-task-spec", - TaskSpec: &v1beta1.EmbeddedTask{ - TypeMeta: runtime.TypeMeta{ - APIVersion: apiVersion, - Kind: kind, - }, - Metadata: v1beta1.PipelineTaskMetadata{Labels: metadataLabel}, - Spec: runtime.RawExtension{ - Raw: customTaskRawSpec, - }, - }, - }, { - Name: "result-consumer", - Params: []v1beta1.Param{{ - Name: "input-result-from-custom-task-ref", Value: *v1beta1.NewArrayOrString("$(tasks.custom-task-ref.results.runResult)"), - }, { - Name: "input-result-from-custom-task-spec", Value: *v1beta1.NewArrayOrString("$(tasks.custom-task-spec.results.runResult)"), - }}, - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Params: []v1beta1.ParamSpec{{ - Name: "input-result-from-custom-task-ref", Type: v1beta1.ParamTypeString, - }, { - Name: "input-result-from-custom-task-spec", Type: v1beta1.ParamTypeString, - }}, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo $(input-result-from-custom-task-ref) $(input-result-from-custom-task-spec)"}, - }}}, - }}, - }}, - Results: []v1beta1.PipelineResult{{ - Name: "prResult-ref", - Value: "$(tasks.custom-task-ref.results.runResult)", - }, { - Name: "prResult-spec", - Value: "$(tasks.custom-task-spec.results.runResult)", - }}, - }, - }, - }, + parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + results: + - name: prResult-ref + value: $(tasks.custom-task-ref.results.runResult) + - name: prResult-spec + value: $(tasks.custom-task-spec.results.runResult) + tasks: + - name: custom-task-ref + taskRef: + apiVersion: %s + kind: %s + - name: custom-task-spec + taskSpec: + apiVersion: %s + kind: %s + metadata: + labels: + test-label: test + spec: %s + - name: result-consumer + params: + - name: input-result-from-custom-task-ref + value: $(tasks.custom-task-ref.results.runResult) + - name: input-result-from-custom-task-spec + value: $(tasks.custom-task-spec.results.runResult) + taskSpec: + params: + - name: input-result-from-custom-task-ref + type: string + - name: input-result-from-custom-task-spec + type: string + steps: + - args: ['-c', 'echo $(input-result-from-custom-task-ref) $(input-result-from-custom-task-spec)'] + command: ['/bin/bash'] + image: ubuntu +`, pipelineRunName, apiVersion, kind, apiVersion, kind, customTaskRawSpec)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %v", pipelineRunName, err) } @@ -266,25 +255,26 @@ func TestPipelineRunCustomTaskTimeout(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(context.Background(), t, c, namespace) }, t.Logf) defer tearDown(context.Background(), t, c, namespace) - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "custom-task-ref", - TaskRef: &v1beta1.TaskRef{ - APIVersion: apiVersion, - Kind: kind, - }, - }}, - }, - } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: pipeline.Name}, - Timeout: &metav1.Duration{Duration: 5 * time.Second}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: custom-task-ref + taskRef: + apiVersion: %s + kind: %s +`, helpers.ObjectNameForTest(t), namespace, apiVersion, kind)) + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + timeout: 5s +`, helpers.ObjectNameForTest(t), namespace, pipeline.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) } diff --git a/test/dag_test.go b/test/dag_test.go index 6f3c2c91eeb..d0f65cbe1ed 100644 --- a/test/dag_test.go +++ b/test/dag_test.go @@ -20,16 +20,16 @@ package test import ( "context" + "fmt" "math" "sort" "strings" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" + "github.com/tektoncd/pipeline/test/parse" + clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/typed/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -54,147 +54,147 @@ func TestDAGPipelineRun(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - // Create the Task that echoes text - repoTaskResource := v1beta1.TaskResource{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "repo", Type: resource.PipelineResourceTypeGit, - }} - echoTask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: "echo-task", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{repoTaskResource}, - Outputs: []v1beta1.TaskResource{repoTaskResource}, - }, - Params: []v1beta1.ParamSpec{{ - Name: "text", Type: v1beta1.ParamTypeString, - Description: "The text that should be echoed", - }}, - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "busybox"}, - Script: `echo $(params["text"])`, - }, { - Container: corev1.Container{Image: "busybox"}, - Script: "ln -s $(resources.inputs.repo.path) $(resources.outputs.repo.path)", - }}, - }, - } + echoTask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: echo-task + namespace: %s +spec: + resources: + inputs: + - name: repo + type: git + outputs: + - name: repo + type: git + params: + - name: text + type: string + description: 'The text that should be echoed' + steps: + - image: busybox + script: 'echo $(params["text"])' + - image: busybox + script: 'ln -s $(resources.inputs.repo.path) $(resources.outputs.repo.path)' +`, namespace)) if _, err := c.TaskClient.Create(ctx, echoTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create echo Task: %s", err) } // Create the repo PipelineResource (doesn't really matter which repo we use) - repoResource := &resource.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{Name: "repo"}, - Spec: resource.PipelineResourceSpec{ - Type: resource.PipelineResourceTypeGit, - Params: []resource.ResourceParam{{ - Name: "Url", - Value: "https://github.com/githubtraining/example-basic", - }}, - }, - } + repoResource := parse.MustParsePipelineResource(t, ` +metadata: + name: repo +spec: + type: git + params: + - name: Url + value: https://github.com/githubtraining/example-basic +`) if _, err := c.PipelineResourceClient.Create(ctx, repoResource, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create simple repo PipelineResource: %s", err) } // Intentionally declaring Tasks in a mixed up order to ensure the order // of execution isn't at all dependent on the order they are declared in - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: "dag-pipeline", Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "repo", Type: resource.PipelineResourceTypeGit, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "pipeline-task-3", - TaskRef: &v1beta1.TaskRef{Name: "echo-task"}, - Params: []v1beta1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("wow"), - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - From: []string{"pipeline-task-2-parallel-1", "pipeline-task-2-parallel-2"}, - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }, { - Name: "pipeline-task-2-parallel-2", - TaskRef: &v1beta1.TaskRef{Name: "echo-task"}, - Params: []v1beta1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("such parallel"), - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - From: []string{"pipeline-task-1"}, - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }, { - Name: "pipeline-task-4", - TaskRef: &v1beta1.TaskRef{Name: "echo-task"}, - Params: []v1beta1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("very cloud native"), - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - RunAfter: []string{"pipeline-task-3"}, - }, { - Name: "pipeline-task-2-parallel-1", - TaskRef: &v1beta1.TaskRef{Name: "echo-task"}, - Params: []v1beta1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("much graph"), - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - From: []string{"pipeline-task-1"}, - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }, { - Name: "pipeline-task-1", - TaskRef: &v1beta1.TaskRef{Name: "echo-task"}, - Params: []v1beta1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("how to ci/cd?"), - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: dag-pipeline + namespace: %s +spec: + resources: + - name: repo + type: git + tasks: + - name: pipeline-task-3 + params: + - name: text + value: wow + resources: + inputs: + - from: + - pipeline-task-2-parallel-1 + - pipeline-task-2-parallel-2 + name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task + - name: pipeline-task-2-parallel-2 + params: + - name: text + value: such parallel + resources: + inputs: + - from: + - pipeline-task-1 + name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task + - name: pipeline-task-4 + params: + - name: text + value: very cloud native + resources: + inputs: + - name: repo + resource: repo + outputs: + - name: repo + resource: repo + runAfter: + - pipeline-task-3 + taskRef: + name: echo-task + - name: pipeline-task-2-parallel-1 + params: + - name: text + value: much graph + resources: + inputs: + - from: + - pipeline-task-1 + name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task + - name: pipeline-task-1 + params: + - name: text + value: how to ci/cd? + resources: + inputs: + - name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task +`, namespace)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create dag-pipeline: %s", err) } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: "dag-pipeline-run", Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: "dag-pipeline"}, - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "repo", - ResourceRef: &v1beta1.PipelineResourceRef{Name: "repo"}, - }}, - }, - } + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: dag-pipeline-run + namespace: %s +spec: + pipelineRef: + name: dag-pipeline + resources: + - name: repo + resourceRef: + name: repo +`, namespace)) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create dag-pipeline-run PipelineRun: %s", err) } diff --git a/test/duplicate_test.go b/test/duplicate_test.go index 86a60e20202..5ac4b84751a 100644 --- a/test/duplicate_test.go +++ b/test/duplicate_test.go @@ -24,9 +24,9 @@ import ( "sync" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" "knative.dev/pkg/test/helpers" @@ -49,18 +49,17 @@ func TestDuplicatePodTaskRun(t *testing.T) { taskrunName := helpers.ObjectNameForTest(t) t.Logf("Creating taskrun %q.", taskrunName) - taskrun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskrunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/echo"}, - Args: []string{"simple"}, - }}}, - }, - }, - } + taskrun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskSpec: + steps: + - image: busybox + command: ['/bin/echo'] + args: ['simple'] +`, taskrunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskrun, metav1.CreateOptions{}); err != nil { t.Fatalf("Error creating taskrun: %v", err) } diff --git a/test/embed_test.go b/test/embed_test.go index b7f9df505c5..02b0285a796 100644 --- a/test/embed_test.go +++ b/test/embed_test.go @@ -21,12 +21,12 @@ package test import ( "context" "fmt" + "strings" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -52,10 +52,10 @@ func TestTaskRun_EmbeddedResource(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - if _, err := c.TaskClient.Create(ctx, getEmbeddedTask(namespace, []string{"/bin/sh", "-c", fmt.Sprintf("echo %s", taskOutput)}), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getEmbeddedTask(t, namespace, []string{"/bin/sh", "-c", fmt.Sprintf("echo %s", taskOutput)}), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", embedTaskName, err) } - if _, err := c.TaskRunClient.Create(ctx, getEmbeddedTaskRun(namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, getEmbeddedTaskRun(t, namespace), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", embedTaskRunName, err) } @@ -68,44 +68,44 @@ func TestTaskRun_EmbeddedResource(t *testing.T) { // completion of the TaskRun means the TaskRun did what it was intended. } -func getEmbeddedTask(namespace string, args []string) *v1beta1.Task { - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: embedTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "docs", Type: resources.PipelineResourceTypeGit, - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "cat /workspace/docs/LICENSE"}, - }}, {Container: corev1.Container{ - Image: "busybox", - Command: args, - }}}, - }, +func getEmbeddedTask(t *testing.T, namespace string, args []string) *v1beta1.Task { + var argsForYaml []string + for _, s := range args { + argsForYaml = append(argsForYaml, fmt.Sprintf("'%s'", s)) } + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: docs + type: git + steps: + - image: ubuntu + command: ['/bin/bash'] + args: ['-c', 'cat /workspace/docs/LICENSE'] + - image: busybox + command: %s +`, embedTaskName, namespace, fmt.Sprintf("[%s]", strings.Join(argsForYaml, ", ")))) } -func getEmbeddedTaskRun(namespace string) *v1beta1.TaskRun { - testSpec := &resources.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeGit, - Params: []v1alpha1.ResourceParam{{ - Name: "URL", - Value: "https://github.com/knative/docs", - }}, - } - return &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: embedTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - Resources: &v1beta1.TaskRunResources{ - Inputs: []v1beta1.TaskResourceBinding{{PipelineResourceBinding: v1beta1.PipelineResourceBinding{ - Name: "docs", ResourceSpec: testSpec, - }}}, - }, - TaskRef: &v1beta1.TaskRef{Name: embedTaskName}, - }, - } +func getEmbeddedTaskRun(t *testing.T, namespace string) *v1beta1.TaskRun { + return parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: docs + resourceSpec: + type: git + params: + - name: URL + value: https://github.com/knative/docs + taskRef: + name: %s +`, embedTaskRunName, namespace, embedTaskName)) } diff --git a/test/entrypoint_test.go b/test/entrypoint_test.go index e96c2136140..b585966622d 100644 --- a/test/entrypoint_test.go +++ b/test/entrypoint_test.go @@ -20,10 +20,11 @@ package test import ( "context" + "fmt" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -45,26 +46,20 @@ func TestEntrypointRunningStepsInOrder(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating TaskRun in namespace %s", namespace) - if _, err := c.TaskRunClient.Create(ctx, &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: epTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "busybox", - WorkingDir: "/workspace", - }, - Script: "sleep 3 && touch foo", - }, { - Container: corev1.Container{ - Image: "ubuntu", - WorkingDir: "/workspace", - }, - Script: "ls foo", - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskSpec: + steps: + - image: busybox + workingDir: /workspace + script: 'sleep 3 && touch foo' + - image: ubuntu + workingDir: /workspace + script: 'ls foo' +`, epTaskRunName, namespace)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/git_checkout_test.go b/test/git_checkout_test.go index 4d11e23caa8..3d77b74f8d2 100644 --- a/test/git_checkout_test.go +++ b/test/git_checkout_test.go @@ -20,11 +20,13 @@ package test import ( "context" + "fmt" "strings" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -107,6 +109,7 @@ func TestGitPipelineRun(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", gitSourceResourceName) + // Still using the struct here rather than YAML because we'd have to conditionally determine which fields to set in the YAML. if _, err := c.PipelineResourceClient.Create(ctx, &v1alpha1.PipelineResource{ ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, Spec: v1alpha1.PipelineResourceSpec{ @@ -123,40 +126,33 @@ func TestGitPipelineRun(t *testing.T) { } t.Logf("Creating PipelineRun %s", gitTestPipelineRunName) - if _, err := c.PipelineRunClient.Create(ctx, &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "git-repo", - ResourceRef: &v1beta1.PipelineResourceRef{Name: gitSourceResourceName}, - }}, - PipelineSpec: &v1beta1.PipelineSpec{ - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "git-check", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "alpine/git", - Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, - }}}, - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "gitsource", - Resource: "git-repo", - }}, - }, - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + resources: + - name: git-repo + type: git + tasks: + - name: git-check + resources: + inputs: + - name: gitsource + resource: git-repo + taskSpec: + resources: + inputs: + - name: gitsource + type: git + steps: + - args: ['--git-dir=/workspace/gitsource/.git', 'show'] + image: alpine/git + resources: + - name: git-repo + resourceRef: + name: %s +`, gitTestPipelineRunName, gitSourceResourceName)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %s", gitTestPipelineRunName, err) } @@ -194,6 +190,7 @@ func TestGitPipelineRunFail(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", gitSourceResourceName) + // Still using the struct here rather than YAML because we'd have to conditionally determine which fields to set in the YAML. if _, err := c.PipelineResourceClient.Create(ctx, &v1alpha1.PipelineResource{ ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, Spec: v1alpha1.PipelineResourceSpec{ @@ -209,40 +206,33 @@ func TestGitPipelineRunFail(t *testing.T) { } t.Logf("Creating PipelineRun %s", gitTestPipelineRunName) - if _, err := c.PipelineRunClient.Create(ctx, &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "git-repo", - ResourceRef: &v1beta1.PipelineResourceRef{Name: gitSourceResourceName}, - }}, - PipelineSpec: &v1beta1.PipelineSpec{ - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "git-check", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "alpine/git", - Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, - }}}, - }}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "gitsource", - Resource: "git-repo", - }}, - }, - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + resources: + - name: git-repo + type: git + tasks: + - name: git-check + resources: + inputs: + - name: gitsource + resource: git-repo + taskSpec: + resources: + inputs: + - name: gitsource + type: git + steps: + - args: ['--git-dir=/workspace/gitsource/.git', 'show'] + image: alpine/git + resources: + - name: git-repo + resourceRef: + name: %s +`, gitTestPipelineRunName, gitSourceResourceName)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %s", gitTestPipelineRunName, err) } diff --git a/test/helm_task_test.go b/test/helm_task_test.go index e73cd3ad62b..09d1274500c 100644 --- a/test/helm_task_test.go +++ b/test/helm_task_test.go @@ -23,11 +23,11 @@ import ( "fmt" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" "github.com/tektoncd/pipeline/pkg/names" - corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -62,37 +62,37 @@ func TestHelmDeployPipelineRun(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", sourceResourceName) - if _, err := c.PipelineResourceClient.Create(ctx, getGoHelloworldGitResource(sourceResourceName), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getGoHelloworldGitResource(t, sourceResourceName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", sourceResourceName, err) } t.Logf("Creating Image PipelineResource %s", sourceImageName) - if _, err := c.PipelineResourceClient.Create(ctx, getHelmImageResource(repo, sourceImageName), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getHelmImageResource(t, repo, sourceImageName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", sourceImageName, err) } t.Logf("Creating Task %s", createImageTaskName) - if _, err := c.TaskClient.Create(ctx, getCreateImageTask(namespace, createImageTaskName), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getCreateImageTask(t, namespace, createImageTaskName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", createImageTaskName, err) } t.Logf("Creating Task %s", helmDeployTaskName) - if _, err := c.TaskClient.Create(ctx, getHelmDeployTask(namespace, helmDeployTaskName), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getHelmDeployTask(t, namespace, helmDeployTaskName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", helmDeployTaskName, err) } t.Logf("Creating Task %s", checkServiceTaskName) - if _, err := c.TaskClient.Create(ctx, getCheckServiceTask(namespace, checkServiceTaskName), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getCheckServiceTask(t, namespace, checkServiceTaskName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", checkServiceTaskName, err) } t.Logf("Creating Pipeline %s", helmDeployPipelineName) - if _, err := c.PipelineClient.Create(ctx, getHelmDeployPipeline(namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName, helmDeployPipelineName), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineClient.Create(ctx, getHelmDeployPipeline(t, namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName, helmDeployPipelineName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", helmDeployPipelineName, err) } t.Logf("Creating PipelineRun %s", helmDeployPipelineRunName) - if _, err := c.PipelineRunClient.Create(ctx, getHelmDeployPipelineRun(namespace, sourceResourceName, sourceImageName, helmDeployPipelineRunName, helmDeployPipelineName), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, getHelmDeployPipelineRun(t, namespace, sourceResourceName, sourceImageName, helmDeployPipelineRunName, helmDeployPipelineName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", helmDeployPipelineRunName, err) } @@ -107,195 +107,180 @@ func TestHelmDeployPipelineRun(t *testing.T) { defer helmCleanup(ctx, c, t, namespace) } -func getGoHelloworldGitResource(sourceResourceName string) *v1alpha1.PipelineResource { - return &resources.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: sourceResourceName, - }, - Spec: resources.PipelineResourceSpec{ - Type: resources.PipelineResourceTypeGit, - Params: []resources.ResourceParam{{ - Name: "url", - Value: "https://github.com/tektoncd/pipeline", - }}, - }, - } +func getGoHelloworldGitResource(t *testing.T, sourceResourceName string) *v1alpha1.PipelineResource { + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + type: git + params: + - name: url + value: https://github.com/tektoncd/pipeline +`, sourceResourceName)) } -func getHelmImageResource(dockerRepo, sourceImageName string) *v1alpha1.PipelineResource { +func getHelmImageResource(t *testing.T, dockerRepo, sourceImageName string) *v1alpha1.PipelineResource { imageName := fmt.Sprintf("%s/%s", dockerRepo, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(sourceImageName)) - return &resources.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: sourceImageName, - }, - Spec: resources.PipelineResourceSpec{ - Type: resources.PipelineResourceTypeImage, - Params: []resources.ResourceParam{{ - Name: "url", - Value: imageName, - }}, - }, - } + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + type: image + params: + - name: url + value: %s +`, sourceImageName, imageName)) } -func getCreateImageTask(namespace, createImageTaskName string) *v1beta1.Task { - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: createImageTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "gitsource", Type: resources.PipelineResourceTypeGit, - }}}, - Outputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "builtimage", Type: resources.PipelineResourceTypeImage, - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "kaniko", - Image: getTestImage(kanikoImage), - Args: []string{ - "--dockerfile=/workspace/gitsource/test/gohelloworld/Dockerfile", - "--context=/workspace/gitsource/", - "--destination=$(outputs.resources.builtimage.url)", - }, - }}}, - }, - } +func getCreateImageTask(t *testing.T, namespace, createImageTaskName string) *v1beta1.Task { + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: gitsource + type: git + outputs: + - name: builtimage + type: image + steps: + - name: kaniko + image: %s + args: ['--dockerfile=/workspace/gitsource/test/gohelloworld/Dockerfile', + '--context=/workspace/gitsource/', + '--destination=$(outputs.resources.builtimage.url)'] +`, createImageTaskName, namespace, getTestImage(kanikoImage))) } -func getHelmDeployTask(namespace, helmDeployTaskName string) *v1beta1.Task { - empty := *v1beta1.NewArrayOrString("") - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helmDeployTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "gitsource", Type: resources.PipelineResourceTypeGit, - }}, {ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "image", Type: resources.PipelineResourceTypeImage, - }}}, - }, - Params: []v1beta1.ParamSpec{{ - Name: "pathToHelmCharts", Type: v1beta1.ParamTypeString, Description: "Path to the helm charts", - }, { - Name: "chartname", Type: v1beta1.ParamTypeString, Default: &empty, - }}, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "alpine/helm:3.5.4", - Args: []string{ - "upgrade", - "--wait", - "--debug", - "--install", - "--namespace", - namespace, - "$(inputs.params.chartname)", - "$(inputs.params.pathToHelmCharts)", - "--set", - "image.repository=$(inputs.resources.image.url)", - "--set", - "service.type=ClusterIP", - }, - }}, {Container: corev1.Container{ - Image: "lachlanevenson/k8s-kubectl", - Command: []string{"kubectl"}, - Args: []string{ - "get", - "all", - "--namespace", - namespace, - }, - }}}, - }, - } +func getHelmDeployTask(t *testing.T, namespace, helmDeployTaskName string) *v1beta1.Task { + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: gitsource + type: git + - name: image + type: image + params: + - name: pathToHelmCharts + type: string + description: 'Path to the helm charts' + - name: chartname + type: string + default: "" + steps: + - image: alpine/helm:3.5.4 + args: ['upgrade', + '--wait', + '--debug', + '--install', + '--namespace', + '%s', + '$(inputs.params.chartname)', + '$(inputs.params.pathToHelmCharts)', + '--set', + 'image.repository=$(inputs.resources.image.url)', + '--set', + 'service.type=ClusterIP'] + - image: lachlanevenson/k8s-kubectl + command: ['kubectl'] + args: ['get', + 'all', + '--namespace', + '%s'] +`, helmDeployTaskName, namespace, namespace, namespace)) } -func getCheckServiceTask(namespace, checkServiceTaskName string) *v1beta1.Task { - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: checkServiceTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Params: []v1beta1.ParamSpec{{ - Name: "serviceUrl", Type: v1beta1.ParamTypeString, Description: "Service url", - }}, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: getTestImage(dockerizeImage), - Args: []string{ - "-wait", - "$(inputs.params.serviceUrl)", - "-timeout", - "1m", - }, - }}}, - }, - } +func getCheckServiceTask(t *testing.T, namespace, checkServiceTaskName string) *v1beta1.Task { + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: serviceUrl + type: string + description: 'Service url' + steps: + - image: %s + args: ['-wait', '$(inputs.params.serviceUrl)', '-timeout', '1m'] +`, checkServiceTaskName, namespace, getTestImage(dockerizeImage))) } -func getHelmDeployPipeline(namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName, helmDeployPipelineName string) *v1beta1.Pipeline { - return &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helmDeployPipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Params: []v1beta1.ParamSpec{{ - Name: "chartname", Type: v1beta1.ParamTypeString, - }}, - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "git-repo", Type: "git", - }, { - Name: "the-image", Type: "image", - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "push-image", - TaskRef: &v1beta1.TaskRef{Name: createImageTaskName}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "gitsource", Resource: "git-repo", - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{{ - Name: "builtimage", Resource: "the-image", - }}, - }, - }, { - Name: "helm-deploy", - TaskRef: &v1beta1.TaskRef{Name: helmDeployTaskName}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "gitsource", Resource: "git-repo", - }, { - Name: "image", Resource: "the-image", From: []string{"push-image"}, - }}, - }, - Params: []v1beta1.Param{{ - Name: "pathToHelmCharts", Value: *v1beta1.NewArrayOrString("/workspace/gitsource/test/gohelloworld/gohelloworld-chart"), - }, { - Name: "chartname", Value: *v1beta1.NewArrayOrString("$(params.chartname)"), - }}, - }, { - Name: "check-service", - TaskRef: &v1beta1.TaskRef{Name: checkServiceTaskName}, - Params: []v1beta1.Param{{ - Name: "serviceUrl", Value: *v1beta1.NewArrayOrString("http://gohelloworld-chart:8080"), - }}, - RunAfter: []string{"helm-deploy"}, - }}, - }, - } +func getHelmDeployPipeline(t *testing.T, namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName, helmDeployPipelineName string) *v1beta1.Pipeline { + return parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: chartname + type: string + resources: + - name: git-repo + type: git + - name: the-image + type: image + tasks: + - name: push-image + taskRef: + name: %s + resources: + inputs: + - name: gitsource + resource: git-repo + outputs: + - name: builtimage + resource: the-image + - name: helm-deploy + taskRef: + name: %s + resources: + inputs: + - name: gitsource + resource: git-repo + - name: image + resource: the-image + params: + - name: pathToHelmCharts + value: /workspace/gitsource/test/gohelloworld/gohelloworld-chart + - name: chartname + value: '$(params.chartname)' + - name: check-service + taskRef: + name: %s + params: + - name: serviceUrl + value: http://gohelloworld-chart:8080 + runAfter: ['helm-deploy'] +`, helmDeployPipelineName, namespace, createImageTaskName, helmDeployTaskName, checkServiceTaskName)) } -func getHelmDeployPipelineRun(namespace, sourceResourceName, sourceImageName, helmDeployPipelineRunName, helmDeployPipelineName string) *v1beta1.PipelineRun { - return &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helmDeployPipelineRunName, Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: helmDeployPipelineName}, - Params: []v1beta1.Param{{ - Name: "chartname", Value: *v1beta1.NewArrayOrString("gohelloworld"), - }}, - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "git-repo", ResourceRef: &v1beta1.PipelineResourceRef{Name: sourceResourceName}, - }, { - Name: "the-image", ResourceRef: &v1beta1.PipelineResourceRef{Name: sourceImageName}, - }}, - }, - } +func getHelmDeployPipelineRun(t *testing.T, namespace, sourceResourceName, sourceImageName, helmDeployPipelineRunName, helmDeployPipelineName string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + params: + - name: chartname + value: gohelloworld + resources: + - name: git-repo + resourceRef: + name: %s + - name: the-image + resourceRef: + name: %s +`, helmDeployPipelineRunName, namespace, helmDeployPipelineName, sourceResourceName, sourceImageName)) } func setupClusterBindingForHelm(ctx context.Context, c *clients, t *testing.T, namespace string) { @@ -338,25 +323,27 @@ func helmCleanup(ctx context.Context, c *clients, t *testing.T, namespace string func removeAllHelmReleases(ctx context.Context, c *clients, t *testing.T, namespace string) { helmRemoveAllTaskName := "helm-remove-all-task" - helmRemoveAllTask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helmRemoveAllTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "helm-remove-all", - Image: "alpine/helm:3.5.4", - Command: []string{"/bin/sh"}, - Args: []string{"-c", fmt.Sprintf("helm ls --short --all --namespace %s | xargs -n1 helm delete --namespace %s", namespace, namespace)}, - }}}, - }, - } + helmRemoveAllTask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - name: helm-remove-all + image: alpine/helm:3.5.4 + command: ['/bin/sh'] + args: ['-c', 'helm ls --short --all --namespace %s | xargs -n1 helm delete --namespace %s'] +`, helmRemoveAllTaskName, namespace, namespace, namespace)) helmRemoveAllTaskRunName := "helm-remove-all-taskrun" - helmRemoveAllTaskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: helmRemoveAllTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: helmRemoveAllTaskName}, - }, - } + helmRemoveAllTaskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s +`, helmRemoveAllTaskRunName, namespace, helmRemoveAllTaskName)) t.Logf("Creating Task %s", helmRemoveAllTaskName) if _, err := c.TaskClient.Create(ctx, helmRemoveAllTask, metav1.CreateOptions{}); err != nil { diff --git a/test/hermetic_taskrun_test.go b/test/hermetic_taskrun_test.go index 3df6755f756..4c0767a38a4 100644 --- a/test/hermetic_taskrun_test.go +++ b/test/hermetic_taskrun_test.go @@ -22,12 +22,11 @@ import ( "context" "fmt" "testing" - "time" + + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" ) // TestHermeticTaskRun make sure that the hermetic execution mode actually drops network from a TaskRun step @@ -44,7 +43,7 @@ func TestHermeticTaskRun(t *testing.T) { tests := []struct { desc string - getTaskRun func(string, string, string) *v1beta1.TaskRun + getTaskRun func(*testing.T, string, string, string) *v1beta1.TaskRun }{ { desc: "run-as-root", @@ -59,7 +58,7 @@ func TestHermeticTaskRun(t *testing.T) { t.Run(test.desc, func(t *testing.T) { // first, run the task run with hermetic=false to prove that it succeeds regularTaskRunName := fmt.Sprintf("not-hermetic-%s", test.desc) - regularTaskRun := test.getTaskRun(regularTaskRunName, namespace, "") + regularTaskRun := test.getTaskRun(t, regularTaskRunName, namespace, "") t.Logf("Creating TaskRun %s, hermetic=false", regularTaskRunName) if _, err := c.TaskRunClient.Create(ctx, regularTaskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", regularTaskRunName, err) @@ -71,7 +70,7 @@ func TestHermeticTaskRun(t *testing.T) { // now, run the task mode with hermetic mode // it should fail, since it shouldn't be able to access any network hermeticTaskRunName := fmt.Sprintf("hermetic-should-fail-%s", test.desc) - hermeticTaskRun := test.getTaskRun(hermeticTaskRunName, namespace, "hermetic") + hermeticTaskRun := test.getTaskRun(t, hermeticTaskRunName, namespace, "hermetic") t.Logf("Creating TaskRun %s, hermetic=true", hermeticTaskRunName) if _, err := c.TaskRunClient.Create(ctx, hermeticTaskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", regularTaskRun.Name, err) @@ -83,62 +82,49 @@ func TestHermeticTaskRun(t *testing.T) { } } -func taskRun(name, namespace, executionMode string) *v1beta1.TaskRun { - return &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: name, - Namespace: namespace, - Annotations: map[string]string{ - "experimental.tekton.dev/execution-mode": executionMode, - }, - }, - Spec: v1beta1.TaskRunSpec{ - Timeout: &metav1.Duration{Duration: time.Minute}, - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{ - { - Container: corev1.Container{ - Name: "access-network", - Image: "ubuntu", - }, - Script: `#!/bin/bash -set -ex -apt-get update -apt-get install -y curl`, - }, - }, - }, - }, - } +func taskRun(t *testing.T, name, namespace, executionMode string) *v1beta1.TaskRun { + return parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + annotations: + experimental.tekton.dev/execution-mode: %s + name: %s + namespace: %s +spec: + taskSpec: + steps: + - image: ubuntu + name: access-network + resources: {} + script: |- + #!/bin/bash + set -ex + apt-get update + apt-get install -y curl + timeout: 1m0s +`, executionMode, name, namespace)) } -func unpriviligedTaskRun(name, namespace, executionMode string) *v1beta1.TaskRun { - return &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: name, - Namespace: namespace, - Annotations: map[string]string{ - "experimental.tekton.dev/execution-mode": executionMode, - }, - }, - Spec: v1beta1.TaskRunSpec{ - Timeout: &metav1.Duration{Duration: time.Minute}, - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{ - { - Container: corev1.Container{ - Name: "curl", - Image: "gcr.io/cloud-builders/curl", - SecurityContext: &corev1.SecurityContext{ - RunAsUser: pointer.Int64Ptr(1000), - RunAsNonRoot: pointer.BoolPtr(true), - AllowPrivilegeEscalation: pointer.BoolPtr(false), - }, - }, - Script: `#!/bin/bash -set -ex -curl google.com`, - }, - }, - }, - }, - } +func unpriviligedTaskRun(t *testing.T, name, namespace, executionMode string) *v1beta1.TaskRun { + return parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + annotations: + experimental.tekton.dev/execution-mode: %s + name: %s + namespace: %s +spec: + taskSpec: + steps: + - image: gcr.io/cloud-builders/curl + name: curl + resources: {} + script: |- + #!/bin/bash + set -ex + curl google.com + securityContext: + allowPrivilegeEscalation: false + runAsNonRoot: true + runAsUser: 1000 + timeout: 1m0s +`, executionMode, name, namespace)) } diff --git a/test/ignore_step_error_test.go b/test/ignore_step_error_test.go index 3bbedfb3c5f..710ebb58a92 100644 --- a/test/ignore_step_error_test.go +++ b/test/ignore_step_error_test.go @@ -22,6 +22,8 @@ import ( "context" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -35,7 +37,7 @@ func TestMissingResultWhenStepErrorIsIgnored(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - pipelineRun := mustParsePipelineRun(t, ` + pipelineRun := parse.MustParsePipelineRun(t, ` metadata: name: pipelinerun-with-failing-step spec: diff --git a/test/kaniko_task_test.go b/test/kaniko_task_test.go index 2f3e82fd63b..bc17c1a5650 100644 --- a/test/kaniko_task_test.go +++ b/test/kaniko_task_test.go @@ -23,12 +23,12 @@ import ( "fmt" "strings" "testing" - "time" + + "github.com/tektoncd/pipeline/test/parse" "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -62,22 +62,22 @@ func TestKanikoTaskRun(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", kanikoGitResourceName) - if _, err := c.PipelineResourceClient.Create(ctx, getGitResource(), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getGitResource(t), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) } t.Logf("Creating Image PipelineResource %s", repo) - if _, err := c.PipelineResourceClient.Create(ctx, getImageResource(repo), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getImageResource(t, repo), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) } t.Logf("Creating Task %s", kanikoTaskName) - if _, err := c.TaskClient.Create(ctx, getTask(repo, namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getTask(t, repo, namespace), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", kanikoTaskName, err) } t.Logf("Creating TaskRun %s", kanikoTaskRunName) - if _, err := c.TaskRunClient.Create(ctx, getTaskRun(namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, getTaskRun(t, namespace), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", kanikoTaskRunName, err) } @@ -132,95 +132,82 @@ func TestKanikoTaskRun(t *testing.T) { } } -func getGitResource() *v1alpha1.PipelineResource { - return &resources.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: kanikoGitResourceName, - }, - Spec: resources.PipelineResourceSpec{ - Type: resources.PipelineResourceTypeGit, - Params: []resources.ResourceParam{ - { - Name: "Url", - Value: "https://github.com/GoogleContainerTools/kaniko", - }, - { - Name: "Revision", - Value: revision, - }, - }, - }, - } +func getGitResource(t *testing.T) *v1alpha1.PipelineResource { + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + type: git + params: + - name: Url + value: https://github.com/GoogleContainerTools/kaniko + - name: Revision + value: %s +`, kanikoGitResourceName, revision)) } -func getImageResource(repo string) *v1alpha1.PipelineResource { - return &resources.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: kanikoImageResourceName, - }, - Spec: resources.PipelineResourceSpec{ - Type: resources.PipelineResourceTypeImage, - Params: []resources.ResourceParam{{ - Name: "url", - Value: repo, - }}, - }, - } +func getImageResource(t *testing.T, repo string) *v1alpha1.PipelineResource { + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + type: image + params: + - name: url + value: %s +`, kanikoImageResourceName, repo)) } -func getTask(repo, namespace string) *v1beta1.Task { - root := int64(0) - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: kanikoTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "gitsource", Type: resources.PipelineResourceTypeGit, - }}}, - Outputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "builtImage", Type: resources.PipelineResourceTypeImage, - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "kaniko", - Image: getTestImage(kanikoImage), - Args: []string{ - "--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label", - fmt.Sprintf("--destination=%s", repo), - "--context=/workspace/gitsource", - "--oci-layout-path=/workspace/output/builtImage", - "--insecure", - "--insecure-pull", - "--insecure-registry=registry." + namespace + ":5000/", - }, - SecurityContext: &corev1.SecurityContext{ - RunAsUser: &root, - }, - }}}, - Sidecars: []v1beta1.Sidecar{{Container: corev1.Container{ - Name: "registry", - Image: getTestImage(registryImage), - }}}, - }, - } +func getTask(t *testing.T, repo, namespace string) *v1beta1.Task { + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + inputs: + - name: gitsource + type: git + outputs: + - name: builtImage + type: image + steps: + - name: kaniko + image: %s + args: ['--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label', + '--destination=%s', + '--context=/workspace/gitsource', + '--oci-layout-path=/workspace/output/builtImage', + '--insecure', + '--insecure-pull', + '--insecure-registry=registry.%s:5000/'] + securityContext: + runAsUser: 0 + sidecars: + - name: registry + image: %s +`, kanikoTaskName, namespace, getTestImage(kanikoImage), repo, namespace, getTestImage(registryImage))) } -func getTaskRun(namespace string) *v1beta1.TaskRun { - return &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: kanikoTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: kanikoTaskName}, - Timeout: &metav1.Duration{Duration: 2 * time.Minute}, - Resources: &v1beta1.TaskRunResources{ - Inputs: []v1beta1.TaskResourceBinding{{PipelineResourceBinding: v1beta1.PipelineResourceBinding{ - Name: "gitsource", ResourceRef: &v1beta1.PipelineResourceRef{Name: kanikoGitResourceName}, - }}}, - Outputs: []v1beta1.TaskResourceBinding{{PipelineResourceBinding: v1beta1.PipelineResourceBinding{ - Name: "builtImage", ResourceRef: &v1beta1.PipelineResourceRef{Name: kanikoImageResourceName}, - }}}, - }, - }, - } +func getTaskRun(t *testing.T, namespace string) *v1beta1.TaskRun { + return parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + timeout: 2m + resources: + inputs: + - name: gitsource + resourceRef: + name: %s + outputs: + - name: builtImage + resourceRef: + name: %s +`, kanikoTaskRunName, namespace, kanikoTaskName, kanikoGitResourceName, kanikoImageResourceName)) } // getRemoteDigest starts a pod to query the registry from the namespace itself, using skopeo (and jq). diff --git a/test/parse/yaml.go b/test/parse/yaml.go new file mode 100644 index 00000000000..c0bdb5440c5 --- /dev/null +++ b/test/parse/yaml.go @@ -0,0 +1,131 @@ +/* + Copyright 2021 The Tekton Authors + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package parse + +import ( + "testing" + + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" + + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/scheme" + "k8s.io/apimachinery/pkg/runtime" +) + +// MustParseTaskRun takes YAML and parses it into a *v1beta1.TaskRun +func MustParseTaskRun(t *testing.T, yaml string) *v1beta1.TaskRun { + var tr v1beta1.TaskRun + yaml = `apiVersion: tekton.dev/v1beta1 +kind: TaskRun +` + yaml + mustParseYAML(t, yaml, &tr) + return &tr +} + +// MustParseAlphaTaskRun takes YAML and parses it into a *v1alpha1.TaskRun +func MustParseAlphaTaskRun(t *testing.T, yaml string) *v1alpha1.TaskRun { + var tr v1alpha1.TaskRun + yaml = `apiVersion: tekton.dev/v1alpha1 +kind: TaskRun +` + yaml + mustParseYAML(t, yaml, &tr) + return &tr +} + +// MustParseTask takes YAML and parses it into a *v1beta1.Task +func MustParseTask(t *testing.T, yaml string) *v1beta1.Task { + var task v1beta1.Task + yaml = `apiVersion: tekton.dev/v1beta1 +kind: Task +` + yaml + mustParseYAML(t, yaml, &task) + return &task +} + +// MustParseAlphaTask takes YAML and parses it into a *v1alpha1.Task +func MustParseAlphaTask(t *testing.T, yaml string) *v1alpha1.Task { + var task v1alpha1.Task + yaml = `apiVersion: tekton.dev/v1alpha1 +kind: Task +` + yaml + mustParseYAML(t, yaml, &task) + return &task +} + +// MustParsePipelineRun takes YAML and parses it into a *v1beta1.PipelineRun +func MustParsePipelineRun(t *testing.T, yaml string) *v1beta1.PipelineRun { + var pr v1beta1.PipelineRun + yaml = `apiVersion: tekton.dev/v1beta1 +kind: PipelineRun +` + yaml + mustParseYAML(t, yaml, &pr) + return &pr +} + +// MustParseAlphaPipelineRun takes YAML and parses it into a *v1alpha1.PipelineRun +func MustParseAlphaPipelineRun(t *testing.T, yaml string) *v1alpha1.PipelineRun { + var pr v1alpha1.PipelineRun + yaml = `apiVersion: tekton.dev/v1alpha1 +kind: PipelineRun +` + yaml + mustParseYAML(t, yaml, &pr) + return &pr +} + +// MustParsePipeline takes YAML and parses it into a *v1beta1.Pipeline +func MustParsePipeline(t *testing.T, yaml string) *v1beta1.Pipeline { + var pipeline v1beta1.Pipeline + yaml = `apiVersion: tekton.dev/v1beta1 +kind: Pipeline +` + yaml + mustParseYAML(t, yaml, &pipeline) + return &pipeline +} + +// MustParseAlphaPipeline takes YAML and parses it into a *v1alpha1.Pipeline +func MustParseAlphaPipeline(t *testing.T, yaml string) *v1alpha1.Pipeline { + var pipeline v1alpha1.Pipeline + yaml = `apiVersion: tekton.dev/v1alpha1 +kind: Pipeline +` + yaml + mustParseYAML(t, yaml, &pipeline) + return &pipeline +} + +// MustParsePipelineResource takes YAML and parses it into a *resourcev1alpha1.PipelineResource +func MustParsePipelineResource(t *testing.T, yaml string) *resourcev1alpha1.PipelineResource { + var resource resourcev1alpha1.PipelineResource + yaml = `apiVersion: tekton.dev/v1alpha1 +kind: PipelineResource +` + yaml + mustParseYAML(t, yaml, &resource) + return &resource +} + +// MustParseCondition takes YAML and parses it into a *v1alpha1.Condition +func MustParseCondition(t *testing.T, yaml string) *v1alpha1.Condition { + var cond v1alpha1.Condition + yaml = `apiVersion: tekton.dev/v1alpha1 +kind: Condition +` + yaml + mustParseYAML(t, yaml, &cond) + return &cond +} + +func mustParseYAML(t *testing.T, yaml string, i runtime.Object) { + if _, _, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(yaml), nil, i); err != nil { + t.Fatalf("mustParseYAML (%s): %v", yaml, err) + } +} diff --git a/test/pipelinefinally_test.go b/test/pipelinefinally_test.go index 4e398233846..181d3a9815b 100644 --- a/test/pipelinefinally_test.go +++ b/test/pipelinefinally_test.go @@ -18,10 +18,13 @@ package test import ( "context" "encoding/json" + "fmt" "sort" "strings" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/test/diff" @@ -91,167 +94,134 @@ func TestPipelineLevelFinally_OneDAGTaskFailed_InvalidTaskResult_Failure(t *test t.Fatalf("Failed to create Task producing task results: %s", err) } - taskConsumingResultInParam := getSuccessTaskConsumingResults(t, namespace, []v1beta1.ParamSpec{{Name: "dagtask-result"}}, []v1beta1.TaskResult{}) + taskConsumingResultInParam := getSuccessTaskConsumingResults(t, namespace, "dagtask-result") if _, err := c.TaskClient.Create(ctx, taskConsumingResultInParam, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task consuming task results in param: %s", err) } - taskConsumingResultInWhenExpression := getSuccessTaskConsumingResults(t, namespace, []v1beta1.ParamSpec{}, []v1beta1.TaskResult{}) + taskConsumingResultInWhenExpression := getSuccessTask(t, namespace) if _, err := c.TaskClient.Create(ctx, taskConsumingResultInWhenExpression, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task consuming task results in when expressions: %s", err) } - we := v1beta1.WhenExpressions{{ - Input: "foo", - Operator: "notin", - Values: []string{"foo"}, - }} - - dag := map[string]pipelineTask{ - "dagtask1": { - TaskName: task.Name, - }, - "dagtask2": { - TaskName: delayedTask.Name, - }, - "dagtask3": { - TaskName: successTask.Name, - Condition: cond.Name, - }, - "dagtask4": { - TaskName: successTask.Name, - When: we, - }, - "dagtask5": { - TaskName: taskProducingResult.Name, - }, - } - - f := map[string]pipelineTask{ - "finaltask1": { - TaskName: successTask.Name, - }, - "finaltask2": { - TaskName: finalTaskWithStatus.Name, - Param: []v1beta1.Param{{ - Name: "dagtask1-status", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask1.status)", - }, - }, { - Name: "dagtask2-status", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask2.status)", - }, - }, { - Name: "dagtask3-status", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask3.status)", - }, - }, { - Name: "dagtasks-aggregate-status", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.status)", - }, - }}, - }, - // final task consuming result from a failed dag task - "finaltaskconsumingdagtask1": { - TaskName: taskConsumingResultInParam.Name, - Param: []v1beta1.Param{{ - Name: "dagtask-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask1.results.result)", - }, - }}, - }, - // final task consuming result from a skipped dag task due to when expression - "finaltaskconsumingdagtask4": { - TaskName: taskConsumingResultInParam.Name, - Param: []v1beta1.Param{{ - Name: "dagtask-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask4.results.result)", - }, - }}, - }, - "finaltaskconsumingdagtask5": { - TaskName: taskConsumingResultInParam.Name, - Param: []v1beta1.Param{{ - Name: "dagtask-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask5.results.result)", - }, - }}, - }, - // final task with when expressions using results from skipped dag task - "guardedfinaltaskconsumingdagtask4": { - TaskName: taskConsumingResultInWhenExpression.Name, - When: v1beta1.WhenExpressions{{ - Input: "$(tasks.dagtask4.results.result)", - Operator: "in", - Values: []string{"aResult"}, - }}, - }, - // final task with when expressions using results from successful dag task - // when expressions evaluate to true - "guardedfinaltaskusingdagtask5result1": { - TaskName: taskConsumingResultInWhenExpression.Name, - When: v1beta1.WhenExpressions{{ - Input: "$(tasks.dagtask5.results.result)", - Operator: "in", - Values: []string{"Hello"}, - }}, - }, - // final task with when expressions using results from successful dag task - // when expressions evaluate to false - "guardedfinaltaskusingdagtask5result2": { - TaskName: taskConsumingResultInWhenExpression.Name, - When: v1beta1.WhenExpressions{{ - Input: "$(tasks.dagtask5.results.result)", - Operator: "notin", - Values: []string{"Hello"}, - }}, - }, - // final task with when expressions using execution status of a dag task - // when expressions evaluate to true - "guardedfinaltaskusingdagtask5status1": { - TaskName: taskConsumingResultInWhenExpression.Name, - When: v1beta1.WhenExpressions{{ - Input: "$(tasks.dagtask5.status)", - Operator: "in", - Values: []string{"Succeeded"}, - }, { - Input: "$(tasks.status)", - Operator: "in", - Values: []string{"Failed"}, - }}, - }, - // final task with when expressions using execution status of a dag task - // when expressions evaluate to false - "guardedfinaltaskusingdagtask5status2": { - TaskName: taskConsumingResultInWhenExpression.Name, - When: v1beta1.WhenExpressions{{ - Input: "$(tasks.dagtask5.status)", - Operator: "in", - Values: []string{"Failed"}, - }, { - Input: "$(tasks.status)", - Operator: "notin", - Values: []string{"Failed"}, - }}, - }, - } - - pipeline := getPipeline(t, namespace, dag, f) - + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + finally: + - name: finaltask1 + taskRef: + name: %s + - name: finaltask2 + params: + - name: dagtask1-status + value: $(tasks.dagtask1.status) + - name: dagtask2-status + value: $(tasks.dagtask2.status) + - name: dagtask3-status + value: $(tasks.dagtask3.status) + - name: dagtasks-aggregate-status + value: $(tasks.status) + taskRef: + name: %s + - name: finaltaskconsumingdagtask1 + params: + - name: dagtask-result + value: $(tasks.dagtask1.results.result) + taskRef: + name: %s + - name: finaltaskconsumingdagtask4 + params: + - name: dagtask-result + value: $(tasks.dagtask4.results.result) + taskRef: + name: %s + - name: finaltaskconsumingdagtask5 + params: + - name: dagtask-result + value: $(tasks.dagtask5.results.result) + taskRef: + name: %s + - name: guardedfinaltaskconsumingdagtask4 + taskRef: + name: %s + when: + - input: $(tasks.dagtask4.results.result) + operator: in + values: + - aResult + - name: guardedfinaltaskusingdagtask5result1 + taskRef: + name: %s + when: + - input: $(tasks.dagtask5.results.result) + operator: in + values: + - Hello + - name: guardedfinaltaskusingdagtask5result2 + taskRef: + name: %s + when: + - input: $(tasks.dagtask5.results.result) + operator: notin + values: + - Hello + - name: guardedfinaltaskusingdagtask5status1 + taskRef: + name: %s + when: + - input: $(tasks.dagtask5.status) + operator: in + values: + - Succeeded + - input: $(tasks.status) + operator: in + values: + - Failed + - name: guardedfinaltaskusingdagtask5status2 + taskRef: + name: %s + when: + - input: $(tasks.dagtask5.status) + operator: in + values: + - Failed + - input: $(tasks.status) + operator: notin + values: + - Failed + tasks: + - name: dagtask1 + taskRef: + name: %s + - name: dagtask2 + taskRef: + name: %s + - name: dagtask3 + taskRef: + name: %s + conditions: + - conditionRef: %s + - name: dagtask4 + taskRef: + name: %s + when: + - input: foo + operator: notin + values: + - foo + - name: dagtask5 + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, + // Finally + successTask.Name, finalTaskWithStatus.Name, taskConsumingResultInParam.Name, + taskConsumingResultInParam.Name, taskConsumingResultInParam.Name, taskConsumingResultInWhenExpression.Name, + taskConsumingResultInWhenExpression.Name, taskConsumingResultInWhenExpression.Name, taskConsumingResultInWhenExpression.Name, + taskConsumingResultInWhenExpression.Name, + // Tasks + task.Name, delayedTask.Name, successTask.Name, cond.Name, successTask.Name, taskProducingResult.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } @@ -378,8 +348,12 @@ func TestPipelineLevelFinally_OneDAGTaskFailed_InvalidTaskResult_Failure(t *test expectedSkippedTasks := []v1beta1.SkippedTask{{ Name: "dagtask3", }, { - Name: "dagtask4", - WhenExpressions: we, + Name: "dagtask4", + WhenExpressions: v1beta1.WhenExpressions{{ + Input: "foo", + Operator: "notin", + Values: []string{"foo"}, + }}, }, { Name: "finaltaskconsumingdagtask1", }, { @@ -435,15 +409,20 @@ func TestPipelineLevelFinally_OneFinalTaskFailed_Failure(t *testing.T) { t.Fatalf("Failed to create final Task: %s", err) } - pt := map[string]pipelineTask{ - "dagtask1": {TaskName: task.Name}, - } - - fpt := map[string]pipelineTask{ - "finaltask1": {TaskName: finalTask.Name}, - } - - pipeline := getPipeline(t, namespace, pt, fpt) + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + finally: + - name: finaltask1 + taskRef: + name: %s + tasks: + - name: dagtask1 + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, finalTask.Name, task.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } @@ -496,7 +475,7 @@ func TestPipelineLevelFinally_OneFinalTask_CancelledRunFinally(t *testing.T) { t.Fatalf("Failed to create dag Task: %s", err) } - task2 := getSuccessTaskConsumingResults(t, namespace, []v1beta1.ParamSpec{{Name: "dagtask1-result"}}, []v1beta1.TaskResult{}) + task2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result") if _, err := c.TaskClient.Create(ctx, task2, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create dag Task: %s", err) } @@ -506,44 +485,37 @@ func TestPipelineLevelFinally_OneFinalTask_CancelledRunFinally(t *testing.T) { t.Fatalf("Failed to create final Task: %s", err) } - finalTask2 := getSuccessTaskConsumingResults(t, namespace, []v1beta1.ParamSpec{{Name: "dagtask1-result"}}, []v1beta1.TaskResult{}) + finalTask2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result") if _, err := c.TaskClient.Create(ctx, finalTask2, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create final Task: %s", err) } - pt := map[string]pipelineTask{ - "dagtask1": { - TaskName: task1.Name, - }, - "dagtask2": { - TaskName: task2.Name, - Param: []v1beta1.Param{{ - Name: "dagtask1-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask1.results.result)", - }, - }}, - }, - } - - fpt := map[string]pipelineTask{ - "finaltask1": { - TaskName: finalTask1.Name, - }, - "finaltask2": { - TaskName: finalTask2.Name, - Param: []v1beta1.Param{{ - Name: "dagtask1-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask1.results.result)", - }, - }}, - }, - } - - pipeline := getPipeline(t, namespace, pt, fpt) + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + finally: + - name: finaltask1 + taskRef: + name: %s + - name: finaltask2 + params: + - name: dagtask1-result + value: $(tasks.dagtask1.results.result) + taskRef: + name: %s + tasks: + - name: dagtask1 + taskRef: + name: %s + - name: dagtask2 + params: + - name: dagtask1-result + value: $(tasks.dagtask1.results.result) + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, finalTask1.Name, finalTask2.Name, task1.Name, task2.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } @@ -618,7 +590,7 @@ func TestPipelineLevelFinally_OneFinalTask_StoppedRunFinally(t *testing.T) { t.Fatalf("Failed to create dag Task: %s", err) } - task2 := getSuccessTaskConsumingResults(t, namespace, []v1beta1.ParamSpec{{Name: "dagtask1-result"}}, []v1beta1.TaskResult{}) + task2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result") if _, err := c.TaskClient.Create(ctx, task2, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create dag Task: %s", err) } @@ -628,44 +600,37 @@ func TestPipelineLevelFinally_OneFinalTask_StoppedRunFinally(t *testing.T) { t.Fatalf("Failed to create final Task: %s", err) } - finalTask2 := getSuccessTaskConsumingResults(t, namespace, []v1beta1.ParamSpec{{Name: "dagtask1-result"}}, []v1beta1.TaskResult{}) + finalTask2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result") if _, err := c.TaskClient.Create(ctx, finalTask2, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create final Task: %s", err) } - pt := map[string]pipelineTask{ - "dagtask1": { - TaskName: task1.Name, - }, - "dagtask2": { - TaskName: task2.Name, - Param: []v1beta1.Param{{ - Name: "dagtask1-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask1.results.result)", - }, - }}, - }, - } - - fpt := map[string]pipelineTask{ - "finaltask1": { - TaskName: finalTask1.Name, - }, - "finaltask2": { - TaskName: finalTask2.Name, - Param: []v1beta1.Param{{ - Name: "dagtask1-result", - Value: v1beta1.ArrayOrString{ - Type: "string", - StringVal: "$(tasks.dagtask1.results.result)", - }, - }}, - }, - } - - pipeline := getPipeline(t, namespace, pt, fpt) + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + finally: + - name: finaltask1 + taskRef: + name: %s + - name: finaltask2 + params: + - name: dagtask1-result + value: $(tasks.dagtask1.results.result) + taskRef: + name: %s + tasks: + - name: dagtask1 + taskRef: + name: %s + - name: dagtask2 + params: + - name: dagtask1-result + value: $(tasks.dagtask1.results.result) + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, finalTask1.Name, finalTask2.Name, task1.Name, task2.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } @@ -760,125 +725,120 @@ func isSkipped(t *testing.T, taskRunName string, conds duckv1beta1.Conditions) b return false } -func getTaskDef(n, namespace, script string, params []v1beta1.ParamSpec, results []v1beta1.TaskResult) *v1beta1.Task { - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: n, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "alpine"}, - Script: script, - }}, - Params: params, - Results: results, - }, - } -} - func getSuccessTask(t *testing.T, namespace string) *v1beta1.Task { - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "exit 0", []v1beta1.ParamSpec{}, []v1beta1.TaskResult{}) + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'exit 0' +`, helpers.ObjectNameForTest(t), namespace)) } func getFailTask(t *testing.T, namespace string) *v1beta1.Task { - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "exit 1", []v1beta1.ParamSpec{}, []v1beta1.TaskResult{}) + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'exit 1' +`, helpers.ObjectNameForTest(t), namespace)) } func getDelaySuccessTask(t *testing.T, namespace string) *v1beta1.Task { - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "sleep 5; exit 0", []v1beta1.ParamSpec{}, []v1beta1.TaskResult{}) + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'sleep 5; exit 0' +`, helpers.ObjectNameForTest(t), namespace)) } func getTaskVerifyingStatus(t *testing.T, namespace string) *v1beta1.Task { - params := []v1beta1.ParamSpec{{ - Name: "dagtask1-status", - }, { - Name: "dagtask2-status", - }, { - Name: "dagtask3-status", - }, { - Name: "dagtasks-aggregate-status", - }} - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "exit 0", params, []v1beta1.TaskResult{}) + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'exit 0' + params: + - name: dagtask1-status + - name: dagtask2-status + - name: dagtask3-status + - name: dagtasks-aggregate-status +`, helpers.ObjectNameForTest(t), namespace)) } func getSuccessTaskProducingResults(t *testing.T, namespace string) *v1beta1.Task { - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "echo -n \"Hello\" > $(results.result.path)", []v1beta1.ParamSpec{}, []v1beta1.TaskResult{{Name: "result"}}) + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'echo -n "Hello" > $(results.result.path)' + results: + - name: result +`, helpers.ObjectNameForTest(t), namespace)) } func getDelaySuccessTaskProducingResults(t *testing.T, namespace string) *v1beta1.Task { - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "sleep 5; echo -n \"Hello\" > $(results.result.path)", []v1beta1.ParamSpec{}, []v1beta1.TaskResult{{Name: "result"}}) + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'sleep 5; echo -n "Hello" > $(results.result.path)' + results: + - name: result +`, helpers.ObjectNameForTest(t), namespace)) } -func getSuccessTaskConsumingResults(t *testing.T, namespace string, params []v1beta1.ParamSpec, results []v1beta1.TaskResult) *v1beta1.Task { - return getTaskDef(helpers.ObjectNameForTest(t), namespace, "exit 0", params, results) +func getSuccessTaskConsumingResults(t *testing.T, namespace string, paramName string) *v1beta1.Task { + return parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'exit 0' + params: + - name: %s +`, helpers.ObjectNameForTest(t), namespace, paramName)) } func getCondition(t *testing.T, namespace string) *v1alpha1.Condition { - return &v1alpha1.Condition{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1alpha1.ConditionSpec{ - Check: v1alpha1.Step{ - Container: corev1.Container{Image: "ubuntu"}, - Script: "exit 1", - }, - }, - } -} - -type pipelineTask struct { - TaskName string - Condition string - Param []v1beta1.Param - When v1beta1.WhenExpressions -} - -func getPipeline(t *testing.T, namespace string, dag map[string]pipelineTask, f map[string]pipelineTask) *v1beta1.Pipeline { - var pt []v1beta1.PipelineTask - var fpt []v1beta1.PipelineTask - for k, v := range dag { - task := v1beta1.PipelineTask{ - Name: k, - TaskRef: &v1beta1.TaskRef{Name: v.TaskName}, - } - if v.Condition != "" { - task.Conditions = []v1beta1.PipelineTaskCondition{{ - ConditionRef: v.Condition, - }} - } - if len(v.Param) != 0 { - task.Params = v.Param - } - if len(v.When) != 0 { - task.WhenExpressions = v.When - } - pt = append(pt, task) - } - for k, v := range f { - finalTask := v1beta1.PipelineTask{ - Name: k, - TaskRef: &v1beta1.TaskRef{Name: v.TaskName}, - } - if len(v.Param) != 0 { - finalTask.Params = v.Param - } - if len(v.When) != 0 { - finalTask.WhenExpressions = v.When - } - fpt = append(fpt, finalTask) - } - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: pt, - Finally: fpt, - }, - } - return pipeline + return parse.MustParseCondition(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + check: + image: ubuntu + script: 'exit 1' +`, helpers.ObjectNameForTest(t), namespace)) } func getPipelineRun(t *testing.T, namespace, p string) *v1beta1.PipelineRun { - return &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: p}, - }, - } + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, p)) } diff --git a/test/pipelinerun_test.go b/test/pipelinerun_test.go index c536aef778f..95ad36cacde 100644 --- a/test/pipelinerun_test.go +++ b/test/pipelinerun_test.go @@ -26,10 +26,11 @@ import ( "testing" "time" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" "github.com/tektoncd/pipeline/pkg/artifacts" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -58,26 +59,26 @@ func TestPipelineRun(t *testing.T) { testSetup func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) expectedTaskRuns []string expectedNumberOfEvents int - pipelineRunFunc func(int, string) *v1beta1.PipelineRun + pipelineRunFunc func(*testing.T, int, string) *v1beta1.PipelineRun } tds := []tests{{ name: "fan-in and fan-out", testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { t.Helper() - for _, task := range getFanInFanOutTasks(namespace) { + for _, task := range getFanInFanOutTasks(t, namespace) { 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() { + for _, res := range getFanInFanOutGitResources(t) { if _, err := c.PipelineResourceClient.Create(ctx, res, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) } } - if _, err := c.PipelineClient.Create(ctx, getFanInFanOutPipeline(index, namespace), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -98,29 +99,27 @@ func TestPipelineRun(t *testing.T) { t.Fatalf("Failed to create SA `%s`: %s", getName(saName, index), err) } - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: getName(taskName, index), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Params: []v1beta1.ParamSpec{{ - Name: "the.path", Type: v1beta1.ParamTypeString, - }, { - Name: "the.dest", Type: v1beta1.ParamTypeString, - }}, - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Name: "config-docker", - Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest", - Command: []string{"skopeo"}, - Args: []string{"copy", `$(params["the.path"])`, "$(params['the.dest'])"}, - }}, - }, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: the.path + type: string + - name: the.dest + type: string + steps: + - name: config-docker + image: gcr.io/tekton-releases/dogfooding/skopeo:latest + command: ['skopeo'] + args: ['copy', '$(params["the.path"])', '$(params["the.dest"])'] +`, getName(taskName, index), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) } - if _, err := c.PipelineClient.Create(ctx, getHelloWorldPipelineWithSingularTask(index, namespace), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -137,20 +136,20 @@ func TestPipelineRun(t *testing.T) { t.Fatalf("Failed to create Condition `%s`: %s", cond1Name, err) } - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: getName(taskName, index), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo hello, world"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: ubuntu + command: ['/bin/bash'] + args: ['-c', 'echo hello, world'] +`, getName(taskName, index), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) } - if _, err := c.PipelineClient.Create(ctx, getPipelineWithFailingCondition(index, namespace), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -175,28 +174,26 @@ func TestPipelineRun(t *testing.T) { t.Fatalf("Failed to create SA `%s`: %s", getName(saName, index), err) } - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: getName(taskName, index), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Params: []v1beta1.ParamSpec{{ - Name: "the.path", Type: v1beta1.ParamTypeString, - }, { - Name: "dest", Type: v1beta1.ParamTypeString, - }}, - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Name: "config-docker", - Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest", - Command: []string{"skopeo"}, - Args: []string{"copy", `$(params["the.path"])`, `$(params['dest'])`}, - }}, - }, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: the.path + type: string + - name: the.dest + type: string + steps: + - name: config-docker + image: gcr.io/tekton-releases/dogfooding/skopeo:latest + command: ['skopeo'] + args: ['copy', '$(params["the.path"])', '$(params["the.dest"])'] +`, getName(taskName, index), 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(index, namespace), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -223,7 +220,7 @@ func TestPipelineRun(t *testing.T) { td.testSetup(ctx, t, c, namespace, i) prName := fmt.Sprintf("%s%d", pipelineRunName, i) - pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(i, namespace), metav1.CreateOptions{}) + pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(t, i, namespace), metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -302,26 +299,27 @@ func TestPipelineRun(t *testing.T) { } } -func getHelloWorldPipelineWithSingularTask(suffix int, namespace string) *v1beta1.Pipeline { - return &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: getName(pipelineName, suffix), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Params: []v1beta1.ParamSpec{{ - Name: "the.path", Type: v1beta1.ParamTypeString, - }, { - Name: "dest", Type: v1beta1.ParamTypeString, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: task1Name, - TaskRef: &v1beta1.TaskRef{Name: getName(taskName, suffix)}, - Params: []v1beta1.Param{{ - Name: "the.path", Value: *v1beta1.NewArrayOrString(`$(params["the.path"])`), - }, { - Name: "dest", Value: *v1beta1.NewArrayOrString("$(params.dest)"), - }}, - }}, - }, - } +func getHelloWorldPipelineWithSingularTask(t *testing.T, suffix int, namespace string) *v1beta1.Pipeline { + return parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: the.path + type: string + - name: dest + type: string + tasks: + - name: %s + params: + - name: the.path + value: $(params["the.path"]) + - name: dest + value: $(params.dest) + taskRef: + name: %s +`, getName(pipelineName, suffix), namespace, task1Name, getName(taskName, suffix))) } // TestPipelineRunRefDeleted tests that a running PipelineRun doesn't fail when the Pipeline @@ -338,7 +336,7 @@ func TestPipelineRunRefDeleted(t *testing.T) { prName := "pipelinerun-referencing-deleted" t.Logf("Creating Pipeline, and PipelineRun %s in namespace %s", prName, namespace) - pipeline := mustParsePipeline(t, ` + pipeline := parse.MustParsePipeline(t, ` metadata: name: pipeline-to-be-deleted spec: @@ -367,7 +365,7 @@ spec: t.Fatalf("Failed to create Task `%s`: %s", prName, err) } - pipelinerun := mustParsePipelineRun(t, ` + pipelinerun := parse.MustParsePipelineRun(t, ` metadata: name: pipelinerun-referencing-deleted spec: @@ -412,38 +410,41 @@ func TestPipelineRunPending(t *testing.T) { t.Logf("Creating Task, Pipeline, and Pending PipelineRun %s in namespace %s", prName, namespace) - if _, err := c.TaskClient.Create(ctx, &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: prName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo hello, world"}, - }}}, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - 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) } - if _, err := c.PipelineClient.Create(ctx, &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: prName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "task", - TaskRef: &v1beta1.TaskRef{Name: prName}, - }}, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineClient.Create(ctx, parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: task + taskRef: + name: %s +`, prName, namespace, prName)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", prName, err) } - pipelineRun, err := c.PipelineRunClient.Create(ctx, &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: prName, Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: prName}, - Status: v1beta1.PipelineRunSpecStatusPending, - }, - }, metav1.CreateOptions{}) + pipelineRun, err := c.PipelineRunClient.Create(ctx, parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + status: PipelineRunPending +`, prName, namespace, prName)), metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -476,167 +477,166 @@ func TestPipelineRunPending(t *testing.T) { } } -func getFanInFanOutTasks(namespace string) []*v1beta1.Task { - workspaceResource := v1beta1.TaskResource{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "workspace", - Type: resource.PipelineResourceTypeGit, - }} - return []*v1beta1.Task{{ - ObjectMeta: metav1.ObjectMeta{Name: "create-file", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "workspace", - Type: resource.PipelineResourceTypeGit, - TargetPath: "brandnewspace", - }}}, - Outputs: []v1beta1.TaskResource{workspaceResource}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "write-data-task-0-step-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo stuff > $(resources.outputs.workspace.path)/stuff"}, - }}, {Container: corev1.Container{ - Name: "write-data-task-0-step-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo other > $(resources.outputs.workspace.path)/other"}, - }}}, - }, - }, { - ObjectMeta: metav1.ObjectMeta{Name: "check-create-files-exists", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{workspaceResource}, - Outputs: []v1beta1.TaskResource{workspaceResource}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "read-from-task-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ stuff == $(cat $(inputs.resources.workspace.path)/stuff) ]]"}, - }}, {Container: corev1.Container{ - Name: "write-data-task-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo something > $(outputs.resources.workspace.path)/something"}, - }}}, - }, - }, { - ObjectMeta: metav1.ObjectMeta{Name: "check-create-files-exists-2", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{workspaceResource}, - Outputs: []v1beta1.TaskResource{workspaceResource}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "read-from-task-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ other == $(cat $(inputs.resources.workspace.path)/other) ]]"}, - }}, {Container: corev1.Container{ - Name: "write-data-task-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo else > $(outputs.resources.workspace.path)/else"}, - }}}, - }, - }, { - ObjectMeta: metav1.ObjectMeta{Name: "read-files", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "workspace", - Type: resource.PipelineResourceTypeGit, - TargetPath: "readingspace", - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "read-from-task-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ something == $(cat $(inputs.resources.workspace.path)/something) ]]"}, - }}, {Container: corev1.Container{ - Name: "read-from-task-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ else == $(cat $(inputs.resources.workspace.path)/else) ]]"}, - }}}, - }, - }} +func getFanInFanOutTasks(t *testing.T, namespace string) []*v1beta1.Task { + return []*v1beta1.Task{parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: create-file + namespace: %s +spec: + resources: + inputs: + - name: workspace + targetPath: brandnewspace + type: git + outputs: + - name: workspace + type: git + steps: + - args: ['-c', 'echo stuff > $(resources.outputs.workspace.path)/stuff'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-0-step-0 + - args: ['-c', 'echo other > $(resources.outputs.workspace.path)/other'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-0-step-1 +`, namespace)), + parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: check-create-files-exists + namespace: %s +spec: + resources: + inputs: + - name: workspace + type: git + outputs: + - name: workspace + type: git + steps: + - args: ['-c', '[[ stuff == $(cat $(inputs.resources.workspace.path)/stuff) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-0 + - args: ['-c', 'echo something > $(outputs.resources.workspace.path)/something'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-1 +`, namespace)), + parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: check-create-files-exists-2 + namespace: %s +spec: + resources: + inputs: + - name: workspace + type: git + outputs: + - name: workspace + type: git + steps: + - args: ['-c', '[[ other == $(cat $(inputs.resources.workspace.path)/other) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-0 + - args: ['-c', 'echo else > $(outputs.resources.workspace.path)/else'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-1 +`, namespace)), + parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: read-files + namespace: %s +spec: + resources: + inputs: + - name: workspace + type: git + targetPath: readingspace + steps: + - args: ['-c', '[[ something == $(cat $(inputs.resources.workspace.path)/something) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-0 + - args: ['-c', '[[ else == $(cat $(inputs.resources.workspace.path)/else) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-1 +`, namespace)), + } } -func getFanInFanOutPipeline(suffix int, namespace string) *v1beta1.Pipeline { - outGitResource := v1beta1.PipelineTaskOutputResource{ - Name: "workspace", - Resource: "git-repo", - } - return &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: getName(pipelineName, suffix), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "git-repo", Type: resource.PipelineResourceTypeGit, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "create-file-kritis", - TaskRef: &v1beta1.TaskRef{Name: "create-file"}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "workspace", Resource: "git-repo", - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{outGitResource}, - }, - }, { - Name: "create-fan-out-1", - TaskRef: &v1beta1.TaskRef{Name: "check-create-files-exists"}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "workspace", Resource: "git-repo", From: []string{"create-file-kritis"}, - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{outGitResource}, - }, - }, { - Name: "create-fan-out-2", - TaskRef: &v1beta1.TaskRef{Name: "check-create-files-exists-2"}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "workspace", Resource: "git-repo", From: []string{"create-file-kritis"}, - }}, - Outputs: []v1beta1.PipelineTaskOutputResource{outGitResource}, - }, - }, { - Name: "check-fan-in", - TaskRef: &v1beta1.TaskRef{Name: "read-files"}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "workspace", Resource: "git-repo", From: []string{"create-fan-out-2", "create-fan-out-1"}, - }}, - }, - }}, - }, - } +func getFanInFanOutPipeline(t *testing.T, suffix int, namespace string) *v1beta1.Pipeline { + return parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + resources: + - name: git-repo + type: git + tasks: + - name: create-file-kritis + resources: + inputs: + - name: workspace + resource: git-repo + outputs: + - name: workspace + resource: git-repo + taskRef: + name: create-file + - name: create-fan-out-1 + resources: + inputs: + - from: + - create-file-kritis + name: workspace + resource: git-repo + outputs: + - name: workspace + resource: git-repo + taskRef: + name: check-create-files-exists + - name: create-fan-out-2 + resources: + inputs: + - from: + - create-file-kritis + name: workspace + resource: git-repo + outputs: + - name: workspace + resource: git-repo + taskRef: + name: check-create-files-exists-2 + - name: check-fan-in + resources: + inputs: + - from: + - create-fan-out-2 + - create-fan-out-1 + name: workspace + resource: git-repo + taskRef: + name: read-files +`, getName(pipelineName, suffix), namespace)) } -func getFanInFanOutGitResources() []*v1alpha1.PipelineResource { - return []*v1alpha1.PipelineResource{{ - ObjectMeta: metav1.ObjectMeta{ - Name: "kritis-resource-git", - }, - Spec: resource.PipelineResourceSpec{ - Type: resource.PipelineResourceTypeGit, - Params: []resource.ResourceParam{ - { - Name: "Url", - Value: "https://github.com/grafeas/kritis", - }, - { - Name: "Revision", - Value: "master", - }, - }, - }, - }} +func getFanInFanOutGitResources(t *testing.T) []*v1alpha1.PipelineResource { + return []*v1alpha1.PipelineResource{parse.MustParsePipelineResource(t, ` +metadata: + name: kritis-resource-git +spec: + type: git + params: + - name: Url + value: https://github.com/grafeas/kritis + - name: Revision + value: master +`)} } func getPipelineRunServiceAccount(suffix int, namespace string) *corev1.ServiceAccount { @@ -650,17 +650,19 @@ func getPipelineRunServiceAccount(suffix int, namespace string) *corev1.ServiceA }}, } } -func getFanInFanOutPipelineRun(suffix int, namespace string) *v1beta1.PipelineRun { - return &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: getName(pipelineRunName, suffix), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: getName(pipelineName, suffix)}, - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "git-repo", - ResourceRef: &v1beta1.PipelineResourceRef{Name: "kritis-resource-git"}, - }}, - }, - } +func getFanInFanOutPipelineRun(t *testing.T, suffix int, namespace string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + resources: + - name: git-repo + resourceRef: + name: kritis-resource-git +`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) } func getPipelineRunSecret(suffix int, namespace string) *corev1.Secret { @@ -692,26 +694,23 @@ func getPipelineRunSecret(suffix int, namespace string) *corev1.Secret { } } -func getHelloWorldPipelineRun(suffix int, namespace string) *v1beta1.PipelineRun { - return &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineRunName, suffix), Namespace: namespace, - Labels: map[string]string{ - "hello-world-key": "hello-world-value", - }, - }, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: getName(pipelineName, suffix)}, - Params: []v1beta1.Param{{ - Name: "the.path", - Value: *v1beta1.NewArrayOrString("docker://gcr.io/build-crd-testing/secret-sauce"), - }, { - Name: "dest", - Value: *v1beta1.NewArrayOrString("dir:///tmp/"), - }}, - ServiceAccountName: fmt.Sprintf("%s%d", saName, suffix), - }, - } +func getHelloWorldPipelineRun(t *testing.T, suffix int, namespace string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + labels: + hello-world-key: hello-world-value + name: %s + namespace: %s +spec: + params: + - name: the.path + value: docker://gcr.io/build-crd-testing/secret-sauce + - name: dest + value: dir:///tmp/ + pipelineRef: + name: %s + serviceAccountName: %s%d +`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix), saName, suffix)) } func getName(namespace string, suffix int) string { @@ -873,23 +872,23 @@ func assertAnnotationsMatch(t *testing.T, expectedAnnotations, actualAnnotations } } -func getPipelineWithFailingCondition(suffix int, namespace string) *v1beta1.Pipeline { - return &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: getName(pipelineName, suffix), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: task1Name, - TaskRef: &v1beta1.TaskRef{Name: getName(taskName, suffix)}, - Conditions: []v1beta1.PipelineTaskCondition{{ - ConditionRef: cond1Name, - }}, - }, { - Name: "task2", - TaskRef: &v1beta1.TaskRef{Name: getName(taskName, suffix)}, - RunAfter: []string{task1Name}, - }}, - }, - } +func getPipelineWithFailingCondition(t *testing.T, suffix int, namespace string) *v1beta1.Pipeline { + return parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: %s + taskRef: + name: %s + conditions: + - conditionRef: %s + - name: task2 + taskRef: + name: %s + runAfter: ['%s'] +`, getName(pipelineName, suffix), namespace, task1Name, getName(taskName, suffix), cond1Name, getName(taskName, suffix), task1Name)) } func getFailingCondition() *v1alpha1.Condition { @@ -909,18 +908,17 @@ func getFailingCondition() *v1alpha1.Condition { } } -func getConditionalPipelineRun(suffix int, namespace string) *v1beta1.PipelineRun { - return &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineRunName, suffix), Namespace: namespace, - Labels: map[string]string{ - "hello-world-key": "hello-world-vaule", - }, - }, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: getName(pipelineName, suffix)}, - }, - } +func getConditionalPipelineRun(t *testing.T, suffix int, namespace string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s + labels: + hello-world-key: hello-world-vaule +spec: + pipelineRef: + name: %s +`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) } func getLimitRange(name, namespace, resourceCPU, resourceMemory, resourceEphemeralStorage string) *corev1.LimitRange { diff --git a/test/remote_test.go b/test/remote_test.go index cb8d88f59f8..2ffa1610774 100644 --- a/test/remote_test.go +++ b/test/remote_test.go @@ -23,15 +23,15 @@ import ( "net/url" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/ghodss/yaml" "github.com/google/go-cmp/cmp" "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/registry" "github.com/google/go-containerregistry/pkg/v1/remote" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" tkremote "github.com/tektoncd/pipeline/pkg/remote/oci" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestCreateImage(t *testing.T) { @@ -40,15 +40,10 @@ func TestCreateImage(t *testing.T) { defer s.Close() u, _ := url.Parse(s.URL) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-create-image", - }, - TypeMeta: metav1.TypeMeta{ - APIVersion: "tekton.dev/v1beta1", - Kind: "Task", - }, - } + task := parse.MustParseTask(t, ` +metadata: + name: test-create-image +`) ref, err := CreateImage(u.Host+"/task/test-create-image", task) if err != nil { diff --git a/test/retry_test.go b/test/retry_test.go index 8d1aba22c65..7bba265ea51 100644 --- a/test/retry_test.go +++ b/test/retry_test.go @@ -20,10 +20,12 @@ package test import ( "context" + "fmt" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -45,23 +47,19 @@ func TestTaskRunRetry(t *testing.T) { // configured to retry 5 times. pipelineRunName := "retry-pipeline" numRetries := 5 - if _, err := c.PipelineRunClient.Create(ctx, &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - PipelineSpec: &v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "retry-me", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "busybox"}, - Script: "exit 1", - }}, - }}, - Retries: numRetries, - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + tasks: + - name: retry-me + retries: %d + taskSpec: + steps: + - image: busybox + script: exit 1 +`, pipelineRunName, numRetries)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %v", pipelineRunName, err) } diff --git a/test/serviceaccount_test.go b/test/serviceaccount_test.go index 64d9a3d4215..9535b3677e6 100644 --- a/test/serviceaccount_test.go +++ b/test/serviceaccount_test.go @@ -23,8 +23,8 @@ import ( "fmt" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -113,60 +113,48 @@ func TestPipelineRunWithServiceAccounts(t *testing.T) { } // Create a Pipeline with multiple tasks - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: "pipelinewithsas", Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "task1", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - }, - Script: `echo task1`, - }}, - }}, - }, { - Name: "task2", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - }, - Script: `echo task2`, - }}, - }}, - }, { - Name: "task3", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - }, - Script: `echo task3`, - }}, - }}, - }}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: pipelinewithsas + namespace: %s +spec: + tasks: + - name: task1 + taskSpec: + steps: + - image: ubuntu + script: echo task1 + - name: task2 + taskSpec: + steps: + - image: ubuntu + script: echo task2 + - name: task3 + taskSpec: + steps: + - image: ubuntu + script: echo task3 +`, namespace)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) } // Create a PipelineRun that uses those ServiceAccount - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: "pipelinerunwithasas", Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: "pipelinewithsas"}, - ServiceAccountName: "sa1", - ServiceAccountNames: []v1beta1.PipelineRunSpecServiceAccountName{{ - TaskName: "task2", ServiceAccountName: "sa2", - }}, - TaskRunSpecs: []v1beta1.PipelineTaskRunSpec{{ - PipelineTaskName: "task3", TaskServiceAccountName: "sa3", - }}, - }, - } + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: pipelinerunwithasas + namespace: %s +spec: + pipelineRef: + name: pipelinewithsas + serviceAccountName: sa1 + serviceAccountNames: + - serviceAccountName: sa2 + taskName: task2 + taskRunSpecs: + - pipelineTaskName: task3 + taskServiceAccountName: sa3 +`, namespace)) _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}) if err != nil { @@ -243,41 +231,38 @@ func TestPipelineRunWithServiceAccountNameAndTaskRunSpec(t *testing.T) { } // Create a Pipeline with multiple tasks - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: "pipelinewithsas", Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "task1", - TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - }, - Script: `echo task1`, - }}, - }}, - }}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: pipelinewithsas + namespace: %s +spec: + tasks: + - name: task1 + taskSpec: + metadata: {} + steps: + - image: ubuntu + script: echo task1 +`, namespace)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) } dnsPolicy := corev1.DNSClusterFirst // Create a PipelineRun that uses those ServiceAccount - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: "pipelinerunwithasas", Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: "pipelinewithsas"}, - ServiceAccountName: "sa1", - TaskRunSpecs: []v1beta1.PipelineTaskRunSpec{{ - PipelineTaskName: "task1", - TaskPodTemplate: &pod.Template{ - DNSPolicy: &dnsPolicy, - }, - }}, - }, - } + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: pipelinerunwithasas + namespace: %s +spec: + pipelineRef: + name: pipelinewithsas + serviceAccountName: sa1 + taskRunSpecs: + - pipelineTaskName: task1 + taskPodTemplate: + dnsPolicy: %s +`, namespace, dnsPolicy)) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", pipelineRun.Name, err) diff --git a/test/sidecar_test.go b/test/sidecar_test.go index 0b5d6f69d85..7d416ce9aba 100644 --- a/test/sidecar_test.go +++ b/test/sidecar_test.go @@ -21,10 +21,11 @@ package test import ( "context" "fmt" + "strings" "testing" - "time" - v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -68,25 +69,30 @@ func TestSidecarTaskSupport(t *testing.T) { t.Run(test.desc, func(t *testing.T) { sidecarTaskName := fmt.Sprintf("%s-%d", sidecarTaskName, i) sidecarTaskRunName := fmt.Sprintf("%s-%d", sidecarTaskRunName, i) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: sidecarTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Name: primaryContainerName, Image: "busybox", Command: test.stepCommand}, - }}, - Sidecars: []v1beta1.Sidecar{{ - Container: corev1.Container{Name: sidecarContainerName, Image: "busybox", Command: test.sidecarCommand}, - }}, - }, - } - - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: sidecarTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: sidecarTaskName}, - Timeout: &metav1.Duration{Duration: 1 * time.Minute}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - name: %s + image: busybox + command: [%s] + sidecars: + - name: %s + image: busybox + command: [%s] +`, sidecarTaskName, namespace, primaryContainerName, stringSliceToYAMLArray(test.stepCommand), sidecarContainerName, stringSliceToYAMLArray(test.sidecarCommand))) + + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + timeout: 1m +`, sidecarTaskRunName, namespace, sidecarTaskName)) t.Logf("Creating Task %q", sidecarTaskName) if _, err := clients.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { @@ -170,3 +176,11 @@ func TestSidecarTaskSupport(t *testing.T) { }) } } + +func stringSliceToYAMLArray(stringSlice []string) string { + var quoted []string + for _, s := range stringSlice { + quoted = append(quoted, fmt.Sprintf("'%s'", s)) + } + return strings.Join(quoted, ", ") +} diff --git a/test/start_time_test.go b/test/start_time_test.go index baecd57e1a4..2e0ed008e5b 100644 --- a/test/start_time_test.go +++ b/test/start_time_test.go @@ -16,11 +16,12 @@ package test import ( "context" + "fmt" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -40,32 +41,24 @@ func TestStartTime(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) t.Logf("Creating TaskRun in namespace %q", namespace) - tr, err := c.TaskRunClient.Create(ctx, &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - GenerateName: "start-time-test-", - Namespace: namespace, - }, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }}, - }, - }, - }, metav1.CreateOptions{}) + tr, err := c.TaskRunClient.Create(ctx, parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + generateName: start-time-test- + namespace: %s +spec: + taskSpec: + steps: + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 +`, namespace)), metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating TaskRun: %v", err) } diff --git a/test/status_test.go b/test/status_test.go index 39057060090..3a65fdace8b 100644 --- a/test/status_test.go +++ b/test/status_test.go @@ -22,6 +22,8 @@ import ( "context" "testing" + "github.com/tektoncd/pipeline/test/parse" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -40,7 +42,7 @@ func TestTaskRunPipelineRunStatus(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := mustParseTask(t, ` + task := parse.MustParseTask(t, ` metadata: name: banana spec: @@ -51,7 +53,7 @@ spec: if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := mustParseTaskRun(t, ` + taskRun := parse.MustParseTaskRun(t, ` metadata: name: apple spec: @@ -67,7 +69,7 @@ spec: t.Errorf("Error waiting for TaskRun to finish: %s", err) } - pipeline := mustParsePipeline(t, ` + pipeline := parse.MustParsePipeline(t, ` metadata: name: tomatoes spec: @@ -78,7 +80,7 @@ spec: if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", "tomatoes", err) } - pipelineRun := mustParsePipelineRun(t, ` + pipelineRun := parse.MustParsePipelineRun(t, ` metadata: name: pear spec: diff --git a/test/taskrun_test.go b/test/taskrun_test.go index 42046970f57..2ca57fa2072 100644 --- a/test/taskrun_test.go +++ b/test/taskrun_test.go @@ -20,10 +20,13 @@ package test import ( "context" + "fmt" "regexp" "strings" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" @@ -47,33 +50,33 @@ func TestTaskRunFailure(t *testing.T) { taskRunName := "failing-taskrun" t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: "failing-task", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "echo hello"}, - }}, {Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "exit 1"}, - }}, {Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 30s"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: failing-task + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'echo hello'] + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'exit 1'] + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 30s'] +`, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: "failing-task"}, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: failing-task +`, taskRunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -145,26 +148,27 @@ func TestTaskRunStatus(t *testing.T) { fqImageName := getTestImage(busyboxImage) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: "status-task", Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - // This was the digest of the latest tag as of 8/12/2019 - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: fqImageName, - Command: []string{"/bin/sh"}, - Args: []string{"-c", "echo hello"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: status-task + namespace: %s +spec: + steps: + - image: %s + command: ['/bin/sh'] + args: ['-c', 'echo hello'] +`, namespace, fqImageName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: "status-task"}, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: status-task +`, taskRunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/tektonbundles_test.go b/test/tektonbundles_test.go index 596d1cf142f..938ddc90543 100644 --- a/test/tektonbundles_test.go +++ b/test/tektonbundles_test.go @@ -30,6 +30,8 @@ import ( "strings" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/ghodss/yaml" "github.com/google/go-containerregistry/pkg/name" v1 "github.com/google/go-containerregistry/pkg/v1" @@ -37,7 +39,6 @@ import ( "github.com/google/go-containerregistry/pkg/v1/layout" "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/google/go-containerregistry/pkg/v1/tarball" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/pod" "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun" corev1 "k8s.io/api/core/v1" @@ -72,30 +73,28 @@ func TestTektonBundlesSimpleWorkingExample(t *testing.T) { t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err) } - task := &v1beta1.Task{ - TypeMeta: metav1.TypeMeta{Kind: "Task", APIVersion: "tekton.dev/v1beta1"}, - ObjectMeta: metav1.ObjectMeta{Name: taskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Name: "hello", Image: "alpine"}, - Script: "echo Hello", - }}, - }, - } - - pipeline := &v1beta1.Pipeline{ - TypeMeta: metav1.TypeMeta{Kind: "Pipeline", APIVersion: "tekton.dev/v1beta1"}, - ObjectMeta: metav1.ObjectMeta{Name: pipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "hello-world", - TaskRef: &v1beta1.TaskRef{ - Name: taskName, - Bundle: repo, - }, - }}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - name: hello + image: alpine + script: 'echo Hello' +`, taskName, namespace)) + + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: hello-world + taskRef: + name: %s + bundle: %s +`, pipelineName, namespace, taskName, repo)) // Write the task and pipeline into an image to the registry in the proper format. rawTask, err := yaml.Marshal(task) @@ -140,14 +139,14 @@ func TestTektonBundlesSimpleWorkingExample(t *testing.T) { publishImg(ctx, t, c, namespace, img, ref) // Now generate a PipelineRun to invoke this pipeline and task. - pr := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{ - Name: pipelineName, - Bundle: repo, - }, - }} + pr := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s + bundle: %s +`, pipelineRunName, pipelineName, repo)) if _, err := c.PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun: %s", err) } @@ -212,19 +211,17 @@ func TestTektonBundlesUsingRegularImage(t *testing.T) { t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err) } - pipeline := &v1beta1.Pipeline{ - TypeMeta: metav1.TypeMeta{Kind: "Pipeline", APIVersion: "tekton.dev/v1beta1"}, - ObjectMeta: metav1.ObjectMeta{Name: pipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "hello-world", - TaskRef: &v1beta1.TaskRef{ - Name: taskName, - Bundle: "registry", - }, - }}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: hello-world + taskRef: + name: %s + bundle: registry +`, pipelineName, namespace, taskName)) // Write the pipeline into an image to the registry in the proper format. We don't write the task because we are // using an non Tekton Bundle. @@ -254,14 +251,14 @@ func TestTektonBundlesUsingRegularImage(t *testing.T) { publishImg(ctx, t, c, namespace, img, ref) // Now generate a PipelineRun to invoke this pipeline and task. - pr := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{ - Name: pipelineName, - Bundle: repo, - }, - }} + pr := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s + bundle: %s +`, pipelineRunName, pipelineName, repo)) if _, err := c.PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun: %s", err) } @@ -297,30 +294,28 @@ func TestTektonBundlesUsingImproperFormat(t *testing.T) { t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err) } - task := &v1beta1.Task{ - TypeMeta: metav1.TypeMeta{Kind: "Task", APIVersion: "tekton.dev/v1beta1"}, - ObjectMeta: metav1.ObjectMeta{Name: taskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Name: "hello", Image: "alpine"}, - Script: "echo Hello", - }}, - }, - } - - pipeline := &v1beta1.Pipeline{ - TypeMeta: metav1.TypeMeta{Kind: "Pipeline", APIVersion: "tekton.dev/v1beta1"}, - ObjectMeta: metav1.ObjectMeta{Name: pipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "hello-world", - TaskRef: &v1beta1.TaskRef{ - Name: taskName, - Bundle: repo, - }, - }}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - name: hello + image: alpine + script: 'echo Hello' +`, taskName, namespace)) + + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: hello-world + taskRef: + name: %s + bundle: %s +`, pipelineName, namespace, taskName, repo)) // Write the pipeline into an image to the registry in the proper format. Write the task using incorrect // annotations. @@ -367,14 +362,14 @@ func TestTektonBundlesUsingImproperFormat(t *testing.T) { publishImg(ctx, t, c, namespace, img, ref) // Now generate a PipelineRun to invoke this pipeline and task. - pr := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{ - Name: pipelineName, - Bundle: repo, - }, - }} + pr := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s + bundle: %s +`, pipelineRunName, pipelineName, repo)) if _, err := c.PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun: %s", err) } diff --git a/test/timeout_test.go b/test/timeout_test.go index 0a08cb82376..a9715421f1b 100644 --- a/test/timeout_test.go +++ b/test/timeout_test.go @@ -25,6 +25,8 @@ import ( "testing" "time" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" corev1 "k8s.io/api/core/v1" @@ -48,36 +50,39 @@ func TestPipelineRunTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Task in namespace %s", namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 10"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 10'] +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "foo", - TaskRef: &v1beta1.TaskRef{Name: task.Name}, - }}, - }, - } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: pipeline.Name}, - Timeout: &metav1.Duration{Duration: 5 * time.Second}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: foo + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, task.Name)) + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + timeout: 5s +`, helpers.ObjectNameForTest(t), namespace, pipeline.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) } @@ -141,21 +146,24 @@ func TestPipelineRunTimeout(t *testing.T) { // Verify that we can create a second Pipeline using the same Task without a Pipeline-level timeout that will not // time out - secondPipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "foo", - TaskRef: &v1beta1.TaskRef{Name: task.Name}, - }}, - }, - } - secondPipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: secondPipeline.Name}, - }, - } + secondPipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: foo + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, task.Name)) + secondPipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, secondPipeline.Name)) if _, err := c.PipelineClient.Create(ctx, secondPipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", secondPipeline.Name, err) } @@ -181,34 +189,25 @@ func TestStepTimeout(t *testing.T) { t.Logf("Creating Task with Step step-no-timeout, Step step-timeout, and Step step-canceled in namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Name: "no-timeout", - Image: "busybox", - }, - Script: "sleep 1", - Timeout: &metav1.Duration{Duration: 2 * time.Second}, - }, { - Container: corev1.Container{ - Name: "timeout", - Image: "busybox", - }, - Script: "sleep 1", - Timeout: &metav1.Duration{Duration: time.Millisecond}, - }, { - Container: corev1.Container{ - Name: "canceled", - Image: "busybox", - }, - Script: "sleep 1", - }}, - }, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskSpec: + steps: + - name: no-timeout + image: busybox + script: sleep 1 + timeout: 2s + - name: timeout + image: busybox + script: sleep 1 + timeout: 1ms + - name: canceled + image: busybox + script: sleep 1 +`, helpers.ObjectNameForTest(t), namespace)) t.Logf("Creating TaskRun %s in namespace %s", taskRun.Name, namespace) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", taskRun.Name, err) @@ -248,7 +247,7 @@ func TestStepTimeoutWithWS(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(context.Background(), t, c, namespace) }, t.Logf) defer tearDown(context.Background(), t, c, namespace) - taskRun := mustParseTaskRun(t, ` + taskRun := parse.MustParseTaskRun(t, ` metadata: name: taskrun-with-timeout-step spec: @@ -289,28 +288,28 @@ func TestTaskRunTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 3000"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 3000'] +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: task.Name}, - // Do not reduce this timeout. Taskrun e2e test is also verifying - // if reconcile is triggered from timeout handler and not by pod informers - Timeout: &metav1.Duration{Duration: timeout}, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + timeout: %s +`, helpers.ObjectNameForTest(t), namespace, task.Name, timeout)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", taskRun.Name, err) } @@ -345,26 +344,26 @@ func TestPipelineTaskTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Tasks in namespace %s", namespace) - task1 := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"sleep"}, - Args: []string{"1s"}, - }}}, - }, - } - task2 := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"sleep"}, - Args: []string{"10s"}, - }}}, - }, - } + task1 := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 1s'] +`, helpers.ObjectNameForTest(t), namespace)) + task2 := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 10s'] +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task1, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task1.Name, err) @@ -373,26 +372,29 @@ func TestPipelineTaskTimeout(t *testing.T) { t.Fatalf("Failed to create Task `%s`: %s", task2.Name, err) } - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "pipelinetask1", - TaskRef: &v1beta1.TaskRef{Name: task1.Name}, - Timeout: &metav1.Duration{Duration: 60 * time.Second}, - }, { - Name: "pipelinetask2", - TaskRef: &v1beta1.TaskRef{Name: task2.Name}, - Timeout: &metav1.Duration{Duration: 5 * time.Second}, - }}, - }, - } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: pipeline.Name}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: pipelinetask1 + taskRef: + name: %s + timeout: 60s + - name: pipelinetask2 + taskRef: + name: %s + timeout: 5s +`, helpers.ObjectNameForTest(t), namespace, task1.Name, task2.Name)) + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, pipeline.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) @@ -489,59 +491,61 @@ func TestPipelineRunTasksTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Task in namespace %s", namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 30"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 30'] +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } t.Logf("Creating Finally Task in namespace %s", namespace) - fTask := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 1"}, - }}}, - }, - } + fTask := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 1'] +`, helpers.ObjectNameForTest(t), namespace)) if _, err := c.TaskClient.Create(ctx, fTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", fTask.Name, err) } - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Tasks: []v1beta1.PipelineTask{{ - Name: "dagtask", - TaskRef: &v1beta1.TaskRef{Name: task.Name}, - }}, - Finally: []v1beta1.PipelineTask{{ - Name: "finallytask", - TaskRef: &v1beta1.TaskRef{Name: fTask.Name}, - }}, - }, - } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: helpers.ObjectNameForTest(t), Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: pipeline.Name}, - Timeouts: &v1beta1.TimeoutFields{ - Pipeline: &metav1.Duration{Duration: 60 * time.Second}, - Tasks: &metav1.Duration{Duration: 20 * time.Second}, - Finally: &metav1.Duration{Duration: 20 * time.Second}, - }, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + tasks: + - name: dagtask + taskRef: + name: %s + finally: + - name: finallytask + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), namespace, task.Name, fTask.Name)) + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + timeouts: + pipeline: 60s + tasks: 20s + finally: 20s +`, helpers.ObjectNameForTest(t), namespace, pipeline.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) } diff --git a/test/v1alpha1/artifact_bucket_test.go b/test/v1alpha1/artifact_bucket_test.go index 87388f576e0..3ddccbd940f 100644 --- a/test/v1alpha1/artifact_bucket_test.go +++ b/test/v1alpha1/artifact_bucket_test.go @@ -26,10 +26,9 @@ import ( "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/config" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -73,55 +72,39 @@ func TestStorageBucketPipelineRun(t *testing.T) { defer deleteBucketSecret(ctx, c, t, namespace) t.Logf("Creating GCS bucket %s", bucketName) - createbuckettask := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: "createbuckettask", - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Volumes: []corev1.Volume{{ - Name: "bucket-secret-volume", - VolumeSource: corev1.VolumeSource{ - Secret: &corev1.SecretVolumeSource{ - SecretName: bucketSecretName, - }, - }, - }}, - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine", - Name: "step1", - Command: []string{"/bin/bash"}, - Args: []string{"-c", fmt.Sprintf("gcloud auth activate-service-account --key-file /var/secret/bucket-secret/bucket-secret-key && gsutil mb gs://%s", bucketName)}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "bucket-secret-volume", - MountPath: fmt.Sprintf("/var/secret/%s", bucketSecretName), - }}, - Env: []corev1.EnvVar{{ - Name: "CREDENTIALS", - Value: fmt.Sprintf("/var/secret/%s/%s", bucketSecretName, bucketSecretKey), - }}, - }, - }}, - }, - }, - } + createbuckettask := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: createbuckettask +spec: + steps: + - name: step1 + image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine + command: ['/bin/bash'] + args: ['-c', 'gcloud auth activate-service-account --key-file /var/secret/bucket-secret/bucket-secret-key && gsutil mb gs://%s'] + volumeMounts: + - name: bucket-secret-volume + mountPath: /var/secret/%s + env: + - name: CREDENTIALS + value: /var/secret/%s/%s + volumes: + - name: bucket-secret-volume + secret: + secretName: %s +`, bucketName, bucketSecretName, bucketSecretName, bucketSecretKey, bucketSecretName)) t.Logf("Creating Task %s", "createbuckettask") if _, err := c.TaskClient.Create(ctx, createbuckettask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", "createbuckettask", err) } - createbuckettaskrun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: "createbuckettaskrun", - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: "createbuckettask", - }, - }, - } + createbuckettaskrun := parse.MustParseAlphaTaskRun(t, ` +metadata: + name: createbuckettaskrun +spec: + taskRef: + name: createbuckettask +`) t.Logf("Creating TaskRun %s", "createbuckettaskrun") if _, err := c.TaskRunClient.Create(ctx, createbuckettaskrun, metav1.CreateOptions{}); err != nil { @@ -152,171 +135,111 @@ func TestStorageBucketPipelineRun(t *testing.T) { defer resetConfigMap(ctx, t, c, systemNamespace, config.GetArtifactBucketConfigName(), originalConfigMapData) t.Logf("Creating Git PipelineResource %s", helloworldResourceName) - helloworldResource := &v1alpha1.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: helloworldResourceName, - }, - Spec: v1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeGit, - Params: []v1alpha1.ResourceParam{ - { - Name: "Url", - Value: "https://github.com/pivotal-nader-ziada/gohelloworld", - }, - { - Name: "Revision", - Value: "master", - }, - }, - }, - } + helloworldResource := parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + params: + - name: Url + value: https://github.com/pivotal-nader-ziada/gohelloworld + - name: Revision + value: master + type: git +`, helloworldResourceName)) if _, err := c.PipelineResourceClient.Create(ctx, helloworldResource, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", helloworldResourceName, err) } t.Logf("Creating Task %s", addFileTaskName) - addFileTask := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: addFileTaskName, - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: helloworldResourceName, - Type: v1alpha1.PipelineResourceTypeGit, - }, - }}, - }, - Outputs: &v1alpha1.Outputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: helloworldResourceName, - Type: v1alpha1.PipelineResourceTypeGit, - }, - }}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Image: "ubuntu", - Name: "addfile", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "'#!/bin/bash\necho hello' > /workspace/helloworldgit/newfile"}, - }, - }, - { - Container: corev1.Container{ - Image: "ubuntu", - Name: "make-executable", - Command: []string{"chmod"}, - Args: []string{"+x", "/workspace/helloworldgit/newfile"}, - }, - }, - }, - }, - }, - } + addFileTask := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + inputs: + resources: + - name: %s + type: git + outputs: + resources: + - name: %s + type: git + steps: + - image: ubuntu + name: addfile + script: |- + echo '#!/bin/bash + echo hello' > /workspace/helloworldgit/newfile + - image: ubuntu + name: make-executable + script: chmod +x /workspace/helloworldgit/newfile +`, addFileTaskName, helloworldResourceName, helloworldResourceName)) if _, err := c.TaskClient.Create(ctx, addFileTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", addFileTaskName, err) } t.Logf("Creating Task %s", runFileTaskName) - readFileTask := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: runFileTaskName, - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: helloworldResourceName, - Type: v1alpha1.PipelineResourceTypeGit, - }, - }}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Image: "runfile", - Name: "addfile", - Command: []string{"/workspace/helloworld/newfile"}, - }, - }, - }, - }, - }, - } + readFileTask := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + resources: + inputs: + - name: %s + type: git + steps: + - command: ['/workspace/hellowrld/newfile'] + image: ubuntu + name: runfile +`, runFileTaskName, helloworldResourceName)) if _, err := c.TaskClient.Create(ctx, readFileTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", runFileTaskName, err) } t.Logf("Creating Pipeline %s", bucketTestPipelineName) - bucketTestPipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: bucketTestPipelineName, - }, - Spec: v1alpha1.PipelineSpec{ - Resources: []v1alpha1.PipelineDeclaredResource{{ - Name: "source-repo", - Type: "git", - }}, - Tasks: []v1alpha1.PipelineTask{ - { - TaskRef: &v1alpha1.TaskRef{ - Name: addFileTaskName, - }, - Name: "addfile", - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "helloworldgit", - Resource: "source-repo", - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{{ - Name: "helloworldgit", - Resource: "source-repo", - }}, - }, - }, - { - TaskRef: &v1alpha1.TaskRef{ - Name: runFileTaskName, - }, - Name: "runfile", - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "helloworldgit", - Resource: "source-repo", - From: []string{"addfile"}, - }}, - }, - }, - }, - }, - } + bucketTestPipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + resources: + - name: source-repo + type: git + tasks: + - name: addfile + resources: + inputs: + - name: helloworldgit + resource: source-repo + outputs: + - name: helloworldgit + resource: source-repo + taskRef: + name: %s + - name: runfile + resources: + inputs: + - name: helloworldgit + resource: source-repo + from: + - addfile + taskRef: + name: %s +`, bucketTestPipelineName, addFileTaskName, runFileTaskName)) if _, err := c.PipelineClient.Create(ctx, bucketTestPipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", bucketTestPipelineName, err) } t.Logf("Creating PipelineRun %s", bucketTestPipelineRunName) - bucketTestPipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: bucketTestPipelineRunName, - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: bucketTestPipelineRunName, - }, - Resources: []v1alpha1.PipelineResourceBinding{{ - Name: "source-repo", - ResourceRef: &v1alpha1.PipelineResourceRef{ - Name: helloworldResourceName, - }, - }}, - }, - } + bucketTestPipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s + resources: + - name: source-repo + resourceRef: + name: %s +`, bucketTestPipelineRunName, bucketTestPipelineName, helloworldResourceName)) if _, err := c.PipelineRunClient.Create(ctx, bucketTestPipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", bucketTestPipelineRunName, err) } @@ -378,39 +301,28 @@ func resetConfigMap(ctx context.Context, t *testing.T, c *clients, namespace, co } func runTaskToDeleteBucket(ctx context.Context, c *clients, t *testing.T, namespace, bucketName, bucketSecretName, bucketSecretKey string) { - deletelbuckettask := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: "deletelbuckettask", - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Volumes: []corev1.Volume{{ - Name: "bucket-secret-volume", - VolumeSource: corev1.VolumeSource{ - Secret: &corev1.SecretVolumeSource{ - SecretName: bucketSecretName, - }, - }, - }}, - }, - }, - } + deletelbuckettask := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: deletelbuckettask +spec: + volumes: + - name: bucket-secret-volume + secret: + secretName: %s +`, bucketSecretName)) t.Logf("Creating Task %s", "deletelbuckettask") if _, err := c.TaskClient.Create(ctx, deletelbuckettask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", "deletelbuckettask", err) } - deletelbuckettaskrun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: "deletelbuckettaskrun", - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: "deletelbuckettask", - }, - }, - } + deletelbuckettaskrun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: deletelbuckettaskrun +spec: + taskRef: + name: deletelbuckettask +`)) t.Logf("Creating TaskRun %s", "deletelbuckettaskrun") if _, err := c.TaskRunClient.Create(ctx, deletelbuckettaskrun, metav1.CreateOptions{}); err != nil { diff --git a/test/v1alpha1/cancel_test.go b/test/v1alpha1/cancel_test.go index 00b9bb03a81..50e420d0a77 100644 --- a/test/v1alpha1/cancel_test.go +++ b/test/v1alpha1/cancel_test.go @@ -26,10 +26,10 @@ import ( "sync" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "gomodules.xyz/jsonpatch/v2" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" knativetest "knative.dev/pkg/test" @@ -53,25 +53,19 @@ func TestTaskRunPipelineRunCancel(t *testing.T) { defer tearDown(ctx, t, c, namespace) pipelineRunName := "cancel-me" - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName, Namespace: namespace}, - Spec: v1alpha1.PipelineRunSpec{ - PipelineSpec: &v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - Name: "task", - Retries: numRetries, - TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "busybox", - }, - Script: "sleep 5000", - }}, - }}, - }}, - }, - }, - } + pipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + tasks: + - name: task + retries: %d + taskSpec: + steps: + - image: busybox + script: 'sleep 5000' +`, pipelineRunName, numRetries)) t.Logf("Creating PipelineRun in namespace %s", namespace) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { diff --git a/test/v1alpha1/cluster_resource_test.go b/test/v1alpha1/cluster_resource_test.go index fcd049f8907..68740fdab40 100644 --- a/test/v1alpha1/cluster_resource_test.go +++ b/test/v1alpha1/cluster_resource_test.go @@ -20,9 +20,11 @@ package test import ( "context" + "fmt" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" corev1 "k8s.io/api/core/v1" @@ -57,17 +59,17 @@ func TestClusterResource(t *testing.T) { } t.Logf("Creating cluster PipelineResource %s", resourceName) - if _, err := c.PipelineResourceClient.Create(ctx, getClusterResource(namespace, resourceName, secretName), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getClusterResource(t, namespace, resourceName, secretName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create cluster Pipeline Resource `%s`: %s", resourceName, err) } t.Logf("Creating Task %s", taskName) - if _, err := c.TaskClient.Create(ctx, getClusterResourceTask(taskName, configName), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getClusterResourceTask(t, taskName, configName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", taskName, err) } t.Logf("Creating TaskRun %s", taskRunName) - if _, err := c.TaskRunClient.Create(ctx, getClusterResourceTaskRun(namespace, taskRunName, taskName, resourceName), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, getClusterResourceTaskRun(t, namespace, taskRunName, taskName, resourceName), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Taskrun `%s`: %s", taskRunName, err) } @@ -77,46 +79,30 @@ func TestClusterResource(t *testing.T) { } } -func getClusterResource(namespace, name, sname string) *v1alpha1.PipelineResource { - return &v1alpha1.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: v1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeCluster, - Params: []v1alpha1.ResourceParam{ - { - Name: "Name", - Value: "helloworld-cluster", - }, - { - Name: "Url", - Value: "https://1.1.1.1", - }, - { - Name: "username", - Value: "test-user", - }, - { - Name: "password", - Value: "test-password", - }, - }, - SecretParams: []v1alpha1.SecretParam{ - { - FieldName: "cadata", - SecretKey: "cadatakey", - SecretName: sname, - }, - { - FieldName: "token", - SecretKey: "tokenkey", - SecretName: sname, - }, - }, - }, - } +func getClusterResource(t *testing.T, namespace, name, sname string) *resourcev1alpha1.PipelineResource { + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + type: cluster + params: + - name: Name + value: helloworld-cluster + - name: Url + value: https://1.1.1.1 + - name: username + value: test-user + - name: password + value: test-password + secrets: + - fieldName: cadata + secretKey: cadatakey + secretName: %s + - fieldName: token + secretKey: tokenkey + secretName: %s +`, name, namespace, sname, sname)) } func getClusterResourceTaskSecret(namespace, name string) *corev1.Secret { @@ -132,92 +118,55 @@ func getClusterResourceTaskSecret(namespace, name string) *corev1.Secret { } } -func getClusterResourceTask(name, configName string) *v1alpha1.Task { - return &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "target-cluster", - Type: v1alpha1.PipelineResourceTypeCluster, - }, - }}, - }, - TaskSpec: v1beta1.TaskSpec{ - Volumes: []corev1.Volume{{ - Name: "config-vol", - VolumeSource: corev1.VolumeSource{ - ConfigMap: &corev1.ConfigMapVolumeSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: configName, - }, - }, - }, - }}, - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Image: "ubuntu", - Name: "check-file-existence", - Command: []string{"cat"}, - Args: []string{"/workspace/target-cluster/kubeconfig"}, - }, - }, - { - Container: corev1.Container{ - Image: "ubuntu", - Name: "check-config-data", - Command: []string{"cat"}, - Args: []string{"/config/test.data"}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "config-vol", - MountPath: "/config", - }}, - }, - }, - { - Container: corev1.Container{ - Image: "ubuntu", - Name: "check-contents", - Command: []string{"bash"}, - Args: []string{"-c", "cmp -b /workspace/target-cluster/kubeconfig /config/test.data"}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "config-vol", - MountPath: "/config", - }}, - }, - }, - }, - }, - }, - } +func getClusterResourceTask(t *testing.T, name, configName string) *v1alpha1.Task { + return parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + inputs: + resources: + - name: target-cluster + type: cluster + volumes: + - name: config-vol + configMap: + name: %s + steps: + - name: check-file-existence + image: ubuntu + command: ['cat'] + args: ['/workspace/target-cluster/kubeconfig'] + - name: check-config-data + image: ubuntu + command: ['cat'] + args: ['/config/test.data'] + volumeMounts: + - name: config-vol + mountPath: /config + - name: check-contents + image: ubuntu + command: ['bash'] + args: ['-c', 'cmp -b /workspace/target-cluster/kubeconfig /config/test.data'] + volumeMounts: + - name: config-vol + mountPath: /config +`, name, configName)) } -func getClusterResourceTaskRun(namespace, name, taskName, resName string) *v1alpha1.TaskRun { - return &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: taskName, - }, - Inputs: &v1alpha1.TaskRunInputs{ - Resources: []v1alpha1.TaskResourceBinding{{ - PipelineResourceBinding: v1alpha1.PipelineResourceBinding{ - Name: "target-cluster", - ResourceRef: &v1alpha1.PipelineResourceRef{ - Name: resName, - }, - }, - }}, - }, - }, - } +func getClusterResourceTaskRun(t *testing.T, namespace, name, taskName, resName string) *v1alpha1.TaskRun { + return parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + inputs: + resources: + - name: target-cluster + resourceRef: + name: %s +`, name, namespace, taskName, resName)) } func getClusterConfigMap(namespace, name string) *corev1.ConfigMap { diff --git a/test/v1alpha1/dag_test.go b/test/v1alpha1/dag_test.go index 134bacfd9a8..f4d3d5a64af 100644 --- a/test/v1alpha1/dag_test.go +++ b/test/v1alpha1/dag_test.go @@ -20,17 +20,16 @@ package test import ( "context" + "fmt" "math" "sort" "strings" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" + "github.com/tektoncd/pipeline/test/parse" + clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/typed/pipeline/v1alpha1" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -57,146 +56,147 @@ func TestDAGPipelineRun(t *testing.T) { defer tearDown(ctx, t, c, namespace) // Create the Task that echoes text - repoTaskResource := v1alpha1.TaskResource{ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "repo", Type: resource.PipelineResourceTypeGit, - }} - echoTask := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: "echo-task", Namespace: namespace}, - Spec: v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1alpha1.TaskResource{repoTaskResource}, - Outputs: []v1alpha1.TaskResource{repoTaskResource}, - }, - Params: []v1alpha1.ParamSpec{{ - Name: "text", Type: v1alpha1.ParamTypeString, - Description: "The text that should be echoed", - }}, - Steps: []v1alpha1.Step{{ - Container: corev1.Container{Image: "busybox"}, - Script: "echo $(params.text)", - }, { - Container: corev1.Container{Image: "busybox"}, - Script: "ln -s $(resources.inputs.repo.path) $(resources.outputs.repo.path)", - }}, - }}, - } + echoTask := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: echo-task + namespace: %s +spec: + resources: + inputs: + - name: repo + type: git + outputs: + - name: repo + type: git + params: + - name: text + type: string + description: 'The text that should be echoed' + steps: + - image: busybox + script: 'echo $(params["text"])' + - image: busybox + script: 'ln -s $(resources.inputs.repo.path) $(resources.outputs.repo.path)' +`, namespace)) if _, err := c.TaskClient.Create(ctx, echoTask, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create echo Task: %s", err) } // Create the repo PipelineResource (doesn't really matter which repo we use) - repoResource := &resource.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{Name: "repo"}, - Spec: resource.PipelineResourceSpec{ - Type: resource.PipelineResourceTypeGit, - Params: []resource.ResourceParam{{ - Name: "Url", - Value: "https://github.com/githubtraining/example-basic", - }}, - }, - } + repoResource := parse.MustParsePipelineResource(t, ` +metadata: + name: repo +spec: + type: git + params: + - name: Url + value: https://github.com/githubtraining/example-basic +`) if _, err := c.PipelineResourceClient.Create(ctx, repoResource, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create simple repo PipelineResource: %s", err) } // Intentionally declaring Tasks in a mixed up order to ensure the order // of execution isn't at all dependent on the order they are declared in - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: "dag-pipeline", Namespace: namespace}, - Spec: v1alpha1.PipelineSpec{ - Resources: []v1alpha1.PipelineDeclaredResource{{ - Name: "repo", Type: resource.PipelineResourceTypeGit, - }}, - Tasks: []v1alpha1.PipelineTask{{ - Name: "pipeline-task-3", - TaskRef: &v1alpha1.TaskRef{Name: "echo-task"}, - Params: []v1alpha1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("wow"), - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - From: []string{"pipeline-task-2-parallel-1", "pipeline-task-2-parallel-2"}, - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }, { - Name: "pipeline-task-2-parallel-2", - TaskRef: &v1alpha1.TaskRef{Name: "echo-task"}, - Params: []v1alpha1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("such parallel"), - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - From: []string{"pipeline-task-1"}, - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }, { - Name: "pipeline-task-4", - TaskRef: &v1alpha1.TaskRef{Name: "echo-task"}, - Params: []v1alpha1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("very cloud native"), - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - RunAfter: []string{"pipeline-task-3"}, - }, { - Name: "pipeline-task-2-parallel-1", - TaskRef: &v1alpha1.TaskRef{Name: "echo-task"}, - Params: []v1alpha1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("much graph"), - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - From: []string{"pipeline-task-1"}, - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }, { - Name: "pipeline-task-1", - TaskRef: &v1alpha1.TaskRef{Name: "echo-task"}, - Params: []v1alpha1.Param{{ - Name: "text", Value: *v1beta1.NewArrayOrString("how to ci/cd?"), - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "repo", Resource: "repo", - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{{ - Name: "repo", Resource: "repo", - }}, - }, - }}, - }, - } + pipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: dag-pipeline + namespace: %s +spec: + resources: + - name: repo + type: git + tasks: + - name: pipeline-task-3 + params: + - name: text + value: wow + resources: + inputs: + - from: + - pipeline-task-2-parallel-1 + - pipeline-task-2-parallel-2 + name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task + - name: pipeline-task-2-parallel-2 + params: + - name: text + value: such parallel + resources: + inputs: + - from: + - pipeline-task-1 + name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task + - name: pipeline-task-4 + params: + - name: text + value: very cloud native + resources: + inputs: + - name: repo + resource: repo + outputs: + - name: repo + resource: repo + runAfter: + - pipeline-task-3 + taskRef: + name: echo-task + - name: pipeline-task-2-parallel-1 + params: + - name: text + value: much graph + resources: + inputs: + - from: + - pipeline-task-1 + name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task + - name: pipeline-task-1 + params: + - name: text + value: how to ci/cd? + resources: + inputs: + - name: repo + resource: repo + outputs: + - name: repo + resource: repo + taskRef: + name: echo-task +`, namespace)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create dag-pipeline: %s", err) } - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: "dag-pipeline-run", Namespace: namespace}, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{Name: "dag-pipeline"}, - Resources: []v1alpha1.PipelineResourceBinding{{ - Name: "repo", - ResourceRef: &v1alpha1.PipelineResourceRef{Name: "repo"}, - }}, - }, - } + pipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: dag-pipeline-run + namespace: %s +spec: + pipelineRef: + name: dag-pipeline + resources: + - name: repo + resourceRef: + name: repo +`, namespace)) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create dag-pipeline-run PipelineRun: %s", err) } diff --git a/test/v1alpha1/duplicate_test.go b/test/v1alpha1/duplicate_test.go index 7ccb5c93604..7ec424b301e 100644 --- a/test/v1alpha1/duplicate_test.go +++ b/test/v1alpha1/duplicate_test.go @@ -24,9 +24,7 @@ import ( "sync" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/pipeline" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -51,24 +49,16 @@ func TestDuplicatePodTaskRun(t *testing.T) { taskrunName := helpers.ObjectNameForTest(t) t.Logf("Creating taskrun %q.", taskrunName) - taskrun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskrunName, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskSpec: &v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/echo"}, - Args: []string{"simple"}, - }, - }}, - }, - }, - }, - } + taskrun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + taskSpec: + steps: + - image: busybox + command: ['/bin/echo'] + args: ['simple'] +`, taskrunName)) if _, err := c.TaskRunClient.Create(ctx, taskrun, metav1.CreateOptions{}); err != nil { t.Fatalf("Error creating taskrun: %v", err) } diff --git a/test/v1alpha1/embed_test.go b/test/v1alpha1/embed_test.go index 2dbdd109072..524bafee0a0 100644 --- a/test/v1alpha1/embed_test.go +++ b/test/v1alpha1/embed_test.go @@ -21,10 +21,10 @@ package test import ( "context" "fmt" + "strings" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -52,10 +52,10 @@ func TestTaskRun_EmbeddedResource(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - if _, err := c.TaskClient.Create(ctx, getEmbeddedTask([]string{"/bin/sh", "-c", fmt.Sprintf("echo %s", taskOutput)}), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getEmbeddedTask(t, []string{"/bin/sh", "-c", fmt.Sprintf("echo %s", taskOutput)}), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", embedTaskName, err) } - if _, err := c.TaskRunClient.Create(ctx, getEmbeddedTaskRun(namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, getEmbeddedTaskRun(t, namespace), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", embedTaskRunName, err) } @@ -68,66 +68,43 @@ func TestTaskRun_EmbeddedResource(t *testing.T) { // completion of the TaskRun means the TaskRun did what it was intended. } -func getEmbeddedTask(args []string) *v1alpha1.Task { - return &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: embedTaskName, - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "docs", - Type: v1alpha1.PipelineResourceTypeGit, - }, - }}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "cat /workspace/docs/LICENSE"}, - }, - }, - { - Container: corev1.Container{ - Image: "busybox", - Command: args, - }, - }, - }, - }, - }, +func getEmbeddedTask(t *testing.T, args []string) *v1alpha1.Task { + var argsForYaml []string + for _, s := range args { + argsForYaml = append(argsForYaml, fmt.Sprintf("'%s'", s)) } + return parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + inputs: + resources: + - name: docs + type: git + steps: + - image: ubuntu + command: ['/bin/bash'] + args: ['-c', 'cat /workspace/docs/LICENSE'] + - image: busybox + command: %s +`, embedTaskName, fmt.Sprintf("[%s]", strings.Join(argsForYaml, ", ")))) } -func getEmbeddedTaskRun(namespace string) *v1alpha1.TaskRun { - testSpec := &v1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeGit, - Params: []v1alpha1.ResourceParam{{ - Name: "URL", - Value: "https://github.com/knative/docs", - }}, - } - return &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: embedTaskRunName, - Namespace: namespace, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: embedTaskName, - }, - Inputs: &v1alpha1.TaskRunInputs{ - Resources: []v1alpha1.TaskResourceBinding{{ - PipelineResourceBinding: v1alpha1.PipelineResourceBinding{ - Name: "docs", - ResourceSpec: testSpec, - }, - }}, - }, - }, - } +func getEmbeddedTaskRun(t *testing.T, namespace string) *v1alpha1.TaskRun { + return parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + inputs: + resources: + - name: docs + resourceSpec: + type: git + params: + - name: URL + value: https://github.com/knative/docs + taskRef: + name: %s +`, embedTaskRunName, namespace, embedTaskName)) } diff --git a/test/v1alpha1/entrypoint_test.go b/test/v1alpha1/entrypoint_test.go index a5a1ccd39f2..cebe467a706 100644 --- a/test/v1alpha1/entrypoint_test.go +++ b/test/v1alpha1/entrypoint_test.go @@ -20,11 +20,11 @@ package test import ( "context" + "fmt" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -46,26 +46,20 @@ func TestEntrypointRunningStepsInOrder(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating TaskRun in namespace %s", namespace) - if _, err := c.TaskRunClient.Create(ctx, &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: epTaskRunName, Namespace: namespace}, - Spec: v1alpha1.TaskRunSpec{ - TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "busybox", - WorkingDir: "/workspace", - }, - Script: "sleep 3 && touch foo", - }, { - Container: corev1.Container{ - Image: "ubuntu", - WorkingDir: "/workspace", - }, - Script: "ls foo", - }}, - }}, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskSpec: + steps: + - image: busybox + workingDir: /workspace + script: 'sleep 3 && touch foo' + - image: ubuntu + workingDir: /workspace + script: 'ls foo' +`, epTaskRunName, namespace)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/v1alpha1/git_checkout_test.go b/test/v1alpha1/git_checkout_test.go index 8378f23db8b..d287dea2834 100644 --- a/test/v1alpha1/git_checkout_test.go +++ b/test/v1alpha1/git_checkout_test.go @@ -20,11 +20,13 @@ package test import ( "context" + "fmt" "strings" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -105,6 +107,7 @@ func TestGitPipelineRun(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", gitSourceResourceName) + // Still using the struct here rather than YAML because we'd have to conditionally determine which fields to set in the YAML. if _, err := c.PipelineResourceClient.Create(ctx, &v1alpha1.PipelineResource{ ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, Spec: v1alpha1.PipelineResourceSpec{ @@ -121,40 +124,33 @@ func TestGitPipelineRun(t *testing.T) { } t.Logf("Creating PipelineRun %s", gitTestPipelineRunName) - if _, err := c.PipelineRunClient.Create(ctx, &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, - Spec: v1alpha1.PipelineRunSpec{ - Resources: []v1alpha1.PipelineResourceBinding{{ - Name: "git-repo", - ResourceRef: &v1alpha1.PipelineResourceRef{Name: gitSourceResourceName}, - }}, - PipelineSpec: &v1alpha1.PipelineSpec{ - Resources: []v1alpha1.PipelineDeclaredResource{{ - Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, - }}, - Tasks: []v1alpha1.PipelineTask{{ - Name: "git-check", - TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1alpha1.TaskResource{{ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, - }}}, - }, - Steps: []v1alpha1.Step{{Container: corev1.Container{ - Image: "alpine/git", - Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, - }}}, - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "gitsource", - Resource: "git-repo", - }}, - }, - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + resources: + - name: git-repo + type: git + tasks: + - name: git-check + resources: + inputs: + - name: gitsource + resource: git-repo + taskSpec: + resources: + inputs: + - name: gitsource + type: git + steps: + - args: ['--git-dir=/workspace/gitsource/.git', 'show'] + image: alpine/git + resources: + - name: git-repo + resourceRef: + name: %s +`, gitTestPipelineRunName, gitSourceResourceName)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %s", gitTestPipelineRunName, err) } @@ -192,6 +188,7 @@ func TestGitPipelineRunFail(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", gitSourceResourceName) + // Still using the struct here rather than YAML because we'd have to conditionally determine which fields to set in the YAML. if _, err := c.PipelineResourceClient.Create(ctx, &v1alpha1.PipelineResource{ ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, Spec: v1alpha1.PipelineResourceSpec{ @@ -207,40 +204,33 @@ func TestGitPipelineRunFail(t *testing.T) { } t.Logf("Creating PipelineRun %s", gitTestPipelineRunName) - if _, err := c.PipelineRunClient.Create(ctx, &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, - Spec: v1alpha1.PipelineRunSpec{ - Resources: []v1alpha1.PipelineResourceBinding{{ - Name: "git-repo", - ResourceRef: &v1alpha1.PipelineResourceRef{Name: gitSourceResourceName}, - }}, - PipelineSpec: &v1alpha1.PipelineSpec{ - Resources: []v1alpha1.PipelineDeclaredResource{{ - Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, - }}, - Tasks: []v1alpha1.PipelineTask{{ - Name: "git-check", - TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1alpha1.TaskResource{{ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, - }}}, - }, - Steps: []v1alpha1.Step{{Container: corev1.Container{ - Image: "alpine/git", - Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, - }}}, - }}, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "gitsource", - Resource: "git-repo", - }}, - }, - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + resources: + - name: git-repo + type: git + tasks: + - name: git-check + resources: + inputs: + - name: gitsource + resource: git-repo + taskSpec: + resources: + inputs: + - name: gitsource + type: git + steps: + - args: ['--git-dir=/workspace/gitsource/.git', 'show'] + image: alpine/git + resources: + - name: git-repo + resourceRef: + name: %s +`, gitTestPipelineRunName, gitSourceResourceName)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %s", gitTestPipelineRunName, err) } diff --git a/test/v1alpha1/kaniko_task_test.go b/test/v1alpha1/kaniko_task_test.go index 734704a0f36..aa80ff1ce1f 100644 --- a/test/v1alpha1/kaniko_task_test.go +++ b/test/v1alpha1/kaniko_task_test.go @@ -23,12 +23,10 @@ import ( "fmt" "strings" "testing" - "time" - - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/test/parse" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -61,22 +59,22 @@ func TestKanikoTaskRun(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Git PipelineResource %s", kanikoGitResourceName) - if _, err := c.PipelineResourceClient.Create(ctx, getGitResource(namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getGitResource(t), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) } t.Logf("Creating Image PipelineResource %s", repo) - if _, err := c.PipelineResourceClient.Create(ctx, getImageResource(namespace, repo), metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineResourceClient.Create(ctx, getImageResource(t, repo), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) } t.Logf("Creating Task %s", kanikoTaskName) - if _, err := c.TaskClient.Create(ctx, getTask(repo, namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskClient.Create(ctx, getTask(t, repo, namespace), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", kanikoTaskName, err) } t.Logf("Creating TaskRun %s", kanikoTaskRunName) - if _, err := c.TaskRunClient.Create(ctx, getTaskRun(namespace), metav1.CreateOptions{}); err != nil { + if _, err := c.TaskRunClient.Create(ctx, getTaskRun(t, namespace), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", kanikoTaskRunName, err) } @@ -128,126 +126,83 @@ func TestKanikoTaskRun(t *testing.T) { } } -func getGitResource(namespace string) *v1alpha1.PipelineResource { - return &v1alpha1.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: kanikoGitResourceName, - }, - Spec: v1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeGit, - Params: []v1alpha1.ResourceParam{ - { - Name: "Url", - Value: "https://github.com/GoogleContainerTools/kaniko", - }, - { - Name: "Revision", - Value: revision, - }, - }, - }, - } +func getGitResource(t *testing.T) *v1alpha1.PipelineResource { + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + type: git + params: + - name: Url + value: https://github.com/GoogleContainerTools/kaniko + - name: Revision + value: %s +`, kanikoGitResourceName, revision)) } -func getImageResource(namespace, repo string) *v1alpha1.PipelineResource { - return &v1alpha1.PipelineResource{ - ObjectMeta: metav1.ObjectMeta{ - Name: kanikoImageResourceName, - }, - Spec: v1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeImage, - Params: []v1alpha1.ResourceParam{{ - Name: "url", - Value: repo, - }}, - }, - } +func getImageResource(t *testing.T, repo string) *v1alpha1.PipelineResource { + return parse.MustParsePipelineResource(t, fmt.Sprintf(` +metadata: + name: %s +spec: + type: image + params: + - name: url + value: %s +`, kanikoImageResourceName, repo)) } -func getTask(repo, namespace string) *v1alpha1.Task { - root := int64(0) - return &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: kanikoTaskName, - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "gitsource", - Type: v1alpha1.PipelineResourceTypeGit, - }, - }}, - }, - Outputs: &v1alpha1.Outputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "builtImage", - Type: v1alpha1.PipelineResourceTypeImage, - }, - }}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "gcr.io/kaniko-project/executor:v1.3.0", - Name: "kaniko", - Args: []string{ - "--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label", - fmt.Sprintf("--destination=%s", repo), - "--context=/workspace/gitsource", - "--oci-layout-path=/workspace/output/builtImage", - "--insecure", - "--insecure-pull", - "--insecure-registry=registry." + namespace + ":5000/", - }, - SecurityContext: &corev1.SecurityContext{RunAsUser: &root}, - }, - }}, - Sidecars: []v1alpha1.Sidecar{{ - Container: corev1.Container{ - Image: "registry", - Name: "registry", - }, - }}, - }, - }, - } +func getTask(t *testing.T, repo, namespace string) *v1alpha1.Task { + return parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + inputs: + resources: + - name: gitsource + type: git + outputs: + resources: + - name: builtImage + type: image + steps: + - name: kaniko + image: gcr.io/kaniko-project/executor:v1.3.0 + args: ['--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label', + '--destination=%s', + '--context=/workspace/gitsource', + '--oci-layout-path=/workspace/output/builtImage', + '--insecure', + '--insecure-pull', + '--insecure-registry=registry.%s:5000/'] + securityContext: + runAsUser: 0 + sidecars: + - name: registry + image: registry +`, kanikoTaskName, repo, namespace)) } -func getTaskRun(namespace string) *v1alpha1.TaskRun { - return &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: kanikoTaskRunName, - Namespace: namespace, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: kanikoTaskName, - }, - Timeout: &metav1.Duration{Duration: 2 * time.Minute}, - Inputs: &v1alpha1.TaskRunInputs{ - Resources: []v1alpha1.TaskResourceBinding{{ - PipelineResourceBinding: v1alpha1.PipelineResourceBinding{ - Name: "gitsource", - ResourceRef: &v1alpha1.PipelineResourceRef{ - Name: kanikoGitResourceName, - }, - }, - }}, - }, - Outputs: &v1alpha1.TaskRunOutputs{ - Resources: []v1alpha1.TaskResourceBinding{{ - PipelineResourceBinding: v1alpha1.PipelineResourceBinding{ - Name: "builtImage", - ResourceRef: &v1alpha1.PipelineResourceRef{ - Name: kanikoImageResourceName, - }, - }, - }}, - }, - }, - } +func getTaskRun(t *testing.T, namespace string) *v1alpha1.TaskRun { + return parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + timeout: 2m + inputs: + resources: + - name: gitsource + resourceRef: + name: %s + outputs: + resources: + - name: builtImage + resourceRef: + name: %s +`, kanikoTaskRunName, namespace, kanikoTaskName, kanikoGitResourceName, kanikoImageResourceName)) } // getRemoteDigest starts a pod to query the registry from the namespace itself, using skopeo (and jq). diff --git a/test/v1alpha1/pipelinerun_test.go b/test/v1alpha1/pipelinerun_test.go index f3a2936b2ac..ce85ad1cde5 100644 --- a/test/v1alpha1/pipelinerun_test.go +++ b/test/v1alpha1/pipelinerun_test.go @@ -26,7 +26,7 @@ import ( "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" @@ -58,26 +58,26 @@ func TestPipelineRun(t *testing.T) { testSetup func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) expectedTaskRuns []string expectedNumberOfEvents int - pipelineRunFunc func(int, string) *v1alpha1.PipelineRun + pipelineRunFunc func(*testing.T, int, string) *v1alpha1.PipelineRun } tds := []tests{{ name: "fan-in and fan-out", testSetup: func(ctx context.Context, t *testing.T, c *clients, namespace string, index int) { t.Helper() - for _, task := range getFanInFanOutTasks() { + for _, task := range getFanInFanOutTasks(t) { 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() { + for _, res := range getFanInFanOutGitResources(t) { if _, err := c.PipelineResourceClient.Create(ctx, res, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", kanikoGitResourceName, err) } } - if _, err := c.PipelineClient.Create(ctx, getFanInFanOutPipeline(index), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -98,41 +98,26 @@ func TestPipelineRun(t *testing.T) { t.Fatalf("Failed to create SA `%s`: %s", getName(saName, index), err) } - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(taskName, index), - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Params: []v1alpha1.ParamSpec{ - { - Name: "path", - Type: v1alpha1.ParamTypeString, - }, - { - Name: "dest", - Type: v1alpha1.ParamTypeString, - }, - }, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - // Reference build: https://github.com/knative/build/tree/master/test/docker-basic - Container: corev1.Container{ - Name: "config-docker", - Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest", - Command: []string{"skopeo"}, - Args: []string{"copy", "$(inputs.params.path)", "$(inputs.params.dest)"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + params: + - name: path + type: string + - name: dest + type: string + steps: + - name: config-docker + image: gcr.io/tekton-releases/dogfooding/skopeo:latest + command: ['skopeo'] + args: ['copy', '$(inputs.params.path)', '$(inputs.params.dest)'] +`, getName(taskName, index))) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) } - if _, err := c.PipelineClient.Create(ctx, getHelloWorldPipelineWithSingularTask(index), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -149,27 +134,20 @@ func TestPipelineRun(t *testing.T) { t.Fatalf("Failed to create Condition `%s`: %s", cond1Name, err) } - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(taskName, index), - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo hello, world"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: ubuntu + command: ['/bin/bash'] + args: ['-c', 'echo hello, world'] +`, getName(taskName, index))) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", getName(taskName, index), err) } - if _, err := c.PipelineClient.Create(ctx, getPipelineWithFailingCondition(index), metav1.CreateOptions{}); err != nil { + 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) } }, @@ -196,7 +174,7 @@ func TestPipelineRun(t *testing.T) { td.testSetup(ctx, t, c, namespace, i) prName := fmt.Sprintf("%s%d", pipelineRunName, i) - pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(i, namespace), metav1.CreateOptions{}) + pipelineRun, err := c.PipelineRunClient.Create(ctx, td.pipelineRunFunc(t, i, namespace), metav1.CreateOptions{}) if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -275,300 +253,187 @@ func TestPipelineRun(t *testing.T) { } } -func getHelloWorldPipelineWithSingularTask(suffix int) *v1alpha1.Pipeline { - return &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineName, suffix), - }, - Spec: v1alpha1.PipelineSpec{ - Params: []v1alpha1.ParamSpec{ - { - Name: "path", - Type: v1alpha1.ParamTypeString, - }, - { - Name: "dest", - Type: v1alpha1.ParamTypeString, - }, - }, - Tasks: []v1alpha1.PipelineTask{{ - Name: task1Name, - TaskRef: &v1alpha1.TaskRef{ - Name: getName(taskName, suffix), - }, - Params: []v1alpha1.Param{ - { - Name: "path", - Value: v1alpha1.ArrayOrString{ - Type: v1alpha1.ParamTypeString, - StringVal: "$(params.path)", - }, - }, - { - Name: "dest", - Value: v1alpha1.ArrayOrString{ - Type: v1alpha1.ParamTypeString, - StringVal: "$(params.dest)", - }, - }, - }, - }}, - }, - } +func getHelloWorldPipelineWithSingularTask(t *testing.T, suffix int) *v1alpha1.Pipeline { + return parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + params: + - name: path + type: string + - name: dest + type: string + tasks: + - name: %s + params: + - name: path + value: $(params["path"]) + - name: dest + value: $(params.dest) + taskRef: + name: %s +`, getName(pipelineName, suffix), task1Name, getName(taskName, suffix))) } -func getFanInFanOutTasks() []*v1alpha1.Task { - inWorkspaceResource := v1alpha1.TaskResource{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "workspace", - Type: v1alpha1.PipelineResourceTypeGit, - }, - } - outWorkspaceResource := v1alpha1.TaskResource{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "workspace", - Type: v1alpha1.PipelineResourceTypeGit, - }, - } +func getFanInFanOutTasks(t *testing.T) []*v1alpha1.Task { return []*v1alpha1.Task{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "create-file", - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "workspace", - Type: v1alpha1.PipelineResourceTypeGit, - TargetPath: "brandnewspace", - }, - }}, - }, - Outputs: &v1alpha1.Outputs{ - Resources: []v1alpha1.TaskResource{outWorkspaceResource}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Name: "write-data-task-0-step-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo stuff > $(outputs.resources.workspace.path)/stuff"}, - }, - }, - { - Container: corev1.Container{ - Name: "write-data-task-0-step-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo other > $(outputs.resources.workspace.path)/other"}, - }, - }, - }, - }, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "check-create-file-exists", - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{inWorkspaceResource}, - }, - Outputs: &v1alpha1.Outputs{ - Resources: []v1alpha1.TaskResource{outWorkspaceResource}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Name: "read-from-task-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ stuff == $(cat $(inputs.resources.workspace.path)/stuff) ]]"}, - }, - }, - { - Container: corev1.Container{ - Name: "write-data-task-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo something > $(outputs.resources.workspace.path)/something"}, - }, - }, - }, - }, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "check-create-file-exists-2", - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{inWorkspaceResource}, - }, - Outputs: &v1alpha1.Outputs{ - Resources: []v1alpha1.TaskResource{outWorkspaceResource}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Name: "read-from-task-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ other == $(cat $(inputs.resources.workspace.path)/other) ]]"}, - }, - }, - { - Container: corev1.Container{ - Name: "write-data-task-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "echo else > $(outputs.resources.workspace.path)/else"}, - }, - }, - }, - }, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "read-files", - }, - Spec: v1alpha1.TaskSpec{ - Inputs: &v1alpha1.Inputs{ - Resources: []v1alpha1.TaskResource{{ - ResourceDeclaration: v1alpha1.ResourceDeclaration{ - Name: "workspace", - Type: v1alpha1.PipelineResourceTypeGit, - TargetPath: "readingspace", - }, - }}, - }, - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Name: "read-from-task-0", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ something == $(cat $(inputs.resources.workspace.path)/something) ]]"}, - }, - }, - { - Container: corev1.Container{ - Name: "read-from-task-1", - Image: "ubuntu", - Command: []string{"/bin/bash"}, - Args: []string{"-c", "[[ else == $(cat $(inputs.resources.workspace.path)/else) ]]"}, - }, - }, - }, - }, - }, - }, + parse.MustParseAlphaTask(t, ` +metadata: + name: create-file +spec: + inputs: + resources: + - name: workspace + targetPath: brandnewspace + type: git + outputs: + resources: + - name: workspace + type: git + steps: + - args: ['-c', 'echo stuff > $(resources.outputs.workspace.path)/stuff'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-0-step-0 + - args: ['-c', 'echo other > $(resources.outputs.workspace.path)/other'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-0-step-1 +`), + parse.MustParseAlphaTask(t, ` +metadata: + name: check-create-files-exists +spec: + inputs: + resources: + - name: workspace + type: git + outputs: + resources: + - name: workspace + type: git + steps: + - args: ['-c', '[[ stuff == $(cat $(inputs.resources.workspace.path)/stuff) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-0 + - args: ['-c', 'echo something > $(outputs.resources.workspace.path)/something'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-1 +`), + parse.MustParseAlphaTask(t, ` +metadata: + name: check-create-files-exists-2 +spec: + inputs: + resources: + - name: workspace + type: git + outputs: + resources: + - name: workspace + type: git + steps: + - args: ['-c', '[[ other == $(cat $(inputs.resources.workspace.path)/other) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-0 + - args: ['-c', 'echo else > $(outputs.resources.workspace.path)/else'] + command: ['/bin/bash'] + image: ubuntu + name: write-data-task-1 +`), + parse.MustParseAlphaTask(t, ` +metadata: + name: read-files +spec: + inputs: + resources: + - name: workspace + type: git + targetPath: readingspace + steps: + - args: ['-c', '[[ something == $(cat $(inputs.resources.workspace.path)/something) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-0 + - args: ['-c', '[[ else == $(cat $(inputs.resources.workspace.path)/else) ]]'] + command: ['/bin/bash'] + image: ubuntu + name: read-from-task-1 +`), } } -func getFanInFanOutPipeline(suffix int) *v1alpha1.Pipeline { - outGitResource := v1alpha1.PipelineTaskOutputResource{ - Name: "workspace", - Resource: "git-repo", - } - - return &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineName, suffix), - }, - Spec: v1alpha1.PipelineSpec{ - Resources: []v1alpha1.PipelineDeclaredResource{{ - Name: "git-repo", - Type: v1alpha1.PipelineResourceTypeGit, - }}, - Tasks: []v1alpha1.PipelineTask{ - { - Name: "create-file-kritis", - TaskRef: &v1alpha1.TaskRef{ - Name: "create-file", - }, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "workspace", - Resource: "git-repo", - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{outGitResource}, - }, - }, - { - Name: "create-fan-out-1", - TaskRef: &v1alpha1.TaskRef{ - Name: "check-create-file-exists", - }, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "workspace", - Resource: "git-repo", - From: []string{"create-file-kritis"}, - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{outGitResource}, - }, - }, - { - Name: "create-fan-out-2", - TaskRef: &v1alpha1.TaskRef{ - Name: "check-create-file-exists-2", - }, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "workspace", - Resource: "git-repo", - From: []string{"create-file-kritis"}, - }}, - Outputs: []v1alpha1.PipelineTaskOutputResource{outGitResource}, - }, - }, - { - Name: "check-fan-in", - TaskRef: &v1alpha1.TaskRef{ - Name: "read-files", - }, - Resources: &v1alpha1.PipelineTaskResources{ - Inputs: []v1alpha1.PipelineTaskInputResource{{ - Name: "workspace", - Resource: "git-repo", - From: []string{"create-fan-out-2", "create-fan-out-1"}, - }}, - }, - }, - }, - }, - } +func getFanInFanOutPipeline(t *testing.T, suffix int) *v1alpha1.Pipeline { + return parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + resources: + - name: git-repo + type: git + tasks: + - name: create-file-kritis + resources: + inputs: + - name: workspace + resource: git-repo + outputs: + - name: workspace + resource: git-repo + taskRef: + name: create-file + - name: create-fan-out-1 + resources: + inputs: + - from: + - create-file-kritis + name: workspace + resource: git-repo + outputs: + - name: workspace + resource: git-repo + taskRef: + name: check-create-files-exists + - name: create-fan-out-2 + resources: + inputs: + - from: + - create-file-kritis + name: workspace + resource: git-repo + outputs: + - name: workspace + resource: git-repo + taskRef: + name: check-create-files-exists-2 + - name: check-fan-in + resources: + inputs: + - from: + - create-fan-out-2 + - create-fan-out-1 + name: workspace + resource: git-repo + taskRef: + name: read-files +`, getName(pipelineName, suffix))) } -func getFanInFanOutGitResources() []*v1alpha1.PipelineResource { - return []*v1alpha1.PipelineResource{{ - ObjectMeta: metav1.ObjectMeta{ - Name: "kritis-resource-git", - }, - Spec: v1alpha1.PipelineResourceSpec{ - Type: v1alpha1.PipelineResourceTypeGit, - Params: []v1alpha1.ResourceParam{ - { - Name: "Url", - Value: "https://github.com/grafeas/kritis", - }, - { - Name: "Revision", - Value: "master", - }, - }, - }, - }} +func getFanInFanOutGitResources(t *testing.T) []*v1alpha1.PipelineResource { + return []*v1alpha1.PipelineResource{parse.MustParsePipelineResource(t, ` +metadata: + name: kritis-resource-git +spec: + type: git + params: + - name: Url + value: https://github.com/grafeas/kritis + - name: Revision + value: master +`)} } func getPipelineRunServiceAccount(suffix int) *corev1.ServiceAccount { @@ -581,26 +446,19 @@ func getPipelineRunServiceAccount(suffix int) *corev1.ServiceAccount { }}, } } -func getFanInFanOutPipelineRun(suffix int, namespace string) *v1alpha1.PipelineRun { - return &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineRunName, suffix), - Namespace: namespace, - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: getName(pipelineName, suffix), - }, - Resources: []v1alpha1.PipelineResourceBinding{ - { - Name: "git-repo", - ResourceRef: &v1alpha1.PipelineResourceRef{ - Name: "kritis-resource-git", - }, - }, - }, - }, - } +func getFanInFanOutPipelineRun(t *testing.T, suffix int, namespace string) *v1alpha1.PipelineRun { + return parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + resources: + - name: git-repo + resourceRef: + name: kritis-resource-git +`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) } func getPipelineRunSecret(suffix int) *corev1.Secret { @@ -631,32 +489,23 @@ func getPipelineRunSecret(suffix int) *corev1.Secret { } } -func getHelloWorldPipelineRun(suffix int, namespace string) *v1alpha1.PipelineRun { - return &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineRunName, suffix), - Namespace: namespace, - Labels: map[string]string{ - "hello-world-key": "hello-world-value", - }, - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: getName(pipelineName, suffix), - }, - ServiceAccountName: fmt.Sprintf("%s%d", saName, suffix), - Params: []v1alpha1.Param{ - { - Name: "path", - Value: *v1beta1.NewArrayOrString("docker://gcr.io/build-crd-testing/secret-sauce"), - }, - { - Name: "dest", - Value: *v1beta1.NewArrayOrString("dir:///tmp/"), - }, - }, - }, - } +func getHelloWorldPipelineRun(t *testing.T, suffix int, namespace string) *v1alpha1.PipelineRun { + return parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + labels: + hello-world-key: hello-world-value + name: %s + namespace: %s +spec: + params: + - name: path + value: docker://gcr.io/build-crd-testing/secret-sauce + - name: dest + value: dir:///tmp/ + pipelineRef: + name: %s + serviceAccountName: %s%d +`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix), saName, suffix)) } func getName(namespace string, suffix int) string { @@ -818,32 +667,22 @@ func assertAnnotationsMatch(t *testing.T, expectedAnnotations, actualAnnotations } } -func getPipelineWithFailingCondition(suffix int) *v1alpha1.Pipeline { - return &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineName, suffix), - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{ - { - Name: task1Name, - TaskRef: &v1alpha1.TaskRef{ - Name: getName(taskName, suffix), - }, - Conditions: []v1alpha1.PipelineTaskCondition{{ - ConditionRef: cond1Name, - }}, - }, - { - Name: "task2", - TaskRef: &v1alpha1.TaskRef{ - Name: getName(taskName, suffix), - }, - RunAfter: []string{task1Name}, - }, - }, - }, - } +func getPipelineWithFailingCondition(t *testing.T, suffix int) *v1alpha1.Pipeline { + return parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + tasks: + - name: %s + taskRef: + name: %s + conditions: + - conditionRef: %s + - name: task2 + taskRef: + name: %s + runAfter: ['%s'] +`, getName(pipelineName, suffix), task1Name, getName(taskName, suffix), cond1Name, getName(taskName, suffix), task1Name)) } func getFailingCondition(namespace string) *v1alpha1.Condition { @@ -864,19 +703,15 @@ func getFailingCondition(namespace string) *v1alpha1.Condition { } } -func getConditionalPipelineRun(suffix int, namespace string) *v1alpha1.PipelineRun { - return &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: getName(pipelineRunName, suffix), - Namespace: namespace, - Labels: map[string]string{ - "hello-world-key": "hello-world-value", - }, - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: getName(pipelineName, suffix), - }, - }, - } +func getConditionalPipelineRun(t *testing.T, suffix int, namespace string) *v1alpha1.PipelineRun { + return parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s + labels: + hello-world-key: hello-world-vaule +spec: + pipelineRef: + name: %s +`, getName(pipelineRunName, suffix), namespace, getName(pipelineName, suffix))) } diff --git a/test/v1alpha1/retry_test.go b/test/v1alpha1/retry_test.go index d52cdb152c9..aeb08ea853f 100644 --- a/test/v1alpha1/retry_test.go +++ b/test/v1alpha1/retry_test.go @@ -20,11 +20,12 @@ package test import ( "context" + "fmt" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -46,23 +47,19 @@ func TestTaskRunRetry(t *testing.T) { // configured to retry 5 times. pipelineRunName := "retry-pipeline" numRetries := 5 - if _, err := c.PipelineRunClient.Create(ctx, &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName}, - Spec: v1alpha1.PipelineRunSpec{ - PipelineSpec: &v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - Name: "retry-me", - TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{Image: "busybox"}, - Script: "exit 1", - }}, - }}, - Retries: numRetries, - }}, - }, - }, - }, metav1.CreateOptions{}); err != nil { + if _, err := c.PipelineRunClient.Create(ctx, parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineSpec: + tasks: + - name: retry-me + retries: %d + taskSpec: + steps: + - image: busybox + script: exit 1 +`, pipelineRunName, numRetries)), metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun %q: %v", pipelineRunName, err) } diff --git a/test/v1alpha1/sidecar_test.go b/test/v1alpha1/sidecar_test.go index a52f4140c3b..273e777d150 100644 --- a/test/v1alpha1/sidecar_test.go +++ b/test/v1alpha1/sidecar_test.go @@ -21,11 +21,10 @@ package test import ( "context" "fmt" + "strings" "testing" - "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -69,31 +68,29 @@ func TestSidecarTaskSupport(t *testing.T) { t.Run(test.desc, func(t *testing.T) { sidecarTaskName := fmt.Sprintf("%s-%d", sidecarTaskName, i) sidecarTaskRunName := fmt.Sprintf("%s-%d", sidecarTaskRunName, i) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: sidecarTaskName}, - Spec: v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "busybox", - Name: primaryContainerName, - Command: test.stepCommand, - }}}, - Sidecars: []v1beta1.Sidecar{{Container: corev1.Container{ - Image: "busybox", - Name: sidecarContainerName, - Command: test.sidecarCommand, - }}}, - }}, - } - - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: sidecarTaskRunName}, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: sidecarTaskName, - }, - Timeout: &metav1.Duration{time.Minute}, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - name: %s + image: busybox + command: [%s] + sidecars: + - name: %s + image: busybox + command: [%s] +`, sidecarTaskName, primaryContainerName, stringSliceToYAMLArray(test.stepCommand), sidecarContainerName, stringSliceToYAMLArray(test.sidecarCommand))) + + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + timeout: 1m +`, sidecarTaskRunName, namespace, sidecarTaskName)) t.Logf("Creating Task %q", sidecarTaskName) if _, err := clients.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { @@ -177,3 +174,11 @@ func TestSidecarTaskSupport(t *testing.T) { }) } } + +func stringSliceToYAMLArray(stringSlice []string) string { + var quoted []string + for _, s := range stringSlice { + quoted = append(quoted, fmt.Sprintf("'%s'", s)) + } + return strings.Join(quoted, ", ") +} diff --git a/test/v1alpha1/start_time_test.go b/test/v1alpha1/start_time_test.go index 8630fe8fe88..0252f919fcd 100644 --- a/test/v1alpha1/start_time_test.go +++ b/test/v1alpha1/start_time_test.go @@ -16,12 +16,12 @@ package test import ( "context" + "fmt" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" ) @@ -42,32 +42,24 @@ func TestStartTime(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) t.Logf("Creating TaskRun in namespace %q", namespace) - tr, err := c.TaskRunClient.Create(ctx, &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - GenerateName: "start-time-test-", - Namespace: namespace, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }, { - Container: corev1.Container{Image: "ubuntu"}, - Script: "sleep 10", - }}, - }}, - }, - }, metav1.CreateOptions{}) + tr, err := c.TaskRunClient.Create(ctx, parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + generateName: start-time-test- + namespace: %s +spec: + taskSpec: + steps: + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 + - image: ubuntu + script: sleep 10 +`, namespace)), metav1.CreateOptions{}) if err != nil { t.Fatalf("Error creating TaskRun: %v", err) } diff --git a/test/v1alpha1/status_test.go b/test/v1alpha1/status_test.go index 63de8993f5c..8d17aa2443a 100644 --- a/test/v1alpha1/status_test.go +++ b/test/v1alpha1/status_test.go @@ -22,9 +22,7 @@ import ( "context" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" + "github.com/tektoncd/pipeline/test/parse" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -44,35 +42,23 @@ func TestTaskRunPipelineRunStatus(t *testing.T) { defer tearDown(ctx, t, c, namespace) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: "banana", - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox", - Command: []string{"ls", "-la"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, ` +metadata: + name: banana +spec: + steps: + - image: busybox + command: ['ls', '-la']`) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: "apple", - }, - Spec: v1alpha1.TaskRunSpec{ - ServiceAccountName: "inexistent", - TaskRef: &v1alpha1.TaskRef{ - Name: "banana", - }, - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, ` +metadata: + name: apple +spec: + taskRef: + name: banana + serviceAccountName: inexistent`) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -82,30 +68,21 @@ func TestTaskRunPipelineRunStatus(t *testing.T) { t.Errorf("Error waiting for TaskRun to finish: %s", err) } - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: "tomatoes", - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - Name: "foo", - TaskRef: &v1alpha1.TaskRef{ - Name: "banana", - }, - }}, - }, - } - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pear", - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: "tomatoes", - }, - ServiceAccountName: "inexistent", - }, - } + pipeline := parse.MustParseAlphaPipeline(t, ` +metadata: + name: tomatoes +spec: + tasks: + - name: foo + taskRef: + name: banana`) + pipelineRun := parse.MustParseAlphaPipelineRun(t, ` +metadata: + name: pear +spec: + pipelineRef: + name: tomatoes + serviceAccountName: inexistent`) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", "tomatoes", err) } diff --git a/test/v1alpha1/taskrun_test.go b/test/v1alpha1/taskrun_test.go index f420a664aa7..903bddfdb8d 100644 --- a/test/v1alpha1/taskrun_test.go +++ b/test/v1alpha1/taskrun_test.go @@ -20,10 +20,11 @@ package test import ( "context" + "fmt" "strings" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -46,51 +47,31 @@ func TestTaskRunFailure(t *testing.T) { taskRunName := "failing-taskrun" t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: "failing-task", - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{ - { - Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "echo hello"}, - }, - }, - { - Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "exit 1"}, - }, - }, - { - Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 30s"}, - }, - }, - }, - }, - }, - } + task := parse.MustParseAlphaTask(t, ` +metadata: + name: failing-task +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'echo hello'] + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'exit 1'] + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 30s'] +`) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskRunName, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: "failing-task", - }, - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + taskRef: + name: failing-task +`, taskRunName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -154,35 +135,25 @@ func TestTaskRunStatus(t *testing.T) { fqImageName := "busybox@sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649" t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: "status-task", - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox@sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "echo hello"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: status-task +spec: + steps: + - image: %s + command: ['/bin/sh'] + args: ['-c', 'echo hello'] +`, fqImageName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskRunName, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: "status-task", - }, - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + taskRef: + name: status-task +`, taskRunName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/v1alpha1/timeout_test.go b/test/v1alpha1/timeout_test.go index 391c1533f1b..995b96dcc47 100644 --- a/test/v1alpha1/timeout_test.go +++ b/test/v1alpha1/timeout_test.go @@ -25,7 +25,7 @@ import ( "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" corev1 "k8s.io/api/core/v1" @@ -48,51 +48,37 @@ func TestPipelineRunTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Task in namespace %s", namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 10"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 10'] +`, helpers.ObjectNameForTest(t))) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task %q: %s", task.Name, err) } - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - Name: "foo", - TaskRef: &v1alpha1.TaskRef{ - Name: task.Name, - }, - }}, - }, - } - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: pipeline.Name, - }, - Timeout: &metav1.Duration{Duration: 5 * time.Second}, - }, - } + pipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + tasks: + - name: foo + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), task.Name)) + pipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s + timeout: 5s +`, helpers.ObjectNameForTest(t), pipeline.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) } @@ -156,29 +142,22 @@ func TestPipelineRunTimeout(t *testing.T) { // Verify that we can create a second Pipeline using the same Task without a Pipeline-level timeout that will not // time out - secondPipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - Name: "foo", - TaskRef: &v1alpha1.TaskRef{ - Name: task.Name, - }, - }}, - }, - } - secondPipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: pipeline.Name, - }, - }, - } + secondPipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + tasks: + - name: foo + taskRef: + name: %s +`, helpers.ObjectNameForTest(t), task.Name)) + secondPipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s +`, helpers.ObjectNameForTest(t), secondPipeline.Name)) if _, err := c.PipelineClient.Create(ctx, secondPipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", secondPipeline.Name, err) } @@ -203,37 +182,28 @@ func TestTaskRunTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Task and TaskRun in namespace %s", namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox", - Command: []string{"/bin/sh"}, - Args: []string{"-c", "sleep 3000"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 3000'] +`, helpers.ObjectNameForTest(t))) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: task.Name, - }, - Timeout: &metav1.Duration{Duration: 30 * time.Second}, - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + timeout: %s +`, helpers.ObjectNameForTest(t), namespace, task.Name, 30*time.Second)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun `%s`: %s", taskRun.Name, err) } @@ -254,38 +224,24 @@ func TestPipelineTaskTimeout(t *testing.T) { defer tearDown(context.Background(), t, c, namespace) t.Logf("Creating Tasks in namespace %s", namespace) - task1 := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox", - Command: []string{"sleep"}, - Args: []string{"1s"}, - }, - }}, - }, - }, - } - task2 := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "busybox", - Command: []string{"sleep"}, - Args: []string{"10s"}, - }, - }}, - }, - }, - } + task1 := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 1s'] +`, helpers.ObjectNameForTest(t))) + task2 := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: busybox + command: ['/bin/sh'] + args: ['-c', 'sleep 10s'] +`, helpers.ObjectNameForTest(t))) if _, err := c.TaskClient.Create(ctx, task1, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task1.Name, err) @@ -294,39 +250,27 @@ func TestPipelineTaskTimeout(t *testing.T) { t.Fatalf("Failed to create Task `%s`: %s", task2.Name, err) } - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{ - { - Name: "pipelinetask1", - TaskRef: &v1alpha1.TaskRef{ - Name: task1.Name, - }, - Timeout: &metav1.Duration{Duration: 60 * time.Second}, - }, - { - Name: "pipelinetask2", - TaskRef: &v1alpha1.TaskRef{ - Name: task2.Name, - }, - Timeout: &metav1.Duration{Duration: 5 * time.Second}, - }, - }, - }, - } - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: helpers.ObjectNameForTest(t), - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: pipeline.Name, - }, - }, - } + pipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + tasks: + - name: pipelinetask1 + taskRef: + name: %s + timeout: 60s + - name: pipelinetask2 + taskRef: + name: %s + timeout: 5s +`, helpers.ObjectNameForTest(t), task1.Name, task2.Name)) + pipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s +`, helpers.ObjectNameForTest(t), pipeline.Name)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err) diff --git a/test/v1alpha1/wait_test.go b/test/v1alpha1/wait_test.go index cacd11431df..db3a582abaa 100644 --- a/test/v1alpha1/wait_test.go +++ b/test/v1alpha1/wait_test.go @@ -21,27 +21,22 @@ import ( "testing" "time" + "github.com/tektoncd/pipeline/test/parse" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/pkg/apis" - duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" -) - -var ( - success = apis.Condition{Type: apis.ConditionSucceeded, Status: corev1.ConditionTrue} - failure = apis.Condition{Type: apis.ConditionSucceeded, Status: corev1.ConditionFalse} ) func TestWaitForTaskRunStateSucceed(t *testing.T) { d := Data{ - TaskRuns: []*v1alpha1.TaskRun{{ - ObjectMeta: metav1.ObjectMeta{Name: "foo"}, - Status: v1alpha1.TaskRunStatus{Status: duckv1beta1.Status{ - Conditions: []apis.Condition{success}, - }}, - }}, + TaskRuns: []*v1alpha1.TaskRun{parse.MustParseAlphaTaskRun(t, ` +metadata: + name: foo +status: + conditions: + - status: "True" + type: Succeeded +`)}, } c, ctx, cancel := fakeClients(t, d) defer cancel() @@ -51,12 +46,14 @@ func TestWaitForTaskRunStateSucceed(t *testing.T) { } func TestWaitForTaskRunStateFailed(t *testing.T) { d := Data{ - TaskRuns: []*v1alpha1.TaskRun{{ - ObjectMeta: metav1.ObjectMeta{Name: "foo"}, - Status: v1alpha1.TaskRunStatus{Status: duckv1beta1.Status{ - Conditions: []apis.Condition{failure}, - }}, - }}, + TaskRuns: []*v1alpha1.TaskRun{parse.MustParseAlphaTaskRun(t, ` +metadata: + name: foo +status: + conditions: + - status: "False" + type: Succeeded +`)}, } c, ctx, cancel := fakeClients(t, d) defer cancel() @@ -68,12 +65,14 @@ func TestWaitForTaskRunStateFailed(t *testing.T) { func TestWaitForPipelineRunStateSucceed(t *testing.T) { d := Data{ - PipelineRuns: []*v1alpha1.PipelineRun{{ - ObjectMeta: metav1.ObjectMeta{Name: "bar"}, - Status: v1alpha1.PipelineRunStatus{Status: duckv1beta1.Status{ - Conditions: []apis.Condition{success}, - }}, - }}, + PipelineRuns: []*v1alpha1.PipelineRun{parse.MustParseAlphaPipelineRun(t, ` +metadata: + name: bar +status: + conditions: + - status: "True" + type: Succeeded +`)}, } c, ctx, cancel := fakeClients(t, d) defer cancel() @@ -85,12 +84,14 @@ func TestWaitForPipelineRunStateSucceed(t *testing.T) { func TestWaitForPipelineRunStateFailed(t *testing.T) { d := Data{ - PipelineRuns: []*v1alpha1.PipelineRun{{ - ObjectMeta: metav1.ObjectMeta{Name: "bar"}, - Status: v1alpha1.PipelineRunStatus{Status: duckv1beta1.Status{ - Conditions: []apis.Condition{failure}, - }}, - }}, + PipelineRuns: []*v1alpha1.PipelineRun{parse.MustParseAlphaPipelineRun(t, ` +metadata: + name: bar +status: + conditions: + - status: "False" + type: Succeeded +`)}, } c, ctx, cancel := fakeClients(t, d) defer cancel() diff --git a/test/v1alpha1/workingdir_test.go b/test/v1alpha1/workingdir_test.go index 248efbc564a..641e1ff8d59 100644 --- a/test/v1alpha1/workingdir_test.go +++ b/test/v1alpha1/workingdir_test.go @@ -20,11 +20,11 @@ package test import ( "context" + "fmt" "strings" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -46,38 +46,28 @@ func TestWorkingDirCreated(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: wdTaskName, - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - WorkingDir: "/workspace/HELLOMOTO", - Args: []string{"-c", "echo YES"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: ubuntu + workingDir: /workspace/HELLOMOTO + args: ['-c', 'echo YES'] +`, wdTaskName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } t.Logf("Creating TaskRun namespace %s", namespace) - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: wdTaskRunName, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: wdTaskName, - }, - ServiceAccountName: "default", - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + taskRef: + name: %s + serviceAccountName: default +`, wdTaskRunName, wdTaskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -124,38 +114,28 @@ func TestWorkingDirIgnoredNonSlashWorkspace(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: wdTaskName, - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "ubuntu", - WorkingDir: "/HELLOMOTO", - Args: []string{"-c", "echo YES"}, - }, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: ubuntu + workingDir: /HELLOMOTO + args: ['-c', 'echo YES'] +`, wdTaskName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } t.Logf("Creating TaskRun namespace %s", namespace) - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: wdTaskRunName, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: wdTaskName, - }, - ServiceAccountName: "default", - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + taskRef: + name: %s + serviceAccountName: default +`, wdTaskRunName, wdTaskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/v1alpha1/workspace_test.go b/test/v1alpha1/workspace_test.go index e59f5be9534..255e53e7e0a 100644 --- a/test/v1alpha1/workspace_test.go +++ b/test/v1alpha1/workspace_test.go @@ -20,12 +20,12 @@ package test import ( "context" + "fmt" "strings" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -44,47 +44,34 @@ func TestWorkspaceReadOnlyDisallowsWrite(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskName, - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "alpine", - }, - Script: "echo foo > /workspace/test/file", - }}, - Workspaces: []v1alpha1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test", - ReadOnly: true, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: alpine + script: 'echo foo > /workspace/test/file' + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test + readOnly: true +`, taskName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1alpha1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskRunName, - }, - Spec: v1alpha1.TaskRunSpec{ - TaskRef: &v1alpha1.TaskRef{ - Name: taskName, - }, - ServiceAccountName: "default", - Workspaces: []v1alpha1.WorkspaceBinding{{ - Name: "test", - SubPath: "", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }}, - }, - } + taskRun := parse.MustParseAlphaTaskRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + taskRef: + name: %s + serviceAccountName: default + workspaces: + - name: test + emptyDir: {} +`, taskRunName, taskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -133,76 +120,53 @@ func TestWorkspacePipelineRunDuplicateWorkspaceEntriesInvalid(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskName, - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "alpine", - }, - Script: "cat /workspace/test/file", - }}, - Workspaces: []v1alpha1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test/file", - ReadOnly: true, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: alpine + script: 'cat /workspace/test/file' + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test + readOnly: true +`, taskName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: pipelineName, - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - TaskRef: &v1alpha1.TaskRef{ - Name: taskName, - }, - Name: taskName, - Workspaces: []v1alpha1.WorkspacePipelineTaskBinding{{ - Name: "test", - Workspace: "foo", - SubPath: "", - }}, - }}, - Workspaces: []v1alpha1.PipelineWorkspaceDeclaration{{ - Name: "foo", - }}, - }, - } + pipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + workspaces: + - name: foo + tasks: + - name: %s + taskRef: + name: %s + workspaces: + - name: test + workspace: foo +`, pipelineName, taskName, taskName)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: pipelineRunName, - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: pipelineName, - }, - Workspaces: []v1alpha1.WorkspaceBinding{ - { - Name: "foo", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }, - { - Name: "foo", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }, - }, - }, - } + pipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s + workspaces: + - name: foo + emptyDir: {} + - name: foo + emptyDir: {} +`, pipelineRunName, pipelineName)) _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}) if err == nil || !strings.Contains(err.Error(), "provided by pipelinerun more than once") { @@ -223,66 +187,48 @@ func TestWorkspacePipelineRunMissingWorkspaceInvalid(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1alpha1.Task{ - ObjectMeta: metav1.ObjectMeta{ - Name: taskName, - }, - Spec: v1alpha1.TaskSpec{ - TaskSpec: v1beta1.TaskSpec{ - Steps: []v1alpha1.Step{{ - Container: corev1.Container{ - Image: "alpine", - }, - Script: "cat /workspace/test/file", - }}, - Workspaces: []v1alpha1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test/file", - ReadOnly: true, - }}, - }, - }, - } + task := parse.MustParseAlphaTask(t, fmt.Sprintf(` +metadata: + name: %s +spec: + steps: + - image: alpine + script: 'cat /workspace/test/file' + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test/file + readOnly: true +`, taskName)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: pipelineName, - }, - Spec: v1alpha1.PipelineSpec{ - Tasks: []v1alpha1.PipelineTask{{ - TaskRef: &v1alpha1.TaskRef{ - Name: taskName, - }, - Name: taskName, - Workspaces: []v1alpha1.WorkspacePipelineTaskBinding{{ - Name: "test", - Workspace: "foo", - SubPath: "", - }}, - }}, - Workspaces: []v1alpha1.PipelineWorkspaceDeclaration{{ - Name: "foo", - }}, - }, - } + pipeline := parse.MustParseAlphaPipeline(t, fmt.Sprintf(` +metadata: + name: %s +spec: + workspaces: + - name: foo + tasks: + - name: %s + taskRef: + name: %s + workspaces: + - name: test + workspace: foo +`, pipelineName, taskName, taskName)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } - pipelineRun := &v1alpha1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: pipelineRunName, - }, - Spec: v1alpha1.PipelineRunSpec{ - PipelineRef: &v1alpha1.PipelineRef{ - Name: pipelineName, - }, - }, - } + pipelineRun := parse.MustParseAlphaPipelineRun(t, fmt.Sprintf(` +metadata: + name: %s +spec: + pipelineRef: + name: %s +`, pipelineRunName, pipelineName)) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun: %s", err) } diff --git a/test/windows_script_test.go b/test/windows_script_test.go index 542c1f26147..8791ad3fe37 100644 --- a/test/windows_script_test.go +++ b/test/windows_script_test.go @@ -17,8 +17,11 @@ package test import ( "context" + "fmt" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" corev1 "k8s.io/api/core/v1" @@ -40,29 +43,25 @@ func TestWindowsScript(t *testing.T) { taskRunName := "windows-script-taskrun" t.Logf("Creating TaskRun in namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "mcr.microsoft.com/powershell:nanoserver", - }, - Script: `#!win pwsh.exe -File -echo Hello`, - }, { - Container: corev1.Container{ - Image: "mcr.microsoft.com/powershell:nanoserver", - }, - Script: `#!win -echo Hello`, - }}, - }, - PodTemplate: &v1beta1.PodTemplate{ - NodeSelector: map[string]string{"kubernetes.io/os": "windows"}, - }, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + podTemplate: + nodeSelector: + kubernetes.io/os: windows + taskSpec: + steps: + - image: mcr.microsoft.com/powershell:nanoserver + script: |- + #!win pwsh.exe -File + echo Hello + - image: mcr.microsoft.com/powershell:nanoserver + script: |- + #!win + echo Hello +`, taskRunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -115,35 +114,29 @@ func TestWindowsScriptFailure(t *testing.T) { taskRunName := "failing-windows-taskrun" t.Logf("Creating TaskRun in namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "mcr.microsoft.com/powershell:nanoserver", - }, - Script: `#!win pwsh.exe -File -echo Hello`, - }, { - Container: corev1.Container{ - Image: "mcr.microsoft.com/powershell:nanoserver", - }, - Script: `#!win pwsh.exe -File -exit 42`, - }, { - Container: corev1.Container{ - Image: "mcr.microsoft.com/powershell:nanoserver", - }, - Script: `#!win pwsh.exe -File -echo Hello`, - }}, - }, - PodTemplate: &v1beta1.PodTemplate{ - NodeSelector: map[string]string{"kubernetes.io/os": "windows"}, - }, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + podTemplate: + nodeSelector: + kubernetes.io/os: windows + taskSpec: + steps: + - image: mcr.microsoft.com/powershell:nanoserver + script: |- + #!win pwsh.exe -File + echo Hello + - image: mcr.microsoft.com/powershell:nanoserver + script: |- + #!win pwsh.exe -File + exit 42 + - image: mcr.microsoft.com/powershell:nanoserver + script: |- + #!win pwsh.exe -File + echo Hello +`, taskRunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/windows_test.go b/test/windows_test.go index e77d38e6583..0c4860cc2c9 100644 --- a/test/windows_test.go +++ b/test/windows_test.go @@ -20,8 +20,11 @@ package test import ( "context" + "fmt" "testing" + "github.com/tektoncd/pipeline/test/parse" + "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" @@ -49,23 +52,20 @@ func TestWindows(t *testing.T) { taskRunName := "windows-taskrun" t.Logf("Creating TaskRun in namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "mcr.microsoft.com/windows/nanoserver:1809", - Command: []string{"cmd.exe"}, - Args: []string{"/c", "echo hello"}, - }, - }}, - }, - PodTemplate: &v1beta1.PodTemplate{ - NodeSelector: map[string]string{"kubernetes.io/os": "windows"}, - }, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + podTemplate: + nodeSelector: + kubernetes.io/os: windows + taskSpec: + steps: + - args: ['-c', 'echo hello'] + command: ['cmd.exe'] + image: mcr.microsoft.com/windows/nanoserver:1809 +`, taskRunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -109,33 +109,26 @@ func TestWindowsFailure(t *testing.T) { taskRunName := "failing-windows-taskrun" t.Logf("Creating TaskRun in namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskSpec: &v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{ - Image: "mcr.microsoft.com/windows/nanoserver:1809", - Command: []string{"cmd.exe"}, - Args: []string{"/c", "echo hello"}, - }}, { - Container: corev1.Container{ - Image: "mcr.microsoft.com/windows/nanoserver:1809", - Command: []string{"cmd.exe"}, - Args: []string{"/c", "exit 1"}, - }}, { - Container: corev1.Container{ - Image: "mcr.microsoft.com/windows/nanoserver:1809", - Command: []string{"cmd.exe"}, - Args: []string{"/c", "ping localhost -n 5"}, - }}, - }, - }, - PodTemplate: &v1beta1.PodTemplate{ - NodeSelector: map[string]string{"kubernetes.io/os": "windows"}, - }, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + podTemplate: + nodeSelector: + kubernetes.io/os: windows + taskSpec: + steps: + - args: ['-c', 'echo hello'] + command: ['cmd.exe'] + image: mcr.microsoft.com/windows/nanoserver:1809 + - args: ['-c', 'exit 1'] + command: ['cmd.exe'] + image: mcr.microsoft.com/windows/nanoserver:1809 + - args: ['-c', 'ping localhost -n 5'] + command: ['cmd.exe'] + image: mcr.microsoft.com/windows/nanoserver:1809 +`, taskRunName, namespace)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/workingdir_test.go b/test/workingdir_test.go index 91ef7f352cb..5e1d47065fc 100644 --- a/test/workingdir_test.go +++ b/test/workingdir_test.go @@ -20,10 +20,12 @@ package test import ( "context" + "fmt" "strings" "testing" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -44,28 +46,30 @@ func TestWorkingDirCreated(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: wdTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "ubuntu", - WorkingDir: "/workspace/HELLOMOTO", - Args: []string{"-c", "echo YES"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: ubuntu + workingDir: /workspace/HELLOMOTO + args: ['-c', 'echo YES'] +`, wdTaskName, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } t.Logf("Creating TaskRun namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: wdTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: wdTaskName}, - ServiceAccountName: "default", - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + serviceAccountName: default +`, wdTaskRunName, namespace, wdTaskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -112,28 +116,30 @@ func TestWorkingDirIgnoredNonSlashWorkspace(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: wdTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "ubuntu", - WorkingDir: "/HELLOMOTO", - Args: []string{"-c", "echo YES"}, - }}}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: ubuntu + workingDir: /HELLOMOTO + args: ['-c', 'echo YES'] +`, wdTaskName, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } t.Logf("Creating TaskRun namespace %s", namespace) - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: wdTaskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: wdTaskName}, - ServiceAccountName: "default", - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + serviceAccountName: default +`, wdTaskRunName, namespace, wdTaskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/workspace_test.go b/test/workspace_test.go index f9b22cce705..5f3bb3d32ae 100644 --- a/test/workspace_test.go +++ b/test/workspace_test.go @@ -20,11 +20,13 @@ package test import ( "context" + "fmt" "strings" "testing" "time" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -42,36 +44,36 @@ func TestWorkspaceReadOnlyDisallowsWrite(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: taskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "alpine"}, - Script: "echo foo > /workspace/test/file", - }}, - Workspaces: []v1beta1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test", - ReadOnly: true, - }}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'echo foo > /workspace/test/file' + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test + readOnly: true +`, taskName, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: taskName}, - ServiceAccountName: "default", - Workspaces: []v1beta1.WorkspaceBinding{{ - Name: "test", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }}, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + serviceAccountName: default + workspaces: + - name: test + emptyDir: {} +`, taskRunName, namespace, taskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } @@ -120,58 +122,56 @@ func TestWorkspacePipelineRunDuplicateWorkspaceEntriesInvalid(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: taskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "alpine"}, - Script: "cat /workspace/test/file", - }}, - Workspaces: []v1beta1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test/file", - ReadOnly: true, - }}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'cat /workspace/test/file' + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test + readOnly: true +`, taskName, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Workspaces: []v1beta1.PipelineWorkspaceDeclaration{{ - Name: "foo", - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "task1", - TaskRef: &v1beta1.TaskRef{Name: taskName}, - Workspaces: []v1beta1.WorkspacePipelineTaskBinding{{ - Name: "test", - Workspace: "foo", - }}, - }}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + workspaces: + - name: foo + tasks: + - name: task1 + taskRef: + name: %s + workspaces: + - name: test + workspace: foo +`, pipelineName, namespace, taskName)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName, Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: pipelineName}, - Workspaces: []v1beta1.WorkspaceBinding{{ - Name: "foo", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }, { - Name: "foo", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }}, - }, - } + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s + workspaces: + - name: foo + emptyDir: {} + - name: foo + emptyDir: {} +`, pipelineRunName, namespace, pipelineName)) _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}) if err == nil || !strings.Contains(err.Error(), "provided by pipelinerun more than once") { @@ -192,51 +192,51 @@ func TestWorkspacePipelineRunMissingWorkspaceInvalid(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: taskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{ - Container: corev1.Container{Image: "alpine"}, - Script: "cat /workspace/test/file", - }}, - Workspaces: []v1beta1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test/file", - ReadOnly: true, - }}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + script: 'cat /workspace/test/file' + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test + readOnly: true +`, taskName, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - pipeline := &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Workspaces: []v1beta1.PipelineWorkspaceDeclaration{{ - Name: "foo", - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "task1", - TaskRef: &v1beta1.TaskRef{Name: taskName}, - Workspaces: []v1beta1.WorkspacePipelineTaskBinding{{ - Name: "test", - Workspace: "foo", - }}, - }}, - }, - } + pipeline := parse.MustParsePipeline(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + workspaces: + - name: foo + tasks: + - name: task1 + taskRef: + name: %s + workspaces: + - name: test + workspace: foo +`, pipelineName, namespace, taskName)) if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Pipeline: %s", err) } - pipelineRun := &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRunName, Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: pipelineName}, - }, - } + pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + pipelineRef: + name: %s +`, pipelineRunName, namespace, pipelineName)) if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create PipelineRun: %s", err) } @@ -261,38 +261,38 @@ func TestWorkspaceVolumeNameMatchesVolumeVariableReplacement(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) defer tearDown(ctx, t, c, namespace) - task := &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: taskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Steps: []v1beta1.Step{{Container: corev1.Container{ - Name: "foo", - Image: "alpine", - Command: []string{"echo"}, - Args: []string{"$(workspaces.test.volume)"}, - }}}, - Workspaces: []v1beta1.WorkspaceDeclaration{{ - Name: "test", - Description: "test workspace", - MountPath: "/workspace/test/file", - ReadOnly: true, - }}, - }, - } + task := parse.MustParseTask(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + steps: + - image: alpine + name: foo + command: ['echo'] + args: ['$(workspaces.test.volume)'] + workspaces: + - name: test + description: 'test workspace' + mountPath: /workspace/test/file + readOnly: true +`, taskName, namespace)) if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task: %s", err) } - taskRun := &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace}, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{Name: taskName}, - ServiceAccountName: "default", - Workspaces: []v1beta1.WorkspaceBinding{{ - Name: "test", - EmptyDir: &corev1.EmptyDirVolumeSource{}, - }}, - }, - } + taskRun := parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + taskRef: + name: %s + serviceAccountName: default + workspaces: + - name: test + emptyDir: {} +`, taskRunName, namespace, taskName)) if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create TaskRun: %s", err) } diff --git a/test/yaml.go b/test/yaml.go deleted file mode 100644 index 1f4d9a7a3c0..00000000000 --- a/test/yaml.go +++ /dev/null @@ -1,79 +0,0 @@ -/* -Copyright 2021 The Tekton Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package test - -import ( - "testing" - - resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" - - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/scheme" - "k8s.io/apimachinery/pkg/runtime" -) - -func mustParseYAML(t *testing.T, yaml string, i runtime.Object) { - if _, _, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(yaml), nil, i); err != nil { - t.Fatalf("mustParseYAML (%s): %v", yaml, err) - } -} - -func mustParseTaskRun(t *testing.T, yaml string) *v1beta1.TaskRun { - var tr v1beta1.TaskRun - yaml = `apiVersion: tekton.dev/v1beta1 -kind: TaskRun -` + yaml - mustParseYAML(t, yaml, &tr) - return &tr -} - -func mustParseTask(t *testing.T, yaml string) *v1beta1.Task { - var task v1beta1.Task - yaml = `apiVersion: tekton.dev/v1beta1 -kind: Task -` + yaml - mustParseYAML(t, yaml, &task) - return &task -} - -func mustParsePipelineRun(t *testing.T, yaml string) *v1beta1.PipelineRun { - var pr v1beta1.PipelineRun - yaml = `apiVersion: tekton.dev/v1beta1 -kind: PipelineRun -` + yaml - mustParseYAML(t, yaml, &pr) - return &pr -} - -func mustParsePipeline(t *testing.T, yaml string) *v1beta1.Pipeline { - var pipeline v1beta1.Pipeline - yaml = `apiVersion: tekton.dev/v1beta1 -kind: Pipeline -` + yaml - mustParseYAML(t, yaml, &pipeline) - return &pipeline -} - -func mustParsePipelineResource(t *testing.T, yaml string) *resourcev1alpha1.PipelineResource { - var resource v1alpha1.PipelineResource - yaml = `apiVersion: tekton.dev/v1alpha1 -kind: PipelineResource -` + yaml - mustParseYAML(t, yaml, &resource) - return &resource -}