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