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