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