Refactor: Cleanup for pre-commit
[ci-management.git] / jjb / integration / run-csit.sh
1 #!/bin/bash -x
2 #
3 # Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 # Modification Copyright 2019 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 {TESTPLAN}
19 # $2 robot options         {TESTOPTIONS}
20
21 echo "---> run-csit.sh"
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 # set and save options for quick failure
123 harden_set && save_set
124
125 if [ $# -eq 0 ]; then
126     echo
127     echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
128     echo
129     echo "    <project>, <functionality>, <robot-options>:  "
130     echo "        The same values as for the '{project}-csit-{functionality}' JJB job template."
131     echo
132     exit 1
133 fi
134
135 if [ -z "$WORKSPACE" ]; then
136     export WORKSPACE=$(git rev-parse --show-toplevel)
137 fi
138
139 if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
140     export TESTPLAN="${1}"
141 else
142     echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
143     exit 2
144 fi
145
146 export TESTOPTIONS="${2}"
147
148 rm -rf "$WORKSPACE/archives/$TESTPLAN"
149 mkdir -p "$WORKSPACE/archives/$TESTPLAN"
150
151 TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
152
153 # Run installation of required libraries
154 source_safely "${WORKSPACE}/prepare-csit.sh"
155
156 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
157 source_safely "${ROBOT3_VENV}/bin/activate"
158
159 WORKDIR=$(mktemp -d --suffix=-robot-workdir)
160 cd "${WORKDIR}"
161
162 # Add csit scripts to PATH
163 export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin"
164 export SCRIPTS="${WORKSPACE}/scripts"
165 export ROBOT_VARIABLES=
166
167 # Sign in to nexus3 docker repo
168 docker login -u docker -p docker nexus3.onap.org:10001
169
170 # Run setup script plan if it exists
171 cd "${TESTPLANDIR}"
172 SETUP="${TESTPLANDIR}/setup.sh"
173 if [ -f "${SETUP}" ]; then
174     echo "Running setup script ${SETUP}"
175     source_safely "${SETUP}"
176 fi
177
178 # show memory consumption after all docker instances initialized
179 docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt"
180
181 # Run test plan
182 cd "$WORKDIR"
183 echo "Reading the testplan:"
184 grep -E -v '(^[[:space:]]*#|^[[:space:]]*$)' "${TESTPLANDIR}/testplan.txt" |\
185     sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
186 cat testplan.txt
187 SUITES=$( xargs -a testplan.txt )
188
189 echo "Versioning information:"
190 python3 --version
191 pip3 freeze
192 python3 -m robot.run --version || :
193
194 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
195 echo "Starting Robot test suites ${SUITES} ..."
196 relax_set
197 # Do NOT quote variables below, it breaks testing
198 python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
199 RESULT=$?
200 load_set
201 echo "RESULT: $RESULT"
202 # Note that the final steps are done in on_exit function after this exit!
203 exit $RESULT