Take CSIT preparations to separate script
[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 function docker_stats(){
22     #General memory details
23     echo "> top -bn1 | head -3"
24     top -bn1 | head -3
25     echo
26
27     echo "> free -h"
28     free -h
29     echo
30
31     #Memory details per Docker
32     echo "> docker ps"
33     docker ps
34     echo
35
36     echo "> docker stats --no-stream"
37     docker stats --no-stream
38     echo
39 }
40
41 if [ $# -eq 0 ]
42 then
43     echo
44     echo "Usage: $0 plans/<project>/<functionality> [<robot-options>]"
45     echo
46     echo "    <project>, <functionality>, <robot-options>:  "
47     echo "        The same values as for the '{project}-csit-{functionality}' JJB job template."
48     echo
49     exit 1
50 fi
51
52 if [ -z "$WORKSPACE" ]; then
53     export WORKSPACE=`git rev-parse --show-toplevel`
54 fi
55
56 rm -rf $WORKSPACE/archives
57 mkdir -p $WORKSPACE/archives
58
59 if [ -f ${WORKSPACE}/${1}/testplan.txt ]; then
60     export TESTPLAN="${1}"
61 else
62     echo "testplan not found: ${WORKSPACE}/${TESTPLAN}/testplan.txt"
63     exit 2
64 fi
65
66 export TESTOPTIONS="${2}"
67
68 TESTPLANDIR=${WORKSPACE}/${TESTPLAN}
69
70 # Assume that if env.properties is set, installation prerequirements are fulfilled
71 # env.properties needs to be sourced for ROBOT_VENV
72 if [ -f ${WORKSPACE}/env.properties ]; then
73     source ${WORKSPACE}/env.properties
74 else
75     source ${WORKSPACE}/prepare-csit.sh
76 fi
77
78 # Activate the virtualenv containing all the required libraries installed by prepare-csit.sh
79 source "${ROBOT_VENV}/bin/activate"
80
81 WORKDIR=`mktemp -d --suffix=-robot-workdir`
82 cd ${WORKDIR}
83
84 set +u
85 set -x
86
87 # Add csit scripts to PATH
88 export PATH=${PATH}:${WORKSPACE}/docker/scripts:${WORKSPACE}/scripts:${ROBOT_VENV}/bin
89 export SCRIPTS=${WORKSPACE}/scripts
90 export ROBOT_VARIABLES=
91
92 # Sign in to nexus3 docker repo
93 docker login -u anonymous -p anonymous nexus3.onap.org:10001
94
95 # Run setup script plan if it exists
96 cd ${TESTPLANDIR}
97 SETUP=${TESTPLANDIR}/setup.sh
98 if [ -f ${SETUP} ]; then
99     echo "Running setup script ${SETUP}"
100     source ${SETUP}
101 fi
102
103 # show memory consumption after all docker instances initialized
104 docker_stats | tee $WORKSPACE/archives/_sysinfo-1-after-setup.txt
105
106 # Run test plan
107 cd $WORKDIR
108 echo "Reading the testplan:"
109 cat ${TESTPLANDIR}/testplan.txt | egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' | sed "s|^|${WORKSPACE}/tests/|" > testplan.txt
110 cat testplan.txt
111 SUITES=$( xargs -a testplan.txt )
112
113 echo ROBOT_VARIABLES=${ROBOT_VARIABLES}
114 echo "Starting Robot test suites ${SUITES} ..."
115 set +e
116 python -m robot.run -N ${TESTPLAN} -v WORKSPACE:/tmp ${ROBOT_VARIABLES} ${TESTOPTIONS} ${SUITES}
117 RESULT=$?
118 set -e
119 echo "RESULT: " $RESULT
120 rsync -av $WORKDIR/ $WORKSPACE/archives
121
122 # Record list of active docker containers
123 docker ps --format "{{.Image}}" > $WORKSPACE/archives/_docker-images.log
124
125 # show memory consumption after all docker instances initialized
126 docker_stats | tee $WORKSPACE/archives/_sysinfo-2-after-robot.txt
127
128 # Run teardown script plan if it exists
129 cd ${TESTPLANDIR}
130 TEARDOWN=${TESTPLANDIR}/teardown.sh
131 if [ -f ${TEARDOWN} ]; then
132     echo "Running teardown script ${TEARDOWN}"
133     source ${TEARDOWN}
134 fi
135
136 # TODO: do something with the output
137
138 exit $RESULT