Support running unit tests from a Makefile

- Use name-extension to allow multiple invocation of make-unit
  job-template
- unit-test-keep-going to allow a single test failure to not prevent
  other tests from being run
- When DEST_GOPATH is set, handle go/GOPATH related issues
- Enable lint and test targets on voltha-go repo

Change-Id: I3dbb710aa1924e91c5ab49980144fa38c379edc8
diff --git a/jjb/defaults.yaml b/jjb/defaults.yaml
index 80d8803..f850d4b 100644
--- a/jjb/defaults.yaml
+++ b/jjb/defaults.yaml
@@ -18,7 +18,7 @@
     destination-dir: ''
     basedir: ''
 
-    # used to rename jobs if version/branch forking is required
+    # used to rename jobs if required
     name-extension: ''
 
     # How long to keep builds and artifacts
@@ -134,3 +134,18 @@
     # Optionally allow JUnit results to be empty when test framework is set up,
     # but no tests exist. Default behavior is to fail when test results are empty.
     junit-allow-empty-results: false
+
+    # Unit test targets
+    # List of targets to run when testing a patchset, run with make or similar
+    # defaults to just 'test', multiple targets should be space separated
+    unit-test-targets: 'test'
+
+    # whether to "keep going" on multiple tests if one fails
+    # maps to the `-k` option passed to make in make-unit.yaml
+    unit-test-keep-going: false
+
+    # golang specific variables
+    # dest-gopath handles checking out patchsets and putting them into a GOPATH
+    # This portion of the path should be included: `$GOPATH/src/<dest-gopath>/<project>"
+    # If blank, golang related variables won't be set
+    dest-gopath: ''
diff --git a/jjb/make-unit.yaml b/jjb/make-unit.yaml
new file mode 100644
index 0000000..ec3a530
--- /dev/null
+++ b/jjb/make-unit.yaml
@@ -0,0 +1,66 @@
+---
+# Makefile based unit test
+
+- job-template:
+    id: 'make-unit-test'
+    name: 'verify_{project}_unit-test{name-extension}'
+
+    description: |
+      Created by {id} job-template from ci-management/jjb/make-unit.yaml<br/>
+      Runs make with the following unit tests targets - '{unit-test-targets}'
+
+    triggers:
+      - cord-infra-gerrit-trigger-patchset:
+          gerrit-server-name: '{gerrit-server-name}'
+          project-regexp: '^{project}$'
+          branch-regexp: '{branch-regexp}'
+          dependency-jobs: '{dependency-jobs}'
+          file-include-regexp: '{all-files-regexp}'
+
+    properties:
+      - cord-infra-properties:
+          build-days-to-keep: '{build-days-to-keep}'
+          artifact-num-to-keep: '{artifact-num-to-keep}'
+
+    wrappers:
+      - lf-infra-wrappers:
+          build-timeout: '{build-timeout}'
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+
+    scm:
+      - cord-infra-gerrit-scm:
+          git-url: '$GIT_URL/$GERRIT_PROJECT'
+          refspec: '$GERRIT_REFSPEC'
+          branch: '$GERRIT_BRANCH'
+          submodule-recursive: 'false'
+          choosing-strategy: gerrit
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+          basedir: '{project}'
+
+    node: '{build-node}'
+    project-type: freestyle
+    concurrent: true
+
+    builders:
+      - inject:
+          properties-content: |
+            DEST_GOPATH={dest-gopath}
+            UNIT_TEST_TARGETS={unit-test-targets}
+            UNIT_TEST_KEEP_GOING={unit-test-keep-going}
+      - shell: !include-raw-escape: shell/make-unit.sh
+
+    publishers:
+      - junit:
+          results: "**/*results.xml,**/*report.xml"
+          allow-empty-results: '{junit-allow-empty-results}'
+      - cobertura:
+          report-file: "**/*coverage.xml"
+          targets:
+            - files:
+                healthy: 80
+                unhealthy: 0
+                failing: 0
+            - method:
+                healthy: 50
+                unhealthy: 0
+                failing: 0
diff --git a/jjb/shell/make-unit.sh b/jjb/shell/make-unit.sh
new file mode 100755
index 0000000..4c83ea8
--- /dev/null
+++ b/jjb/shell/make-unit.sh
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+
+# Copyright 2019-present Open Networking Foundation
+#
+# 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.
+
+# make-unit.sh - run one or more make targets for unit testing
+set -eu -o pipefail
+
+# when not running under Jenkins, use current dir as workspace, a blank project
+# name
+WORKSPACE=${WORKSPACE:-.}
+GERRIT_PROJECT=${GERRIT_PROJECT:-}
+
+# Fixes to for golang projects to support GOPATH
+# If $DEST_GOPATH is not an empty string:
+# - set create GOPATH, and destination directory within in
+# - set PATH to include $GOPATH/bin and the system go binaries
+# - symlink from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH
+# - start tests in that directory
+
+DEST_GOPATH=${DEST_GOPATH:-}
+if [ ! -z "$DEST_GOPATH" ]; then
+  export GOPATH=${GOPATH:-~/go}
+  mkdir -p "$GOPATH/src/$DEST_GOPATH"
+  export PATH=$PATH:/usr/lib/go-1.10/bin:/usr/local/go/bin:$GOPATH/bin
+  test_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT"
+  ln -r -s "$WORKSPACE/$GERRIT_PROJECT" "$test_path"
+else
+  test_path="$WORKSPACE/$GERRIT_PROJECT"
+fi
+
+# Use "test" as the default target, can be a space separated list
+UNIT_TEST_TARGETS=${UNIT_TEST_TARGETS:-test}
+
+# Default to fail on the first test that fails
+UNIT_TEST_KEEP_GOING=${UNIT_TEST_KEEP_GOING:-false}
+
+if [ ! -f "$test_path/Makefile" ]; then
+  echo "Makefile not found at $test_path!"
+  exit 1
+else
+  pushd "$test_path"
+
+  # we want to split the make targets apart on spaces, so:
+  # shellcheck disable=SC2086
+  if [ "$UNIT_TEST_KEEP_GOING" = "true" ]; then
+    make -k $UNIT_TEST_TARGETS
+  else
+    make $UNIT_TEST_TARGETS
+  fi
+
+  popd
+fi
diff --git a/jjb/verify/voltha-go.yaml b/jjb/verify/voltha-go.yaml
index ad03382..2da0fd8 100644
--- a/jjb/verify/voltha-go.yaml
+++ b/jjb/verify/voltha-go.yaml
@@ -13,103 +13,13 @@
     name: 'verify-voltha-go-jobs'
     jobs:
       - 'verify-licensed'
-#     - 'verify-sonarqube':
-#         dependency-jobs: 'verify_voltha-go_licensed'
-#      - 'voltha-go-tests':
-#          dependency-jobs: 'verify_voltha-go_sonarqube'
-
-# disabled until codebase is ready to pass tests
-- job-template:
-    id: 'voltha-go-tests'
-    name: 'verify_{project}_tests'
-
-    description: |
-      Created by {id} job-template from ci-management/jjb/verify/voltha-go.yaml
-
-    triggers:
-      - cord-infra-gerrit-trigger-patchset:
-          gerrit-server-name: '{gerrit-server-name}'
-          project-regexp: '^{project}$'
-          branch-regexp: '{branch-regexp}'
-          dependency-jobs: '{dependency-jobs}'
-          file-include-regexp: '{all-files-regexp}'
-
-    properties:
-      - cord-infra-properties:
-          build-days-to-keep: '{build-days-to-keep}'
-          artifact-num-to-keep: '{artifact-num-to-keep}'
-
-    wrappers:
-      - lf-infra-wrappers:
-          build-timeout: 20
-          jenkins-ssh-credential: '{jenkins-ssh-credential}'
-
-    scm:
-      - cord-infra-gerrit-scm:
-          git-url: '$GIT_URL/$GERRIT_PROJECT'
-          refspec: '$GERRIT_REFSPEC'
-          branch: '$GERRIT_BRANCH'
-          submodule-recursive: 'false'
-          choosing-strategy: 'gerrit'
-          jenkins-ssh-credential: '{jenkins-ssh-credential}'
-          basedir: '{project}'
-
-    node: 'ubuntu16.04-basebuild-1c-2g'
-    project-type: freestyle
-    concurrent: true
-
-    builders:
-      - shell: |
-          #!/usr/bin/env bash
-          set -eux -o pipefail
-
-          export GOPATH=~/go
-          export PATH=$PATH:/usr/lib/go-1.10/bin:/usr/local/go/bin:~/go/bin
-
-          # move code the proper location
-          mkdir -p $GOPATH/src/github.com/opencord
-          mkdir -p $GOPATH/src/protos
-          cp voltha-go/protos/*.proto $GOPATH/src/protos/
-          cp voltha-go/protos/scripts/* $GOPATH/src/protos/
-          mv voltha-go $GOPATH/src/github.com/opencord/voltha-go
-
-          # get prereqs
-          go get -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
-          go get -v github.com/golang/protobuf/protoc-gen-go
-          go get -v github.com/stretchr/testify
-
-          # build protobufs
-          sh $GOPATH/src/protos/build_protos.sh $GOPATH/src/protos
-
-          # get prereqs
-          pushd $GOPATH/src/github.com/opencord/voltha-go/rw_core
-          go get -v -d ./...
-
-          rm -rf $GOPATH/src/go.etcd.io/etcd/vendor/golang.org/x/net/trace
-
-          # build
-          go build -v -o $GOPATH/src/rw_core
-
-          # tests
-          go test -v ./...  2>&1 | go-junit-report > $WORKSPACE/junit-report.xml
-
-          go test -coverprofile=coverage.txt -covermode=count ./...
-          gocover-cobertura < coverage.txt > $WORKSPACE/coverage.xml
-
-          popd
-
-
-    publishers:
-      - junit:
-          results: "junit-report.xml"
-      - cobertura:
-          report-file: "coverage.xml"
-          targets:
-            - files:
-                healthy: 80
-                unhealthy: 0
-                failing: 0
-            - method:
-                healthy: 50
-                unhealthy: 0
-                failing: 0
+      - 'make-unit-test':
+          dest-gopath: "github.com/opencord"
+          name-extension: "-lint"
+          unit-test-targets: 'lint'
+          unit-test-keep-going: 'true'
+      - 'make-unit-test':
+          dest-gopath: "github.com/opencord"
+          name-extension: "-tests"
+          unit-test-targets: 'test'
+          unit-test-keep-going: 'true'