[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / csit / 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 # Modifications copyright (C) 2021 Nordix Foundation..
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 # functions
24 #
25
26 function on_exit(){
27     rc=$?
28     if [[ ${WORKSPACE} ]]; then
29         if [[ ${WORKDIR} ]]; then
30             rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN"
31         fi
32         # Record list of active docker containers
33         docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log"
34
35         # show memory consumption after all docker instances initialized
36         docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt"
37     fi
38     # Run teardown script plan if it exists
39     cd "${TESTPLANDIR}"
40     TEARDOWN="${TESTPLANDIR}/teardown.sh"
41     if [ -f "${TEARDOWN}" ]; then
42         echo "Running teardown script ${TEARDOWN}"
43         source_safely "${TEARDOWN}"
44     fi
45     # TODO: do something with the output
46      exit $rc
47 }
48 # ensure that teardown and other finalizing steps are always executed
49 trap on_exit EXIT
50
51 function docker_stats(){
52     #General memory details
53     echo "> top -bn1 | head -3"
54     top -bn1 | head -3
55     echo
56
57     echo "> free -h"
58     free -h
59     echo
60
61     #Memory details per Docker
62     echo "> docker ps"
63     docker ps
64     echo
65
66     echo "> docker stats --no-stream"
67     docker stats --no-stream
68     echo
69 }
70
71 # save current set options
72 function save_set() {
73     RUN_CSIT_SAVE_SET="$-"
74     RUN_CSIT_SHELLOPTS="$SHELLOPTS"
75 }
76
77 # load the saved set options
78 function load_set() {
79     _setopts="$-"
80
81     # bash shellopts
82     for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do
83         set +o ${i}
84     done
85     for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do
86         set -o ${i}
87     done
88
89     # other options
90     for i in $(echo "$_setopts" | sed 's/./& /g') ; do
91         set +${i}
92     done
93     set -${RUN_CSIT_SAVE_SET}
94 }
95
96 # set options for quick bailout when error
97 function harden_set() {
98     set -xeo pipefail
99     set +u # enabled it would probably fail too many often
100 }
101
102 # relax set options so the sourced file will not fail
103 # the responsibility is shifted to the sourced file...
104 function relax_set() {
105     set +e
106     set +o pipefail
107 }
108
109 # wrapper for sourcing a file
110 function source_safely() {
111     [ -z "$1" ] && return 1
112     relax_set
113     . "$1"
114     load_set
115 }
116
117 #
118 # main
119 #
120
121 # set and save options for quick failure
122 harden_set && save_set
123
124 if [ $# -eq 0 ]
125 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 prerequired 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 "${ROBOT_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:${ROBOT_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 cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
185 cat testplan.txt
186 SUITES=$( xargs -a testplan.txt )
187
188 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
189 echo "Starting Robot test suites ${SUITES} ..."
190 relax_set
191 python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
192 RESULT=$?
193 load_set
194 echo "RESULT: $RESULT"
195 # Note that the final steps are done in on_exit function after this exit!
196 exit $RESULT