Use helpers from prow-tests image for tests (#232)

* Use helpers from prow-tests image for tests

We're consolidating the test infrastructure into a single place, so all repos get the same fixes, updates and new features.

`presubmit-tests.sh` helper is implemented by knative/test-infra#12
`e2e-tests.sh` helper is implemented by knative/test-infra#17

* Explicitly fail test if setup fails
This commit is contained in:
Adriano Cunha 2018-07-21 10:17:34 -07:00 committed by Google Prow Robot
parent 9379197eee
commit 72cb87519a
3 changed files with 16 additions and 232 deletions

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# This script runs the end-to-end tests for build templates.
# This script runs the end-to-end tests.
# If you already have a Knative cluster setup and kubectl pointing
# to it, call this script with the --run-tests arguments and it will use
@ -23,115 +23,22 @@
# Calling this script without arguments will create a new cluster in
# project $PROJECT_ID, run the tests and delete the cluster.
source "$(dirname $(readlink -f ${BASH_SOURCE}))/library.sh"
# Test cluster parameters and location of test files
readonly E2E_CLUSTER_NAME=docs-e2e-cluster${BUILD_NUMBER}
readonly E2E_NETWORK_NAME=docs-e2e-net${BUILD_NUMBER}
readonly E2E_CLUSTER_ZONE=us-central1-a
readonly E2E_CLUSTER_NODES=2
readonly E2E_CLUSTER_MACHINE=n1-standard-2
readonly TEST_RESULT_FILE=/tmp/docs-e2e-result
readonly ISTIO_YAML=https://storage.googleapis.com/knative-releases/latest/istio.yaml
readonly SERVING_RELEASE=https://storage.googleapis.com/knative-releases/latest/release.yaml
# This script.
readonly SCRIPT_CANONICAL_PATH="$(readlink -f ${BASH_SOURCE})"
# Helper functions.
function exit_if_test_failed() {
[[ $? -eq 0 ]] && return 0
[[ -n $1 ]] && echo "ERROR: $1"
echo "***************************************"
echo "*** TEST FAILED ***"
echo "*** Start of information dump ***"
echo "***************************************"
echo ">>> All resources:"
kubectl get all --all-namespaces
echo "***************************************"
echo "*** TEST FAILED ***"
echo "*** End of information dump ***"
echo "***************************************"
exit 1
}
# Load github.com/knative/test-infra/images/prow-tests/scripts/e2e-tests.sh
[ -f /workspace/e2e-tests.sh ] \
&& source /workspace/e2e-tests.sh \
|| eval "$(docker run --entrypoint sh gcr.io/knative-tests/test-infra/prow-tests -c 'cat e2e-tests.sh')"
[ -v KNATIVE_TEST_INFRA ] || exit 1
# Script entry point.
cd ${DOCS_ROOT_DIR}
# Show help if bad arguments are passed.
if [[ -n $1 && $1 != "--run-tests" ]]; then
echo "usage: $0 [--run-tests]"
exit 1
fi
# No argument provided, create the test cluster.
if [[ -z $1 ]]; then
header "Creating test cluster"
# Smallest cluster required to run the end-to-end-tests
CLUSTER_CREATION_ARGS=(
--gke-create-args="--enable-autoscaling --min-nodes=1 --max-nodes=${E2E_CLUSTER_NODES} --scopes=cloud-platform"
--gke-shape={\"default\":{\"Nodes\":${E2E_CLUSTER_NODES}\,\"MachineType\":\"${E2E_CLUSTER_MACHINE}\"}}
--provider=gke
--deployment=gke
--gcp-node-image=cos
--cluster="${E2E_CLUSTER_NAME}"
--gcp-zone="${E2E_CLUSTER_ZONE}"
--gcp-network="${E2E_NETWORK_NAME}"
--gke-environment=prod
)
if (( ! IS_PROW )); then
CLUSTER_CREATION_ARGS+=(--gcp-project=${PROJECT_ID:?"PROJECT_ID must be set to the GCP project where the tests are run."})
fi
# SSH keys are not used, but kubetest checks for their existence.
# Touch them so if they don't exist, empty files are create to satisfy the check.
touch $HOME/.ssh/google_compute_engine.pub
touch $HOME/.ssh/google_compute_engine
# Assume test failed (see more details at the end of this script).
echo -n "1"> ${TEST_RESULT_FILE}
kubetest "${CLUSTER_CREATION_ARGS[@]}" \
--up \
--down \
--extract "gke-${GKE_VERSION}" \
--test-cmd "${SCRIPT_CANONICAL_PATH}" \
--test-cmd-args --run-tests
result="$(cat ${TEST_RESULT_FILE})"
echo "Test result code is $result"
exit $result
fi
# --run-tests passed as first argument, run the tests.
initialize $@
# Install Knative Serving if not using an existing cluster
if (( IS_PROW )) || [[ -n ${PROJECT_ID} ]]; then
header "Starting Knative Serving"
acquire_cluster_admin_role $(gcloud config get-value core/account) ${E2E_CLUSTER_NAME} ${E2E_CLUSTER_ZONE}
subheader "Installing Istio"
kubectl apply -f ${ISTIO_YAML}
wait_until_pods_running istio-system
exit_if_test_failed "could not install Istio"
kubectl label namespace default istio-injection=enabled
subheader "Installing Knative Serving"
kubectl apply -f ${SERVING_RELEASE}
exit_if_test_failed "could not install Knative Serving"
wait_until_pods_running knative-serving
wait_until_pods_running build-system
if (( ! USING_EXISTING_CLUSTER )); then
start_latest_knative_serving || fail_test
fi
# TODO(#30): Add tests.
header "TODO(#30): Write integration tests"
# kubetest teardown might fail and thus incorrectly report failure of the
# script, even if the tests pass.
# We store the real test result to return it later, ignoring any teardown
# failure in kubetest.
# TODO(adrcunha): Get rid of this workaround.
echo -n "0"> ${TEST_RESULT_FILE}
echo "**************************************"
echo "*** ALL TESTS PASSED ***"
echo "**************************************"
exit 0
success

View File

@ -1,76 +0,0 @@
#!/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 is a collection of useful bash functions and constants, intended
# to be used in test scripts and the like. It doesn't do anything when
# called from command line.
# Default GKE version to be used
readonly GKE_VERSION=latest
# Useful environment variables
[[ -n "${PROW_JOB_ID}" ]] && IS_PROW=1 || IS_PROW=0
readonly IS_PROW
readonly DOCS_ROOT_DIR="$(dirname $(readlink -f ${BASH_SOURCE}))/.."
# Simple header for logging purposes.
function header() {
echo "================================================="
echo ${1^^}
echo "================================================="
}
# Simple subheader for logging purposes.
function subheader() {
echo "-------------------------------------------------"
echo $1
echo "-------------------------------------------------"
}
# Waits until all pods are running in the given namespace or Completed.
# Parameters: $1 - namespace.
function wait_until_pods_running() {
echo -n "Waiting until all pods in namespace $1 are up"
for i in {1..150}; do # timeout after 5 minutes
local pods="$(kubectl get pods -n $1 2>/dev/null | grep -v NAME)"
local not_running=$(echo "${pods}" | grep -v Running | grep -v Completed | wc -l)
if [[ -n "${pods}" && ${not_running} == 0 ]]; then
echo -e "\nAll pods are up:"
kubectl get pods -n $1
return 0
fi
echo -n "."
sleep 2
done
echo -e "\n\nERROR: timeout waiting for pods to come up"
kubectl get pods -n $1
return 1
}
# Sets the given user as cluster admin.
# Parameters: $1 - user
# $2 - cluster name
# $3 - cluster zone
function acquire_cluster_admin_role() {
# Get the password of the admin and use it, as the service account (or the user)
# might not have the necessary permission.
local password=$(gcloud --format="value(masterAuth.password)" \
container clusters describe $2 --zone=$3)
kubectl --username=admin --password=$password \
create clusterrolebinding cluster-admin-binding \
--clusterrole=cluster-admin \
--user=$1
}

View File

@ -18,15 +18,11 @@
# It is started by prow for each PR.
# For convenience, it can also be executed manually.
set -o errexit
set -o pipefail
# Extensions or file patterns that don't require presubmit tests
readonly NO_PRESUBMIT_FILES=(\.md \.png ^OWNERS)
source "$(dirname $(readlink -f ${BASH_SOURCE}))/library.sh"
# Helper functions.
# Load github.com/knative/test-infra/images/prow-tests/scripts/presubmit-tests.sh
[ -f /workspace/presubmit-tests.sh ] \
&& source /workspace/presubmit-tests.sh \
|| eval "$(docker run --entrypoint sh gcr.io/knative-tests/test-infra/prow-tests -c 'cat presubmit-tests.sh')"
[ -v KNATIVE_TEST_INFRA ] || exit 1
function build_tests() {
header "TODO(#67): Write build tests"
@ -40,47 +36,4 @@ function integration_tests() {
./test/e2e-tests.sh
}
# Script entry point.
# Parse script argument:
# --all-tests or no arguments: run all tests
# --build-tests: run only the build tests
# --unit-tests: run only the unit tests
# --integration-tests: run only the integration tests
RUN_BUILD_TESTS=0
RUN_UNIT_TESTS=0
RUN_INTEGRATION_TESTS=0
[[ -z "$1" || "$1" == "--all-tests" ]] && RUN_BUILD_TESTS=1 && RUN_UNIT_TESTS=1 && RUN_INTEGRATION_TESTS=1
[[ "$1" == "--build-tests" ]] && RUN_BUILD_TESTS=1
[[ "$1" == "--unit-tests" ]] && RUN_UNIT_TESTS=1
[[ "$1" == "--integration-tests" ]] && RUN_INTEGRATION_TESTS=1
readonly RUN_BUILD_TESTS
readonly RUN_UNIT_TESTS
readonly RUN_INTEGRATION_TESTS
if ! (( RUN_BUILD_TESTS+RUN_UNIT_TESTS+RUN_INTEGRATION_TESTS )); then
echo "error: unknown argument $1";
exit 1
fi
cd ${DOCS_ROOT_DIR}
# Skip presubmit tests if whitelisted files were changed.
if [[ -n "${PULL_PULL_SHA}" ]]; then
# On a presubmit job
changes="$(git diff --name-only ${PULL_PULL_SHA} ${PULL_BASE_SHA})"
no_presubmit_pattern="${NO_PRESUBMIT_FILES[*]}"
no_presubmit_pattern="\(${no_presubmit_pattern// /\\|}\)$"
echo -e "Changed files in commit ${PULL_PULL_SHA}:\n${changes}"
if [[ -z "$(echo "${changes}" | grep -v ${no_presubmit_pattern})" ]]; then
# Nothing changed other than files that don't require presubmit tests
header "Commit only contains changes that don't affect tests, skipping"
exit 0
fi
fi
# Tests to be performed, in the right order if --all-tests is passed.
if (( RUN_BUILD_TESTS )); then build_tests; fi
if (( RUN_UNIT_TESTS )); then unit_tests; fi
if (( RUN_INTEGRATION_TESTS )); then integration_tests; fi
main $@