From 830c7352e4a584109109705f281c77a319c71017 Mon Sep 17 00:00:00 2001 From: Chitrang Patel Date: Tue, 4 Oct 2022 12:42:58 -0400 Subject: [PATCH] Propagated Parameters e2e tests Prior to this, we only had examples for propagated parameters. This PR adds e2e tests for testing this feature instead of simply relying on the examples. --- test/propagated_params_test.go | 161 +++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 test/propagated_params_test.go diff --git a/test/propagated_params_test.go b/test/propagated_params_test.go new file mode 100644 index 00000000000..ebc49504ec4 --- /dev/null +++ b/test/propagated_params_test.go @@ -0,0 +1,161 @@ +//go:build e2e +// +build e2e + +/* +Copyright 2022 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 ( + "context" + "fmt" + "testing" + + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/test/parse" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + knativetest "knative.dev/pkg/test" +) + +func TestPropagatedParams(t *testing.T) { + t.Parallel() + type tests struct { + name string + pipelineName string + taskName string + pipelineRunFunc func(*testing.T, string, string, string) *v1beta1.PipelineRun + } + + tds := []tests{{ + name: "propagated parameters fully", + pipelineName: "propagated-parameters-fully", + taskName: "echo-hello", + pipelineRunFunc: getPropagatedParamPipelineRun, + }, { + name: "propagated parameters with inner scope", + pipelineName: "propagated-parameters-inner-scope", + taskName: "echo-hello", + pipelineRunFunc: getPropagatedParamWithScopePipelineRun, + }, { + name: "propagated parameters with default inner scope", + pipelineName: "propagated-parameters-default-inner-scope", + taskName: "echo-hello", + pipelineRunFunc: getPropagatedParamWithDefaultScopePipelineRun, + }} + + for _, td := range tds { + td := td + t.Run(td.name, func(t *testing.T) { + t.Parallel() + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + propagatedParamFlags := requireAllGates(map[string]string{ + "enable-api-fields": "alpha", + }) + c, namespace := setup(ctx, t, propagatedParamFlags) + + knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) + defer tearDown(ctx, t, c, namespace) + + t.Logf("Setting up test resources for %q test in namespace %s", td.name, namespace) + pipelineRun := td.pipelineRunFunc(t, namespace, td.pipelineName, td.taskName) + prName := pipelineRun.Name + _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) + } + + t.Logf("Waiting for PipelineRun %s in namespace %s to complete", prName, namespace) + if err := WaitForPipelineRunState(ctx, c, prName, timeout, PipelineRunSucceed(prName), "PipelineRunSuccess"); err != nil { + t.Fatalf("Error waiting for PipelineRun %s to finish: %s", prName, err) + } + t.Log("Checking if parameter replacements have been updated in the spec.") + cl, _ := c.PipelineRunClient.Get(ctx, prName, metav1.GetOptions{}) + if cl.Status.PipelineSpec.Tasks[0].TaskSpec.Steps[0].Script != "echo Hello World!" { + t.Fatalf(`Expected replaced parameter value %s but found %s`, "echo Hello World!", cl.Status.PipelineSpec.Tasks[0].TaskSpec.Steps[0].Script) + } + t.Logf("Successfully finished test %q", td.name) + }) + } +} + +func getPropagatedParamPipelineRun(t *testing.T, namespace string, pipelineName string, taskName string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: HELLO + value: "Hello World!" + pipelineSpec: + tasks: + - name: %s + taskSpec: + steps: + - name: echo + image: ubuntu + script: echo $(params.HELLO) +`, pipelineName, namespace, taskName)) +} + +func getPropagatedParamWithScopePipelineRun(t *testing.T, namespace string, pipelineName string, taskName string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: HELLO + value: "Pipeline Hello World!" + pipelineSpec: + tasks: + - name: %s + params: + - name: HELLO + value: "Hello World!" + taskSpec: + steps: + - name: echo + image: ubuntu + script: echo $(params.HELLO) +`, pipelineName, namespace, taskName)) +} + +func getPropagatedParamWithDefaultScopePipelineRun(t *testing.T, namespace string, pipelineName string, taskName string) *v1beta1.PipelineRun { + return parse.MustParsePipelineRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s +spec: + params: + - name: HELLO + value: "Hello World!" + pipelineSpec: + tasks: + - name: %s + taskSpec: + params: + - name: HELLO + default: "Default Hello World" + steps: + - name: echo + image: ubuntu + script: echo $(params.HELLO) +`, pipelineName, namespace, taskName)) +}