diff options
author | Matthew Watkins <mwatkins@linuxfoundation.org> | 2023-01-04 11:29:33 +0000 |
---|---|---|
committer | Matthew Watkins <mwatkins@linuxfoundation.org> | 2023-02-14 10:51:50 +0000 |
commit | 795671a1f575b00012e1b5865cd338e45cc69b64 (patch) | |
tree | 3a07efab7eed5c4c95cf23fb2ea678a00d115b01 /jjb/integration/run-csit.sh | |
parent | 381d669737e2f88b4c6800f00b21deb279e793b2 (diff) |
Chore: Updates to improve overall robustness of robot framework tooling
Improved scripts to better meet modern coding best practices. Improved
many script functions and behaviours to allow some code to run outside
Jenkins jobs. Addressed many warnings produced by linting tools.
Issue-ID: CCSDK-3820
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
Change-Id: I2e4f702a0b58cbace341c31669b4ffcb023d499c
Diffstat (limited to 'jjb/integration/run-csit.sh')
-rw-r--r-- | jjb/integration/run-csit.sh | 149 |
1 files changed, 82 insertions, 67 deletions
diff --git a/jjb/integration/run-csit.sh b/jjb/integration/run-csit.sh index bd35ac438..877cebbba 100644 --- a/jjb/integration/run-csit.sh +++ b/jjb/integration/run-csit.sh @@ -15,13 +15,35 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# $1 project/functionality -# $2 robot options +# $1 project/functionality {TESTPLAN} +# $2 robot options {TESTOPTIONS} echo "---> run-csit.sh" WORKDIR=$(mktemp -d --suffix=-robot-workdir) +# Exit if no arguments are provided and required variables not set +if [[ $# -eq 0 ]] && [[ -z "${TESTPLAN}" ]] && [[ -z "${TESTOPTIONS}" ]]; then + echo + echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]" + echo + echo " <project>, <functionality>, <robot-options>: " + echo " The same values as for the JJB job template:" + echo ' {project}-csit-{functionality}' + echo + exit 1 + +elif [[ $# -ne 2 ]] && [[ -z "${TESTPLAN}" ]] && [[ -z "${TESTOPTIONS}" ]]; then + echo + echo "Script called without arguments, but the following variables" + echo " must be set: {TESTPLAN} {TESTOPTIONS}" + echo + exit 1 + +elif [[ $# -eq 2 ]]; then + export TESTPLAN=$1; export TESTOPTIONS=$2 +fi + # Python version should match that used to setup # robot-framework in other jobs/stages # Use pyenv for selecting the python version @@ -43,12 +65,52 @@ fi # functions # +# load the saved set options +function load_set { + _setopts="$-" + + # bash shellopts + for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do + set +o "${i}" + done + for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do + set -o "${i}" + done + + # other options + for i in $(echo "$_setopts" | sed 's/./& /g') ; do + set +"${i}" + done + set -"${RUN_CSIT_SAVE_SET}" +} + +# set options for quick bailout when error +function harden_set { + set -xeo pipefail + set +u # enabled it would probably fail too many often +} + +# relax set options so the sourced file will not fail +# the responsibility is shifted to the sourced file... +function relax_set { + set +e + set +o pipefail +} + +# save current set options +function save_set { + RUN_CSIT_SAVE_SET="$-" + RUN_CSIT_SHELLOPTS="$SHELLOPTS" +} + # wrapper for sourcing a file function source_safely { - [ -z "$1" ] && return 1 - relax_set - . "$1" - load_set + if [[ -z "$1" ]] && return 1; then + relax_set + # shellcheck disable=SC1090 + source "$1" + load_set + fi } function on_exit { @@ -66,11 +128,10 @@ function on_exit { # Run teardown script plan if it exists cd "${TESTPLANDIR}" TEARDOWN="${TESTPLANDIR}/teardown.sh" - if [ -f "${TEARDOWN}" ]; then + if [[ -f "${TEARDOWN}" ]]; then echo "Running teardown script ${TEARDOWN}" source_safely "${TEARDOWN}" fi - # TODO: do something with the output exit $rc } # ensure that teardown and other finalizing steps are always executed @@ -96,44 +157,6 @@ function docker_stats { echo } -# save current set options -function save_set { - RUN_CSIT_SAVE_SET="$-" - RUN_CSIT_SHELLOPTS="$SHELLOPTS" -} - -# load the saved set options -function load_set { - _setopts="$-" - - # bash shellopts - for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do - set +o ${i} - done - for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do - set -o ${i} - done - - # other options - for i in $(echo "$_setopts" | sed 's/./& /g') ; do - set +${i} - done - set -${RUN_CSIT_SAVE_SET} -} - -# set options for quick bailout when error -function harden_set { - set -xeo pipefail - set +u # enabled it would probably fail too many often -} - -# relax set options so the sourced file will not fail -# the responsibility is shifted to the sourced file... -function relax_set { - set +e - set +o pipefail -} - # # main # @@ -141,29 +164,21 @@ function relax_set { # set and save options for quick failure harden_set && save_set -if [ $# -eq 0 ]; then - echo - echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]" - echo - echo " <project>, <functionality>, <robot-options>: " - echo " The same values as for the '{project}-csit-{functionality}' JJB job template." - echo - exit 1 -fi - -if [ -z "$WORKSPACE" ]; then - export WORKSPACE=$(git rev-parse --show-toplevel) +if [[ -z "${WORKSPACE}" ]]; then + if (git rev-parse --show-toplevel > /dev/null 2>&1); then + WORKSPACE=$(git rev-parse --show-toplevel) + export WORKSPACE + else + WORKSPACE=$(pwd) + export WORKSPACE + fi fi -if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then - export TESTPLAN="${1}" -else +if [[ ! -f "${WORKSPACE}/${TESTPLAN}/testplan.txt" ]]; then echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt" exit 2 fi -export TESTOPTIONS="${2}" - rm -rf "$WORKSPACE/archives/$TESTPLAN" mkdir -p "$WORKSPACE/archives/$TESTPLAN" @@ -178,7 +193,7 @@ source_safely "${ROBOT3_VENV}/bin/activate" cd "${WORKDIR}" # Add csit scripts to PATH -export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin" +export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin" export SCRIPTS="${WORKSPACE}/scripts" export ROBOT_VARIABLES= @@ -199,7 +214,8 @@ docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt" # Run test plan cd "$WORKDIR" echo "Reading the testplan:" -cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt +grep -E -v '(^[[:space:]]*#|^[[:space:]]*$)' "${TESTPLANDIR}/testplan.txt" |\ + sed "s|^|${WORKSPACE}/tests/|" > testplan.txt cat testplan.txt SUITES=$( xargs -a testplan.txt ) @@ -212,10 +228,9 @@ python3 --version pip freeze python3 -m robot.run --version || : -python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES} +python -m robot.run -N "${TESTPLAN}" -v WORKSPACE:/tmp "${ROBOT_VARIABLES}" "${TESTOPTIONS}" "${SUITES}" RESULT=$? load_set echo "RESULT: $RESULT" # Note that the final steps are done in on_exit function after this exit! exit $RESULT - |