blob: 9a344c1ffde5335a2008779e0df39b9c8c08799d [file] [log] [blame]
Ruslan Kashapovaad22402021-02-23 10:08:00 +02001#!/bin/bash -x
2#
3# Copyright 2016-2017 Huawei Technologies Co., Ltd.
4# Modification Copyright 2019-2021 © 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#
18# $1 project/functionality
19# $2 robot options
20
Ruslan Kashapovd44fcee2021-02-26 11:42:29 +020021# Branched from ccsdk/distribution to this repository Feb 23, 2021
Ruslan Kashapovaad22402021-02-23 10:08:00 +020022
Matthew Watkins438fbd42022-12-15 17:03:16 +000023echo "---> run-csit.sh"
24
Matthew Watkins46d6e4f2022-11-17 14:55:28 -080025WORKDIR=$(mktemp -d --suffix=-robot-workdir)
Matthew Watkins46d6e4f2022-11-17 14:55:28 -080026
Matthew Watkins438fbd42022-12-15 17:03:16 +000027# Version should match those used to setup robot-framework in other jobs/stages
28# Use pyenv for selecting the python version
29if [[ -d "/opt/pyenv" ]]; then
30 echo "Setup pyenv:"
31 export PYENV_ROOT="/opt/pyenv"
32 export PATH="$PYENV_ROOT/bin:$PATH"
33 pyenv versions
34 if command -v pyenv 1>/dev/null 2>&1; then
35 eval "$(pyenv init - --no-rehash)"
36 # Choose the latest numeric Python version from installed list
37 version=$(pyenv versions --bare | sed '/^[^0-9]/d' | sort -V | tail -n 1)
38 pyenv local "${version}"
39 fi
40fi
41
Ruslan Kashapovaad22402021-02-23 10:08:00 +020042#
43# functions
44#
45
Matthew Watkinsfdaccbf2022-11-23 14:31:40 +000046# wrapper for sourcing a file
47function source_safely() {
48 [ -z "$1" ] && return 1
49 relax_set
50 . "$1"
51 load_set
52}
53# Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
54source_safely "${ROBOT3_VENV}/bin/activate"
55
Ruslan Kashapovaad22402021-02-23 10:08:00 +020056function on_exit(){
57 rc=$?
58 if [[ ${WORKSPACE} ]]; then
59 if [[ ${WORKDIR} ]]; then
60 rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
61 fi
62 # Record list of active docker containers
63 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
64
65 # show memory consumption after all docker instances initialized
66 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
67 fi
68 # Run teardown script plan if it exists
69 cd "${TESTPLANDIR}"
70 TEARDOWN="${TESTPLANDIR}/teardown.sh"
71 if [ -f "${TEARDOWN}" ]; then
72 echo "Running teardown script ${TEARDOWN}"
73 source_safely "${TEARDOWN}"
74 fi
75 # TODO: do something with the output
76 exit $rc
77}
78# ensure that teardown and other finalizing steps are always executed
79trap on_exit EXIT
80
81function docker_stats(){
82 #General memory details
83 echo "> top -bn1 | head -3"
84 top -bn1 | head -3
85 echo
86
87 echo "> free -h"
88 free -h
89 echo
90
91 #Memory details per Docker
92 echo "> docker ps"
93 docker ps
94 echo
95
96 echo "> docker stats --no-stream"
97 docker stats --no-stream
98 echo
99}
100
101# save current set options
102function save_set() {
103 RUN_CSIT_SAVE_SET="$-"
104 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
105}
106
107# load the saved set options
108function load_set() {
109 _setopts="$-"
110
111 # bash shellopts
112 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
113 set +o ${i}
114 done
115 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
116 set -o ${i}
117 done
118
119 # other options
120 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
121 set +${i}
122 done
123 set -${RUN_CSIT_SAVE_SET}
124}
125
126# set options for quick bailout when error
127function harden_set() {
128 set -xeo pipefail
129 set +u # enabled it would probably fail too many often
130}
131
132# relax set options so the sourced file will not fail
133# the responsibility is shifted to the sourced file...
134function relax_set() {
135 set +e
136 set +o pipefail
137}
138
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200139#
140# main
141#
142
143# set and save options for quick failure
144harden_set && save_set
145
146if [ $# -eq 0 ]
147then
148 echo
149 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
150 echo
151 echo " <project>, <functionality>, <robot-options>: "
152 echo " The same values as for the '{project}-csit-{functionality}' JJB job template."
153 echo
154 exit 1
155fi
156
157if [ -z "$WORKSPACE" ]; then
158 export WORKSPACE=$(git rev-parse --show-toplevel)
159fi
160
161if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
162 export TESTPLAN="${1}"
163else
164 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
165 exit 2
166fi
167
168export TESTOPTIONS="${2}"
169
170rm -rf "$WORKSPACE/archives/$TESTPLAN"
171mkdir -p "$WORKSPACE/archives/$TESTPLAN"
172
173TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
174
175# Run installation of prerequired libraries
176source_safely "${WORKSPACE}/prepare-csit.sh"
177
Matthew Watkins17cbd7b2022-11-17 16:39:39 -0800178# Use robot framework working directory
179cd "${WORKDIR}"
180
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200181# Add csit scripts to PATH
Matthew Watkins46d6e4f2022-11-17 14:55:28 -0800182export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200183export SCRIPTS="${WORKSPACE}/scripts"
184export ROBOT_VARIABLES=
185
186# Sign in to nexus3 docker repo
187docker login -u docker -p docker nexus3.onap.org:10001
188
189# Run setup script plan if it exists
190cd "${TESTPLANDIR}"
191SETUP="${TESTPLANDIR}/setup.sh"
192if [ -f "${SETUP}" ]; then
193 echo "Running setup script ${SETUP}"
194 source_safely "${SETUP}"
195fi
196
197# show memory consumption after all docker instances initialized
198docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
199
200# Run test plan
201cd "$WORKDIR"
202echo "Reading the testplan:"
203cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
204cat testplan.txt
205SUITES=$( xargs -a testplan.txt )
206
207echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
208echo "Starting Robot test suites ${SUITES} ..."
209relax_set
Matthew Watkins438fbd42022-12-15 17:03:16 +0000210
211echo "Versioning information:"
212python3 --version
213pip freeze
214python3 -m robot.run --version || :
215
emaclee02a89e42022-02-03 15:45:26 +0000216python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200217RESULT=$?
218load_set
219echo "RESULT: $RESULT"
220# Note that the final steps are done in on_exit function after this exit!
221exit $RESULT