Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | #*----------------------------------------------------------------------------*# |
| 3 | #* Copyright (c) FIRST 2014. All Rights Reserved. *# |
| 4 | #* Open Source Software - may be modified and shared by FRC teams. The code *# |
| 5 | #* must be accompanied by the FIRST BSD license file in the root directory of *# |
| 6 | #* the project. *# |
| 7 | #*----------------------------------------------------------------------------*# |
| 8 | |
| 9 | # This file is intended to be run in the DEFAULT_TEST_DIR on the roboRIO. |
| 10 | # Do not attempt to run this file on your local system. |
| 11 | # There is one file (delploy-and-run-test-on-robot.sh) that is designed to |
| 12 | # deploy this file allong with the compiled tests for you. |
| 13 | |
| 14 | # Configurable variables |
| 15 | source config.sh |
| 16 | |
| 17 | DEFAULT_TEST_DIR=${DEFAULT_DESTINATION_DIR} |
| 18 | DEFAULT_PATH_TO_JRE=/usr/local/frc/JRE/bin/java |
| 19 | |
| 20 | usage="$(basename "$0") [-h] (java|cpp) name [-d test_dir] [-m] [-A] [arg] [arg]... |
| 21 | A script designed to run the integration tests. |
| 22 | This script should only be run on the roborio. |
| 23 | Where: |
| 24 | -h Show this help text |
| 25 | name The name of the user trying to run the tests (used for driver station) |
| 26 | -d The directory where the tests have been placed. |
| 27 | This is done to prevent overwriting an already running program |
| 28 | in the case where another user already has the driver station mutex. |
| 29 | This scrip will automatically move the test into the ${DEFAULT_TEST_DIR} |
| 30 | directory when the driver station mutex is released. |
| 31 | Default: Assumes the test is in the same directory as this scrip. |
| 32 | -m The driver station mutex will be handled manually. |
| 33 | -A Do not use the default arguments for the given language. |
| 34 | arg The arguments to be passed to test." |
| 35 | |
| 36 | mutexTaken=false |
| 37 | driverStationEnabled=false |
| 38 | # This function should run even if the script exits abnormally |
| 39 | function finish { |
| 40 | if [ "$driverStationEnabled" == true ]; then |
| 41 | /usr/local/frc/bin/teststand ds --name="${NAME}" disable |
| 42 | driverStationEnabled=false |
| 43 | fi |
| 44 | if [ "$mutexTaken" == true ]; then |
| 45 | /usr/local/frc/bin/teststand give --name="${NAME}" |
| 46 | mutexTaken=false |
| 47 | fi |
| 48 | } |
| 49 | trap finish EXIT SIGINT |
| 50 | |
| 51 | # This function should be run asynchronysly to enable the tests 10 |
| 52 | # seconds after they have been run. |
| 53 | function enableIn10Seconds { |
| 54 | /usr/local/frc/bin/teststand ds --name="${NAME}" disable |
| 55 | driverStationEnabled=true |
| 56 | sleep 10 |
| 57 | /usr/local/frc/bin/teststand ds --name="${NAME}" enable |
| 58 | } |
| 59 | |
| 60 | # Are you trying to run this program on a platform other than the roboRIO? |
| 61 | if [[ ! -e "${DEFAULT_TEST_DIR}" ]]; then |
| 62 | printf "Failed to find %s\nAre you trying to run this file on your local computer?\n" "${DEFAULT_TEST_DIR}" |
| 63 | printf "This script should only be run on the roboRIO.\n\n" |
| 64 | echo "$usage" |
| 65 | exit 1 |
| 66 | fi |
| 67 | |
| 68 | LANGUAGE=none |
| 69 | TEST_FILE=none |
| 70 | NAME=$2 |
| 71 | TEST_DIR="$DEFAULT_TEST_DIR" |
| 72 | # Begin searching for options from the third paramater on |
| 73 | PARAM_ARGS=${@:3} |
| 74 | # Where the test arguments start |
| 75 | TEST_RUN_ARGS=${@:3} |
| 76 | RUN_WITH_DEFAULT_ARGS=true |
| 77 | DEFAULT_ARGS="" |
| 78 | MUTEX_OVERRIDE=false |
| 79 | |
| 80 | # Determine the language that we are attempting to run |
| 81 | if [[ "$1" = java ]]; then |
| 82 | LANGUAGE=$1 |
| 83 | TEST_FILE=$DEFAULT_JAVA_TEST_NAME |
| 84 | DEFAULT_ARGS=$DEFAULT_JAVA_TEST_ARGS |
| 85 | elif [[ "$1" = cpp ]]; then |
| 86 | LANGUAGE=$1 |
| 87 | TEST_FILE=$DEFAULT_CPP_TEST_NAME |
| 88 | DEFAULT_ARGS=$DEFAULT_CPP_TEST_ARGS |
| 89 | elif [[ "$1" = "-h" ]]; then |
| 90 | #If the first argument is the help option |
| 91 | #Allow it to be searhced for in getopts |
| 92 | PARAM_ARGS=${@} |
| 93 | else |
| 94 | printf "Invalid language selection: %s\n\n" "$1" >&2 |
| 95 | echo "$usage" >&2 |
| 96 | exit 1 |
| 97 | fi |
| 98 | |
| 99 | PARAM_COUNTER=2 |
| 100 | printf "Param Args ${PARAM_ARGS}\n" |
| 101 | |
| 102 | # Check for optional paramaters |
| 103 | while getopts ':hmd:A' option $PARAM_ARGS ; do |
| 104 | case "$option" in |
| 105 | h) |
| 106 | # Print the help message |
| 107 | printf "Usage:\n" |
| 108 | echo "$usage" |
| 109 | exit |
| 110 | ;; |
| 111 | A) |
| 112 | # Remove the default arguments |
| 113 | RUN_WITH_DEFAULT_ARGS=false |
| 114 | PARAM_COUNTER=$[$PARAM_COUNTER +1] |
| 115 | ;; |
| 116 | m) |
| 117 | MUTEX_OVERRIDE=true |
| 118 | PARAM_COUNTER=$[$PARAM_COUNTER +1] |
| 119 | ;; |
| 120 | d) |
| 121 | TEST_DIR=$OPTARG |
| 122 | # Since we are selecting the directory the run args start from the 5th argument |
| 123 | PARAM_COUNTER=$[$PARAM_COUNTER +2] |
| 124 | ;; |
| 125 | ?) |
| 126 | # When an unknown param is found then we are done so break |
| 127 | break |
| 128 | ;; |
| 129 | esac |
| 130 | done |
| 131 | |
| 132 | TEST_RUN_ARGS=${@:$[$PARAM_COUNTER +1]} |
| 133 | |
| 134 | if [[ "$RUN_WITH_DEFAULT_ARGS" == true ]]; then |
| 135 | TEST_RUN_ARGS="${DEFAULT_ARGS} ${TEST_RUN_ARGS}" |
| 136 | fi |
| 137 | |
| 138 | # Make sure at least two paramaters are passed or four if running with -d option |
| 139 | if [[ $# -lt $PARAM_COUNTER ]]; then |
| 140 | printf "Invalid arg count. Should be %s, was %s.\n" "${PARAM_COUNTER}" "$#" |
| 141 | echo "$usage" |
| 142 | exit 1 |
| 143 | fi |
| 144 | |
| 145 | # If the mutex has been retrived a higher level in the tree |
| 146 | if [ "$MUTEX_OVERRIDE" == false ]; then |
| 147 | # Attempt to take the mutex for the driver station |
| 148 | mutexTaken=true |
| 149 | /usr/local/frc/bin/teststand take --name="${NAME}" |
| 150 | else |
| 151 | printf "Override driver station control enabled.\n" |
| 152 | fi |
| 153 | |
| 154 | # Kill all running robot programs |
| 155 | killall java FRCUserProgram |
| 156 | |
| 157 | # Once we have the mutex no other tests are running |
| 158 | # If we are running with the -d argument move the test to the DEFAULT_TEST_DIR |
| 159 | if [[ ! -e "${TEST_DIR}/${TEST_FILE}" ]]; then |
| 160 | printf "Failed to find %s.\nDid you copy the test file correctly?\n" "${TEST_DIR}/${TEST_FILE}" |
| 161 | echo "$usage" |
| 162 | exit 1 |
| 163 | elif [[ $TEST_DIR != "$DEFAULT_TEST_DIR" ]]; then |
| 164 | mv "${TEST_DIR}/${TEST_FILE}" "${DEFAULT_TEST_DIR}" |
| 165 | fi |
| 166 | |
| 167 | # Make sure the excecutable file has permission to run |
| 168 | |
| 169 | # Setup the driver station to enable automatically in 10 seconds without waiting for the function to excecute. |
| 170 | enableIn10Seconds& |
| 171 | |
| 172 | # Get the serial number and FPGADeviceCode for this rio |
| 173 | export serialnum=`/sbin/fw_printenv -n serial#` |
| 174 | export eval `/sbin/fw_printenv DeviceCode FPGADeviceCode DeviceDesc TargetClass` |
| 175 | |
| 176 | # Store the run command for the language |
| 177 | RUN_COMMAND=none |
| 178 | if [[ ${LANGUAGE} = java ]]; then |
| 179 | chmod a+x ${DEFAULT_JAVA_TEST_NAME} |
| 180 | RUN_COMMAND="env LD_LIBRARY_PATH=/opt/GenICam_v3_0_NI/bin/Linux32_ARM/:/usr/local/frc/lib ${DEFAULT_PATH_TO_JRE} -ea -jar ${DEFAULT_JAVA_TEST_NAME} ${TEST_RUN_ARGS}" |
| 181 | elif [[ ${LANGUAGE} = cpp ]]; then |
| 182 | chmod a+x ${DEFAULT_CPP_TEST_NAME} |
| 183 | RUN_COMMAND="./${DEFAULT_CPP_TEST_NAME} ${TEST_RUN_ARGS}" |
| 184 | fi |
| 185 | |
| 186 | printf "Running: %s\n\n" "${RUN_COMMAND}" |
| 187 | COREDUMP_DIR=${DEFAULT_DESTINATION_TEST_RESULTS_DIR}/coredump |
| 188 | mkdir -p ${COREDUMP_DIR} |
| 189 | CORE_LOCATION=${COREDUMP_DIR}/core |
| 190 | echo ${CORE_LOCATION} > /proc/sys/kernel/core_pattern |
| 191 | ulimit -c unlimited |
| 192 | eval ${RUN_COMMAND} |
| 193 | if [[ $? -gt 127 && -e ${CORE_LOCATION} ]]; then |
| 194 | mv ${CORE_LOCATION} ${COREDUMP_DIR}/core.${LANGUAGE} |
| 195 | if [[ ${LANGUAGE} = java ]]; then |
| 196 | cp -p ${DEFAULT_JAVA_TEST_NAME} ${COREDUMP_DIR}/ |
| 197 | elif [[ ${LANGUAGE} = cpp ]]; then |
| 198 | cp -p ${DEFAULT_CPP_TEST_NAME} ${COREDUMP_DIR}/ |
| 199 | fi |
| 200 | fi |