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