diff --git a/Makefile b/Makefile index 17a1cd95d..c9a0d5ad5 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,14 @@ IMG ?= controller:latest # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false" +# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. +ENVTEST_K8S_VERSION = 1.22 + +HOME ?= /tmp/kubebuilder-testing +ifeq ($(HOME), /) +HOME = /tmp/kubebuilder-testing +endif + # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) GOBIN=$(shell go env GOPATH)/bin @@ -22,8 +30,8 @@ verify: fmt vet lint # Run tests test: generate verify manifests unit -unit: - hack/unit-tests.sh +unit: envtest + KUBEBUILDER_ASSETS=$(shell $(ENVTEST) --bin-dir=$(shell pwd)/bin use $(ENVTEST_K8S_VERSION) -p path) go test ./... -coverprofile cover.out # Build operator binaries build: operator config-sync-controllers azure-config-credentials-injector @@ -91,6 +99,11 @@ GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint golangci-lint: $(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint) +ENVTEST = $(shell pwd)/bin/setup-envtest +.PHONY: envtest +envtest: ## Download envtest-setup locally if necessary. + $(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) + # go-get-tool will 'go get' any package $2 and install it to $1. PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) define go-get-tool diff --git a/hack/unit-tests.sh b/hack/unit-tests.sh deleted file mode 100755 index c06120e8b..000000000 --- a/hack/unit-tests.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/bash - -set -o errexit -set -o pipefail - -source ./hack/go-get-tool.sh - -REPO_ROOT=$(realpath "$(dirname "${BASH_SOURCE[0]}")/..") - -ENVTEST_VERSION=v0.7.0 -ENVTEST_ASSETS_DIR=/tmp/testbin -ENVTEST_SETUP_SCRIPT=https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/${ENVTEST_VERSION}/hack/setup-envtest.sh - -function setupEnvtest() { - echo "Envtest version: ${ENVTEST_VERSION}." - mkdir -p ${ENVTEST_ASSETS_DIR} - test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh ${ENVTEST_SETUP_SCRIPT} - source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh - fetch_envtest_tools ${ENVTEST_ASSETS_DIR} - setup_envtest_env ${ENVTEST_ASSETS_DIR} - - # Ensure that some home var is set and that it's not the root - export HOME=${HOME:=/tmp/kubebuilder/testing} - if [ $HOME == "/" ]; then - export HOME=/tmp/kubebuilder/testing - fi -} - - -OPENSHIFT_CI=${OPENSHIFT_CI:-""} -ARTIFACT_DIR=${ARTIFACT_DIR:-""} - -function go_test() { - go test "$@" ./... -coverprofile cover.out -} - -runTestCI() { - local GO_JUNIT_REPORT_PATH=$LOCAL_BINARIES_PATH/go-junit-report - echo "CI env detected, run tests with jUnit report extraction" - if [ -n "$ARTIFACT_DIR" ] && [ -d "$ARTIFACT_DIR" ]; then - local JUNIT_LOCATION="$ARTIFACT_DIR"/junit_cluster_cloud_controller_manager_operator.xml - echo "jUnit location: $JUNIT_LOCATION" - go-get-tool "$GO_JUNIT_REPORT_PATH" github.com/jstemmer/go-junit-report - go_test -v | tee >($GO_JUNIT_REPORT_PATH > "$JUNIT_LOCATION") - else - echo "\$ARTIFACT_DIR not set or does not exists, no jUnit will be published" - go_test - fi -} - -function runTests() { - if [ "$OPENSHIFT_CI" == "true" ]; then - runTestCI - else - go_test -test.short - fi -} - - -cd "${REPO_ROOT}" && \ -setupEnvtest && \ -runTests