e9c7585109fd78a11b149dfa9c20765ccdde69d2
[policy/docker.git] / csit / run-project-csit.sh
1 #!/bin/bash -x
2 #
3 # Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 # Modification Copyright 2019 © Samsung Electronics Co., Ltd.
5 # Modification Copyright 2021 © AT&T Intellectual Property.
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 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}/csit/archives/${PROJECT}"
31         fi
32         # Record list of active docker containers
33         docker ps --format "{{.Image}}" > "${WORKSPACE}/csit/archives/${PROJECT}/_docker-images.log"
34
35         # show memory consumption after all docker instances initialized
36         docker_stats | tee "${WORKSPACE}/csit/archives/${PROJECT}/_sysinfo-2-after-robot.txt"
37     fi
38     # Run teardown script plan if it exists
39     cd "${TESTPLANDIR}/plans/"
40     TEARDOWN="${TESTPLANDIR}/plans/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
49 # ensure that teardown and other finalizing steps are always executed
50 trap on_exit EXIT
51
52 function docker_stats(){
53     #General memory details
54     echo "> top -bn1 | head -3"
55     top -bn1 | head -3
56     echo
57
58     echo "> free -h"
59     free -h
60     echo
61
62     #Memory details per Docker
63     echo "> docker ps"
64     docker ps
65     echo
66
67     echo "> docker stats --no-stream"
68     docker stats --no-stream
69     echo
70 }
71
72 # save current set options
73 function save_set() {
74     RUN_CSIT_SAVE_SET="$-"
75     RUN_CSIT_SHELLOPTS="${SHELLOPTS}"
76 }
77
78 # load the saved set options
79 function load_set() {
80     _setopts="$-"
81
82     # bash shellopts
83     for i in $(echo "${SHELLOPTS}" | tr ':' ' ') ; do
84         set +o ${i}
85     done
86     for i in $(echo "${RUN_CSIT_SHELLOPTS}" | tr ':' ' ') ; do
87         set -o ${i}
88     done
89
90     # other options
91     for i in $(echo "$_setopts" | sed 's/./& /g') ; do
92         set +${i}
93     done
94     set -${RUN_CSIT_SAVE_SET}
95 }
96
97 # set options for quick bailout when error
98 function harden_set() {
99     set -xeo pipefail
100     set +u # enabled it would probably fail too many often
101 }
102
103 # relax set options so the sourced file will not fail
104 # the responsibility is shifted to the sourced file...
105 function relax_set() {
106     set +e
107     set +o pipefail
108 }
109
110 # wrapper for sourcing a file
111 function source_safely() {
112     [ -z "$1" ] && return 1
113     relax_set
114     . "$1"
115     load_set
116 }
117
118 #
119 # main
120 #
121
122 # set and save options for quick failure
123 harden_set && save_set
124
125 if [ $# -eq 0 ]
126 then
127     echo
128     echo "Usage: $0 <project> [<robot-options>]"
129     echo
130     echo "    <project> <robot-options>:  "
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 # Add csit scripts to PATH
140 export PATH="${PATH}:${WORKSPACE}/csit:${WORKSPACE}/scripts:${ROBOT_VENV}/bin"
141 export SCRIPTS="${WORKSPACE}/csit"
142 export ROBOT_VARIABLES=
143
144 # get the plan from git clone
145 source ${SCRIPTS}/get-branch-mariadb.sh
146
147 export PROJECT="${1}"
148
149 cd ${WORKSPACE}
150
151 case "${PROJECT}" in
152 xacml-pdp)
153     export TESTPLANDIR="${WORKSPACE}/csit/${PROJECT}"
154     ;;
155 *)
156     export TESTPLANDIR="${WORKSPACE}/${PROJECT}/csit"
157
158     rm -rf ${WORKSPACE}/${PROJECT}
159     mkdir ${WORKSPACE}/${PROJECT}
160
161     # get the plan from git clone
162     if ! `git clone -b ${GERRIT_BRANCH} --single-branch https://github.com/onap/policy-${PROJECT}.git ${PROJECT}` ; then
163         echo "repo not found: policy/${PROJECT}"
164         exit 1
165     fi
166
167     if [ ! -f "${TESTPLANDIR}/plans/testplan.txt" ]; then
168     echo "testplan not found: ${TESTPLANDIR}/plans/testplan.txt"
169         exit 2
170     fi
171     ;;
172 esac
173
174 export TESTOPTIONS="${2}"
175
176 rm -rf "${WORKSPACE}/csit/archives/${PROJECT}"
177 mkdir -p "${WORKSPACE}/csit/archives/${PROJECT}"
178
179 # Run installation of prerequired libraries
180 source_safely "${SCRIPTS}/prepare-csit.sh"
181
182 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
183 source_safely "${ROBOT_VENV}/bin/activate"
184
185 WORKDIR=$(mktemp -d --suffix=-robot-workdir)
186 cd "${WORKDIR}"
187
188 # Sign in to nexus3 docker repo
189 docker login -u docker -p docker nexus3.onap.org:10001
190
191 # Generate keystore to be used by repos
192 ${SCRIPTS}/gen_keystore.sh
193 cp ${SCRIPTS}/config/ks.jks ${SCRIPTS}/config/drools/custom/policy-keystore
194 cp ${SCRIPTS}/config/ks.jks ${SCRIPTS}/config/drools-apps/custom/policy-keystore
195
196 # Run setup script plan if it exists
197 cd "${TESTPLANDIR}/plans/"
198 SETUP="${TESTPLANDIR}/plans/setup.sh"
199 if [ -f "${SETUP}" ]; then
200     echo "Running setup script ${SETUP}"
201     source_safely "${SETUP}"
202 fi
203
204 # show memory consumption after all docker instances initialized
205 docker_stats | tee "${WORKSPACE}/csit/archives/${PROJECT}/_sysinfo-1-after-setup.txt"
206
207 # Run test plan
208 cd "${WORKDIR}"
209 echo "Reading the testplan:"
210 cat "${TESTPLANDIR}/plans/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${TESTPLANDIR}/tests/|" > testplan.txt
211 cat testplan.txt
212 SUITES=$( xargs -a testplan.txt )
213
214 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
215 echo "Starting Robot test suites ${SUITES} ..."
216 relax_set
217 python -m robot.run -N ${PROJECT} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${SUITES}
218 RESULT=$?
219 load_set
220 echo "RESULT: ${RESULT}"
221 # Note that the final steps are done in on_exit function after this exit!
222 exit ${RESULT}