blob: 0981fc6edd76315277d294c9ac3bf26cefa7f027 [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
JvD_Ericssoncb853792023-12-15 09:22:40 +000046# relax set options so the sourced file will not fail
47# the responsibility is shifted to the sourced file...
48function relax_set() {
49 set +e
50 set +o pipefail
51}
52
53# load the saved set options
54function load_set() {
55 _setopts="$-"
56
57 # bash shellopts
58 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
59 set +o ${i}
60 done
61 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
62 set -o ${i}
63 done
64
65 # other options
66 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
67 set +${i}
68 done
69 set -${RUN_CSIT_SAVE_SET}
70}
71
Matthew Watkinsfdaccbf2022-11-23 14:31:40 +000072# wrapper for sourcing a file
73function source_safely() {
74 [ -z "$1" ] && return 1
75 relax_set
76 . "$1"
77 load_set
78}
79# Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
80source_safely "${ROBOT3_VENV}/bin/activate"
81
Ruslan Kashapovaad22402021-02-23 10:08:00 +020082function on_exit(){
83 rc=$?
84 if [[ ${WORKSPACE} ]]; then
85 if [[ ${WORKDIR} ]]; then
86 rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
87 fi
88 # Record list of active docker containers
89 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
90
91 # show memory consumption after all docker instances initialized
92 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
93 fi
94 # Run teardown script plan if it exists
95 cd "${TESTPLANDIR}"
96 TEARDOWN="${TESTPLANDIR}/teardown.sh"
97 if [ -f "${TEARDOWN}" ]; then
98 echo "Running teardown script ${TEARDOWN}"
99 source_safely "${TEARDOWN}"
100 fi
101 # TODO: do something with the output
102 exit $rc
103}
104# ensure that teardown and other finalizing steps are always executed
105trap on_exit EXIT
106
107function docker_stats(){
108 #General memory details
109 echo "> top -bn1 | head -3"
110 top -bn1 | head -3
111 echo
112
113 echo "> free -h"
114 free -h
115 echo
116
117 #Memory details per Docker
118 echo "> docker ps"
119 docker ps
120 echo
121
122 echo "> docker stats --no-stream"
123 docker stats --no-stream
124 echo
125}
126
127# save current set options
128function save_set() {
129 RUN_CSIT_SAVE_SET="$-"
130 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
131}
132
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200133# set options for quick bailout when error
134function harden_set() {
135 set -xeo pipefail
136 set +u # enabled it would probably fail too many often
137}
138
JvD_Ericssoncb853792023-12-15 09:22:40 +0000139function run_test_plan() {
140 testplan=$1
141
142 cd "$WORKDIR"
143 echo "Reading the testplan:"
144 cat "${TESTPLANDIR}/${testplan}.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > ${testplan}.txt
145 cat ${testplan}.txt
146 SUITES=$( xargs -a ${testplan}.txt )
147
emacleef66fb4f2024-01-15 12:58:27 +0000148 python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp --legacy-output ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
JvD_Ericssoncb853792023-12-15 09:22:40 +0000149 RESULT=$?
150 load_set
151 echo "RESULT: $RESULT"
152 return $RESULT
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200153}
154
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200155#
156# main
157#
158
159# set and save options for quick failure
160harden_set && save_set
161
162if [ $# -eq 0 ]
163then
164 echo
165 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
166 echo
167 echo " <project>, <functionality>, <robot-options>: "
168 echo " The same values as for the '{project}-csit-{functionality}' JJB job template."
169 echo
170 exit 1
171fi
172
173if [ -z "$WORKSPACE" ]; then
174 export WORKSPACE=$(git rev-parse --show-toplevel)
175fi
176
JvD_Ericssoncb853792023-12-15 09:22:40 +0000177if [ -f "${WORKSPACE}/${1}/testplanCps.txt" ]; then
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200178 export TESTPLAN="${1}"
179else
JvD_Ericssoncb853792023-12-15 09:22:40 +0000180 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplanCps.txt or testplanNcmp.txt"
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200181 exit 2
182fi
183
184export TESTOPTIONS="${2}"
185
186rm -rf "$WORKSPACE/archives/$TESTPLAN"
187mkdir -p "$WORKSPACE/archives/$TESTPLAN"
188
189TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
190
191# Run installation of prerequired libraries
192source_safely "${WORKSPACE}/prepare-csit.sh"
193
Matthew Watkins17cbd7b2022-11-17 16:39:39 -0800194# Use robot framework working directory
195cd "${WORKDIR}"
196
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200197# Add csit scripts to PATH
Matthew Watkins46d6e4f2022-11-17 14:55:28 -0800198export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200199export SCRIPTS="${WORKSPACE}/scripts"
200export ROBOT_VARIABLES=
201
202# Sign in to nexus3 docker repo
203docker login -u docker -p docker nexus3.onap.org:10001
204
205# Run setup script plan if it exists
206cd "${TESTPLANDIR}"
207SETUP="${TESTPLANDIR}/setup.sh"
208if [ -f "${SETUP}" ]; then
209 echo "Running setup script ${SETUP}"
210 source_safely "${SETUP}"
211fi
212
213# show memory consumption after all docker instances initialized
214docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
215
216# Run test plan
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200217echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
218echo "Starting Robot test suites ${SUITES} ..."
219relax_set
Matthew Watkins438fbd42022-12-15 17:03:16 +0000220
221echo "Versioning information:"
222python3 --version
223pip freeze
224python3 -m robot.run --version || :
225
JvD_Ericssoncb853792023-12-15 09:22:40 +0000226run_test_plan "testplanCps"
227CPSRESULT="$?"
228
229cd "${TESTPLANDIR}"
230checkandmount="${TESTPLANDIR}/sdnc/check_sdnc_mount_node.sh"
231if [ -f "${checkandmount}" ]; then
232 echo "Running check_sdnc_mount_node script ${checkandmount}"
233 source_safely "${checkandmount}"
234fi
235
236run_test_plan "testplanNcmp"
237NCMPRESULT="$?"
238
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200239# Note that the final steps are done in on_exit function after this exit!
JvD_Ericssoncb853792023-12-15 09:22:40 +0000240exit $CPSRESULT || $NCMPRESULT