Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 1 | #!/bin/bash -x |
| 2 | # |
| 3 | # Copyright 2016-2017 Huawei Technologies Co., Ltd. |
| 4 | # Modification Copyright 2019-2021 © Samsung Electronics Co., Ltd. |
halil.cakal | 3a9613f | 2024-06-17 09:24:59 +0100 | [diff] [blame^] | 5 | # Modification Copyright (C) 2024 Nordix Foundation. |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 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 project/functionality |
| 20 | # $2 robot options |
| 21 | |
Ruslan Kashapov | d44fcee | 2021-02-26 11:42:29 +0200 | [diff] [blame] | 22 | # Branched from ccsdk/distribution to this repository Feb 23, 2021 |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 23 | |
Matthew Watkins | 438fbd4 | 2022-12-15 17:03:16 +0000 | [diff] [blame] | 24 | echo "---> run-csit.sh" |
| 25 | |
Matthew Watkins | 46d6e4f | 2022-11-17 14:55:28 -0800 | [diff] [blame] | 26 | WORKDIR=$(mktemp -d --suffix=-robot-workdir) |
Matthew Watkins | 46d6e4f | 2022-11-17 14:55:28 -0800 | [diff] [blame] | 27 | |
Matthew Watkins | 438fbd4 | 2022-12-15 17:03:16 +0000 | [diff] [blame] | 28 | # Version should match those used to setup robot-framework in other jobs/stages |
| 29 | # Use pyenv for selecting the python version |
| 30 | if [[ -d "/opt/pyenv" ]]; then |
| 31 | echo "Setup pyenv:" |
| 32 | export PYENV_ROOT="/opt/pyenv" |
| 33 | export PATH="$PYENV_ROOT/bin:$PATH" |
| 34 | pyenv versions |
| 35 | if command -v pyenv 1>/dev/null 2>&1; then |
| 36 | eval "$(pyenv init - --no-rehash)" |
| 37 | # Choose the latest numeric Python version from installed list |
| 38 | version=$(pyenv versions --bare | sed '/^[^0-9]/d' | sort -V | tail -n 1) |
| 39 | pyenv local "${version}" |
| 40 | fi |
| 41 | fi |
| 42 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 43 | # |
| 44 | # functions |
| 45 | # |
| 46 | |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 47 | # relax set options so the sourced file will not fail |
| 48 | # the responsibility is shifted to the sourced file... |
| 49 | function relax_set() { |
| 50 | set +e |
| 51 | set +o pipefail |
| 52 | } |
| 53 | |
| 54 | # load the saved set options |
| 55 | function load_set() { |
| 56 | _setopts="$-" |
| 57 | |
| 58 | # bash shellopts |
| 59 | for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do |
| 60 | set +o ${i} |
| 61 | done |
| 62 | for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do |
| 63 | set -o ${i} |
| 64 | done |
| 65 | |
| 66 | # other options |
| 67 | for i in $(echo "$_setopts" | sed 's/./& /g') ; do |
| 68 | set +${i} |
| 69 | done |
| 70 | set -${RUN_CSIT_SAVE_SET} |
| 71 | } |
| 72 | |
Matthew Watkins | fdaccbf | 2022-11-23 14:31:40 +0000 | [diff] [blame] | 73 | # wrapper for sourcing a file |
| 74 | function source_safely() { |
| 75 | [ -z "$1" ] && return 1 |
| 76 | relax_set |
| 77 | . "$1" |
| 78 | load_set |
| 79 | } |
| 80 | # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh |
| 81 | source_safely "${ROBOT3_VENV}/bin/activate" |
| 82 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 83 | function on_exit(){ |
| 84 | rc=$? |
| 85 | if [[ ${WORKSPACE} ]]; then |
| 86 | if [[ ${WORKDIR} ]]; then |
| 87 | rsync -av "$WORKDIR/" "$WORKSPACE/archives/$TESTPLAN" |
| 88 | fi |
| 89 | # Record list of active docker containers |
| 90 | docker ps --format "{{.Image}}" > "$WORKSPACE/archives/$TESTPLAN/_docker-images.log" |
| 91 | |
| 92 | # show memory consumption after all docker instances initialized |
| 93 | docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-2-after-robot.txt" |
| 94 | fi |
| 95 | # Run teardown script plan if it exists |
| 96 | cd "${TESTPLANDIR}" |
| 97 | TEARDOWN="${TESTPLANDIR}/teardown.sh" |
| 98 | if [ -f "${TEARDOWN}" ]; then |
| 99 | echo "Running teardown script ${TEARDOWN}" |
| 100 | source_safely "${TEARDOWN}" |
| 101 | fi |
| 102 | # TODO: do something with the output |
| 103 | exit $rc |
| 104 | } |
| 105 | # ensure that teardown and other finalizing steps are always executed |
| 106 | trap on_exit EXIT |
| 107 | |
| 108 | function docker_stats(){ |
| 109 | #General memory details |
| 110 | echo "> top -bn1 | head -3" |
| 111 | top -bn1 | head -3 |
| 112 | echo |
| 113 | |
| 114 | echo "> free -h" |
| 115 | free -h |
| 116 | echo |
| 117 | |
| 118 | #Memory details per Docker |
| 119 | echo "> docker ps" |
| 120 | docker ps |
| 121 | echo |
| 122 | |
| 123 | echo "> docker stats --no-stream" |
| 124 | docker stats --no-stream |
| 125 | echo |
| 126 | } |
| 127 | |
| 128 | # save current set options |
| 129 | function save_set() { |
| 130 | RUN_CSIT_SAVE_SET="$-" |
| 131 | RUN_CSIT_SHELLOPTS="$SHELLOPTS" |
| 132 | } |
| 133 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 134 | # set options for quick bailout when error |
| 135 | function harden_set() { |
| 136 | set -xeo pipefail |
| 137 | set +u # enabled it would probably fail too many often |
| 138 | } |
| 139 | |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 140 | function run_test_plan() { |
| 141 | testplan=$1 |
| 142 | |
| 143 | cd "$WORKDIR" |
| 144 | echo "Reading the testplan:" |
| 145 | cat "${TESTPLANDIR}/${testplan}.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > ${testplan}.txt |
| 146 | cat ${testplan}.txt |
| 147 | SUITES=$( xargs -a ${testplan}.txt ) |
| 148 | |
emaclee | f66fb4f | 2024-01-15 12:58:27 +0000 | [diff] [blame] | 149 | python3 -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp --legacy-output ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES} |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 150 | RESULT=$? |
| 151 | load_set |
| 152 | echo "RESULT: $RESULT" |
| 153 | return $RESULT |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 156 | # |
| 157 | # main |
| 158 | # |
| 159 | |
| 160 | # set and save options for quick failure |
| 161 | harden_set && save_set |
| 162 | |
| 163 | if [ $# -eq 0 ] |
| 164 | then |
| 165 | echo |
| 166 | echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]" |
| 167 | echo |
| 168 | echo " <project>, <functionality>, <robot-options>: " |
| 169 | echo " The same values as for the '{project}-csit-{functionality}' JJB job template." |
| 170 | echo |
| 171 | exit 1 |
| 172 | fi |
| 173 | |
| 174 | if [ -z "$WORKSPACE" ]; then |
| 175 | export WORKSPACE=$(git rev-parse --show-toplevel) |
| 176 | fi |
| 177 | |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 178 | if [ -f "${WORKSPACE}/${1}/testplanCps.txt" ]; then |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 179 | export TESTPLAN="${1}" |
| 180 | else |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 181 | echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplanCps.txt or testplanNcmp.txt" |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 182 | exit 2 |
| 183 | fi |
| 184 | |
| 185 | export TESTOPTIONS="${2}" |
| 186 | |
| 187 | rm -rf "$WORKSPACE/archives/$TESTPLAN" |
| 188 | mkdir -p "$WORKSPACE/archives/$TESTPLAN" |
| 189 | |
halil.cakal | 3a9613f | 2024-06-17 09:24:59 +0100 | [diff] [blame^] | 190 | rm -rf "$WORKSPACE/nginx-log" |
| 191 | mkdir -p "$WORKSPACE/nginx-log" |
| 192 | chmod -R 755 "$WORKSPACE/nginx-log" |
| 193 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 194 | TESTPLANDIR="${WORKSPACE}/${TESTPLAN}" |
| 195 | |
| 196 | # Run installation of prerequired libraries |
| 197 | source_safely "${WORKSPACE}/prepare-csit.sh" |
| 198 | |
Matthew Watkins | 17cbd7b | 2022-11-17 16:39:39 -0800 | [diff] [blame] | 199 | # Use robot framework working directory |
| 200 | cd "${WORKDIR}" |
| 201 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 202 | # Add csit scripts to PATH |
Matthew Watkins | 46d6e4f | 2022-11-17 14:55:28 -0800 | [diff] [blame] | 203 | export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT3_VENV}/bin" |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 204 | export SCRIPTS="${WORKSPACE}/scripts" |
| 205 | export ROBOT_VARIABLES= |
| 206 | |
| 207 | # Sign in to nexus3 docker repo |
| 208 | docker login -u docker -p docker nexus3.onap.org:10001 |
| 209 | |
| 210 | # Run setup script plan if it exists |
| 211 | cd "${TESTPLANDIR}" |
| 212 | SETUP="${TESTPLANDIR}/setup.sh" |
| 213 | if [ -f "${SETUP}" ]; then |
| 214 | echo "Running setup script ${SETUP}" |
| 215 | source_safely "${SETUP}" |
| 216 | fi |
| 217 | |
| 218 | # show memory consumption after all docker instances initialized |
| 219 | docker_stats | tee "$WORKSPACE/archives/$TESTPLAN/_sysinfo-1-after-setup.txt" |
| 220 | |
| 221 | # Run test plan |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 222 | echo ROBOT_VARIABLES="${ROBOT_VARIABLES}" |
| 223 | echo "Starting Robot test suites ${SUITES} ..." |
| 224 | relax_set |
Matthew Watkins | 438fbd4 | 2022-12-15 17:03:16 +0000 | [diff] [blame] | 225 | |
| 226 | echo "Versioning information:" |
| 227 | python3 --version |
| 228 | pip freeze |
| 229 | python3 -m robot.run --version || : |
| 230 | |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 231 | run_test_plan "testplanCps" |
| 232 | CPSRESULT="$?" |
| 233 | |
| 234 | cd "${TESTPLANDIR}" |
| 235 | checkandmount="${TESTPLANDIR}/sdnc/check_sdnc_mount_node.sh" |
| 236 | if [ -f "${checkandmount}" ]; then |
| 237 | echo "Running check_sdnc_mount_node script ${checkandmount}" |
| 238 | source_safely "${checkandmount}" |
| 239 | fi |
| 240 | |
| 241 | run_test_plan "testplanNcmp" |
| 242 | NCMPRESULT="$?" |
| 243 | |
Ruslan Kashapov | aad2240 | 2021-02-23 10:08:00 +0200 | [diff] [blame] | 244 | # Note that the final steps are done in on_exit function after this exit! |
JvD_Ericsson | cb85379 | 2023-12-15 09:22:40 +0000 | [diff] [blame] | 245 | exit $CPSRESULT || $NCMPRESULT |