From af63f080744ef7af2089127065460f934752efa7 Mon Sep 17 00:00:00 2001 From: James Munnelly Date: Thu, 23 Jan 2020 12:57:03 +0000 Subject: [PATCH] Fixup using devel tools in CI Signed-off-by: James Munnelly --- devel/bin/BUILD.bazel | 21 +++++++++++++++++++ devel/bin/ginkgo | 31 +++++++++++++++++++++++++++++ devel/bin/helm | 7 ++++++- devel/bin/kind | 7 ++++++- devel/bin/kubectl | 7 ++++++- devel/ci-run-e2e.sh | 4 +++- devel/lib/lib.sh | 18 +++++++++++++++-- devel/run-e2e.sh | 19 +++++++++++------- devel/setup-e2e-deps.sh | 8 ++++++-- test/e2e/framework/config/config.go | 11 +++++++++- 10 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 devel/bin/BUILD.bazel create mode 100755 devel/bin/ginkgo diff --git a/devel/bin/BUILD.bazel b/devel/bin/BUILD.bazel new file mode 100644 index 000000000..32e4a4522 --- /dev/null +++ b/devel/bin/BUILD.bazel @@ -0,0 +1,21 @@ +genrule( + name = "_ginkgo", + srcs = ["@com_github_onsi_ginkgo//ginkgo"], + outs = ["ginkgo"], + cmd = "cp $(SRCS) $@", + visibility = ["//visibility:public"], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/devel/bin/ginkgo b/devel/bin/ginkgo new file mode 100755 index 000000000..9b9500a76 --- /dev/null +++ b/devel/bin/ginkgo @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +# Copyright 2020 The Jetstack cert-manager contributors. +# +# 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. + +set -o nounset +set -o errexit +set -o pipefail + +if ! command -v bazel &>/dev/null; then + echo "Install bazel at https://bazel.build" >&2 + exit 1 +fi + +if [ -z "${GINKGO:-}" ]; then + bazel build //devel/bin:ginkgo + export GINKGO="$(bazel info bazel-genfiles)/devel/bin/ginkgo" +fi + +"${GINKGO}" "$@" diff --git a/devel/bin/helm b/devel/bin/helm index 55ca30b0d..763643f17 100755 --- a/devel/bin/helm +++ b/devel/bin/helm @@ -23,4 +23,9 @@ if ! command -v bazel &>/dev/null; then exit 1 fi -bazel run //hack/bin:helm -- "$@" +if [ -z "${HELM:-}" ]; then + bazel build //hack/bin:helm + export HELM="$(bazel info bazel-genfiles)/hack/bin/helm" +fi + +"${HELM}" "$@" diff --git a/devel/bin/kind b/devel/bin/kind index 72df73b1c..75261dad7 100755 --- a/devel/bin/kind +++ b/devel/bin/kind @@ -23,4 +23,9 @@ if ! command -v bazel &>/dev/null; then exit 1 fi -bazel run //hack/bin:kind -- "$@" +if [ -z "${KIND:-}" ]; then + bazel build //hack/bin:kind + export KIND="$(bazel info bazel-genfiles)/hack/bin/kind" +fi + +"${KIND}" "$@" diff --git a/devel/bin/kubectl b/devel/bin/kubectl index 63a6fbfcd..7eda48b1c 100755 --- a/devel/bin/kubectl +++ b/devel/bin/kubectl @@ -23,4 +23,9 @@ if ! command -v bazel &>/dev/null; then exit 1 fi -bazel run //hack/bin:kubectl -- "$@" +if [ -z "${KUBECTL:-}" ]; then + bazel build //hack/bin:kubectl + export KUBECTL="$(bazel info bazel-genfiles)/hack/bin/kubectl" +fi + +"${KUBECTL}" "$@" diff --git a/devel/ci-run-e2e.sh b/devel/ci-run-e2e.sh index 07f6effd8..17a250435 100755 --- a/devel/ci-run-e2e.sh +++ b/devel/ci-run-e2e.sh @@ -24,8 +24,10 @@ set -o pipefail SCRIPT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )" export REPO_ROOT="${SCRIPT_ROOT}/.." +source "${SCRIPT_ROOT}/lib/lib.sh" + # Configure PATH to use bazel provided e2e tools -export PATH="${SCRIPT_ROOT}/bin:$PATH" +setup_tools echo "Ensuring a kind cluster exists..." "${SCRIPT_ROOT}/cluster/create.sh" diff --git a/devel/lib/lib.sh b/devel/lib/lib.sh index ada3024bf..f0bc897be 100644 --- a/devel/lib/lib.sh +++ b/devel/lib/lib.sh @@ -18,12 +18,26 @@ set -o nounset set -o errexit set -o pipefail -SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") -export REPO_ROOT="$SCRIPT_ROOT/../.." +LIB_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )" +export REPO_ROOT="$LIB_ROOT/../.." export SKIP_BUILD_ADDON_IMAGES="${SKIP_BUILD_ADDON_IMAGES:-}" export KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kind}" +# setup_tools will build and set up the environment to use bazel-provided +# versions of the tools required for development +setup_tools() { + check_bazel + bazel build //hack/bin:helm //hack/bin:kind //hack/bin:kubectl //devel/bin:ginkgo + local bindir="$(bazel info bazel-genfiles)" + export HELM="${bindir}/hack/bin/helm" + export KIND="${bindir}/hack/bin/kind" + export KUBECTL="${bindir}/hack/bin/kubectl" + export GINKGO="${bindir}/devel/bin/ginkgo" + # Configure PATH to use bazel provided e2e tools + export PATH="${SCRIPT_ROOT}/bin:$PATH" +} + # check_tool ensures that the tool with the given name is available, or advises # users to setup their PATH for the test/e3e/bin directory if not. check_tool() { diff --git a/devel/run-e2e.sh b/devel/run-e2e.sh index a01a41f9d..4c040fc28 100755 --- a/devel/run-e2e.sh +++ b/devel/run-e2e.sh @@ -22,19 +22,24 @@ set -o pipefail # kind cluster. # If a cluster does not already exist, create one with 'cluster/create.sh'. -export SCRIPT_ROOT=$(dirname "${BASH_SOURCE}") +SCRIPT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )" +export REPO_ROOT="${SCRIPT_ROOT}/.." source "${SCRIPT_ROOT}/lib/lib.sh" +# Configure PATH to use bazel provided e2e tools +setup_tools + +# Ensure bazel is installed check_bazel +# Create output directory for JUnit output mkdir -p "${REPO_ROOT}/_artifacts" -bazel build //hack/bin:helm //test/e2e:e2e.test -# Set KUBECONFIG environment variable if not already set -export KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" + +# Build the e2e test binary +bazel build //test/e2e:e2e.test + # Run e2e tests -bazel run @com_github_onsi_ginkgo//ginkgo -- \ - -nodes 10 \ - -flakeAttempts ${FLAKE_ATTEMPTS:-1} \ +ginkgo -nodes 10 -flakeAttempts ${FLAKE_ATTEMPTS:-1} \ $(bazel info bazel-genfiles)/test/e2e/e2e.test \ -- \ --repo-root="${REPO_ROOT}" \ diff --git a/devel/setup-e2e-deps.sh b/devel/setup-e2e-deps.sh index 2c43d2cdd..393ac91c6 100755 --- a/devel/setup-e2e-deps.sh +++ b/devel/setup-e2e-deps.sh @@ -24,8 +24,12 @@ set -o pipefail # If you are running the *full* test suite, you should be sure to run this # script beforehand. -SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}") -export REPO_ROOT="${REPO_ROOT:-$SCRIPT_ROOT/..}" +SCRIPT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )" +export REPO_ROOT="${SCRIPT_ROOT}/.." +source "${SCRIPT_ROOT}/lib/lib.sh" + +# Configure PATH to use bazel provided e2e tools +setup_tools echo "Installing cert-manager into the kind cluster..." "${SCRIPT_ROOT}/addon/certmanager/install.sh" diff --git a/test/e2e/framework/config/config.go b/test/e2e/framework/config/config.go index 62e552ed3..6c5d105ce 100644 --- a/test/e2e/framework/config/config.go +++ b/test/e2e/framework/config/config.go @@ -20,6 +20,7 @@ import ( "flag" "fmt" "os" + "path/filepath" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/clientcmd" @@ -62,8 +63,16 @@ func (c *Config) Validate() error { // Register flags common to all e2e test suites. func (c *Config) AddFlags(fs *flag.FlagSet) { + kubeConfigFile := os.Getenv(clientcmd.RecommendedConfigPathEnvVar) + if kubeConfigFile == "" { + homeDir, err := os.UserHomeDir() + if err != nil { + panic("Failed to get user home directory: " + err.Error()) + } + kubeConfigFile = filepath.Join(homeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName) + } // Kubernetes API server config - fs.StringVar(&c.KubeConfig, "kubernetes-config", os.Getenv(clientcmd.RecommendedConfigPathEnvVar), "Path to config containing embedded authinfo for kubernetes. Default value is from environment variable "+clientcmd.RecommendedConfigPathEnvVar) + fs.StringVar(&c.KubeConfig, "kubernetes-config", kubeConfigFile, "Path to config containing embedded authinfo for kubernetes. Default value is from environment variable "+clientcmd.RecommendedConfigPathEnvVar) fs.StringVar(&c.KubeContext, "kubernetes-context", "", "config context to use for kuberentes. If unset, will use value from 'current-context'") fs.StringVar(&c.Kubectl, "kubectl-path", "kubectl", "path to the kubectl binary to use during e2e tests.") fs.BoolVar(&c.Cleanup, "cleanup", true, "If true, addons will be cleaned up both before and after provisioning")