blob: 877cebbba48ecc5f410be46b74b78cb1dceefd80 [file] [log] [blame]
Matthew Watkins8850db12022-12-16 18:01:56 +00001#!/bin/bash -x
2#
3# Copyright 2016-2017 Huawei Technologies Co., Ltd.
4# Modification Copyright 2019 Samsung Electronics Co., Ltd.
5#
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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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.
17#
Matthew Watkins795671a2023-01-04 11:29:33 +000018# $1 project/functionality {TESTPLAN}
19# $2 robot options {TESTOPTIONS}
Matthew Watkins8850db12022-12-16 18:01:56 +000020
21echo "---> run-csit.sh"
22
23WORKDIR=$(mktemp -d --suffix=-robot-workdir)
24
Matthew Watkins795671a2023-01-04 11:29:33 +000025# Exit if no arguments are provided and required variables not set
26if [[ $# -eq 0 ]] && [[ -z "${TESTPLAN}" ]] && [[ -z "${TESTOPTIONS}" ]]; then
27 echo
28 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
29 echo
30 echo " <project>, <functionality>, <robot-options>: "
31 echo " The same values as for the JJB job template:"
32 echo ' {project}-csit-{functionality}'
33 echo
34 exit 1
35
36elif [[ $# -ne 2 ]] && [[ -z "${TESTPLAN}" ]] && [[ -z "${TESTOPTIONS}" ]]; then
37 echo
38 echo "Script called without arguments, but the following variables"
39 echo " must be set: {TESTPLAN} {TESTOPTIONS}"
40 echo
41 exit 1
42
43elif [[ $# -eq 2 ]]; then
44 export TESTPLAN=$1; export TESTOPTIONS=$2
45fi
46
Matthew Watkins8850db12022-12-16 18:01:56 +000047# Python version should match that used to setup
48# robot-framework in other jobs/stages
49# Use pyenv for selecting the python version
50if [[ -d "/opt/pyenv" ]]; then
51 echo "Setup pyenv:"
52 export PYENV_ROOT="/opt/pyenv"
53 export PATH="$PYENV_ROOT/bin:$PATH"
54 pyenv versions
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}"
61 fi
62fi
63
64#
65# functions
66#
67
Matthew Watkins795671a2023-01-04 11:29:33 +000068# load the saved set options
69function load_set {
70 _setopts="$-"
71
72 # bash shellopts
73 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
74 set +o "${i}"
75 done
76 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
77 set -o "${i}"
78 done
79
80 # other options
81 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
82 set +"${i}"
83 done
84 set -"${RUN_CSIT_SAVE_SET}"
85}
86
87# set options for quick bailout when error
88function harden_set {
89 set -xeo pipefail
90 set +u # enabled it would probably fail too many often
91}
92
93# relax set options so the sourced file will not fail
94# the responsibility is shifted to the sourced file...
95function relax_set {
96 set +e
97 set +o pipefail
98}
99
100# save current set options
101function save_set {
102 RUN_CSIT_SAVE_SET="$-"
103 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
104}
105
Matthew Watkins8850db12022-12-16 18:01:56 +0000106# wrapper for sourcing a file
107function source_safely {
Matthew Watkins795671a2023-01-04 11:29:33 +0000108 if [[ -z "$1" ]] && return 1; then
109 relax_set
110 # shellcheck disable=SC1090
111 source "$1"
112 load_set
113 fi
Matthew Watkins8850db12022-12-16 18:01:56 +0000114}
115
116function on_exit {
117 rc=$?
118 if [[ ${WORKSPACE} ]]; then
119 if [[ ${WORKDIR} ]]; then
120 rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
121 fi
122 # Record list of active docker containers
123 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
124
125 # show memory consumption after all docker instances initialized
126 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
127 fi
128 # Run teardown script plan if it exists
129 cd "${TESTPLANDIR}"
130 TEARDOWN="${TESTPLANDIR}/teardown.sh"
Matthew Watkins795671a2023-01-04 11:29:33 +0000131 if [[ -f "${TEARDOWN}" ]]; then
Matthew Watkins8850db12022-12-16 18:01:56 +0000132 echo "Running teardown script ${TEARDOWN}"
133 source_safely "${TEARDOWN}"
134 fi
Matthew Watkins8850db12022-12-16 18:01:56 +0000135 exit $rc
136}
137# ensure that teardown and other finalizing steps are always executed
138trap on_exit EXIT
139
140function docker_stats {
141 #General memory details
142 echo "> top -bn1 | head -3"
143 top -bn1 | head -3
144 echo
145
146 echo "> free -h"
147 free -h
148 echo
149
150 #Memory details per Docker
151 echo "> docker ps"
152 docker ps
153 echo
154
155 echo "> docker stats --no-stream"
156 docker stats --no-stream
157 echo
158}
159
Matthew Watkins8850db12022-12-16 18:01:56 +0000160#
161# main
162#
163
164# set and save options for quick failure
165harden_set && save_set
166
Matthew Watkins795671a2023-01-04 11:29:33 +0000167if [[ -z "${WORKSPACE}" ]]; then
168 if (git rev-parse --show-toplevel > /dev/null 2>&1); then
169 WORKSPACE=$(git rev-parse --show-toplevel)
170 export WORKSPACE
171 else
172 WORKSPACE=$(pwd)
173 export WORKSPACE
174 fi
Matthew Watkins8850db12022-12-16 18:01:56 +0000175fi
176
Matthew Watkins795671a2023-01-04 11:29:33 +0000177if [[ ! -f "${WORKSPACE}/${TESTPLAN}/testplan.txt" ]]; then
Matthew Watkins8850db12022-12-16 18:01:56 +0000178 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
179 exit 2
180fi
181
Matthew Watkins8850db12022-12-16 18:01:56 +0000182rm -rf "$WORKSPACE/archives/$TESTPLAN"
183mkdir -p "$WORKSPACE/archives/$TESTPLAN"
184
185TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
186
187# Run installation of required libraries
188source_safely "${WORKSPACE}/prepare-csit.sh"
189
190# Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
191source_safely "${ROBOT3_VENV}/bin/activate"
192
193cd "${WORKDIR}"
194
195# Add csit scripts to PATH
Matthew Watkins795671a2023-01-04 11:29:33 +0000196export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
Matthew Watkins8850db12022-12-16 18:01:56 +0000197export SCRIPTS="${WORKSPACE}/scripts"
198export ROBOT_VARIABLES=
199
200# Sign in to nexus3 docker repo
201docker login -u docker -p docker nexus3.onap.org:10001
202
203# Run setup script plan if it exists
204cd "${TESTPLANDIR}"
205SETUP="${TESTPLANDIR}/setup.sh"
206if [ -f "${SETUP}" ]; then
207 echo "Running setup script ${SETUP}"
208 source_safely "${SETUP}"
209fi
210
211# show memory consumption after all docker instances initialized
212docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
213
214# Run test plan
215cd "$WORKDIR"
216echo "Reading the testplan:"
Matthew Watkins795671a2023-01-04 11:29:33 +0000217grep -E -v '(^[[:space:]]*#|^[[:space:]]*$)' "${TESTPLANDIR}/testplan.txt" |\
218 sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
Matthew Watkins8850db12022-12-16 18:01:56 +0000219cat testplan.txt
220SUITES=$( xargs -a testplan.txt )
221
222echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
223echo "Starting Robot test suites ${SUITES} ..."
224relax_set
225
226echo "Versioning information:"
227python3 --version
228pip freeze
229python3 -m robot.run --version || :
230
Matthew Watkins795671a2023-01-04 11:29:33 +0000231python -m robot.run -N "${TESTPLAN}" -v WORKSPACE:/tmp "${ROBOT_VARIABLES}" "${TESTOPTIONS}" "${SUITES}"
Matthew Watkins8850db12022-12-16 18:01:56 +0000232RESULT=$?
233load_set
234echo "RESULT: $RESULT"
235# Note that the final steps are done in on_exit function after this exit!
236exit $RESULT