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