Change docker login user
[integration/csit.git] / run-csit.sh
1 #!/bin/bash -x
2 #
3 # Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 # Modification Copyright 2019 © Samsung Electronics Co., Ltd.
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
20
21 #
22 # functions
23 #
24
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
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
98 if [ $# -eq 0 ]
99 then
100     echo
101     echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
102     echo
103     echo "    <project>, <functionality>, <robot-options>:  "
104     echo "        The same values as for the '{project}-csit-{functionality}' JJB job template."
105     echo
106     exit 1
107 fi
108
109 if [ -z "$WORKSPACE" ]; then
110     export WORKSPACE=$(git rev-parse --show-toplevel)
111 fi
112
113 rm -rf "$WORKSPACE/archives"
114 mkdir -p "$WORKSPACE/archives"
115
116 if [ -f "${WORKSPACE}/${1}/testplan.txt" ]; then
117     export TESTPLAN="${1}"
118 else
119     echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
120     exit 2
121 fi
122
123 export TESTOPTIONS="${2}"
124
125 TESTPLANDIR="${WORKSPACE}/${TESTPLAN}"
126
127 # Run installation of prerequired libraries
128 source_safely "${WORKSPACE}/prepare-csit.sh"
129
130 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
131 source_safely "${ROBOT_VENV}/bin/activate"
132
133 WORKDIR=$(mktemp -d --suffix=-robot-workdir)
134 cd "${WORKDIR}"
135
136 # Add csit scripts to PATH
137 export PATH="${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin"
138 export SCRIPTS="${WORKSPACE}/scripts"
139 export ROBOT_VARIABLES=
140
141 # Sign in to nexus3 docker repo
142 docker login -u docker -p docker nexus3.onap.org:10001
143
144 # Run setup script plan if it exists
145 cd "${TESTPLANDIR}"
146 SETUP="${TESTPLANDIR}/setup.sh"
147 if [ -f "${SETUP}" ]; then
148     echo "Running setup script ${SETUP}"
149     source_safely "${SETUP}"
150 fi
151
152 # show memory consumption after all docker instances initialized
153 docker_stats | tee "$WORKSPACE/archives/_sysinfo-1-after-setup.txt"
154
155 # Run test plan
156 cd "$WORKDIR"
157 echo "Reading the testplan:"
158 cat "${TESTPLANDIR}/testplan.txt" | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
159 cat testplan.txt
160 SUITES=$( xargs -a testplan.txt )
161
162 echo ROBOT_VARIABLES="${ROBOT_VARIABLES}"
163 echo "Starting Robot test suites ${SUITES} ..."
164 relax_set
165 python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
166 RESULT=$?
167 load_set
168 echo "RESULT: $RESULT"
169 rsync -av "$WORKDIR/" "$WORKSPACE/archives"
170
171 # Record list of active docker containers
172 docker ps --format "{{.Image}}" > "$WORKSPACE/archives/_docker-images.log"
173
174 # show memory consumption after all docker instances initialized
175 docker_stats | tee "$WORKSPACE/archives/_sysinfo-2-after-robot.txt"
176
177 # Run teardown script plan if it exists
178 cd "${TESTPLANDIR}"
179 TEARDOWN="${TESTPLANDIR}/teardown.sh"
180 if [ -f "${TEARDOWN}" ]; then
181     echo "Running teardown script ${TEARDOWN}"
182     source_safely "${TEARDOWN}"
183 fi
184
185 # TODO: do something with the output
186
187 exit $RESULT