Gary Wu | 13111e9 | 2018-09-27 11:31:33 -0700 | [diff] [blame] | 1 | #!/bin/bash -x |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2016-2017 Huawei Technologies Co., Ltd. |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 4 | # Modification Copyright 2019 © Samsung Electronics Co., Ltd. |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # $1 project/functionality |
| 19 | # $2 robot options |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 20 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 21 | # |
| 22 | # functions |
| 23 | # |
| 24 | |
Lasse Kaihlavirta | ff18aa2 | 2020-03-19 17:01:24 +0200 | [diff] [blame] | 25 | function on_exit(){ |
| 26 | rc=$? |
| 27 | rsync -av "$WORKDIR/" "$WORKSPACE/archives" |
| 28 | |
| 29 | # Record list of active docker containers |
| 30 | docker ps --format "{{.Image}}" > "$WORKSPACE/archives/_docker-images.log" |
| 31 | |
| 32 | # show memory consumption after all docker instances initialized |
| 33 | docker_stats | tee "$WORKSPACE/archives/_sysinfo-2-after-robot.txt" |
| 34 | |
| 35 | # Run teardown script plan if it exists |
| 36 | cd "${TESTPLANDIR}" |
| 37 | TEARDOWN="${TESTPLANDIR}/teardown.sh" |
| 38 | if [ -f "${TEARDOWN}" ]; then |
| 39 | echo "Running teardown script ${TEARDOWN}" |
| 40 | source_safely "${TEARDOWN}" |
| 41 | fi |
| 42 | # TODO: do something with the output |
| 43 | exit $rc |
| 44 | } |
| 45 | # ensure that teardown and other finalizing steps are always executed |
| 46 | trap on_exit EXIT |
| 47 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 48 | function docker_stats(){ |
| 49 | #General memory details |
| 50 | echo "> top -bn1 | head -3" |
| 51 | top -bn1 | head -3 |
| 52 | echo |
| 53 | |
| 54 | echo "> free -h" |
| 55 | free -h |
| 56 | echo |
| 57 | |
| 58 | #Memory details per Docker |
| 59 | echo "> docker ps" |
| 60 | docker ps |
| 61 | echo |
| 62 | |
| 63 | echo "> docker stats --no-stream" |
| 64 | docker stats --no-stream |
| 65 | echo |
| 66 | } |
| 67 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 68 | # save current set options |
| 69 | function save_set() { |
| 70 | RUN_CSIT_SAVE_SET="$-" |
| 71 | RUN_CSIT_SHELLOPTS="$SHELLOPTS" |
| 72 | } |
| 73 | |
| 74 | # load the saved set options |
| 75 | function load_set() { |
| 76 | _setopts="$-" |
| 77 | |
| 78 | # bash shellopts |
| 79 | for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do |
| 80 | set +o ${i} |
| 81 | done |
| 82 | for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do |
| 83 | set -o ${i} |
| 84 | done |
| 85 | |
| 86 | # other options |
| 87 | for i in $(echo "$_setopts" | sed 's/./& /g') ; do |
| 88 | set +${i} |
| 89 | done |
| 90 | set -${RUN_CSIT_SAVE_SET} |
| 91 | } |
| 92 | |
| 93 | # set options for quick bailout when error |
| 94 | function harden_set() { |
| 95 | set -xeo pipefail |
| 96 | set +u # enabled it would probably fail too many often |
| 97 | } |
| 98 | |
| 99 | # relax set options so the sourced file will not fail |
| 100 | # the responsibility is shifted to the sourced file... |
| 101 | function relax_set() { |
| 102 | set +e |
| 103 | set +o pipefail |
| 104 | } |
| 105 | |
| 106 | # wrapper for sourcing a file |
| 107 | function source_safely() { |
| 108 | [ -z "$1" ] && return 1 |
| 109 | relax_set |
| 110 | . "$1" |
| 111 | load_set |
| 112 | } |
| 113 | |
| 114 | # |
| 115 | # main |
| 116 | # |
| 117 | |
| 118 | # set and save options for quick failure |
| 119 | harden_set && save_set |
| 120 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 121 | if [ $# -eq 0 ] |
| 122 | then |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 123 | echo |
| 124 | echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 125 | echo |
| 126 | echo " <project>, <functionality>, <robot-options>: " |
| 127 | echo " The same values as for the '{project}-csit-{functionality}' JJB job template." |
| 128 | echo |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 129 | exit 1 |
| 130 | fi |
| 131 | |
| 132 | if [ -z "$WORKSPACE" ]; then |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 133 | export WORKSPACE=$(git rev-parse --show-toplevel) |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 134 | fi |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 135 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 136 | rm -rf "$WORKSPACE/archives" |
| 137 | mkdir -p "$WORKSPACE/archives" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 138 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 139 | if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 140 | export TESTPLAN="${1}" |
| 141 | else |
Gary Wu | 13111e9 | 2018-09-27 11:31:33 -0700 | [diff] [blame] | 142 | echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 143 | exit 2 |
| 144 | fi |
| 145 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 146 | export TESTOPTIONS="${2}" |
| 147 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 148 | TESTPLANDIR="${WORKSPACE}/${TESTPLAN}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 149 | |
kaihlavi | 9f5700f | 2019-08-27 11:30:51 +0300 | [diff] [blame] | 150 | # Run installation of prerequired libraries |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 151 | source_safely "${WORKSPACE}/prepare-csit.sh" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 152 | |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 153 | # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 154 | source_safely "${ROBOT_VENV}/bin/activate" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 155 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 156 | WORKDIR=$(mktemp -d --suffix=-robot-workdir) |
| 157 | cd "${WORKDIR}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 158 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 159 | # Add csit scripts to PATH |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 160 | export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin" |
| 161 | export SCRIPTS="${WORKSPACE}/scripts" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 162 | export ROBOT_VARIABLES= |
| 163 | |
| 164 | # Sign in to nexus3 docker repo |
Bogumil Zebek | f1903ee | 2020-03-19 18:26:54 +0100 | [diff] [blame] | 165 | docker login -u docker -p docker nexus3.onap.org:10001 |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 166 | |
| 167 | # Run setup script plan if it exists |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 168 | cd "${TESTPLANDIR}" |
| 169 | SETUP="${TESTPLANDIR}/setup.sh" |
| 170 | if [ -f "${SETUP}" ]; then |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 171 | echo "Running setup script ${SETUP}" |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 172 | source_safely "${SETUP}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 173 | fi |
| 174 | |
| 175 | # show memory consumption after all docker instances initialized |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 176 | docker_stats | tee "$WORKSPACE/archives/_sysinfo-1-after-setup.txt" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 177 | |
| 178 | # Run test plan |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 179 | cd "$WORKDIR" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 180 | echo "Reading the testplan:" |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 181 | cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 182 | cat testplan.txt |
| 183 | SUITES=$( xargs -a testplan.txt ) |
| 184 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 185 | echo ROBOT_VARIABLES="${ROBOT_VARIABLES}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 186 | echo "Starting Robot test suites ${SUITES} ..." |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 187 | relax_set |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 188 | python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES} |
| 189 | RESULT=$? |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 190 | load_set |
| 191 | echo "RESULT: $RESULT" |
Lasse Kaihlavirta | ff18aa2 | 2020-03-19 17:01:24 +0200 | [diff] [blame] | 192 | # Note that the final steps are done in on_exit function after this exit! |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 193 | exit $RESULT |