Skip to content

Commit

Permalink
Integration tests for Tekton (#528)
Browse files Browse the repository at this point in the history
* Integration tests for Tekton

* Use knative/client instead of fork

* also put back the e2e-tests into its original format where it doesn't
run the tekton tests

* Run ./hack/build.sh with Go 1.12

* Pull buildah and kn tasks directly from catalog

* Revert "Use knative/client instead of fork"

This reverts commit 2ab272f.

* Revert "Revert "Use knative/client instead of fork""

This reverts commit f14c210.

* Update Tekton to 0.8.0

* Revert "Use knative/client instead of fork"

This reverts commit 2ab272f.

* Use knative/client instead of fork

This reverts commit 8eb87ad.

* Export variables after calling initialize

* Run against arbitrary Docker registry

* Conditionally install Tekton

* Use knative/client instead of fork

This reverts commit 3800adb.

* Revert "Use knative/client instead of fork"

This reverts commit 71a3d33.

* Simplify passing the flag to test

* Simplify imports
  • Loading branch information
mgencur authored and knative-prow-robot committed Dec 6, 2019
1 parent 556f457 commit 60567a9
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 34 deletions.
33 changes: 33 additions & 0 deletions test/e2e-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/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.

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

function cluster_setup() {
header "Building client"
${REPO_ROOT_DIR}/hack/build.sh -f || 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
}
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

# 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

# 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
}
86 changes: 86 additions & 0 deletions test/e2e/tekton_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 {
return wait.PollImmediate(Interval, Timeout, func() (bool, error) {
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
})
}
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
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

0 comments on commit 60567a9

Please sign in to comment.