Chore: Add gerrit maven verify GHA workflow
[sdnc/oam.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 # Modification Copyright 2021 © highstreet-technologies GmbH
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # $1 project/functionality
20 # $2 robot options
21
22
23 #
24 # functions
25 #
26
27 function on_exit(){
28     rc=$?
29     if [[ ${WORKSPACE} ]]; then
30         if [[ ${WORKDIR} ]]; then
31             rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
32         fi
33         # Record list of active docker containers
34         docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
35
36         # show memory consumption after all docker instances initialized
37         docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
38     fi
39     # Run teardown script plan if it exists
40     cd "${TESTPLANDIR}"
41     TEARDOWN="${TESTPLANDIR}/teardown.sh"
42     if [ -f "${TEARDOWN}" ]; then
43         echo "Running teardown script ${TEARDOWN}"
44         source_safely "${TEARDOWN}"
45     fi
46     # TODO: do something with the output
47      exit $rc
48 }
49 # ensure that teardown and other finalizing steps are always executed
50 trap on_exit EXIT
51
52 function docker_stats(){
53     #General memory details
54     echo "> top -bn1 | head -3"
55     top -bn1 | head -3
56     echo
57
58     echo "> free -h"
59     free -h
60     echo
61
62     #Memory details per Docker
63     echo "> docker ps"
64     docker ps
65     echo
66
67     echo "> docker stats --no-stream"
68     docker stats --no-stream
69     echo
70 }
71
72 # save current set options
73 function save_set() {
74     RUN_CSIT_SAVE_SET="$-"
75     RUN_CSIT_SHELLOPTS="$SHELLOPTS"
76 }
77
78 # load the saved set options
79 function load_set() {
80     _setopts="$-"
81
82     # bash shellopts
83     for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
84         set +o ${i}
85     done
86     for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
87         set -o ${i}
88     done
89
90     # other options
91     for i in $(echo "$_setopts" | sed 's/./& /g') ; do
92         set +${i}
93     done
94     set -${RUN_CSIT_SAVE_SET}
95 }
96
97 # set options for quick bailout when error
98 function harden_set() {
99     set -xeo pipefail
100     set +u # enabled it would probably fail too many often
101 }
102
103 # relax set options so the sourced file will not fail
104 # the responsibility is shifted to the sourced file...
105 function relax_set() {
106     set +e
107     set +o pipefail
108 }
109
110 # wrapper for sourcing a file
111 function source_safely() {
112     [ -z "$1" ] && return 1
113     relax_set
114     . "$1"
115     load_set
116 }
117
118 #
119 # main
120 #
121
122
123 # set and save options for quick failure
124 harden_set && save_set
125
126 if [ $# -eq 0 ]
127 then
128     echo
129     echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
130     echo
131     echo "    <project>, <functionality>, <robot-options>:  "
132     echo "        The same values as for the '{project}-csit-{functionality}' JJB job template."
133     echo
134     exit 1
135 fi
136
137 if [ -z "$WORKSPACE" ]; then
138     export WORKSPACE=$(git rev-parse --show-toplevel)
139 fi
140
141 if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
142     export TESTPLAN="${1}"
143 else
144     echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
145     exit 2
146 fi
147
148 export TESTOPTIONS="${2}"
149
150 rm -rf "$WORKSPACE/archives/$TESTPLAN"
151 mkdir -p "$WORKSPACE/archives/$TESTPLAN"
152
153 TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
154
155 # Set env variables
156 source_safely "${WORKSPACE}/sdnc-csit.env"
157 if [[ -z $ROBOT_IMAGE ]]; then
158   # Run installation of prerequired libraries
159   source_safely "${WORKSPACE}/prepare-csit.sh"
160   # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
161   source_safely "${ROBOT_VENV}/bin/activate"
162 fi
163
164 WORKDIR=$(mktemp -d --suffix=-robot-workdir)
165 chmod a+rwx "${WORKDIR}"
166 echo "Additional info"
167 ls -lsa "${WORKDIR}"
168 id
169
170 # Add csit scripts to PATH
171 export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin"
172 export SCRIPTS="${WORKSPACE}/scripts"
173 export ROBOT_VARIABLES=
174
175 # Sign in to nexus3 docker repo
176 docker login -u docker -p docker nexus3.onap.org:10001
177
178 # Run setup script plan if it exists
179 cd "${TESTPLANDIR}"
180 SETUP="${TESTPLANDIR}/setup.sh"
181 if [ -f "${SETUP}" ]; then
182     echo "Running setup script ${SETUP}"
183     source_safely "${SETUP}"
184 fi
185
186 # show memory consumption after all docker instances initialized
187 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
188
189 # Run test plan
190 cd "$WORKDIR"
191 echo "Reading the testplan:"
192 cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
193 cat testplan.txt
194 SUITES=$( xargs -a testplan.txt )
195
196 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
197 echo "Starting Robot test suites ${SUITES} ..."
198 relax_set
199
200 if [[ -z $SDNC_RELEASE_WITHOUT_ROBOT ]] ; then
201     if [[ -z $SDNC_READY_STATE_TIME_OUT ]] ; then
202         # Runs an alternative robotframework setup as docker image in $ROBOT_IMAGE
203         # test suites will be executed within this docker container
204         # and results are stored as usual
205         if [[ -z $ROBOT_IMAGE ]]; then
206             echo "*** TRACE **** python is $(which python) [version $(python --version)]"
207             env
208             python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
209         else
210             echo "*** TRACE **** python is running in a container"
211             docker run --rm --net="host" \
212             --env-file ${WORKSPACE}/sdnc-csit-robot.env \
213             -v ${WORKSPACE}:${WORKSPACE} -v ${WORKDIR}:${WORKDIR} $ROBOT_IMAGE  \
214             python3 -B -m robot -N ${TESTPLAN} -v WORKSPACE:/tmp --outputdir ${WORKDIR} ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
215         fi
216     else
217             echo "[INFO] Skip Robot test suite, because SDNC is not in ready state"
218             echo "[ERROR] SDNC is not in ready state, check karaf.log!"
219             false
220     fi
221 else
222     echo "[WARNING] SDNC_RELEASE_WITHOUT_ROBOT is TRUE "
223     echo "[WARNING] Dummy Robot test suite is executed, job remains ok. "
224     docker run --rm --net="host" \
225     -v ${WORKSPACE}:${WORKSPACE} -v ${WORKDIR}:${WORKDIR} $ROBOT_IMAGE  \
226     python3 -B -m robot -N ${TESTPLAN} -v WORKSPACE:/tmp --outputdir ${WORKDIR} ${ROBOT_VARIABLES} ${TESTOPTIONS} ${WORKSPACE}/tests/sdnr/debug/10_dummy.robot
227    true
228 fi
229 RESULT=$?
230 load_set
231 echo "RESULT: $RESULT"
232 # Note that the final steps are done in on_exit function after this exit!
233 exit $RESULT