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