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 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 25 | function docker_stats(){ |
| 26 | #General memory details |
| 27 | echo "> top -bn1 | head -3" |
| 28 | top -bn1 | head -3 |
| 29 | echo |
| 30 | |
| 31 | echo "> free -h" |
| 32 | free -h |
| 33 | echo |
| 34 | |
| 35 | #Memory details per Docker |
| 36 | echo "> docker ps" |
| 37 | docker ps |
| 38 | echo |
| 39 | |
| 40 | echo "> docker stats --no-stream" |
| 41 | docker stats --no-stream |
| 42 | echo |
| 43 | } |
| 44 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 45 | # save current set options |
| 46 | function save_set() { |
| 47 | RUN_CSIT_SAVE_SET="$-" |
| 48 | RUN_CSIT_SHELLOPTS="$SHELLOPTS" |
| 49 | } |
| 50 | |
| 51 | # load the saved set options |
| 52 | function load_set() { |
| 53 | _setopts="$-" |
| 54 | |
| 55 | # bash shellopts |
| 56 | for i in $(echo "$SHELLOPTS" | tr ':' ' ') ; do |
| 57 | set +o ${i} |
| 58 | done |
| 59 | for i in $(echo "$RUN_CSIT_SHELLOPTS" | tr ':' ' ') ; do |
| 60 | set -o ${i} |
| 61 | done |
| 62 | |
| 63 | # other options |
| 64 | for i in $(echo "$_setopts" | sed 's/./& /g') ; do |
| 65 | set +${i} |
| 66 | done |
| 67 | set -${RUN_CSIT_SAVE_SET} |
| 68 | } |
| 69 | |
| 70 | # set options for quick bailout when error |
| 71 | function harden_set() { |
| 72 | set -xeo pipefail |
| 73 | set +u # enabled it would probably fail too many often |
| 74 | } |
| 75 | |
| 76 | # relax set options so the sourced file will not fail |
| 77 | # the responsibility is shifted to the sourced file... |
| 78 | function relax_set() { |
| 79 | set +e |
| 80 | set +o pipefail |
| 81 | } |
| 82 | |
| 83 | # wrapper for sourcing a file |
| 84 | function source_safely() { |
| 85 | [ -z "$1" ] && return 1 |
| 86 | relax_set |
| 87 | . "$1" |
| 88 | load_set |
| 89 | } |
| 90 | |
| 91 | # |
| 92 | # main |
| 93 | # |
| 94 | |
| 95 | # set and save options for quick failure |
| 96 | harden_set && save_set |
| 97 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 98 | if [ $# -eq 0 ] |
| 99 | then |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 100 | echo |
| 101 | echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 102 | echo |
| 103 | echo " <project>, <functionality>, <robot-options>: " |
| 104 | echo " The same values as for the '{project}-csit-{functionality}' JJB job template." |
| 105 | echo |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 106 | exit 1 |
| 107 | fi |
| 108 | |
| 109 | if [ -z "$WORKSPACE" ]; then |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 110 | export WORKSPACE=$(git rev-parse --show-toplevel) |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 111 | fi |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 112 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 113 | rm -rf "$WORKSPACE/archives" |
| 114 | mkdir -p "$WORKSPACE/archives" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 115 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 116 | if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 117 | export TESTPLAN="${1}" |
| 118 | else |
Gary Wu | 13111e9 | 2018-09-27 11:31:33 -0700 | [diff] [blame] | 119 | echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 120 | exit 2 |
| 121 | fi |
| 122 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 123 | export TESTOPTIONS="${2}" |
| 124 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 125 | TESTPLANDIR="${WORKSPACE}/${TESTPLAN}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 126 | |
kaihlavi | 9f5700f | 2019-08-27 11:30:51 +0300 | [diff] [blame] | 127 | # Run installation of prerequired libraries |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 128 | source_safely "${WORKSPACE}/prepare-csit.sh" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 129 | |
kaihlavi | 203e43a | 2019-08-16 16:02:38 +0300 | [diff] [blame] | 130 | # 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] | 131 | source_safely "${ROBOT_VENV}/bin/activate" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 132 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 133 | WORKDIR=$(mktemp -d --suffix=-robot-workdir) |
| 134 | cd "${WORKDIR}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 135 | |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 136 | # Add csit scripts to PATH |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 137 | export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin" |
| 138 | export SCRIPTS="${WORKSPACE}/scripts" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 139 | export ROBOT_VARIABLES= |
| 140 | |
| 141 | # Sign in to nexus3 docker repo |
Bogumil Zebek | f1903ee | 2020-03-19 18:26:54 +0100 | [diff] [blame] | 142 | docker login -u docker -p docker nexus3.onap.org:10001 |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 143 | |
| 144 | # Run setup script plan if it exists |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 145 | cd "${TESTPLANDIR}" |
| 146 | SETUP="${TESTPLANDIR}/setup.sh" |
| 147 | if [ -f "${SETUP}" ]; then |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 148 | echo "Running setup script ${SETUP}" |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 149 | source_safely "${SETUP}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 150 | fi |
| 151 | |
| 152 | # show memory consumption after all docker instances initialized |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 153 | docker_stats | tee "$WORKSPACE/archives/_sysinfo-1-after-setup.txt" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 154 | |
| 155 | # Run test plan |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 156 | cd "$WORKDIR" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 157 | echo "Reading the testplan:" |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 158 | 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] | 159 | cat testplan.txt |
| 160 | SUITES=$( xargs -a testplan.txt ) |
| 161 | |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 162 | echo ROBOT_VARIABLES="${ROBOT_VARIABLES}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 163 | echo "Starting Robot test suites ${SUITES} ..." |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 164 | relax_set |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 165 | python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES} |
| 166 | RESULT=$? |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 167 | load_set |
| 168 | echo "RESULT: $RESULT" |
| 169 | rsync -av "$WORKDIR/" "$WORKSPACE/archives" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 170 | |
| 171 | # Record list of active docker containers |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 172 | docker ps --format "{{.Image}}" > "$WORKSPACE/archives/_docker-images.log" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 173 | |
| 174 | # show memory consumption after all docker instances initialized |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 175 | docker_stats | tee "$WORKSPACE/archives/_sysinfo-2-after-robot.txt" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 176 | |
| 177 | # Run teardown script plan if it exists |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 178 | cd "${TESTPLANDIR}" |
| 179 | TEARDOWN="${TESTPLANDIR}/teardown.sh" |
| 180 | if [ -f "${TEARDOWN}" ]; then |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 181 | echo "Running teardown script ${TEARDOWN}" |
Petr Ospalý | 5cf1391 | 2019-09-09 18:00:05 +0200 | [diff] [blame] | 182 | source_safely "${TEARDOWN}" |
Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame] | 183 | fi |
| 184 | |
| 185 | # TODO: do something with the output |
| 186 | |
| 187 | exit $RESULT |