Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -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] [-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 | This scrip will automatically move the test into the ${DEFAULT_TEST_DIR} |
| 29 | directory. |
| 30 | Default: Assumes the test is in the same directory as this scrip. |
| 31 | -A Do not use the default arguments for the given language. |
| 32 | arg The arguments to be passed to test." |
| 33 | |
| 34 | |
| 35 | # Are you trying to run this program on a platform other than the roboRIO? |
| 36 | if [[ ! -e "${DEFAULT_TEST_DIR}" ]]; then |
| 37 | printf "Failed to find %s\nAre you trying to run this file on your local computer?\n" "${DEFAULT_TEST_DIR}" |
| 38 | printf "This script should only be run on the roboRIO.\n\n" |
| 39 | echo "$usage" |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | LANGUAGE=none |
| 44 | TEST_FILE=none |
| 45 | NAME=$2 |
| 46 | TEST_DIR="$DEFAULT_TEST_DIR" |
| 47 | # Begin searching for options from the third paramater on |
| 48 | PARAM_ARGS=${@:3} |
| 49 | # Where the test arguments start |
| 50 | TEST_RUN_ARGS=${@:3} |
| 51 | RUN_WITH_DEFAULT_ARGS=true |
| 52 | DEFAULT_ARGS="" |
| 53 | |
| 54 | # Determine the language that we are attempting to run |
| 55 | if [[ "$1" = java ]]; then |
| 56 | LANGUAGE=$1 |
| 57 | TEST_FILE=$DEFAULT_JAVA_TEST_NAME |
| 58 | DEFAULT_ARGS=$DEFAULT_JAVA_TEST_ARGS |
| 59 | elif [[ "$1" = cpp ]]; then |
| 60 | LANGUAGE=$1 |
| 61 | TEST_FILE=$DEFAULT_CPP_TEST_NAME |
| 62 | DEFAULT_ARGS=$DEFAULT_CPP_TEST_ARGS |
| 63 | elif [[ "$1" = "-h" ]]; then |
| 64 | #If the first argument is the help option |
| 65 | #Allow it to be searhced for in getopts |
| 66 | PARAM_ARGS=${@} |
| 67 | else |
| 68 | printf "Invalid language selection: %s\n\n" "$1" >&2 |
| 69 | echo "$usage" >&2 |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | PARAM_COUNTER=2 |
| 74 | printf "Param Args ${PARAM_ARGS}\n" |
| 75 | |
| 76 | # Check for optional paramaters |
| 77 | while getopts ':hmd:A' option $PARAM_ARGS ; do |
| 78 | case "$option" in |
| 79 | h) |
| 80 | # Print the help message |
| 81 | printf "Usage:\n" |
| 82 | echo "$usage" |
| 83 | exit |
| 84 | ;; |
| 85 | A) |
| 86 | # Remove the default arguments |
| 87 | RUN_WITH_DEFAULT_ARGS=false |
| 88 | PARAM_COUNTER=$[$PARAM_COUNTER +1] |
| 89 | ;; |
| 90 | d) |
| 91 | TEST_DIR=$OPTARG |
| 92 | # Since we are selecting the directory the run args start from the 5th argument |
| 93 | PARAM_COUNTER=$[$PARAM_COUNTER +2] |
| 94 | ;; |
| 95 | ?) |
| 96 | # When an unknown param is found then we are done so break |
| 97 | break |
| 98 | ;; |
| 99 | esac |
| 100 | done |
| 101 | |
| 102 | TEST_RUN_ARGS=${@:$[$PARAM_COUNTER +1]} |
| 103 | |
| 104 | if [[ "$RUN_WITH_DEFAULT_ARGS" == true ]]; then |
| 105 | TEST_RUN_ARGS="${DEFAULT_ARGS} ${TEST_RUN_ARGS}" |
| 106 | fi |
| 107 | |
| 108 | # Make sure at least two paramaters are passed or four if running with -d option |
| 109 | if [[ $# -lt $PARAM_COUNTER ]]; then |
| 110 | printf "Invalid arg count. Should be %s, was %s.\n" "${PARAM_COUNTER}" "$#" |
| 111 | echo "$usage" |
| 112 | exit 1 |
| 113 | fi |
| 114 | |
| 115 | # Make sure the webserver is disabled to save memory |
| 116 | /usr/local/natinst/etc/init.d/systemWebServer stop |
| 117 | |
| 118 | # Kill all running robot programs |
| 119 | killall java FRCUserProgram |
| 120 | |
| 121 | # If we are running with the -d argument move the test to the DEFAULT_TEST_DIR |
| 122 | if [[ ! -e "${TEST_DIR}/${TEST_FILE}" ]]; then |
| 123 | printf "Failed to find %s.\nDid you copy the test file correctly?\n" "${TEST_DIR}/${TEST_FILE}" |
| 124 | echo "$usage" |
| 125 | exit 1 |
| 126 | elif [[ $TEST_DIR != "$DEFAULT_TEST_DIR" ]]; then |
| 127 | mv "${TEST_DIR}/${TEST_FILE}" "${DEFAULT_TEST_DIR}" |
| 128 | fi |
| 129 | |
| 130 | # Make sure the excecutable file has permission to run |
| 131 | |
| 132 | # Get the serial number and FPGADeviceCode for this rio |
| 133 | export serialnum=`/sbin/fw_printenv -n serial#` |
| 134 | export eval `/sbin/fw_printenv DeviceCode FPGADeviceCode DeviceDesc TargetClass` |
| 135 | |
| 136 | # Store the run command for the language |
| 137 | RUN_COMMAND=none |
| 138 | if [[ ${LANGUAGE} = java ]]; then |
| 139 | chmod a+x ${DEFAULT_JAVA_TEST_NAME} |
| 140 | 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}" |
| 141 | elif [[ ${LANGUAGE} = cpp ]]; then |
| 142 | chmod a+x ${DEFAULT_CPP_TEST_NAME} |
| 143 | RUN_COMMAND="./${DEFAULT_CPP_TEST_NAME} ${TEST_RUN_ARGS}" |
| 144 | fi |
| 145 | |
| 146 | printf "Running: %s\n\n" "${RUN_COMMAND}" |
| 147 | COREDUMP_DIR=${DEFAULT_DESTINATION_TEST_RESULTS_DIR}/coredump |
| 148 | mkdir -p ${COREDUMP_DIR} |
| 149 | CORE_LOCATION=${COREDUMP_DIR}/core |
| 150 | echo ${CORE_LOCATION} > /proc/sys/kernel/core_pattern |
| 151 | ulimit -c unlimited |
| 152 | eval ${RUN_COMMAND} |
| 153 | if [[ $? -gt 127 && -e ${CORE_LOCATION} ]]; then |
| 154 | mv ${CORE_LOCATION} ${COREDUMP_DIR}/core.${LANGUAGE} |
| 155 | if [[ ${LANGUAGE} = java ]]; then |
| 156 | cp -p ${DEFAULT_JAVA_TEST_NAME} ${COREDUMP_DIR}/ |
| 157 | elif [[ ${LANGUAGE} = cpp ]]; then |
| 158 | cp -p ${DEFAULT_CPP_TEST_NAME} ${COREDUMP_DIR}/ |
| 159 | fi |
| 160 | fi |