Skip to content

Commit

Permalink
update prow build
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Ng <[email protected]>
  • Loading branch information
mikeshng committed Mar 5, 2021
1 parent 84d23a2 commit 0d01685
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Temporary Build Files
test
build/_output
build/_test
build-harness-extensions
Expand Down
11 changes: 11 additions & 0 deletions Makefile.prow
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
# limitations under the License.

IMG ?= $(shell cat COMPONENT_NAME 2> /dev/null)
export GOPACKAGES = $(shell go list ./... | grep -v /manager | grep -v /bindata | grep -v /vendor | grep -v /internal | grep -v /build | grep -v /test )

.PHONY: build

-include /opt/build-harness/Makefile.prow

build:
@common/scripts/gobuild.sh build/_output/bin/$(IMG) ./cmd/manager

.PHONY: lint

lint:
@build/run-code-lint.sh

.PHONY: test

test:
@build/run-unit-tests.sh
57 changes: 26 additions & 31 deletions build/run-unit-tests.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
#!/bin/bash
#!/bin/bash -e
###############################################################################
# (c) Copyright IBM Corporation 2019, 2020. All Rights Reserved.
# Note to U.S. Government Users Restricted Rights:
# U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule
# Contract with IBM Corp.
# Copyright (c) Red Hat, Inc.
# Copyright Contributors to the Open Cluster Management project
###############################################################################

#
# Copyright 2019 The Kubernetes 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.
_script_dir=$(dirname "$0")
if ! which patter > /dev/null; then echo "Installing patter ..."; pushd $(mktemp -d) && GOSUMDB=off go get -u github.com/apg/patter && popd; fi
if ! which gocovmerge > /dev/null; then echo "Installing gocovmerge..."; pushd $(mktemp -d) && GOSUMDB=off go get -u github.com/wadey/gocovmerge && popd; fi

echo "UNIT TESTS GO HERE!"
export GOFLAGS=""
mkdir -p test/unit/coverage
echo 'mode: atomic' > test/unit/coverage/cover.out
echo '' > test/unit/coverage/cover.tmp
echo -e "${GOPACKAGES// /\\n}" | xargs -n1 -I{} $_script_dir/test-package.sh {} ${GOPACKAGES// /,}

echo "Install Kubebuilder components for test framework usage!"
if [ ! -f test/unit/coverage/cover.out ]; then
echo "Coverage file test/unit/coverage/cover.out does not exist"
exit 0
fi

_OS=$(go env GOOS)
_ARCH=$(go env GOARCH)
KubeBuilderVersion="2.3.1"
COVERAGE=$(go tool cover -func=test/unit/coverage/cover.out | grep "total:" | awk '{ print $3 }' | sed 's/[][()><%]/ /g')
echo "-------------------------------------------------------------------------"
echo "TOTAL COVERAGE IS ${COVERAGE}%"
echo "-------------------------------------------------------------------------"

# download kubebuilder and extract it to tmp
curl -L https://go.kubebuilder.io/dl/"$KubeBuilderVersion"/"${_OS}"/"${_ARCH}" | tar -xz -C /tmp/

# move to a long-term location and put it on your path
# (you'll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
sudo mv /tmp/kubebuilder_"$KubeBuilderVersion"_"${_OS}"_"${_ARCH}" /usr/local/kubebuilder
export PATH=$PATH:/usr/local/kubebuilder/bin

# Run unit test
export IMAGE_NAME_AND_VERSION=${1}
make test
go tool cover -html=test/unit/coverage/cover.out -o=test/unit/coverage/cover.html
41 changes: 41 additions & 0 deletions build/test-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash -e
###############################################################################
# (c) Copyright IBM Corporation 2019, 2020. All Rights Reserved.
# Note to U.S. Government Users Restricted Rights:
# U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule
# Contract with IBM Corp.
# Copyright (c) Red Hat, Inc.
# Copyright Contributors to the Open Cluster Management project
###############################################################################

# NOTE: This script should not be called directly. Please run `make test`.

set -o pipefail

_package=$1
_cover_pkgs=$2
echo -e "\nTesting package $_package"

# Make sure temporary files do not exist
rm -f test/unit/coverage/cover.tmp

# Support for TAP output
_package_base=${PROJECT_DIR/$GOPATH\/src\/} # TODO need a better solution since $(go list) doesn't work any more (won't work with go 1.11)
_tap_out_dir=$GOPATH/src/$_package_base/test/out
_tap_name="${_package/$_package_base/}"
_tap_name=${_tap_name//\//_}

mkdir -p $_tap_out_dir

# Run tests
# DO NOT USE -coverpkg=./...
go test -v -cover -coverpkg=$_cover_pkgs -covermode=atomic -coverprofile=test/unit/coverage/cover.tmp $_package 2> >( grep -v "warning: no packages being tested depend on" >&2 ) | $GOPATH/bin/patter | tee $_tap_out_dir/$_tap_name.tap | grep -v "TAP version 13" | grep -v ": PASS:" | grep -v -i "# /us"

# Merge coverage files
if [ -f test/unit/coverage/cover.tmp ]; then
$GOPATH/bin/gocovmerge test/unit/coverage/cover.tmp test/unit/coverage/cover.out > test/unit/coverage/cover.all
mv test/unit/coverage/cover.all test/unit/coverage/cover.out
fi

# Clean up temporary files
rm -f test/unit/coverage/cover.tmp

0 comments on commit 0d01685

Please sign in to comment.