Skip to content

Commit

Permalink
Propagated Parameters e2e tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chitrangpatel committed Oct 12, 2022
1 parent 5d34b0c commit 02b79a2
Showing 1 changed file with 382 additions and 0 deletions.
382 changes: 382 additions & 0 deletions test/propagated_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
//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/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
"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"
runtime "k8s.io/apimachinery/pkg/runtime"
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
knativetest "knative.dev/pkg/test"
)

var (
ignoreTypeMeta = cmpopts.IgnoreFields(metav1.TypeMeta{}, "Kind", "APIVersion")
ignoreObjectMeta = cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion", "UID", "CreationTimestamp", "Generation", "ManagedFields", "Labels")
ignoreCondition = cmpopts.IgnoreFields(apis.Condition{}, "LastTransitionTime.Inner.Time", "Message")
ignorePipelineRunStatus = cmpopts.IgnoreFields(v1beta1.PipelineRunStatusFields{}, "StartTime", "CompletionTime", "FinallyStartTime")
ignoreTaskRunStatus = cmpopts.IgnoreFields(v1beta1.TaskRunStatusFields{}, "StartTime", "CompletionTime")
ignoreConditions = cmpopts.IgnoreFields(duckv1beta1.Status{}, "Conditions")
ignoreContainerStates = cmpopts.IgnoreFields(corev1.ContainerState{}, "Terminated")
ignoreStepState = cmpopts.IgnoreFields(v1beta1.StepState{}, "ImageID")
)

func TestPropagatedParams(t *testing.T) {
t.Parallel()
type tests struct {
name string
pipelineName string
taskName string
pipelineRunFunc func(*testing.T, string) (*v1beta1.PipelineRun, *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)

embeddedStatus := GetEmbeddedStatus(ctx, t, c.KubeClient)
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, expectedResolvedPipelineRun := td.pipelineRunFunc(t, namespace)
if embeddedStatus == config.MinimalEmbeddedStatus {
expectedResolvedPipelineRun.Status.ChildReferences = []v1beta1.ChildStatusReference{}
for k, v := range expectedResolvedPipelineRun.Status.TaskRuns {
k := k
v := v
cr := v1beta1.ChildStatusReference{
TypeMeta: runtime.TypeMeta{APIVersion: "tekton.dev/v1beta1", Kind: "TaskRun"},
Name: k,
PipelineTaskName: v.PipelineTaskName,
}
expectedResolvedPipelineRun.Status.ChildReferences = append(expectedResolvedPipelineRun.Status.ChildReferences, cr)
}
expectedResolvedPipelineRun.Status.TaskRuns = nil
}
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)
}
cl, _ := c.PipelineRunClient.Get(ctx, prName, metav1.GetOptions{})
d := cmp.Diff(expectedResolvedPipelineRun, cl,
ignoreTypeMeta,
ignoreObjectMeta,
ignoreCondition,
ignorePipelineRunStatus,
ignoreTaskRunStatus,
ignoreConditions,
ignoreContainerStates,
ignoreStepState,
)
if d != "" {
t.Fatalf(`The resolved spec does not match the expected spec. Here is the diff: %v`, d)
}
t.Logf("Successfully finished test %q", td.name)
})
}
}

func getPropagatedParamPipelineRun(t *testing.T, namespace string) (*v1beta1.PipelineRun, *v1beta1.PipelineRun) {
pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: propagated-parameters-fully
namespace: %s
spec:
params:
- name: HELLO
value: "Hello World!"
pipelineSpec:
tasks:
- name: echo-hello
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
finally:
- name: echo-hello-finally
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
`, namespace))
expectedPipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: propagated-parameters-fully
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
params:
- name: HELLO
value: "Hello World!"
pipelineSpec:
tasks:
- name: echo-hello
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
finally:
- name: echo-hello-finally
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
status:
taskRuns:
propagated-parameters-fully-echo-hello:
pipelineTaskName: echo-hello
status:
podName: propagated-parameters-fully-echo-hello-pod
steps:
- name: echo
container: step-echo
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo Hello World!
propagated-parameters-fully-echo-hello-finally:
pipelineTaskName: echo-hello-finally
status:
podName: propagated-parameters-fully-echo-hello-finally-pod
steps:
- name: echo
container: step-echo
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo Hello World!
pipelineSpec:
tasks:
- name: echo-hello
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo Hello World!
finally:
- name: echo-hello-finally
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo Hello World!
`, namespace))
return pipelineRun, expectedPipelineRun
}

func getPropagatedParamWithScopePipelineRun(t *testing.T, namespace string) (*v1beta1.PipelineRun, *v1beta1.PipelineRun) {
pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: propagated-parameters-inner-scope
namespace: %s
spec:
params:
- name: HELLO
value: "Pipeline Hello World!"
pipelineSpec:
tasks:
- name: echo-hello
params:
- name: HELLO
value: "Hello World!"
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
`, namespace))
expectedPipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: propagated-parameters-inner-scope
namespace: %s
spec:
timeout: 1h
serviceAccountName: default
params:
- name: HELLO
value: "Pipeline Hello World!"
pipelineSpec:
tasks:
- name: echo-hello
params:
- name: HELLO
value: "Hello World!"
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
status:
taskRuns:
propagated-parameters-inner-scope-echo-hello:
pipelineTaskName: echo-hello
status:
podName: propagated-parameters-inner-scope-echo-hello-pod
steps:
- name: echo
container: step-echo
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo Hello World!
pipelineSpec:
tasks:
- name: echo-hello
params:
- name: HELLO
value: "Hello World!"
taskSpec:
steps:
- name: echo
image: ubuntu
script: echo Hello World!
`, namespace))

return pipelineRun, expectedPipelineRun
}

func getPropagatedParamWithDefaultScopePipelineRun(t *testing.T, namespace string) (*v1beta1.PipelineRun, *v1beta1.PipelineRun) {
pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: propagated-parameters-default-inner-scope
namespace: %s
spec:
params:
- name: HELLO
value: "Hello World!"
pipelineSpec:
tasks:
- name: echo-hello
taskSpec:
params:
- name: HELLO
type: string
default: "Default Hello World"
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
`, namespace))
expectedPipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: propagated-parameters-default-inner-scope
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
params:
- name: HELLO
value: "Hello World!"
pipelineSpec:
tasks:
- name: echo-hello
taskSpec:
params:
- name: HELLO
type: string
default: "Default Hello World"
steps:
- name: echo
image: ubuntu
script: echo $(params.HELLO)
status:
taskRuns:
propagated-parameters-default-inner-scope-echo-hello:
pipelineTaskName: echo-hello
status:
podName: propagated-parameters-default-inner-scope-echo-hello-pod
steps:
- name: echo
container: step-echo
taskSpec:
params:
- name: HELLO
type: string
default: "Default Hello World"
steps:
- name: echo
image: ubuntu
script: echo Hello World!
pipelineSpec:
tasks:
- name: echo-hello
taskSpec:
params:
- name: HELLO
type: string
default: "Default Hello World"
steps:
- name: echo
image: ubuntu
script: echo Hello World!
`, namespace))
return pipelineRun, expectedPipelineRun
}

0 comments on commit 02b79a2

Please sign in to comment.