blob: 52d169327df713422e3060669bdc00fa4767a976 [file] [log] [blame]
Gary Wu13111e92018-09-27 11:31:33 -07001#!/bin/bash -x
Gary Wu9abb61c2018-09-27 10:38:50 -07002#
3# Copyright 2016-2017 Huawei Technologies Co., Ltd.
kaihlavi203e43a2019-08-16 16:02:38 +03004# Modification Copyright 2019 © Samsung Electronics Co., Ltd.
Gary Wu9abb61c2018-09-27 10:38:50 -07005#
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
Gary Wu9abb61c2018-09-27 10:38:50 -070020
Petr Ospalý5cf13912019-09-09 18:00:05 +020021#
22# functions
23#
24
Lasse Kaihlavirtaff18aa22020-03-19 17:01:24 +020025function on_exit(){
26 rc=$?
Lasse Kaihlavirtafc0b7c32020-09-14 17:57:56 +030027 if [[ ${WORKSPACE} ]]; then
28 if [[ ${WORKDIR} ]]; then
29 rsync -av "$WORKDIR/" "$WORKSPACE/archives"
30 fi
31 # Record list of active docker containers
32 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/_docker-images.log"
Lasse Kaihlavirtaff18aa22020-03-19 17:01:24 +020033
Lasse Kaihlavirtafc0b7c32020-09-14 17:57:56 +030034 # show memory consumption after all docker instances initialized
35 docker_stats | tee "$WORKSPACE/archives/_sysinfo-2-after-robot.txt"
36 fi
Lasse Kaihlavirtaff18aa22020-03-19 17:01:24 +020037 # Run teardown script plan if it exists
38 cd "${TESTPLANDIR}"
39 TEARDOWN="${TESTPLANDIR}/teardown.sh"
40 if [ -f "${TEARDOWN}" ]; then
41 echo "Running teardown script ${TEARDOWN}"
42 source_safely "${TEARDOWN}"
43 fi
44 # TODO: do something with the output
45 exit $rc
46}
47# ensure that teardown and other finalizing steps are always executed
48trap on_exit EXIT
49
Gary Wu9abb61c2018-09-27 10:38:50 -070050function docker_stats(){
51 #General memory details
52 echo "> top -bn1 | head -3"
53 top -bn1 | head -3
54 echo
55
56 echo "> free -h"
57 free -h
58 echo
59
60 #Memory details per Docker
61 echo "> docker ps"
62 docker ps
63 echo
64
65 echo "> docker stats --no-stream"
66 docker stats --no-stream
67 echo
68}
69
Petr Ospalý5cf13912019-09-09 18:00:05 +020070# save current set options
71function save_set() {
72 RUN_CSIT_SAVE_SET="$-"
73 RUN_CSIT_SHELLOPTS="$SHELLOPTS"
74}
75
76# load the saved set options
77function load_set() {
78 _setopts="$-"
79
80 # bash shellopts
81 for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
82 set +o ${i}
83 done
84 for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
85 set -o ${i}
86 done
87
88 # other options
89 for i in $(echo "$_setopts" | sed 's/./& /g') ; do
90 set +${i}
91 done
92 set -${RUN_CSIT_SAVE_SET}
93}
94
95# set options for quick bailout when error
96function harden_set() {
97 set -xeo pipefail
98 set +u # enabled it would probably fail too many often
99}
100
101# relax set options so the sourced file will not fail
102# the responsibility is shifted to the sourced file...
103function relax_set() {
104 set +e
105 set +o pipefail
106}
107
108# wrapper for sourcing a file
109function source_safely() {
110 [ -z "$1" ] && return 1
111 relax_set
112 . "$1"
113 load_set
114}
115
116#
117# main
118#
119
120# set and save options for quick failure
121harden_set && save_set
122
Gary Wu9abb61c2018-09-27 10:38:50 -0700123if [ $# -eq 0 ]
124then
kaihlavi203e43a2019-08-16 16:02:38 +0300125 echo
126 echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
Gary Wu9abb61c2018-09-27 10:38:50 -0700127 echo
128 echo " <project>, <functionality>, <robot-options>: "
129 echo " The same values as for the '{project}-csit-{functionality}' JJB job template."
130 echo
Gary Wu9abb61c2018-09-27 10:38:50 -0700131 exit 1
132fi
133
134if [ -z "$WORKSPACE" ]; then
Petr Ospalý5cf13912019-09-09 18:00:05 +0200135 export WORKSPACE=$(git rev-parse --show-toplevel)
Gary Wu9abb61c2018-09-27 10:38:50 -0700136fi
kaihlavi203e43a2019-08-16 16:02:38 +0300137
Petr Ospalý5cf13912019-09-09 18:00:05 +0200138rm -rf "$WORKSPACE/archives"
139mkdir -p "$WORKSPACE/archives"
Gary Wu9abb61c2018-09-27 10:38:50 -0700140
Petr Ospalý5cf13912019-09-09 18:00:05 +0200141if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
Gary Wu9abb61c2018-09-27 10:38:50 -0700142 export TESTPLAN="${1}"
143else
Gary Wu13111e92018-09-27 11:31:33 -0700144 echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
Gary Wu9abb61c2018-09-27 10:38:50 -0700145 exit 2
146fi
147
Gary Wu9abb61c2018-09-27 10:38:50 -0700148export TESTOPTIONS="${2}"
149
Petr Ospalý5cf13912019-09-09 18:00:05 +0200150TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
Gary Wu9abb61c2018-09-27 10:38:50 -0700151
kaihlavi9f5700f2019-08-27 11:30:51 +0300152# Run installation of prerequired libraries
Petr Ospalý5cf13912019-09-09 18:00:05 +0200153source_safely "${WORKSPACE}/prepare-csit.sh"
Gary Wu9abb61c2018-09-27 10:38:50 -0700154
kaihlavi203e43a2019-08-16 16:02:38 +0300155# Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
Petr Ospalý5cf13912019-09-09 18:00:05 +0200156source_safely "${ROBOT_VENV}/bin/activate"
Gary Wu9abb61c2018-09-27 10:38:50 -0700157
Petr Ospalý5cf13912019-09-09 18:00:05 +0200158WORKDIR=$(mktemp -d --suffix=-robot-workdir)
159cd "${WORKDIR}"
Gary Wu9abb61c2018-09-27 10:38:50 -0700160
Gary Wu9abb61c2018-09-27 10:38:50 -0700161# Add csit scripts to PATH
Petr Ospalý5cf13912019-09-09 18:00:05 +0200162export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin"
163export SCRIPTS="${WORKSPACE}/scripts"
Gary Wu9abb61c2018-09-27 10:38:50 -0700164export ROBOT_VARIABLES=
165
166# Sign in to nexus3 docker repo
Bogumil Zebekf1903ee2020-03-19 18:26:54 +0100167docker login -u docker -p docker nexus3.onap.org:10001
Gary Wu9abb61c2018-09-27 10:38:50 -0700168
169# Run setup script plan if it exists
Petr Ospalý5cf13912019-09-09 18:00:05 +0200170cd "${TESTPLANDIR}"
171SETUP="${TESTPLANDIR}/setup.sh"
172if [ -f "${SETUP}" ]; then
Gary Wu9abb61c2018-09-27 10:38:50 -0700173 echo "Running setup script ${SETUP}"
Petr Ospalý5cf13912019-09-09 18:00:05 +0200174 source_safely "${SETUP}"
Gary Wu9abb61c2018-09-27 10:38:50 -0700175fi
176
177# show memory consumption after all docker instances initialized
Petr Ospalý5cf13912019-09-09 18:00:05 +0200178docker_stats | tee "$WORKSPACE/archives/_sysinfo-1-after-setup.txt"
Gary Wu9abb61c2018-09-27 10:38:50 -0700179
180# Run test plan
Petr Ospalý5cf13912019-09-09 18:00:05 +0200181cd "$WORKDIR"
Gary Wu9abb61c2018-09-27 10:38:50 -0700182echo "Reading the testplan:"
Petr Ospalý5cf13912019-09-09 18:00:05 +0200183cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
Gary Wu9abb61c2018-09-27 10:38:50 -0700184cat testplan.txt
185SUITES=$( xargs -a testplan.txt )
186
Petr Ospalý5cf13912019-09-09 18:00:05 +0200187echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
Gary Wu9abb61c2018-09-27 10:38:50 -0700188echo "Starting Robot test suites ${SUITES} ..."
Petr Ospalý5cf13912019-09-09 18:00:05 +0200189relax_set
Gary Wu9abb61c2018-09-27 10:38:50 -0700190python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
191RESULT=$?
Petr Ospalý5cf13912019-09-09 18:00:05 +0200192load_set
193echo "RESULT: $RESULT"
Lasse Kaihlavirtaff18aa22020-03-19 17:01:24 +0200194# Note that the final steps are done in on_exit function after this exit!
Gary Wu9abb61c2018-09-27 10:38:50 -0700195exit $RESULT