Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
hack: containerize unit test (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongchaodeng authored Dec 4, 2017
1 parent 0fd48c2 commit 0095267
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
1 change: 0 additions & 1 deletion hack/ci/run
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cleanup() {

trap cleanup EXIT


./hack/update_vendor.sh 1>/dev/null

GIT_VERSION=$(git rev-parse HEAD)
Expand Down
2 changes: 0 additions & 2 deletions hack/lib/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env bash

function go_build {
echo "building "${1}"..."
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $GO_BUILD_FLAGS -o ${bin_dir}/etcd-${1} -installsuffix cgo -ldflags "$go_ldflags" ./cmd/${1}/
Expand Down
8 changes: 8 additions & 0 deletions hack/lib/test_lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function listPkgs() {
go list ./cmd/... ./pkg/... ./test/... | grep -v generated
}

function listFiles() {
# pipeline is much faster than for loop
listPkgs | xargs -I {} find "${GOPATH}/src/{}" -name '*.go' | grep -v generated
}
36 changes: 8 additions & 28 deletions hack/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -o errexit
set -o nounset
set -o pipefail

source "hack/lib/test_lib.sh"

# KUBECONFIG can be an empty string and so needs to be explicitly declared to avoid an unbound variable error
KUBECONFIG=${KUBECONFIG:-""}
Expand All @@ -12,15 +13,6 @@ if [ -z "${PASSES-}" ]; then
PASSES="fmt build e2e e2eslow unit"
fi

function listPkgs() {
go list ./cmd/... ./pkg/... ./test/... | grep -v generated
}

function listFiles() {
# pipeline is much faster than for loop
listPkgs | xargs -I {} find "${GOPATH}/src/{}" -name '*.go' | grep -v generated
}

function fmt_pass {
if ! "./hack/k8s/codegen/verify-generated.sh"; then
exit 1
Expand Down Expand Up @@ -152,25 +144,13 @@ function upgrade_pass {
}

function unit_pass {
# coverage.txt is the combined coverage report consumed by codecov
echo "mode: atomic" > coverage.txt
TEST_PKGS=$(listPkgs | grep -v e2e)
for pkg in $TEST_PKGS
do
build_flags=("-i") # cache package compilation data for faster repeated builds
for i in {1..2}; do
go test ${build_flags[@]} -race -covermode=atomic -coverprofile=profile.out $pkg
# Expand empty array would cause "unbound variable".
# Expand one empty string array is equal to nothing.
build_flags=("")
done
if [ -f profile.out ]; then
tail -n +2 profile.out >> coverage.txt
rm profile.out
fi
done
# Send reports to codecov, CODECOV_TOKEN env must be present in Jenkins
(curl -s https://codecov.io/bash | bash) || true
DOCKER_REPO_ROOT="/go/src/github.com/coreos/etcd-operator"
docker run --rm \
-v "${PWD}":"${DOCKER_REPO_ROOT}" \
-w "${DOCKER_REPO_ROOT}" \
-e "CODECOV_TOKEN" \
golang:1.9 \
"./hack/unit_test"
}

for p in $PASSES
Expand Down
29 changes: 29 additions & 0 deletions hack/unit_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

source "hack/lib/test_lib.sh"

# coverage.txt is the combined coverage report consumed by codecov
echo "mode: atomic" > coverage.txt

TEST_PKGS=$(listPkgs | grep -v e2e)
for pkg in $TEST_PKGS
do
build_flags=("-i") # cache package compilation data for faster repeated builds
for i in {1..2}; do
go test ${build_flags[@]} -race -covermode=atomic -coverprofile=profile.out $pkg
# Expand empty array would cause "unbound variable".
# Expand one empty string array is equal to nothing.
build_flags=("")
done
if [ -f profile.out ]; then
tail -n +2 profile.out >> coverage.txt
rm profile.out
fi
done

# Send reports to codecov, CODECOV_TOKEN env must be present in Jenkins
(curl -s https://codecov.io/bash | bash) || true

0 comments on commit 0095267

Please sign in to comment.