Add a new replica table in clampacm database in db migrator
[policy/docker.git] / csit / run-project-csit.sh
1 #!/bin/bash
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-2023 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         # Record list of active docker containers
31         docker ps --format "table {{ .Names }}\t{{ .Status }}"
32
33         # show memory consumption after all docker instances initialized
34         docker_stats
35
36         source_safely ${WORKSPACE}/compose/stop-compose.sh
37         rsync "${WORKSPACE}/compose/docker_compose.log" "${WORKSPACE}/csit/archives/${PROJECT}"
38
39         if [[ ${WORKDIR} ]]; then
40             rsync -av "${WORKDIR}/" "${WORKSPACE}/csit/archives/${PROJECT}"
41         fi
42         rm -rf ${WORKSPACE}/models
43     fi
44     # TODO: do something with the output
45      exit $rc
46 }
47
48 # ensure that teardown and other finalizing steps are always executed
49 trap on_exit EXIT
50
51 function docker_stats(){
52     # General memory details
53     if [ "$(uname -s)" == "Darwin" ]
54     then
55         sh -c "top -l1 | head -10"
56         echo
57     else
58         sh -c "top -bn1 | head -3"
59         echo
60
61         sh -c "free -h"
62         echo
63     fi
64
65     # Memory details per Docker
66     docker ps --format "table {{ .Names }}\t{{ .Status }}"
67     echo
68
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     WORKSPACE=$(git rev-parse --show-toplevel)
138     export WORKSPACE
139 fi
140
141 # Add csit scripts to PATH
142 export PATH="${PATH}:${WORKSPACE}/csit:${WORKSPACE}/scripts:${ROBOT_VENV}/bin"
143 export SCRIPTS="${WORKSPACE}/csit/resources/scripts"
144 export ROBOT_VARIABLES=
145
146 export PROJECT="${1}"
147
148 cd "${WORKSPACE}"
149
150 rm -rf "${WORKSPACE}/csit/archives/${PROJECT}"
151 mkdir -p "${WORKSPACE}/csit/archives/${PROJECT}"
152
153 # Run installation of pre-required libraries
154 source_safely "${SCRIPTS}/prepare-robot-env.sh"
155
156 # Activate the virtualenv containing all the required libraries installed by prepare-robot-env.sh
157 source_safely "${ROBOT_VENV}/bin/activate"
158
159 export TEST_PLAN_DIR="${WORKSPACE}/csit/resources/tests"
160 export TEST_OPTIONS="${2}"
161
162 WORKDIR=$(mktemp -d)
163 cd "${WORKDIR}"
164
165 # Sign in to nexus3 docker repo
166 docker login -u docker -p docker nexus3.onap.org:10001
167
168 # Run setup script plan if it exists
169 SETUP="${SCRIPTS}/setup-${PROJECT}.sh"
170 if [ -f "${SETUP}" ]; then
171     echo "Running setup script ${SETUP}"
172     source_safely "${SETUP}"
173 fi
174
175 # show memory consumption after all docker instances initialized
176 docker_stats | tee "${WORKSPACE}/csit/archives/${PROJECT}/_sysinfo-1-after-setup.txt"
177
178 # Run test plan
179 cd "${WORKDIR}"
180 echo "Reading the testplan:"
181 echo "${SUITES}" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${TEST_PLAN_DIR}/|" > testplan.txt
182 cat testplan.txt
183 SUITES=$( xargs < testplan.txt )
184
185 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
186 echo "Starting Robot test suites ${SUITES} ..."
187 relax_set
188 python3 -m robot.run -N "${PROJECT}" -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${SUITES}
189 RESULT=$?
190 load_set
191 echo "RESULT: ${RESULT}"
192 # Note that the final steps are done in on_exit function after this exit!
193 exit ${RESULT}