Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Joey Armstrong | 518f357 | 2024-02-11 07:56:25 -0500 | [diff] [blame] | 3 | # Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # make-unit.sh - run one or more make targets for unit testing |
| 18 | set -eu -o pipefail |
| 19 | |
| 20 | # when not running under Jenkins, use current dir as workspace, a blank project |
| 21 | # name |
| 22 | WORKSPACE=${WORKSPACE:-.} |
| 23 | GERRIT_PROJECT=${GERRIT_PROJECT:-} |
| 24 | |
| 25 | # Fixes to for golang projects to support GOPATH |
| 26 | # If $DEST_GOPATH is not an empty string: |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 27 | # - create GOPATH within WORKSPACE, and destination directory within |
Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 28 | # - set PATH to include $GOPATH/bin and the system go binaries |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 29 | # - move project from $WORKSPACE/$GERRIT_PROJECT to new location in $GOPATH |
| 30 | # - start tests within that directory |
Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 31 | |
| 32 | DEST_GOPATH=${DEST_GOPATH:-} |
| 33 | if [ ! -z "$DEST_GOPATH" ]; then |
Zack Williams | 76356cb | 2019-08-30 10:44:42 -0700 | [diff] [blame] | 34 | export GOPATH=${GOPATH:-$WORKSPACE/go} |
Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 35 | mkdir -p "$GOPATH/src/$DEST_GOPATH" |
Zack Williams | c9abcdc | 2019-05-09 21:23:38 -0700 | [diff] [blame] | 36 | export PATH=$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin:$GOPATH/bin |
Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 37 | test_path="$GOPATH/src/$DEST_GOPATH/$GERRIT_PROJECT" |
Matteo Scandolo | dfc7529 | 2019-08-29 14:27:49 -0700 | [diff] [blame] | 38 | mv "$WORKSPACE/$GERRIT_PROJECT" "$test_path" |
Zack Williams | cee76d2 | 2019-04-29 13:08:20 -0700 | [diff] [blame] | 39 | else |
| 40 | test_path="$WORKSPACE/$GERRIT_PROJECT" |
| 41 | fi |
| 42 | |
| 43 | # Use "test" as the default target, can be a space separated list |
| 44 | UNIT_TEST_TARGETS=${UNIT_TEST_TARGETS:-test} |
| 45 | |
| 46 | # Default to fail on the first test that fails |
| 47 | UNIT_TEST_KEEP_GOING=${UNIT_TEST_KEEP_GOING:-false} |
| 48 | |
| 49 | if [ ! -f "$test_path/Makefile" ]; then |
| 50 | echo "Makefile not found at $test_path!" |
| 51 | exit 1 |
| 52 | else |
| 53 | pushd "$test_path" |
| 54 | |
| 55 | # we want to split the make targets apart on spaces, so: |
| 56 | # shellcheck disable=SC2086 |
| 57 | if [ "$UNIT_TEST_KEEP_GOING" = "true" ]; then |
| 58 | make -k $UNIT_TEST_TARGETS |
| 59 | else |
| 60 | make $UNIT_TEST_TARGETS |
| 61 | fi |
| 62 | |
| 63 | popd |
| 64 | fi |