3 # Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 # Modification Copyright 2019 Samsung Electronics Co., Ltd.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # $1 project/functionality {TESTPLAN}
19 # $2 robot options {TESTOPTIONS}
21 echo "---> run-csit.sh"
23 WORKDIR=$(mktemp -d --suffix=-robot-workdir)
25 # Exit if no arguments are provided and required variables not set
26 if [[ $# -eq 0 ]] && [[ -z "${TESTPLAN}" ]] && [[ -z "${TESTOPTIONS}" ]]; then
28 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
30 echo " <project>, <functionality>, <robot-options>: "
31 echo " The same values as for the JJB job template:"
32 echo ' {project}-csit-{functionality}'
36 elif [[ $# -ne 2 ]] && [[ -z "${TESTPLAN}" ]] && [[ -z "${TESTOPTIONS}" ]]; then
38 echo "Script called without arguments, but the following variables"
39 echo " must be set: {TESTPLAN} {TESTOPTIONS}"
43 elif [[ $# -eq 2 ]]; then
44 export TESTPLAN=$1; export TESTOPTIONS=$2
47 # Python version should match that used to setup
48 # robot-framework in other jobs/stages
49 # Use pyenv for selecting the python version
50 if [[ -d "/opt/pyenv" ]]; then
52 export PYENV_ROOT="/opt/pyenv"
53 export PATH="$PYENV_ROOT/bin:$PATH"
55 if command -v pyenv 1>/dev/null 2>&1; then
56 eval "$(pyenv init - --no-rehash)"
57 # Choose the latest numeric Python version from installed list
58 version=$(pyenv versions --bare \
59 | sed '/^[^0-9]/d' | sort -V | tail -n 1)
60 pyenv local "${version}"
68 # load the saved set options
73 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
76 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
81 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
84 set -"${RUN_CSIT_SAVE_SET}"
87 # set options for quick bailout when error
90 set +u # enabled it would probably fail too many often
93 # relax set options so the sourced file will not fail
94 # the responsibility is shifted to the sourced file...
100 # save current set options
102 RUN_CSIT_SAVE_SET="$-"
103 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
106 # wrapper for sourcing a file
107 function source_safely {
108 if [[ -z "$1" ]] && return 1; then
110 # shellcheck disable=SC1090
118 if [[ ${WORKSPACE} ]]; then
119 if [[ ${WORKDIR} ]]; then
120 rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
122 # Record list of active docker containers
123 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
125 # show memory consumption after all docker instances initialized
126 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
128 # Run teardown script plan if it exists
130 TEARDOWN="${TESTPLANDIR}/teardown.sh"
131 if [[ -f "${TEARDOWN}" ]]; then
132 echo "Running teardown script ${TEARDOWN}"
133 source_safely "${TEARDOWN}"
137 # ensure that teardown and other finalizing steps are always executed
140 function docker_stats {
141 #General memory details
142 echo "> top -bn1 | head -3"
150 #Memory details per Docker
155 echo "> docker stats --no-stream"
156 docker stats --no-stream
164 # set and save options for quick failure
165 harden_set && save_set
167 if [[ -z "${WORKSPACE}" ]]; then
168 if (git rev-parse --show-toplevel > /dev/null 2>&1); then
169 WORKSPACE=$(git rev-parse --show-toplevel)
177 if [[ ! -f "${WORKSPACE}/${TESTPLAN}/testplan.txt" ]]; then
178 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
182 rm -rf "$WORKSPACE/archives/$TESTPLAN"
183 mkdir -p "$WORKSPACE/archives/$TESTPLAN"
185 TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
187 # Run installation of required libraries
188 source_safely "${WORKSPACE}/prepare-csit.sh"
190 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
191 source_safely "${ROBOT3_VENV}/bin/activate"
195 # Add csit scripts to PATH
196 export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
197 export SCRIPTS="${WORKSPACE}/scripts"
198 export ROBOT_VARIABLES=
200 # Sign in to nexus3 docker repo
201 docker login -u docker -p docker nexus3.onap.org:10001
203 # Run setup script plan if it exists
205 SETUP="${TESTPLANDIR}/setup.sh"
206 if [ -f "${SETUP}" ]; then
207 echo "Running setup script ${SETUP}"
208 source_safely "${SETUP}"
211 # show memory consumption after all docker instances initialized
212 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
216 echo "Reading the testplan:"
217 grep -E -v '(^[[:space:]]*#|^[[:space:]]*$)' "${TESTPLANDIR}/testplan.txt" |\
218 sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
220 SUITES=$( xargs -a testplan.txt )
222 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
223 echo "Starting Robot test suites ${SUITES} ..."
226 echo "Versioning information:"
229 python3 -m robot.run --version || :
231 python -m robot.run -N "${TESTPLAN}" -v WORKSPACE:/tmp "${ROBOT_VARIABLES}" "${TESTOPTIONS}" "${SUITES}"
234 echo "RESULT: $RESULT"
235 # Note that the final steps are done in on_exit function after this exit!