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