Don't save private key
[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 # Modification Copyright 2021. Nordix Foundation.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20 # $1 functionality
21 # $2 robot options
22
23 #
24 # functions
25 #
26
27 function on_exit(){
28     rc=$?
29     if [[ ${WORKSPACE} ]]; then
30         if [[ ${WORKDIR} ]]; then
31             rsync -av "${WORKDIR}/" "${WORKSPACE}/csit/archives/${PROJECT}"
32         fi
33         # Record list of active docker containers
34         docker ps --format "{{.Image}}" > "${WORKSPACE}/csit/archives/${PROJECT}/_docker-images.log"
35
36         # show memory consumption after all docker instances initialized
37         docker_stats | tee "${WORKSPACE}/csit/archives/${PROJECT}/_sysinfo-2-after-robot.txt"
38     fi
39     # Run teardown script plan if it exists
40     cd "${TESTPLANDIR}/plans/"
41     TEARDOWN="${TESTPLANDIR}/plans/teardown.sh"
42     if [ -f "${TEARDOWN}" ]; then
43         echo "Running teardown script ${TEARDOWN}"
44         source_safely "${TEARDOWN}"
45     fi
46     # TODO: do something with the output
47      exit $rc
48 }
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 <project> [<robot-options>]"
130     echo
131     echo "    <project> <robot-options>:  "
132     echo
133     exit 1
134 fi
135
136 if [ -z "${WORKSPACE}" ]; then
137     export WORKSPACE=$(git rev-parse --show-toplevel)
138 fi
139
140 # Add csit scripts to PATH
141 export PATH="${PATH}:${WORKSPACE}/csit:${WORKSPACE}/scripts:${ROBOT_VENV}/bin"
142 export SCRIPTS="${WORKSPACE}/csit"
143 export ROBOT_VARIABLES=
144
145 # get the plan from git clone
146 source ${SCRIPTS}/get-branch-mariadb.sh
147
148 export PROJECT="${1}"
149
150 cd ${WORKSPACE}
151
152 export TESTPLANDIR="${WORKSPACE}/csit/${PROJECT}"
153 export TESTOPTIONS="${2}"
154
155 rm -rf "${WORKSPACE}/csit/archives/${PROJECT}"
156 mkdir -p "${WORKSPACE}/csit/archives/${PROJECT}"
157
158 # Run installation of prerequired libraries
159 source_safely "${SCRIPTS}/prepare-csit.sh"
160
161 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
162 source_safely "${ROBOT_VENV}/bin/activate"
163
164 WORKDIR=$(mktemp -d --suffix=-robot-workdir)
165 cd "${WORKDIR}"
166
167 # Sign in to nexus3 docker repo
168 docker login -u docker -p docker nexus3.onap.org:10001
169
170 # Generate truststore and keystore to be used by repos
171 ${SCRIPTS}/gen_truststore.sh
172 ${SCRIPTS}/gen_keystore.sh
173 cp ${SCRIPTS}/config/ks.jks ${SCRIPTS}/config/drools/custom/policy-keystore
174 cp ${SCRIPTS}/config/ks.jks ${SCRIPTS}/config/drools-apps/custom/policy-keystore
175 cp ${SCRIPTS}/config/policy-truststore \
176     ${SCRIPTS}/config/drools/custom/policy-truststore
177 cp ${SCRIPTS}/config/policy-truststore \
178     ${SCRIPTS}/config/drools-apps/custom/policy-truststore
179 chmod 644 \
180     ${SCRIPTS}/config/drools/custom/policy-* \
181     ${SCRIPTS}/config/drools-apps/custom/policy-*
182
183 # Run setup script plan if it exists
184 cd "${TESTPLANDIR}/plans/"
185 SETUP="${TESTPLANDIR}/plans/setup.sh"
186 if [ -f "${SETUP}" ]; then
187     echo "Running setup script ${SETUP}"
188     source_safely "${SETUP}"
189 fi
190
191 # show memory consumption after all docker instances initialized
192 docker_stats | tee "${WORKSPACE}/csit/archives/${PROJECT}/_sysinfo-1-after-setup.txt"
193
194 # Run test plan
195 cd "${WORKDIR}"
196 echo "Reading the testplan:"
197 cat "${TESTPLANDIR}/plans/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${TESTPLANDIR}/tests/|" > testplan.txt
198 cat testplan.txt
199 SUITES=$( xargs -a testplan.txt )
200
201 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
202 echo "Starting Robot test suites ${SUITES} ..."
203 relax_set
204 python3 -m robot.run -N ${PROJECT} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${SUITES}
205 RESULT=$?
206 load_set
207 echo "RESULT: ${RESULT}"
208 # Note that the final steps are done in on_exit function after this exit!
209 exit ${RESULT}