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