test: make tests actually work when packages have vendored imports
authorDan Williams <dcbw@redhat.com>
Wed, 19 Apr 2017 04:39:09 +0000 (23:39 -0500)
committerDan Williams <dcbw@redhat.com>
Wed, 19 Apr 2017 04:40:46 +0000 (23:40 -0500)
Go's "..." syntax (eg, ./plugins/...) doesn't traverse symlinks, so
go test wasn't finding the vendor/ directory for imports.  To get around
that we have to specify each testable package specifically rather
than use "...".

build
test

diff --git a/build b/build
index 7de707d..8b1f39a 100755 (executable)
--- a/build
+++ b/build
@@ -2,7 +2,7 @@
 set -e
 
 ORG_PATH="github.com/containernetworking"
-REPO_PATH="${ORG_PATH}/plugins"
+export REPO_PATH="${ORG_PATH}/plugins"
 
 if [ ! -h gopath/src/${REPO_PATH} ]; then
        mkdir -p gopath/src/${ORG_PATH}
diff --git a/test b/test
index bf76a76..f563f8d 100755 (executable)
--- a/test
+++ b/test
@@ -9,6 +9,8 @@
 # CNI_PATH=../cni/bin ./test
 set -e
 
+source ./build
+
 if [ -z "$CNI_PATH" ]; then
        echo "Need a valid CNI_PATH"
        exit 1
@@ -23,12 +25,36 @@ fi
 #add our build path to CNI_PATH
 CNI_PATH=$(pwd)/bin:${CNI_PATH}
 
-## Build everything
-./build
-
 echo "Running tests"
 
-TEST=${PKG:-./plugins/...}
-sudo -E bash -c "umask 0; PATH=${PATH} CNI_PATH=${CNI_PATH} go test ${TEST}"
+TESTABLE="plugins/sample"
+
+# user has not provided PKG override
+if [ -z "$PKG" ]; then
+       TEST=$TESTABLE
+       FMT=$TESTABLE
+
+# user has provided PKG override
+else
+       # strip out slashes and dots from PKG=./foo/
+       TEST=${PKG//\//}
+       TEST=${TEST//./}
+
+       # only run gofmt on packages provided by user
+       FMT="$TEST"
+fi
+
+# split TEST into an array and prepend REPO_PATH to each local package
+split=(${TEST// / })
+TEST=${split[@]/#/${REPO_PATH}/}
+
+sudo -E bash -c "umask 0; PATH=${GOROOT}/bin:$(pwd)/bin:${PATH} CNI_PATH=${CNI_PATH} go test ${TEST}"
+
+echo "Checking gofmt..."
+fmtRes=$(gofmt -l $FMT)
+if [ -n "${fmtRes}" ]; then
+       echo -e "gofmt checking failed:\n${fmtRes}"
+       exit 255
+fi