|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2020 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +############################################################################### |
| 18 | + |
| 19 | +# To run locally, set AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID |
| 20 | + |
| 21 | +set -o errexit |
| 22 | +set -o nounset |
| 23 | +set -o pipefail |
| 24 | + |
| 25 | +REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. |
| 26 | +cd "${REPO_ROOT}" || exit 1 |
| 27 | + |
| 28 | +# shellcheck source=../hack/ensure-go.sh |
| 29 | +source "${REPO_ROOT}/hack/ensure-go.sh" |
| 30 | +# shellcheck source=../hack/ensure-kind.sh |
| 31 | +source "${REPO_ROOT}/hack/ensure-kind.sh" |
| 32 | +# shellcheck source=../hack/ensure-kubectl.sh |
| 33 | +source "${REPO_ROOT}/hack/ensure-kubectl.sh" |
| 34 | +# shellcheck source=../hack/ensure-kustomize.sh |
| 35 | +source "${REPO_ROOT}/hack/ensure-kustomize.sh" |
| 36 | +# shellcheck source=../hack/parse-prow-creds.sh |
| 37 | +source "${REPO_ROOT}/hack/parse-prow-creds.sh" |
| 38 | + |
| 39 | +random-string() { |
| 40 | + cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1 |
| 41 | +} |
| 42 | + |
| 43 | +# build Kubernetes E2E binaries |
| 44 | +build_k8s() { |
| 45 | + # possibly enable bazel build caching before building kubernetes |
| 46 | + if [[ "${BAZEL_REMOTE_CACHE_ENABLED:-false}" == "true" ]]; then |
| 47 | + create_bazel_cache_rcs.sh || true |
| 48 | + fi |
| 49 | + |
| 50 | + pushd "$(go env GOPATH)/src/k8s.io/kubernetes" |
| 51 | + |
| 52 | + # make sure we have e2e requirements |
| 53 | + bazel build //cmd/kubectl //test/e2e:e2e.test //vendor/github.com/onsi/ginkgo/ginkgo |
| 54 | + |
| 55 | + # ensure the e2e script will find our binaries ... |
| 56 | + mkdir -p "${PWD}/_output/bin/" |
| 57 | + cp -f "${PWD}/bazel-bin/test/e2e/e2e.test" "${PWD}/_output/bin/e2e.test" |
| 58 | + |
| 59 | + PATH="$(dirname "$(find "${PWD}/bazel-bin/" -name kubectl -type f)"):${PATH}" |
| 60 | + export PATH |
| 61 | + |
| 62 | + # attempt to release some memory after building |
| 63 | + sync || true |
| 64 | + (echo 1 > /proc/sys/vm/drop_caches) 2>/dev/null || true |
| 65 | + |
| 66 | + popd |
| 67 | +} |
| 68 | + |
| 69 | +create_cluster() { |
| 70 | + # export cluster template which contains the manifests needed for creating the Azure cluster to run the tests |
| 71 | + if [[ -n ${CI_VERSION:-} || -n ${USE_CI_ARTIFACTS:-} ]]; then |
| 72 | + KUBERNETES_BRANCH="$(cd $(go env GOPATH)/src/k8s.io/kubernetes && git rev-parse --abbrev-ref HEAD)" |
| 73 | + if [[ "${KUBERNETES_BRANCH:-}" =~ "release-" ]]; then |
| 74 | + CI_VERSION_URL="https://dl.k8s.io/ci/latest-${KUBERNETES_BRANCH/release-}.txt" |
| 75 | + else |
| 76 | + CI_VERSION_URL="https://dl.k8s.io/ci/k8s-master.txt" |
| 77 | + fi |
| 78 | + export CLUSTER_TEMPLATE="test/cluster-template-conformance-ci-version.yaml" |
| 79 | + export CI_VERSION="${CI_VERSION:-$(curl -sSL ${CI_VERSION_URL})}" |
| 80 | + export KUBERNETES_VERSION="${CI_VERSION}" |
| 81 | + else |
| 82 | + export CLUSTER_TEMPLATE="test/cluster-template-conformance.yaml" |
| 83 | + fi |
| 84 | + |
| 85 | + export CLUSTER_NAME="capz-$(head /dev/urandom | LC_ALL=C tr -dc a-z0-9 | head -c 6 ; echo '')" |
| 86 | + # Need a cluster with at least 2 nodes |
| 87 | + export CONTROL_PLANE_MACHINE_COUNT=${CONTROL_PLANE_MACHINE_COUNT:-1} |
| 88 | + export WORKER_MACHINE_COUNT=${WORKER_MACHINE_COUNT:-2} |
| 89 | + export REGISTRY=capz |
| 90 | + export JOB_NAME="${JOB_NAME:-"cluster-api-provider-azure-conformance"}" |
| 91 | + # timestamp is in RFC-3339 format to match kubetest |
| 92 | + export TIMESTAMP=$(date -u '+%Y-%m-%dT%H:%M:%SZ') |
| 93 | + ${REPO_ROOT}/hack/create-dev-cluster.sh |
| 94 | +} |
| 95 | + |
| 96 | +run_upstream_e2e_tests() { |
| 97 | + # export the target cluster KUBECONFIG if not already set |
| 98 | + export KUBECONFIG="${KUBECONFIG:-${PWD}/kubeconfig}" |
| 99 | + # ginkgo regexes |
| 100 | + SKIP="${SKIP:-}" |
| 101 | + FOCUS="${FOCUS:-"\\[Conformance\\]"}" |
| 102 | + # if we set PARALLEL=true, skip serial tests set --ginkgo-parallel |
| 103 | + if [[ "${PARALLEL:-false}" == "true" ]]; then |
| 104 | + export GINKGO_PARALLEL=y |
| 105 | + if [[ -z "${SKIP}" ]]; then |
| 106 | + SKIP="\\[Serial\\]" |
| 107 | + else |
| 108 | + SKIP="\\[Serial\\]|${SKIP}" |
| 109 | + fi |
| 110 | + fi |
| 111 | + |
| 112 | + # get the number of worker nodes |
| 113 | + NUM_NODES="$(kubectl get nodes --kubeconfig="$KUBECONFIG" \ |
| 114 | + -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.taints}{"\n"}{end}' \ |
| 115 | + | grep -cv "node-role.kubernetes.io/master" )" |
| 116 | + |
| 117 | + # wait for all the nodes to be ready |
| 118 | + kubectl wait --for=condition=Ready node --kubeconfig="$KUBECONFIG" --all || true |
| 119 | + |
| 120 | + # setting this env prevents ginkg e2e from trying to run provider setup |
| 121 | + export KUBERNETES_CONFORMANCE_TEST="y" |
| 122 | + # run the tests |
| 123 | + (cd "$(go env GOPATH)/src/k8s.io/kubernetes" && ./hack/ginkgo-e2e.sh \ |
| 124 | + '--provider=skeleton' "--num-nodes=${NUM_NODES}" \ |
| 125 | + "--ginkgo.focus=${FOCUS}" "--ginkgo.skip=${SKIP}" \ |
| 126 | + "--report-dir=${ARTIFACTS}" '--disable-log-dump=true') |
| 127 | + |
| 128 | + unset KUBECONFIG |
| 129 | + unset KUBERNETES_CONFORMANCE_TEST |
| 130 | +} |
| 131 | + |
| 132 | +get_logs() { |
| 133 | + kubectl logs deploy/capz-controller-manager -n capz-system manager > "${ARTIFACTS}/logs/capz-manager.log" || true |
| 134 | +} |
| 135 | + |
| 136 | +# cleanup all resources we use |
| 137 | +cleanup() { |
| 138 | + timeout 600 kubectl \ |
| 139 | + delete cluster "${CLUSTER_NAME}" || true |
| 140 | + timeout 600 kubectl \ |
| 141 | + wait --for=delete cluster/"${CLUSTER_NAME}" || true |
| 142 | + make kind-reset || true |
| 143 | + # clean up e2e.test symlink |
| 144 | + (cd "$(go env GOPATH)/src/k8s.io/kubernetes" && rm -f _output/bin/e2e.test) || true |
| 145 | +} |
| 146 | + |
| 147 | +on_exit() { |
| 148 | + unset KUBECONFIG |
| 149 | + get_logs |
| 150 | + # cleanup |
| 151 | + if [[ -z "${SKIP_CLEANUP:-}" ]]; then |
| 152 | + cleanup |
| 153 | + fi |
| 154 | +} |
| 155 | + |
| 156 | +trap on_exit EXIT |
| 157 | +ARTIFACTS="${ARTIFACTS:-${PWD}/_artifacts}" |
| 158 | +mkdir -p "${ARTIFACTS}/logs" |
| 159 | + |
| 160 | +# create cluster |
| 161 | +if [[ -z "${SKIP_CREATE_CLUSTER:-}" ]]; then |
| 162 | + create_cluster |
| 163 | +fi |
| 164 | + |
| 165 | +# build k8s binaries and run upstream e2e tests |
| 166 | +if [[ -z "${SKIP_UPSTREAM_E2E_TESTS:-}" ]]; then |
| 167 | + build_k8s |
| 168 | + run_upstream_e2e_tests |
| 169 | +fi |
| 170 | + |
| 171 | +if [[ "${#}" -gt 0 ]]; then |
| 172 | + # disable error exit so we can run post-command cleanup |
| 173 | + set +o errexit |
| 174 | + "${@}" |
| 175 | + EXIT_VALUE="${?}" |
| 176 | + exit ${EXIT_VALUE} |
| 177 | +fi |
0 commit comments