Skip to content

Commit

Permalink
Add upgrade test against previous server version to prevent regressions
Browse files Browse the repository at this point in the history
This commit adds an upgrade test cases to test against previous server
version by adding a light-weight test checking the existing functionalities of
simple resources so as to prevent regressions introduced in the current
release. It creates a pipelineRun with a basic TaskRun and a basic
Pipeline with a Simple PipelineTask and examines the runs created are successful.

This helps prevent the regression where the client has introduced regressions towards the last release i.e. [Regression: PipelineTaskRunSpec.Metadata incompatible with older webhooks](tektoncd#4913)
  • Loading branch information
JeromeJu committed Jul 7, 2023
1 parent 1457f43 commit 384459a
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 2 deletions.
10 changes: 8 additions & 2 deletions test/e2e-tests-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
# and deploy Tekton Pipelines to it for running upgrading tests. There are
# two scenarios we need to cover in this script:

# Scenario 1: install the previous release, upgrade to the current release, and
# Scenario 1: install the previous release, test on previous server version
# to prevent regression, and then upgrade to the current release, and
# validate whether the Tekton pipeline works.

# Scenario 2: install the previous release, create the pipelines and tasks, upgrade
Expand All @@ -41,17 +42,22 @@ header "Setting up environment"
set +o errexit
set +o pipefail

export SYSTEM_NAMESPACE=tekton-pipelines

# First, we will verify if Scenario 1 works.
# Install the previous release.
header "Install the previous release of Tekton pipeline $PREVIOUS_PIPELINE_VERSION"
install_pipeline_crd_version $PREVIOUS_PIPELINE_VERSION

failed=0
# The upgrade test for the old server version to prevent regression on existing changes.
go_test_e2e -timeout=20m ./test -run ^TestUpgradeAgainstPreviousVersion || failed=1

# Upgrade to the current release.
header "Upgrade to the current release of Tekton pipeline"
install_pipeline_crd

# Run the integration tests.
failed=0
go_test_e2e -timeout=20m ./test || failed=1

# Run the post-integration tests.
Expand Down
244 changes: 244 additions & 0 deletions test/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
//go:build e2e
// +build e2e

/*
Copyright 2023 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"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"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"
"knative.dev/pkg/test/helpers"
)

var (
filterOwnerReference = cmpopts.IgnoreFields(metav1.OwnerReference{}, "UID", "Controller", "BlockOwnerDeletion")
filterV1SidecarState = cmpopts.IgnoreFields(v1.SidecarState{}, "ImageID")
filterVolumeMountsName = cmpopts.IgnoreFields(corev1.VolumeMount{}, "Name")
filterTaskRunStatusProvenance = cmpopts.IgnoreFields(v1.TaskRunStatusFields{}, "Provenance")
filterPipelineRunStatusProvenance = cmpopts.IgnoreFields(v1.PipelineRunStatusFields{}, "Provenance")
)

// TestUpgradeAgainstPreviousVersion creates a pipelineRun with a basic
// TaskRun and a basic Pipeline and examines the events and runs created
// are successful.
func TestUpgradeAgainstPreviousVersion(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, namespace := setup(ctx, t)

knativetest.CleanupOnInterrupt(func() { tearDown(context.Background(), t, c, namespace) }, t.Logf)
defer tearDown(context.Background(), t, c, namespace)

t.Logf("Creating Task in namespace %s", namespace)
task := parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
params:
- name: rsp
type: string
default: "response"
steps:
- name: echo-param
image: alpine
script: |
echo "$(params.rsp)"
- name: check-workspace
image: alpine
script: |
if [ "$(workspaces.taskWorkspace.bound)" == "true" ]; then
echo "Workspace provided"
fi
workspaces:
- name: taskWorkspace
`, task1Name, namespace))
if _, err := c.V1TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", task.Name, err)
}

pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
tasks:
- name: %s
taskRef:
name: %s
workspaces:
- name: taskWorkspace
workspace: workspace
workspaces:
- name: workspace
`, helpers.ObjectNameForTest(t), namespace, task1Name, task.Name))
pipelineName := pipeline.Name
pipelineRun := parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
pipelineRef:
name: %s
workspaces:
- name: workspace
emptyDir: {}
timeouts:
pipeline: 180s
`, helpers.ObjectNameForTest(t), namespace, pipeline.Name))
pipelineRunName := pipelineRun.Name

if _, err := c.V1PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err)
}
if _, err := c.V1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun `%s`: %s", pipelineRunName, err)
}

t.Logf("Waiting for PipelineRun %s in namespace %s to complete", pipelineRunName, namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, timeout, PipelineRunSucceed(pipelineRunName), "PipelineRunSuccess", v1Version); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to finish: %s", pipelineRunName, err)
}

taskRunName := strings.Join([]string{pipelineRunName, task1Name}, "-")
r, err := c.V1TaskRunClient.Get(ctx, taskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected TaskRun %s: %s", taskRunName, err)
}

expectedTaskRun := getExpectedTaskRunUpgrade(t, taskRunName, namespace, pipelineRunName, task1Name)
if d := cmp.Diff(expectedTaskRun, r, append([]cmp.Option{filterV1TaskRunSA, filterV1StepState, filterOwnerReference, filterV1TaskRunStatus, filterV1SidecarState, filterVolumeMountsName, filterTaskRunStatusProvenance}, filterV1TaskRunFields...)...); d != "" {
t.Errorf("Cannot get expected TaskRun, -want, +got: %v", d)
}

pr, err := c.V1PipelineRunClient.Get(ctx, pipelineRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected PipelineRun %s: %s", pipelineRunName, err)
}

expectedPipelineRun := getExpectedPipelineRunUpgrade(t, pipelineRunName, namespace, pipelineName, taskRunName)
if d := cmp.Diff(expectedPipelineRun, pr, append([]cmp.Option{filterV1PipelineRunStatus, filterV1PipelineRunSA, filterPipelineRunStatusProvenance}, filterV1PipelineRunFields...)...); d != "" {
t.Errorf("Cannot get expected PipelineRun, -want, +got: %v", d)
}
}

func getExpectedTaskRunUpgrade(t *testing.T, taskRunName, namespace, pipelineRunName, task1Name string) *v1.TaskRun {
expectedTaskRun := parse.MustParseV1TaskRun(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
ownerReferences:
- apiVersion: "tekton.dev/v1"
kind: "PipelineRun"
name: %s
spec:
taskRef:
name: %s
kind: Task
timeout: 1h
workspaces:
- name: taskWorkspace
emptyDir: {}
status:
conditions:
- type: Succeeded
reason: Succeeded
status: "True"
podName: %s-pod
taskSpec:
steps:
- computeResources: {}
image: alpine
name: echo-param
script: |
echo "response"
- name: check-workspace
image: alpine
script: |
if [ "true" == "true" ]; then
echo "Workspace provided"
fi
workspaces:
- name: taskWorkspace
params:
- name: rsp
type: string
default: response
steps:
- container: step-echo
name: step-echo
terminated:
reason: Completed
- container: check-workspace
name: check-workspace
terminated:
reason: Completed
`, taskRunName, namespace, pipelineRunName, task1Name, taskRunName))
return expectedTaskRun
}

func getExpectedPipelineRunUpgrade(t *testing.T, pipelineRunName, namespace, pipelineName, taskRunName string) *v1.PipelineRun {
expectedPipelineRun := parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
timeouts:
pipeline: 3m
pipelineRef:
name: %s
kind: Pipeline
workspaces:
- name: workspace
emptyDir: {}
status:
conditions:
- type: Succeeded
reason: Succeeded
status: "True"
pipelineSpec:
tasks:
- name: task1
taskRef:
name: task1
kind: Task
workspaces:
- name: taskWorkspace
workspace: workspace
workspaces:
- name: workspace
emptyDir: {}
childReferences:
- name: %s
pipelineTaskName: task1
apiVersion: "tekton.dev/v1"
kind: TaskRun
`, pipelineRunName, namespace, pipelineName, taskRunName))
return expectedPipelineRun
}

0 comments on commit 384459a

Please sign in to comment.