blob: 93941e21632805b681d1aa95caec816a3f76dc2f [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.
halil.cakal3a9613f2024-06-17 09:24:59 +01005# Modification Copyright (C) 2024 Nordix Foundation.
Ruslan Kashapovaad22402021-02-23 10:08:00 +02006#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# $1 project/functionality
20# $2 robot options
21
Ruslan Kashapovd44fcee2021-02-26 11:42:29 +020022# Branched from ccsdk/distribution to this repository Feb 23, 2021
Ruslan Kashapovaad22402021-02-23 10:08:00 +020023
Matthew Watkins438fbd42022-12-15 17:03:16 +000024echo "---> run-csit.sh"
25
Matthew Watkins46d6e4f2022-11-17 14:55:28 -080026WORKDIR=$(mktemp -d --suffix=-robot-workdir)
Matthew Watkins46d6e4f2022-11-17 14:55:28 -080027
Matthew Watkins438fbd42022-12-15 17:03:16 +000028# Version should match those used to setup robot-framework in other jobs/stages
29# Use pyenv for selecting the python version
30if [[ -d "/opt/pyenv" ]]; then
31 echo "Setup pyenv:"
32 export PYENV_ROOT="/opt/pyenv"
33 export PATH="$PYENV_ROOT/bin:$PATH"
34 pyenv versions
35 if command -v pyenv 1>/dev/null 2>&1; then
36 eval "$(pyenv init - --no-rehash)"
37 # Choose the latest numeric Python version from installed list
38 version=$(pyenv versions --bare | sed '/^[^0-9]/d' | sort -V | tail -n 1)
39 pyenv local "${version}"
40 fi
41fi
42
Ruslan Kashapovaad22402021-02-23 10:08:00 +020043#
44# functions
45#
46
JvD_Ericssoncb853792023-12-15 09:22:40 +000047# relax set options so the sourced file will not fail
48# the responsibility is shifted to the sourced file...
49function relax_set() {
50 set +e
51 set +o pipefail
52}
53
54# load the saved set options
55function load_set() {
56 _setopts="$-"
57
58 # bash shellopts
59 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
60 set +o ${i}
61 done
62 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
63 set -o ${i}
64 done
65
66 # other options
67 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
68 set +${i}
69 done
70 set -${RUN_CSIT_SAVE_SET}
71}
72
Matthew Watkinsfdaccbf2022-11-23 14:31:40 +000073# wrapper for sourcing a file
74function source_safely() {
75 [ -z "$1" ] && return 1
76 relax_set
77 . "$1"
78 load_set
79}
80# Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
81source_safely "${ROBOT3_VENV}/bin/activate"
82
Ruslan Kashapovaad22402021-02-23 10:08:00 +020083function on_exit(){
84 rc=$?
85 if [[ ${WORKSPACE} ]]; then
86 if [[ ${WORKDIR} ]]; then
87 rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
88 fi
89 # Record list of active docker containers
90 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
91
92 # show memory consumption after all docker instances initialized
93 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
94 fi
95 # Run teardown script plan if it exists
96 cd "${TESTPLANDIR}"
97 TEARDOWN="${TESTPLANDIR}/teardown.sh"
98 if [ -f "${TEARDOWN}" ]; then
99 echo "Running teardown script ${TEARDOWN}"
100 source_safely "${TEARDOWN}"
101 fi
102 # TODO: do something with the output
103 exit $rc
104}
105# ensure that teardown and other finalizing steps are always executed
106trap on_exit EXIT
107
108function docker_stats(){
109 #General memory details
110 echo "> top -bn1 | head -3"
111 top -bn1 | head -3
112 echo
113
114 echo "> free -h"
115 free -h
116 echo
117
118 #Memory details per Docker
119 echo "> docker ps"
120 docker ps
121 echo
122
123 echo "> docker stats --no-stream"
124 docker stats --no-stream
125 echo
126}
127
128# save current set options
129function save_set() {
130 RUN_CSIT_SAVE_SET="$-"
131 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
132}
133
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200134# set options for quick bailout when error
135function harden_set() {
136 set -xeo pipefail
137 set +u # enabled it would probably fail too many often
138}
139
JvD_Ericssoncb853792023-12-15 09:22:40 +0000140function run_test_plan() {
141 testplan=$1
142
143 cd "$WORKDIR"
144 echo "Reading the testplan:"
145 cat "${TESTPLANDIR}/${testplan}.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > ${testplan}.txt
146 cat ${testplan}.txt
147 SUITES=$( xargs -a ${testplan}.txt )
148
emacleef66fb4f2024-01-15 12:58:27 +0000149 python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp --legacy-output ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
JvD_Ericssoncb853792023-12-15 09:22:40 +0000150 RESULT=$?
151 load_set
152 echo "RESULT: $RESULT"
153 return $RESULT
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200154}
155
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200156#
157# main
158#
159
160# set and save options for quick failure
161harden_set && save_set
162
163if [ $# -eq 0 ]
164then
165 echo
166 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
167 echo
168 echo " <project>, <functionality>, <robot-options>: "
169 echo " The same values as for the '{project}-csit-{functionality}' JJB job template."
170 echo
171 exit 1
172fi
173
174if [ -z "$WORKSPACE" ]; then
175 export WORKSPACE=$(git rev-parse --show-toplevel)
176fi
177
JvD_Ericssoncb853792023-12-15 09:22:40 +0000178if [ -f "${WORKSPACE}/${1}/testplanCps.txt" ]; then
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200179 export TESTPLAN="${1}"
180else
JvD_Ericssoncb853792023-12-15 09:22:40 +0000181 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplanCps.txt or testplanNcmp.txt"
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200182 exit 2
183fi
184
185export TESTOPTIONS="${2}"
186
187rm -rf "$WORKSPACE/archives/$TESTPLAN"
188mkdir -p "$WORKSPACE/archives/$TESTPLAN"
189
190TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
191
192# Run installation of prerequired libraries
193source_safely "${WORKSPACE}/prepare-csit.sh"
194
Matthew Watkins17cbd7b2022-11-17 16:39:39 -0800195# Use robot framework working directory
196cd "${WORKDIR}"
197
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200198# Add csit scripts to PATH
Matthew Watkins46d6e4f2022-11-17 14:55:28 -0800199export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200200export SCRIPTS="${WORKSPACE}/scripts"
201export ROBOT_VARIABLES=
202
203# Sign in to nexus3 docker repo
204docker login -u docker -p docker nexus3.onap.org:10001
205
206# Run setup script plan if it exists
207cd "${TESTPLANDIR}"
208SETUP="${TESTPLANDIR}/setup.sh"
209if [ -f "${SETUP}" ]; then
210 echo "Running setup script ${SETUP}"
211 source_safely "${SETUP}"
212fi
213
214# show memory consumption after all docker instances initialized
215docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
216
217# Run test plan
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200218echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
219echo "Starting Robot test suites ${SUITES} ..."
220relax_set
Matthew Watkins438fbd42022-12-15 17:03:16 +0000221
222echo "Versioning information:"
223python3 --version
224pip freeze
225python3 -m robot.run --version || :
226
JvD_Ericssoncb853792023-12-15 09:22:40 +0000227run_test_plan "testplanCps"
228CPSRESULT="$?"
229
230cd "${TESTPLANDIR}"
231checkandmount="${TESTPLANDIR}/sdnc/check_sdnc_mount_node.sh"
232if [ -f "${checkandmount}" ]; then
233 echo "Running check_sdnc_mount_node script ${checkandmount}"
234 source_safely "${checkandmount}"
235fi
236
237run_test_plan "testplanNcmp"
238NCMPRESULT="$?"
239
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200240# Note that the final steps are done in on_exit function after this exit!
JvD_Ericssoncb853792023-12-15 09:22:40 +0000241exit $CPSRESULT || $NCMPRESULT