blob: 534eafd3a7cf6ff386d55647012da4ead5c12ecf [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001#!/usr/bin/env bash
2#*----------------------------------------------------------------------------*#
3#* Copyright (c) 2014-2019 FIRST. 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
James Kuszmaulb13e13f2023-11-22 20:44:04 -080012# deploy this file along with the compiled tests for you.
Brian Silverman8fce7482020-01-05 13:18:21 -080013
Maxwell Henderson80bec322024-01-09 15:48:44 -080014set -e
15
Brian Silverman8fce7482020-01-05 13:18:21 -080016# Configurable variables
17source config.sh
18
19DEFAULT_TEST_DIR=${DEFAULT_DESTINATION_DIR}
20DEFAULT_PATH_TO_JRE=/usr/local/frc/JRE/bin/java
21
22usage="$(basename "$0") [-h] (java|cpp) [-d test_dir] [-A] [arg] [arg]...
23A script designed to run the integration tests.
24This script should only be run on the roborio.
25Where:
26 -h Show this help text
27 -d The directory where the tests have been placed.
28 This is done to prevent overwriting an already running program.
James Kuszmaulb13e13f2023-11-22 20:44:04 -080029 This script will automatically move the test into the ${DEFAULT_TEST_DIR}
Brian Silverman8fce7482020-01-05 13:18:21 -080030 directory.
James Kuszmaulb13e13f2023-11-22 20:44:04 -080031 Default: Assumes the test is in the same directory as this script.
Brian Silverman8fce7482020-01-05 13:18:21 -080032 -A Do not use the default arguments for the given language.
33 arg The arguments to be passed to test."
34
35
36# Are you trying to run this program on a platform other than the roboRIO?
37if [[ ! -e "${DEFAULT_TEST_DIR}" ]]; then
38 printf "Failed to find %s\nAre you trying to run this file on your local computer?\n" "${DEFAULT_TEST_DIR}"
39 printf "This script should only be run on the roboRIO.\n\n"
40 echo "$usage"
41 exit 1
42fi
43
44LANGUAGE=none
45TEST_FILE=none
46TEST_DIR="$DEFAULT_TEST_DIR"
James Kuszmaulb13e13f2023-11-22 20:44:04 -080047# Begin searching for options from the second parameter on
Brian Silverman8fce7482020-01-05 13:18:21 -080048PARAM_ARGS=${@:2}
49# Where the test arguments start
50TEST_RUN_ARGS=${@:2}
51RUN_WITH_DEFAULT_ARGS=true
52DEFAULT_ARGS=""
53
54# Determine the language that we are attempting to run
55if [[ "$1" = java ]]; then
56 LANGUAGE=$1
57 TEST_FILE=$DEFAULT_JAVA_TEST_NAME
58 DEFAULT_ARGS=$DEFAULT_JAVA_TEST_ARGS
59elif [[ "$1" = cpp ]]; then
60 LANGUAGE=$1
61 TEST_FILE=$DEFAULT_CPP_TEST_NAME
62 DEFAULT_ARGS=$DEFAULT_CPP_TEST_ARGS
63elif [[ "$1" = "-h" ]]; then
64 #If the first argument is the help option
65 #Allow it to be searhced for in getopts
66 PARAM_ARGS=${@}
67else
68 printf "Invalid language selection: %s\n\n" "$1" >&2
69 echo "$usage" >&2
70 exit 1
71fi
72
73PARAM_COUNTER=1
74printf "Param Args ${PARAM_ARGS}\n"
75
Austin Schuh1e69f942020-11-14 15:06:14 -080076# Check for optional parameters
Brian Silverman8fce7482020-01-05 13:18:21 -080077while 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
100done
101
102TEST_RUN_ARGS=${@:$[$PARAM_COUNTER +1]}
103
104if [[ "$RUN_WITH_DEFAULT_ARGS" == true ]]; then
105 TEST_RUN_ARGS="${DEFAULT_ARGS} ${TEST_RUN_ARGS}"
106fi
107
Austin Schuh1e69f942020-11-14 15:06:14 -0800108# Make sure at least two parameters are passed or four if running with -d option
Brian Silverman8fce7482020-01-05 13:18:21 -0800109if [[ $# -lt $PARAM_COUNTER ]]; then
110 printf "Invalid arg count. Should be %s, was %s.\n" "${PARAM_COUNTER}" "$#"
111 echo "$usage"
112 exit 1
113fi
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
Maxwell Henderson80bec322024-01-09 15:48:44 -0800119killall java FRCUserProgram || true
Brian Silverman8fce7482020-01-05 13:18:21 -0800120
121# If we are running with the -d argument move the test to the DEFAULT_TEST_DIR
122if [[ ! -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
126elif [[ $TEST_DIR != "$DEFAULT_TEST_DIR" ]]; then
127 mv "${TEST_DIR}/${TEST_FILE}" "${DEFAULT_TEST_DIR}"
128fi
129
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800130# Make sure the executable file has permission to run
Brian Silverman8fce7482020-01-05 13:18:21 -0800131
132# Get the serial number and FPGADeviceCode for this rio
133export serialnum=`/sbin/fw_printenv -n serial#`
134export eval `/sbin/fw_printenv DeviceCode FPGADeviceCode DeviceDesc TargetClass`
135
136# Store the run command for the language
137RUN_COMMAND=none
138if [[ ${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}"
141elif [[ ${LANGUAGE} = cpp ]]; then
142 chmod a+x ${DEFAULT_CPP_TEST_NAME}
143 RUN_COMMAND="./${DEFAULT_CPP_TEST_NAME} ${TEST_RUN_ARGS}"
144fi
145
146printf "Running: %s\n\n" "${RUN_COMMAND}"
147COREDUMP_DIR=${DEFAULT_DESTINATION_TEST_RESULTS_DIR}/coredump
148mkdir -p ${COREDUMP_DIR}
149CORE_LOCATION=${COREDUMP_DIR}/core
150echo ${CORE_LOCATION} > /proc/sys/kernel/core_pattern
151ulimit -c unlimited
152eval ${RUN_COMMAND}
153if [[ $? -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
160fi