From cc4eda7ea78785bb5985f18793d6de6ebfe9c77c Mon Sep 17 00:00:00 2001 From: Nan Liu Date: Fri, 1 Jul 2016 10:30:55 -0700 Subject: [PATCH] Update snap build scripts (#1027) * gofmt is asleep at the wheel in current test. * Update snap build scripts * make SNAP_PATH optional. * remove options around build-plugins. * use test as branch name when checking out git ref. * fix docker test so it works regardless of directory. * simplify travis.ci config. * Make sure gopath and path configured correctly. --- .travis.yml | 6 - Makefile | 14 +- .../mock/mock_medium_test.go | 1 - .../snap-publisher-file/file/file.go | 5 +- scripts/Dockerfile | 10 +- scripts/build.sh | 88 ------------- scripts/{build-plugin.sh => build_plugin.sh} | 26 +++- scripts/build_snap.sh | 65 +++++++++ scripts/common.sh | 124 ++++++++++++++++++ scripts/deps.sh | 31 ++--- scripts/gitcookie.sh | 2 - scripts/run_tests_with_docker.sh | 32 +++-- scripts/test.sh | 122 +++++++---------- 13 files changed, 306 insertions(+), 220 deletions(-) delete mode 100755 scripts/build.sh rename scripts/{build-plugin.sh => build_plugin.sh} (55%) create mode 100755 scripts/build_snap.sh create mode 100644 scripts/common.sh diff --git a/.travis.yml b/.travis.yml index f66bd3d58..d02f5dc6e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,17 +5,11 @@ go: - 1.6.2 before_install: - bash scripts/gitcookie.sh -- go get github.com/tools/godep -- go get github.com/axw/gocov/gocov -- go get github.com/mattn/goveralls -- go get golang.org/x/tools/cmd/goimports - go get github.com/smartystreets/goconvey/convey - if [ ! -d $SNAP_SOURCE ]; then mkdir -p $HOME/gopath/src/github.com/intelsdi-x; ln -s $TRAVIS_BUILD_DIR $SNAP_SOURCE; fi # CI for forks not from intelsdi-x -- go get golang.org/x/tools/cmd/cover env: global: - SNAP_SOURCE=/home/travis/gopath/src/github.com/intelsdi-x/snap - - SNAP_PATH=/home/travis/gopath/src/github.com/intelsdi-x/snap/build - GO15VENDOREXPERIMENT=1 matrix: - SNAP_TEST_TYPE=legacy diff --git a/Makefile b/Makefile index 99a61873f..081cb4538 100644 --- a/Makefile +++ b/Makefile @@ -21,21 +21,19 @@ default: deps: bash -c "./scripts/deps.sh" test: - export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh $(SNAP_TEST_TYPE)" + bash -c "./scripts/test.sh $(SNAP_TEST_TYPE)" test-legacy: - export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh legacy" + bash -c "./scripts/test.sh legacy" test-small: - export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh small" + bash -c "./scripts/test.sh small" test-medium: - export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh medium" + bash -c "./scripts/test.sh medium" test-large: - export SNAP_PATH=`pwd`/build; bash -c "./scripts/test.sh large" + bash -c "./scripts/test.sh large" check: $(MAKE) test all: - bash -c "./scripts/build.sh $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) true" -snap: - bash -c "./scripts/build.sh $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))" + bash -c "./scripts/build_snap.sh" install: cp build/bin/snapd /usr/local/bin/ cp build/bin/snapctl /usr/local/bin/ diff --git a/plugin/collector/snap-collector-mock1/mock/mock_medium_test.go b/plugin/collector/snap-collector-mock1/mock/mock_medium_test.go index f2f705af3..91d725343 100644 --- a/plugin/collector/snap-collector-mock1/mock/mock_medium_test.go +++ b/plugin/collector/snap-collector-mock1/mock/mock_medium_test.go @@ -218,4 +218,3 @@ func TestGetConfigPolicy(t *testing.T) { So(configPolicy, ShouldNotBeNil) }) } - diff --git a/plugin/publisher/snap-publisher-file/file/file.go b/plugin/publisher/snap-publisher-file/file/file.go index 8fb3b5a3e..23e5fd680 100644 --- a/plugin/publisher/snap-publisher-file/file/file.go +++ b/plugin/publisher/snap-publisher-file/file/file.go @@ -26,13 +26,13 @@ import ( "errors" "fmt" "os" + "strings" log "github.com/Sirupsen/logrus" "github.com/intelsdi-x/snap/control/plugin" "github.com/intelsdi-x/snap/control/plugin/cpolicy" "github.com/intelsdi-x/snap/core/ctypes" - "strings" ) const ( @@ -81,6 +81,7 @@ func (f *filePublisher) Publish(contentType string, content []byte, config map[s return nil } + // formatMetricTagsAsString returns metric's tags as a string in the following format tagKey:tagValue where the next tags are separated by semicolon func formatMetricTagsAsString(metricTags map[string]string) string { var tags string @@ -90,7 +91,7 @@ func formatMetricTagsAsString(metricTags map[string]string) string { // trim the last semicolon tags = strings.TrimSuffix(tags, "; ") - return "tags["+tags+"]" + return "tags[" + tags + "]" } func Meta() *plugin.PluginMeta { diff --git a/scripts/Dockerfile b/scripts/Dockerfile index 0c5b4bbe3..4d2c67c07 100644 --- a/scripts/Dockerfile +++ b/scripts/Dockerfile @@ -1,15 +1,9 @@ -FROM golang:latest +FROM golang:latest ENV GOPATH=$GOPATH:/app -ENV SNAP_PATH=/go/src/github.com/intelsdi-x/snap/build -RUN apt-get update && \ - apt-get -y install facter +RUN apt-get update WORKDIR /go/src/github.com/intelsdi-x/ RUN git clone https://github.com/intelsdi-x/gomit.git WORKDIR /go/src/github.com/intelsdi-x/snap ADD . /go/src/github.com/intelsdi-x/snap -RUN go get github.com/tools/godep && \ - go get golang.org/x/tools/cmd/goimports && \ - go get golang.org/x/tools/cmd/cover && \ - go get github.com/smartystreets/goconvey RUN scripts/deps.sh RUN make diff --git a/scripts/build.sh b/scripts/build.sh deleted file mode 100755 index 3cf336a0c..000000000 --- a/scripts/build.sh +++ /dev/null @@ -1,88 +0,0 @@ -#!/bin/bash -e - -#http://www.apache.org/licenses/LICENSE-2.0.txt -# -# -#Copyright 2015 Intel Corporation -# -#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. - -GITVERSION=`git describe --always --exact-match 2> /dev/null || echo "$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)-$(git log --pretty=format:"%h" -1)"` -SOURCEDIR=$1 -BUILDPLUGINS=$2 -SPLUGINFOLDER=$3 -SPLUGIN=$4 -BUILDDIR=$SOURCEDIR/build -PLUGINDIR=plugin -BINDIR=$BUILDDIR/bin -COLLECTORDIR=$BUILDDIR/$PLUGINDIR/collector -PUBLISHERDIR=$BUILDDIR/$PLUGINDIR/publisher -PROCESSORDIR=$BUILDDIR/$PLUGINDIR/processor -BUILDCMD='go build -a -ldflags "-w" -tags netgo' - -echo -echo "**** snap build ($GITVERSION) ****" -echo - -# Disable CGO for builds. -export CGO_ENABLED=0 - -# Clean build bin dir -rm -rf $BINDIR/* - -# Make dir -mkdir -p $BINDIR -mkdir -p $COLLECTORDIR -mkdir -p $PUBLISHERDIR -mkdir -p $PROCESSORDIR - -# snapd -echo "Source Dir = $SOURCEDIR" -echo " Building snapd" -go build -ldflags "-w -X main.gitversion=$GITVERSION" -o $BINDIR/snapd . || exit 1 - -# snapctl -echo " Building snapctl" -cd $SOURCEDIR/cmd -for d in *; do - if [[ -d $d ]]; then - echo " $d => $BINDIR/$d" - go build -ldflags "-w -X main.gitversion=$GITVERSION" -o $BINDIR/$d ./$d/ || exit 3 - fi -done - - -if [ "$BUILDPLUGINS" == "true" ]; then - if [ "$SPLUGIN" ] && [ -n "$SPLUGINFOLDER" ] - then - echo " Building Plugin: $SPLUGIN" - # Built-in single plugin building - cd $SOURCEDIR/plugin/$SPLUGINFOLDER/ - target=./$SPLUGIN/ - destination=$BUILDDIR/$PLUGINDIR/$SPLUGINFOLDER/$SPLUGIN - echo " $SPLUGIN => $destination" - $BUILDCMD -o $destination $target || exit 2 - cd $SOURCEDIR - else - # Build source plugins into build dir - rm -rf $BUILDDIR/$PLUGINDIR/* - cd $SOURCEDIR - echo " Building Plugin(s)" - find ./plugin/* -iname "snap-*" -print0 | xargs -0 -P 4 -n 1 $SOURCEDIR/scripts/build-plugin.sh $BUILDDIR/$PLUGINDIR/ || exit 2 - - cd $SOURCEDIR - fi -fi - -echo -echo "*******************" diff --git a/scripts/build-plugin.sh b/scripts/build_plugin.sh similarity index 55% rename from scripts/build-plugin.sh rename to scripts/build_plugin.sh index 4a76f607f..08f51d356 100755 --- a/scripts/build-plugin.sh +++ b/scripts/build_plugin.sh @@ -17,10 +17,24 @@ #See the License for the specific language governing permissions and #limitations under the License. -BUILDCMD='go build -a -ldflags "-w"' -BUILDDIR=$1 -PLUGIN=$2 -PLUGINNAME=`echo $PLUGIN | grep -oh "snap-.*"` +set -e +set -u +set -o pipefail -echo " $PLUGINNAME => $BUILDDIR" -$BUILDCMD -o $BUILDDIR/$PLUGINNAME $PLUGIN || exit 2 +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__proj_dir="$(dirname "$__dir")" + +# shellcheck source=scripts/common.sh +. "${__dir}/common.sh" + +build_dir="${__proj_dir}/build" +plugin_dir="${build_dir}/plugin" + +plugin_src_path=$1 +plugin_name=$(basename "${plugin_src_path}") +go_build=(go build -a -ldflags "-w") + +_debug "plugin source: ${plugin_src_path}" +_info "building ${plugin_name}" + +(cd "${plugin_src_path}" && "${go_build[@]}" -o "${plugin_dir}/${plugin_name}" . || exit 1) diff --git a/scripts/build_snap.sh b/scripts/build_snap.sh new file mode 100755 index 000000000..d503326b7 --- /dev/null +++ b/scripts/build_snap.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +#http://www.apache.org/licenses/LICENSE-2.0.txt +# +# +#Copyright 2015 Intel Corporation +# +#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. + +git_branch=$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-) +git_branch="${git_branch:-test}" +git_sha=$(git log --pretty=format:"%h" -1) +git_version=$(git describe --always --exact-match 2> /dev/null || echo "${git_branch}-${git_sha}") + +set -e +set -u +set -o pipefail + +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__proj_dir="$(dirname "$__dir")" + +# shellcheck source=scripts/common.sh +. "${__dir}/common.sh" + +_info "project path: ${__proj_dir}" + +build_dir="${__proj_dir}/build" +bin_dir="${build_dir}/bin" +plugin_dir="${build_dir}/plugin" +go_build=(go build -ldflags "-w -X main.gitversion=${git_version}") + +_info "snap build version: ${git_version}" +_info "git commit: $(git log --pretty=format:"%H" -1)" + +# Disable CGO for builds. +export CGO_ENABLED=0 + +# rebuild binaries: +_debug "removing: ${bin_dir:?}/*" +rm -rf "${bin_dir:?}/"* +mkdir -p "${bin_dir}" + +_info "building snapd" +"${go_build[@]}" -o "${bin_dir}/snapd" . || exit 1 + +_info "building snapctl" +(cd "${__proj_dir}/cmd/snapctl" && "${go_build[@]}" -o "${bin_dir}/snapctl" . || exit 1) + +# rebuild plugins: +_debug "removing: ${plugin_dir:?}/*" +rm -rf "${plugin_dir:?}/"* +mkdir -p "${plugin_dir}" + +_info "building plugins" +find "${__proj_dir}/plugin/" -type d -iname "snap-*" -print0 | xargs -0 -n 1 -I{} "${__dir}/build_plugin.sh" {} diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 000000000..501d4a6c3 --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,124 @@ +#!/bin/bash + +set -e +set -u +set -o pipefail + +LOG_LEVEL="${LOG_LEVEL:-6}" +NO_COLOR="${NO_COLOR:-}" + +trap_exitcode() { + exit $? +} + +trap trap_exitcode SIGINT + +_go_get() { + local _url=$1 + local _util + + _util=$(basename "${_url}") + + type -p "${_util}" > /dev/null || go get "${_url}" && _debug "go get ${_util} ${_url}" +} + +_fmt () { + local color_debug="\x1b[35m" + local color_info="\x1b[32m" + local color_notice="\x1b[34m" + local color_warning="\x1b[33m" + local color_error="\x1b[31m" + local colorvar=color_$1 + + local color="${!colorvar:-$color_error}" + local color_reset="\x1b[0m" + if [ "${NO_COLOR}" = "true" ] || [[ "${TERM:-}" != "xterm"* ]] || [ -t 1 ]; then + # Don't use colors on pipes or non-recognized terminals + color=""; color_reset="" + fi + echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" "${1}")${color_reset}"; +} + +_debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${*}" 1>&2 || true; } +_info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${*}" 1>&2 || true; } +_notice () { [ "${LOG_LEVEL}" -ge 5 ] && echo "$(_fmt notice) ${*}" 1>&2 || true; } +_warning () { [ "${LOG_LEVEL}" -ge 4 ] && echo "$(_fmt warning) ${*}" 1>&2 || true; } +_error () { [ "${LOG_LEVEL}" -ge 3 ] && echo "$(_fmt error) ${*}" 1>&2 || true; exit 1; } + +test_dirs=$(find . -type f -name '*.go' -not -path "./.*" -not -path "*/_*" -not -path "./Godeps/*" -not -path "./vendor/*" -print0 | xargs -0 -n1 dirname| sort -u) + +_debug "go code directories: +${test_dirs}" + +_go_get() { + local _url=$1 + local _util + + _util=$(basename "${_url}") + + type -p "${_util}" > /dev/null || go get "${_url}" && _debug "go get ${_util} ${_url}" +} + +_path_prepend() { + if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then + PATH="$1${PATH:+":$PATH"}" + _debug "Update PATH: ${PATH}" + fi +} + +_go_path() { + [[ ! -z $GOPATH ]] || _error "Error \$GOPATH unset" + + _debug "GOPATH: ${GOPATH}" + _debug "PATH: ${PATH}" + + # NOTE: handles colon separated gopath + go_bin_path=${GOPATH//://bin:}/bin + + _path_prepend "${go_bin_path}" +} + +_goimports() { + _go_get golang.org/x/tools/cmd/goimports + test -z "$(goimports -l -d $(find . -type f -name '*.go' -not -path "./vendor/*") | tee /dev/stderr)" +} + +_gofmt() { + test -z "$(gofmt -l -d $(find . -type f -name '*.go' -not -path "./vendor/*") | tee /dev/stderr)" +} + +_golint() { + _go_get github.com/golang/lint/golint + golint ./... +} + +_go_vet() { + go vet ${test_dirs} +} + +_go_race() { + go test -race ./... +} + +_go_test() { + _info "running test type: ${TEST_TYPE}" + # Standard go tooling behavior is to ignore dirs with leading underscors + for dir in $test_dirs; + do + if [[ -z ${go_cover+x} ]]; then + _debug "running go test with cover in ${dir}" + go test --tags="${TEST_TYPE}" -covermode=count -coverprofile="${dir}/profile.tmp" "${dir}" + if [ -f "${dir}/profile.tmp" ]; then + tail -n +2 "${dir}/profile.tmp" >> "profile-${TEST_TYPE}.cov" + rm "${dir}/profile.tmp" + fi + else + _debug "running go test without cover in ${dir}" + go test --tags="${TEST_TYPE}" "${dir}" + fi + done +} + +_go_cover() { + go tool cover -func "profile-${TEST_TYPE}.cov" +} diff --git a/scripts/deps.sh b/scripts/deps.sh index c9977e21f..1350ecbcb 100755 --- a/scripts/deps.sh +++ b/scripts/deps.sh @@ -1,9 +1,9 @@ -#!/bin/bash -e +#!/bin/bash #http://www.apache.org/licenses/LICENSE-2.0.txt # # -#Copyright 2015 Intel Corporation +#Copyright 2016 Intel Corporation # #Licensed under the Apache License, Version 2.0 (the "License"); #you may not use this file except in compliance with the License. @@ -17,18 +17,19 @@ #See the License for the specific language governing permissions and #limitations under the License. -# This script runs the correct godep sequences for snap and built-in plugins -# This will rebase back to the committed version. It should be run from snap/. -ctrl_c() -{ - exit $? -} -trap ctrl_c SIGINT +set -e +set -u +set -o pipefail -# Godep -echo "Getting godep if not found" -go get github.com/tools/godep +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__proj_dir="$(dirname "$__dir")" -# First load snap deps -echo "Checking snap root for deps" -godep restore \ No newline at end of file +# shellcheck source=scripts/common.sh +. "${__dir}/common.sh" + +_go_path + +_go_get github.com/tools/godep + +_info "restoring dependency with godep" +(cd "${__proj_dir}" && godep restore) diff --git a/scripts/gitcookie.sh b/scripts/gitcookie.sh index ed8b1240c..6be85e484 100644 --- a/scripts/gitcookie.sh +++ b/scripts/gitcookie.sh @@ -7,5 +7,3 @@ tr , \\t <<\__END__ >>~/.gitcookies go.googlesource.com,FALSE,/,TRUE,2147483647,o,git-snappytheturtle1202.gmail.com=1/4zg9qf0zy1bmnMpEnqT7IL-rcL5_y8XP7bpfYE5DzN0 go-review.googlesource.com,FALSE,/,TRUE,2147483647,o,git-snappytheturtle1202.gmail.com=1/4zg9qf0zy1bmnMpEnqT7IL-rcL5_y8XP7bpfYE5DzN0 __END__ - - diff --git a/scripts/run_tests_with_docker.sh b/scripts/run_tests_with_docker.sh index 74e59a54d..555dd3aa0 100755 --- a/scripts/run_tests_with_docker.sh +++ b/scripts/run_tests_with_docker.sh @@ -17,14 +17,24 @@ #See the License for the specific language governing permissions and #limitations under the License. -if [[ $# -ne 1 ]]; then - echo "ERROR; missing SNAP_TEST_TYPE (Usage: $0 SNAP_TEST_TYPE)" - exit -2 -elif [[ “$1” != “legacy” && "$1" != "small" && “$1” != “medium” && “$1” != “large” ]]; then - echo "Error; invalid SNAP_TEST_TYPE (value must be one of 'legacy', 'small', 'medium', or 'large'; received $1)" - exit -1 -fi -SNAP_TEST_TYPE=$1 - -docker build -t intelsdi-x/snap-test -f scripts/Dockerfile . -docker run -it intelsdi-x/snap-test scripts/test.sh $SNAP_TEST_TYPE +SNAP_TEST_TYPE="${SNAP_TEST_TYPE:-$1}" + +UNIT_TEST="${UNIT_TEST:-"gofmt goimports go_test go_cover"}" + +set -e +set -u +set -o pipefail + +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__proj_dir="$(dirname "$__dir")" + +# shellcheck source=scripts/common.sh +. "${__dir}/common.sh" + +_debug "script directory ${__dir}" +_debug "project directory ${__proj_dir}" + +[[ "$SNAP_TEST_TYPE" =~ ^(small|medium|large|legacy)$ ]] || _error "invalid TEST_TYPE (value must be 'small', 'medium', 'large', or 'legacy', recieved:${SNAP_TEST_TYPE}" + +(cd ${__proj_dir} && docker build -t intelsdi-x/snap-test -f "${__dir}/Dockerfile" .) +docker run -it intelsdi-x/snap-test scripts/test.sh "${SNAP_TEST_TYPE}" diff --git a/scripts/test.sh b/scripts/test.sh index c367be72a..b755c93a3 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash -e +#!/bin/bash #http://www.apache.org/licenses/LICENSE-2.0.txt # @@ -17,85 +17,61 @@ #See the License for the specific language governing permissions and #limitations under the License. -# The script does automatic checking on a Go package and its sub-packages, including: -# 1. gofmt (http://golang.org/cmd/gofmt/) -# 2. goimports (https://github.com/bradfitz/goimports) -# 3. golint (https://github.com/golang/lint) (disabled) -# 4. go vet (http://golang.org/cmd/vet) (disabled) -# 5. race detector (http://blog.golang.org/race-detector) (disabled) -# 6. test coverage (http://blog.golang.org/cover) - -if [[ $# -ne 1 ]]; then - echo "ERROR; missing SNAP_TEST_TYPE (Usage: $0 SNAP_TEST_TYPE)" - exit -2 -elif [[ “$1” != “legacy” && "$1" != "small" && “$1” != “medium” && “$1” != “large” ]]; then - echo "Error; invalid SNAP_TEST_TYPE (value must be one of 'legacy', 'small', 'medium', or 'large'; received $1)" - exit -1 -fi -SNAP_TEST_TYPE=$1 +# Support travis.ci environment matrix: +SNAP_TEST_TYPE="${SNAP_TEST_TYPE:-$1}" -# If the following plugins don't exist, exit -[ -f $SNAP_PATH/plugin/snap-collector-mock1 ] || { echo 'Error: $SNAP_PATH/plugin/snap-collector-mock1 does not exist. Run make to build it.' ; exit 1; } -[ -f $SNAP_PATH/plugin/snap-collector-mock2 ] || { echo 'Error: $SNAP_PATH/plugin/snap-collector-mock2 does not exist. Run make to build it.' ; exit 1; } -[ -f $SNAP_PATH/plugin/snap-processor-passthru ] || { echo 'Error: $SNAP_PATH/plugin/snap-processor-passthru does not exist. Run make to build it.' ; exit 1; } -[ -f $SNAP_PATH/plugin/snap-publisher-file ] || { echo 'Error: $SNAP_PATH/plugin/snap-publisher-file does not exist. Run make to build it.' ; exit 1; } - -TEST_DIRS="cmd/ control/ core/ mgmt/ pkg/ snapd.go scheduler/" -# VET_DIRS="./cmd/... ./control/... ./core/... ./mgmt/... ./pkg/... ./scheduler/... ." +UNIT_TEST="${UNIT_TEST:-"gofmt goimports go_test go_cover"}" set -e +set -u +set -o pipefail + +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__proj_dir="$(dirname "$__dir")" + +SNAP_PATH="${SNAP_PATH:-"${__proj_dir}/build"}" +export SNAP_PATH +test_dirs=$(find . -type f -name '*.go' -not -path "./.*" -not -path "*/_*" -not -path "./Godeps/*" -not -path "./vendor/*" -print0 | xargs -0 -n1 dirname| sort -u) +export test_dirs + +# shellcheck source=scripts/common.sh +. "${__dir}/common.sh" + +_debug "script directory ${__dir}" +_debug "project directory ${__proj_dir}" + +[[ "$SNAP_TEST_TYPE" =~ ^(small|medium|large|legacy)$ ]] || _error "invalid/missing SNAP_TEST_TYPE (value must be 'small', 'medium', 'large', or 'legacy', received:${SNAP_TEST_TYPE}" +# If the following plugins don't exist, exit +[ -f $SNAP_PATH/plugin/snap-collector-mock1 ] || { _error 'Error: $SNAP_PATH/plugin/snap-collector-mock1 does not exist. Run make to build it.'; } +[ -f $SNAP_PATH/plugin/snap-collector-mock2 ] || { _error 'Error: $SNAP_PATH/plugin/snap-collector-mock2 does not exist. Run make to build it.'; } +[ -f $SNAP_PATH/plugin/snap-processor-passthru ] || { _error 'Error: $SNAP_PATH/plugin/snap-processor-passthru does not exist. Run make to build it.'; } +[ -f $SNAP_PATH/plugin/snap-publisher-file ] || { _error 'Error: $SNAP_PATH/plugin/snap-publisher-file does not exist. Run make to build it.'; } + +_go_path # If the following tools don't exist, get them -echo "Getting GoConvey if not found" -go get github.com/smartystreets/goconvey -echo "Getting goimports if not found" -go get golang.org/x/tools/cmd/goimports -echo "Getting cover if not found" -go get golang.org/x/tools/cmd/cover - -# Automatic checks -echo "gofmt" -test -z "$(gofmt -s -l -d $TEST_DIRS | tee /dev/stderr)" - -echo "goimports" -test -z "$(goimports -l -d $TEST_DIRS | tee /dev/stderr)" - -# Useful but should not fail on link per: https://github.com/golang/lint -# "The suggestions made by golint are exactly that: suggestions. Golint is not perfect, -# and has both false positives and false negatives. Do not treat its output as a gold standard. -# We will not be adding pragmas or other knobs to suppress specific warnings, so do not expect -# or require code to be completely "lint-free". In short, this tool is not, and will never be, -# trustworthy enough for its suggestions to be enforced automatically, for example as part of -# a build process" -# echo "golint" -# golint ./... - -# Disabling running go vet currently due to the inconsistency in it's reported -# outputs depending on whether the project was pulled down via 'go get' or 'git clone'. -# We will look to re-enable go vet checking when we can provide consistency in outputs -# no matter how a developer gets the project. -#echo "go vet" -#go vet $VET_DIRS - -# go test -race ./... - Lets disable for now +_go_get github.com/smartystreets/goconvey # Run test coverage on each subdirectories and merge the coverage profile. -echo "mode: count ($SNAP_TEST_TYPE)" > profile-$SNAP_TEST_TYPE.cov -echo "" -echo "==================== $SNAP_TEST_TYPE ====================" - -# Standard go tooling behavior is to ignore dirs with leading underscors -for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -not -path './examples/*' -not -path './scripts/*' -type d); -do - if ls $dir/*.go &> /dev/null; then - go test -tags=$SNAP_TEST_TYPE -covermode=count -coverprofile=$dir/profile.tmp $dir - if [ -f $dir/profile.tmp ] - then - cat $dir/profile.tmp | tail -n +2 >> profile-$SNAP_TEST_TYPE.cov - rm $dir/profile.tmp - fi +echo "mode: count (${SNAP_TEST_TYPE})" > "profile-${SNAP_TEST_TYPE}.cov" + +TEST_TYPE=$SNAP_TEST_TYPE +export TEST_TYPE + +go_tests=(gofmt goimports golint go_vet go_race go_test go_cover) + +_debug "available unit tests: ${go_tests[*]}" +_debug "user specified tests: ${UNIT_TEST}" + +((n_elements=${#go_tests[@]}, max=n_elements - 1)) + +for ((i = 0; i <= max; i++)); do + if [[ "${UNIT_TEST}" =~ (^| )"${go_tests[i]}"( |$) ]]; then + _info "running ${go_tests[i]}" + _"${go_tests[i]}" + else + _debug "skipping ${go_tests[i]}" fi done -go tool cover -func profile-$SNAP_TEST_TYPE.cov -echo "==================== end $SNAP_TEST_TYPE ====================" +_info "test complete: ${SNAP_TEST_TYPE}"