Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration tests for Tekton #528

Merged
merged 16 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions test/e2e-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# Copyright 2019 The Knative 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.

function cluster_setup() {
header "Building client"
${REPO_ROOT_DIR}/hack/build.sh -f || return 1
}

function knative_setup() {
adrcunha marked this conversation as resolved.
Show resolved Hide resolved
local version=${KNATIVE_VERSION:-latest}
header "Installing Knative serving (${version})"

if [ "${version}" = "latest" ]; then
start_latest_knative_serving
else
start_release_knative_serving "${version}"
fi
}
10 changes: 1 addition & 9 deletions test/e2e-smoke-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@
# the cluster.

source $(dirname $0)/../vendor/knative.dev/test-infra/scripts/e2e-tests.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source $(dirname $0)/../vendor/knative.dev/test-infra/scripts/e2e-tests.sh


# Helper functions.

# Build kn before integration tests, so we fail fast in case of error.
./hack/build.sh -f

function knative_setup() {
start_latest_knative_serving
}
source $(dirname $0)/e2e-common.sh

# Will create and delete this namespace and use it for smoke tests
export KN_E2E_SMOKE_TESTS_NAMESPACE=kne2esmoketests
Expand Down
21 changes: 1 addition & 20 deletions test/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,10 @@
# of this specified version will be installed in the Kubernetes cluster, and
# all the tests will run against Knative serving of this specific version.
source $(dirname $0)/../vendor/knative.dev/test-infra/scripts/e2e-tests.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source $(dirname $0)/../vendor/knative.dev/test-infra/scripts/e2e-tests.sh


# Helper functions.

# Build kn before integration tests, so we fail fast in case of error.
function cluster_setup() {
header "Building client"
${REPO_ROOT_DIR}/hack/build.sh -u || return 1
}

function knative_setup() {
local version=${KNATIVE_VERSION:-latest}
header "Installing Knative serving (${version})"

if [ "${version}" = "latest" ]; then
start_latest_knative_serving
else
start_release_knative_serving "${version}"
fi
}
source $(dirname $0)/e2e-common.sh

# Add local dir to have access to built kn
export PATH=$PATH:${REPO_ROOT_DIR}
export KNATIVE_VERSION=${KNATIVE_VERSION:-latest}

# Script entry point.

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,11 @@ func matchRegexp(t *testing.T, matchingRegexp, actual string) bool {
}
return matched
}

func currentDir(t *testing.T) string {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Unable to read current dir:", err)
}
return dir
}
23 changes: 19 additions & 4 deletions test/e2e/e2e_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,28 @@ package e2e

import (
"flag"
"os"
)

var _ = initializeFlags()
// Flags holds the command line flags or defaults for settings in the user's environment.
// See ClientFlags for the list of supported fields.
var Flags = initializeFlags()

func initializeFlags() bool {
// ClientFlags define the flags that are needed to run the e2e tests.
type ClientFlags struct {
EmitMetrics bool
DockerConfigJSON string
}

func initializeFlags() *ClientFlags {
var f ClientFlags
// emitmetrics is a required flag for running periodic test jobs, add it here as a no-op to avoid the error
emitMetrics := flag.Bool("emitmetrics", false,
flag.BoolVar(&f.EmitMetrics, "emitmetrics", false,
"Set this flag to true if you would like tests to emit metrics, e.g. latency of resources being realized in the system.")
return *emitMetrics

dockerConfigJSON := os.Getenv("DOCKER_CONFIG_JSON")
flag.StringVar(&f.DockerConfigJSON, "dockerconfigjson", dockerConfigJSON,
"Provide the path to Docker configuration file in json format. Defaults to $DOCKER_CONFIG_JSON")

return &f
}
90 changes: 90 additions & 0 deletions test/e2e/tekton_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2019 The Knative 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.

// +build tekton

package e2e

import (
"strings"
"testing"
"time"

"gotest.tools/assert"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/client/pkg/util"
)

const (
// Interval specifies the time between two polls.
Interval = 10 * time.Second
// Timeout specifies the timeout for the function PollImmediate to reach a certain status.
Timeout = 5 * time.Minute
)

func TestTektonPipeline(t *testing.T) {
test := NewE2eTest(t)
test.Setup(t)

kubectl := kubectl{t, Logger{}}
basedir := currentDir(t) + "/../resources/tekton"

// create secret for the kn-deployer-account service account
_, err := kubectl.RunWithOpts([]string{"create", "-n", test.env.Namespace, "secret",
"generic", "container-registry",
"--from-file=.dockerconfigjson=" + Flags.DockerConfigJSON,
"--type=kubernetes.io/dockerconfigjson"}, runOpts{})
assert.NilError(t, err)

_, err = kubectl.RunWithOpts([]string{"apply", "-n", test.env.Namespace, "-f", basedir + "/kn-deployer-rbac.yaml"}, runOpts{})
assert.NilError(t, err)

_, err = kubectl.RunWithOpts([]string{"apply", "-n", test.env.Namespace, "-f", "https://raw.githubusercontent.com/tektoncd/catalog/master/buildah/buildah.yaml"}, runOpts{})
assert.NilError(t, err)

_, err = kubectl.RunWithOpts([]string{"apply", "-n", test.env.Namespace, "-f", "https://raw.githubusercontent.com/tektoncd/catalog/master/kn/kn.yaml"}, runOpts{})
assert.NilError(t, err)

_, err = kubectl.RunWithOpts([]string{"apply", "-n", test.env.Namespace, "-f", basedir + "/kn-pipeline.yaml"}, runOpts{})
assert.NilError(t, err)

_, err = kubectl.RunWithOpts([]string{"apply", "-n", test.env.Namespace, "-f", basedir + "/kn-pipeline-resource.yaml"}, runOpts{})
assert.NilError(t, err)

_, err = kubectl.RunWithOpts([]string{"create", "-n", test.env.Namespace, "-f", basedir + "/kn-pipeline-run.yaml"}, runOpts{})
assert.NilError(t, err)

err = waitForPipelineSuccess(t, kubectl, test.env.Namespace)
assert.NilError(t, err)

const serviceName = "hello"
out, err := test.kn.RunWithOpts([]string{"service", "describe", serviceName}, runOpts{NoNamespace: false})
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, serviceName, test.kn.namespace))
assert.Assert(t, util.ContainsAll(out, "Conditions", "ConfigurationsReady", "Ready", "RoutesReady"))

// tear down only if the test passes, we want to keep the pods otherwise
test.Teardown(t)
}

func waitForPipelineSuccess(t *testing.T, k kubectl, namespace string) error {
waitErr := wait.PollImmediate(Interval, Timeout, func() (bool, error) {
mgencur marked this conversation as resolved.
Show resolved Hide resolved
out, err := k.RunWithOpts([]string{"get", "pipelinerun", "-n", namespace, "-o=jsonpath='{.items[0].status.conditions[?(@.type==\"Succeeded\")].status}'"}, runOpts{})
return strings.Contains(out, "True"), err
})
if waitErr != nil {
return waitErr
}
return nil
}
50 changes: 50 additions & 0 deletions test/resources/tekton/kn-deployer-rbac-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2019 The Knative 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.
# Define a ServiceAccount named kn-deployer-account that has permission to
# manage Knative services.
apiVersion: v1
kind: ServiceAccount
metadata:
name: kn-deployer-account
namespace: ${KN_E2E_NAMESPACE}0
secrets:
- name: container-registry
imagePullSecrets:
- name: container-registry

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: kn-deployer
rules:
- apiGroups: ["serving.knative.dev"]
resources: ["services"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: kn-deployer-binding
subjects:
- kind: ServiceAccount
name: kn-deployer-account
namespace: ${KN_E2E_NAMESPACE}0
roleRef:
kind: ClusterRole
name: kn-deployer
apiGroup: rbac.authorization.k8s.io
32 changes: 32 additions & 0 deletions test/resources/tekton/kn-pipeline-resource-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2019 The Knative 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.
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
name: buildah-build-kn-create-source
spec:
type: git
params:
- name: url
value: "https://github.com/knative/client"
---
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
name: buildah-build-kn-create-image
spec:
type: image
params:
- name: url
value: "${CONTAINER_REGISTRY}/helloworld:tkn"
38 changes: 38 additions & 0 deletions test/resources/tekton/kn-pipeline-run.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2019 The Knative 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.
apiVersion: tekton.dev/v1alpha1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the version be parametrized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the tests fail with a newer version of Tekton then these files will need to be updated at that point and committed. The tests are there for testing regressions with future versions of Tekton / kn. They're not there to test compatibility of multiple versions of Tekton with different apiVersion versions; so IMO adding this as a parameter would not bring any benefits.

kind: PipelineRun
metadata:
generateName: buildah-build-kn-create-
spec:
serviceAccount: kn-deployer-account
pipelineRef:
name: buildah-build-kn-create
resources:
- name: source
resourceRef:
name: buildah-build-kn-create-source
- name: image
resourceRef:
name: buildah-build-kn-create-image
params:
- name: ARGS
value:
- "service"
- "create"
- "hello"
- "--force"
- "--service-account=kn-deployer-account"
- "--image=$(inputs.resources.image.url)"
- "--env=TARGET=Tekton"
Loading