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