Fix: Rebase around the CSIT repo script

Address several script failures revealed in sandbox testing

Issue-ID: CIMAN-33
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
Change-Id: I1f063cd9b4979649aee980c2875f119d5f67126a
diff --git a/jjb/integration/run-csit.sh b/jjb/integration/run-csit.sh
index 877cebb..4ee5d75 100644
--- a/jjb/integration/run-csit.sh
+++ b/jjb/integration/run-csit.sh
@@ -20,99 +20,10 @@
 
 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
-if [[ -d "/opt/pyenv" ]]; then
-    echo "Setup pyenv:"
-    export PYENV_ROOT="/opt/pyenv"
-    export PATH="$PYENV_ROOT/bin:$PATH"
-    pyenv versions
-    if command -v pyenv 1>/dev/null 2>&1; then
-        eval "$(pyenv init - --no-rehash)"
-        # Choose the latest numeric Python version from installed list
-        version=$(pyenv versions --bare \
-            | sed '/^[^0-9]/d' | sort -V | tail -n 1)
-        pyenv local "${version}"
-    fi
-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 {
-    if [[ -z "$1" ]] && return 1; then
-        relax_set
-        # shellcheck disable=SC1090
-        source "$1"
-        load_set
-    fi
-}
-
 function on_exit {
     rc=$?
     if [[ ${WORKSPACE} ]]; then
@@ -128,10 +39,11 @@
     # 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
@@ -157,6 +69,52 @@
     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
+}
+
+# wrapper for sourcing a file
+function source_safely {
+    [ -z "$1" ] && return 1
+    relax_set
+    . "$1"
+    load_set
+}
+
 #
 # main
 #
@@ -164,21 +122,29 @@
 # set and save options for quick failure
 harden_set && save_set
 
-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
+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 [[ ! -f "${WORKSPACE}/${TESTPLAN}/testplan.txt" ]]; then
+if [ -z "$WORKSPACE" ]; then
+    export WORKSPACE=$(git rev-parse --show-toplevel)
+fi
+
+if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
+    export TESTPLAN="${1}"
+else
     echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
     exit 2
 fi
 
+export TESTOPTIONS="${2}"
+
 rm -rf "$WORKSPACE/archives/$TESTPLAN"
 mkdir -p "$WORKSPACE/archives/$TESTPLAN"
 
@@ -190,6 +156,7 @@
 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
 source_safely "${ROBOT3_VENV}/bin/activate"
 
+WORKDIR=$(mktemp -d --suffix=-robot-workdir)
 cd "${WORKDIR}"
 
 # Add csit scripts to PATH
@@ -219,16 +186,16 @@
 cat testplan.txt
 SUITES=$( xargs -a testplan.txt )
 
+echo "Versioning information:"
+python3 --version
+pip3 freeze
+python3 -m robot.run --version || :
+
 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
 echo "Starting Robot test suites ${SUITES} ..."
 relax_set
-
-echo "Versioning information:"
-python3 --version
-pip freeze
-python3 -m robot.run --version || :
-
-python -m robot.run -N "${TESTPLAN}" -v WORKSPACE:/tmp "${ROBOT_VARIABLES}" "${TESTOPTIONS}" "${SUITES}"
+# Do NOT quote variables below, it breaks testing
+python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
 RESULT=$?
 load_set
 echo "RESULT: $RESULT"