blob: 6703160a37f2e71443b42cd746e6fe8f794c7dc1 [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 Watkins46d6e4f2022-11-17 14:55:28 -080023WORKDIR=$(mktemp -d --suffix=-robot-workdir)
Matthew Watkins46d6e4f2022-11-17 14:55:28 -080024
Ruslan Kashapovaad22402021-02-23 10:08:00 +020025#
26# functions
27#
28
Matthew Watkinsa52a51a2022-12-07 13:30:09 +000029echo "---> run-csit.sh"
30
Matthew Watkinsfdaccbf2022-11-23 14:31:40 +000031# wrapper for sourcing a file
32function source_safely() {
33 [ -z "$1" ] && return 1
34 relax_set
35 . "$1"
36 load_set
37}
38# Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
39source_safely "${ROBOT3_VENV}/bin/activate"
40
Ruslan Kashapovaad22402021-02-23 10:08:00 +020041function on_exit(){
42 rc=$?
43 if [[ ${WORKSPACE} ]]; then
44 if [[ ${WORKDIR} ]]; then
45 rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
46 fi
47 # Record list of active docker containers
48 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
49
50 # show memory consumption after all docker instances initialized
51 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
52 fi
53 # Run teardown script plan if it exists
54 cd "${TESTPLANDIR}"
55 TEARDOWN="${TESTPLANDIR}/teardown.sh"
56 if [ -f "${TEARDOWN}" ]; then
57 echo "Running teardown script ${TEARDOWN}"
58 source_safely "${TEARDOWN}"
59 fi
60 # TODO: do something with the output
61 exit $rc
62}
63# ensure that teardown and other finalizing steps are always executed
64trap on_exit EXIT
65
66function docker_stats(){
67 #General memory details
68 echo "> top -bn1 | head -3"
69 top -bn1 | head -3
70 echo
71
72 echo "> free -h"
73 free -h
74 echo
75
76 #Memory details per Docker
77 echo "> docker ps"
78 docker ps
79 echo
80
81 echo "> docker stats --no-stream"
82 docker stats --no-stream
83 echo
84}
85
86# save current set options
87function save_set() {
88 RUN_CSIT_SAVE_SET="$-"
89 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
90}
91
92# load the saved set options
93function load_set() {
94 _setopts="$-"
95
96 # bash shellopts
97 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
98 set +o ${i}
99 done
100 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
101 set -o ${i}
102 done
103
104 # other options
105 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
106 set +${i}
107 done
108 set -${RUN_CSIT_SAVE_SET}
109}
110
111# set options for quick bailout when error
112function harden_set() {
113 set -xeo pipefail
114 set +u # enabled it would probably fail too many often
115}
116
117# relax set options so the sourced file will not fail
118# the responsibility is shifted to the sourced file...
119function relax_set() {
120 set +e
121 set +o pipefail
122}
123
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200124#
125# main
126#
127
128# set and save options for quick failure
129harden_set && save_set
130
131if [ $# -eq 0 ]
132then
133 echo
134 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
135 echo
136 echo " <project>, <functionality>, <robot-options>: "
137 echo " The same values as for the '{project}-csit-{functionality}' JJB job template."
138 echo
139 exit 1
140fi
141
142if [ -z "$WORKSPACE" ]; then
143 export WORKSPACE=$(git rev-parse --show-toplevel)
144fi
145
146if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
147 export TESTPLAN="${1}"
148else
149 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
150 exit 2
151fi
152
153export TESTOPTIONS="${2}"
154
155rm -rf "$WORKSPACE/archives/$TESTPLAN"
156mkdir -p "$WORKSPACE/archives/$TESTPLAN"
157
158TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
159
160# Run installation of prerequired libraries
161source_safely "${WORKSPACE}/prepare-csit.sh"
162
Matthew Watkins17cbd7b2022-11-17 16:39:39 -0800163# Use robot framework working directory
164cd "${WORKDIR}"
165
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200166# Add csit scripts to PATH
Matthew Watkins46d6e4f2022-11-17 14:55:28 -0800167export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200168export SCRIPTS="${WORKSPACE}/scripts"
169export ROBOT_VARIABLES=
170
171# Sign in to nexus3 docker repo
172docker login -u docker -p docker nexus3.onap.org:10001
173
174# Run setup script plan if it exists
175cd "${TESTPLANDIR}"
176SETUP="${TESTPLANDIR}/setup.sh"
177if [ -f "${SETUP}" ]; then
178 echo "Running setup script ${SETUP}"
179 source_safely "${SETUP}"
180fi
181
182# show memory consumption after all docker instances initialized
183docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
184
185# Run test plan
186cd "$WORKDIR"
187echo "Reading the testplan:"
188cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
189cat testplan.txt
190SUITES=$( xargs -a testplan.txt )
191
192echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
193echo "Starting Robot test suites ${SUITES} ..."
194relax_set
emaclee02a89e42022-02-03 15:45:26 +0000195python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
Ruslan Kashapovaad22402021-02-23 10:08:00 +0200196RESULT=$?
197load_set
198echo "RESULT: $RESULT"
199# Note that the final steps are done in on_exit function after this exit!
200exit $RESULT