Skip to content

Commit

Permalink
Run integration tests on presubmit
Browse files Browse the repository at this point in the history
Using the scripts from knative/test-infra, run the integration tests
against a cluster created with boskos. A lot of the boskos stuff is
still kind of magical, for example I have no idea what is controlling
the $PROJECT_ID that is being used by knative projects on presubmit, but
after wrestling with it a bit and hacking around `--run-tests` I was
able to get it to work locally and deploy a cluster (Using boskos if
running from Prow, or kubetest otherwise). I'll follow up on this more
later on and update the docs when I have more info.

Fixes #16
  • Loading branch information
bobcatfish authored and knative-prow-robot committed Oct 4, 2018
1 parent 859ac08 commit 1d8f2b4
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
33 changes: 33 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,37 @@ You can run this locally with:

```shell
test/presubmit-tests.sh
test/presubmit-tests.sh --build-tests
test/presubmit-tests.sh --unit-tests
```

Prow is configured in [the knative `config.yaml` in `knative/test-infra`](https://github.com/knative/test-infra/blob/master/ci/prow/config.yaml)
via the sections for `knative/build-pipeline`.

### Running presubmit integration tests

When run using Prow, integration tests will try to get a new cluster using [boskos](https://github.com/kubernetes/test-infra/tree/master/boskos) and
[these hardcoded GKE projects](https://github.com/knative/test-infra/blob/master/ci/prow/boskos/resources.yaml#L15),
which only [the `knative/test-infra` OWNERS](https://github.com/knative/test-infra/blob/master/OWNERS)
have access to.

If you would like to run the integration tests against your cluster, you can use the
`K8S_CLUSTER_OVERRIDE` environment variable to force the scripts to use your own cluster,
and provide `KO_DOCKER_REPO` (as specified in the [DEVELOPMENT.md](../DEVELOPMENT.md#environment-setup)):

```shell
export K8S_CLUSTER_OVERRIDE=my_k8s_cluster # corresponds to a `context` in your kubeconfig
export KO_DOCKER_REPO=gcr.io/my_docker_repo # required for deployments using `ko`
test/presubmit-tests.sh --integration-tests
```

Or you can set `$PROJECT_ID` to a GCP project and rely on
[kubetest](https://github.com/kubernetes/test-infra/tree/master/kubetest)
to setup a cluster for you:

```shell
export K8S_CLUSTER_OVERRIDE=
export PROJECT_ID=my_gcp_project
export KO_DOCKER_REPO=gcr.io/my_docker_repo # required for deployments using `ko`
test/presubmit-tests.sh --integration-tests
```
75 changes: 75 additions & 0 deletions test/cluster-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Copyright 2018 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.

# This script calls out to scripts in knative/test-infra to setup a cluster
# and deploy the Pipeline CRD to it for running integration tests.

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

set -o xtrace
set -o errexit
set -o pipefail

function take_down_pipeline() {
header "Tearing down Pipeline CRD"
ko delete --ignore-not-found=true -f config/
}
trap take_down_pipeline EXIT

# Called by `fail_test` (provided by `e2e-tests.sh`) to dump info on test failure
function dump_extra_cluster_state() {
for crd in pipelines pipelineruns tasks taskruns resources pipelineparams
do
echo ">>> $crd:"
kubectl get $crd -o yaml --all-namespaces
done
echo ">>> Pipeline controller log:"
kubectl -n knative-build-pipeline logs $(get_app_pod build-pipeline-controller knative-build-pipeline)
echo ">>> Pipeline webhook log:"
kubectl -n knative-build-pipeline logs $(get_app_pod build-pipeline-webhook knative-build-pipeline)
}

set +o xtrace
header "Setting up environment"
# The intialize method will attempt to create a new cluster in $PROJECT_ID unless
# the `--run-tests` parameter is provided and `K8S_CLUSTER_OVERRIDE` is set, however
# since knative/test-infra/scripts/presubmit-tests.sh doesn't propagate `--run-tests`
# (and it's kind of a confusing param name), we'll infer it from the presence of
# `K8S_CLUSTER_OVERRIDE`
if [[ -z ${K8S_CLUSTER_OVERRIDE} ]]; then
initialize
else
initialize --run-tests
fi
set -o xtrace

# The scripts were initially setup to use the knative/serving style `DOCKER_REPO_OVERRIDE`
# before knative/serving was updated to use `ko` + `KO_DOCKER_REPO`. If the scripts were
# called with `KO_DOCKER_REPO` already set (i.e. using a user's own local cluster, we should
# respect that).
if ! [[ -z ${KO_DOCKER_REPO} ]]; then
export DOCKER_REPO_OVERRIDE=${KO_DOCKER_REPO}
fi

# Deploy the latest version of the Pipeline CRD.
# TODO(#59) do we need to deploy the Build CRD as well?
header "Deploying Pipeline CRD"
ko apply -f config/

# Wait for pods to be running in the namespaces we are deploying to
set +o xtrace
wait_until_pods_running knative-build-pipeline || fail_test "Pipeline CRD did not come up"
set -o xtrace
1 change: 1 addition & 0 deletions test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func tearDownMain(kubeClient *knativetest.KubeClient, logger *logging.BaseLogger
func TestMain(m *testing.M) {
initializeLogsAndMetrics()
logger := logging.GetContextLogger("TestMain")
logger.Infof("Using kubeconfig at `%s` with cluster `%s`", knativetest.Flags.Kubeconfig, knativetest.Flags.Cluster)

namespace = AppendRandomString("arendelle")
kubeClient := createNamespace(namespace, logger)
Expand Down
24 changes: 23 additions & 1 deletion test/presubmit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# limitations under the License.

# This script runs the presubmit tests; it is started by prow for each PR.
# When running `--integration-tests`, use `K8S_CLUSTER_OVERRIDE` to point at your
# own cluster (a context in your kubeconfig), or the script will attempt to invoke
# boskos to obtain one of the knative testing clusters in GKE.

set -o xtrace

Expand Down Expand Up @@ -43,7 +46,26 @@ function unit_tests() {

function integration_tests() {
echo "Running integration tests"
echo "TODO(#16): add integration tests"

# The logic to create resource names (which are used by kubetest) in `e2e-tests.sh` requires
# that the variable `BUILD_NUMBER` be set, which is provided by Prow, so if we aren't running
# this from Prow we need to provide our own.
export BUILD_NUMBER=${BUILD_NUMBER:-$RANDOM}

# Use knative test-infra scripts to make `fail-test` and `success` available.
# which will output helpful information for debugging failures, and ensure test
# results are made available.
source $(dirname $0)/../vendor/github.com/knative/test-infra/scripts/e2e-tests.sh
# Setup the cluster (if not using an existing cluster) and deploy the Pipeline CRD
source $(dirname $0)/cluster-setup.sh

options=""
(( EMIT_METRICS )) && options="-emitmetrics"
report_go_test \
-v -tags=e2e -count=1 -timeout=20m ./test \
${options} || fail_test

success
}

main $@

0 comments on commit 1d8f2b4

Please sign in to comment.