bash: Update jjb version in jjb-sandbox-env function
[vpp.git] / extras / bash / functions.bash
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14
15 # This file is meant to be sourced in a .bashrc file to add useful
16 # bash functions to an interactive shell
17
18 # Bash function to run vpp 'make test' testcases
19 # repeatedly, stopping on test failure or when
20 # a test log contains the optionally specified text
21 vpp-make-test()
22 {
23     local options
24     local usage
25     local all
26     local debug
27     local grep_for
28     local show_grep
29     local run_make_test
30     local old_pwd
31     local test_desc
32     local is_feature="false"
33     local retry_count=100
34     local tester=${GERRIT_USER:-$USER}
35     local jobs="auto"
36
37     if [ -z "$WS_ROOT" ] ; then
38         echo "ERROR: WS_ROOT is not set!"
39         return
40     elif [ ! -d "$WS_ROOT/src/vppinfra" ] ; then
41         echo "ERROR: WS_ROOT is not set to a VPP workspace!"
42         return
43     fi
44
45     options=$(getopt -o "adfg:j:r:" -- "$@")
46     if [ $? -eq 1 ] ; then
47         usage=true
48     else
49         eval set -- $options
50     fi
51     while [ -z "$usage" ] ; do
52         case "$1" in
53             -a)
54                 all="-all"
55                 ;;
56             -d)
57                 debug="-debug"
58                 ;;
59             -f)
60                 is_feature="true"
61                 retry_count=1
62                 ;;
63             -g)
64                 shift
65                 show_grep=$1
66                 grep_for="${1//-/\\-}"
67                 ;;
68             -j)
69                 shift
70                 jobs=$1
71                 if [ $((jobs)) != $jobs ] ; then
72                     echo "ERROR: Invalid option value for -j option ($jobs)!"
73                     usage=true;
74                 fi
75                 ;;
76             -r)
77                 shift
78                 retry_count=$1
79                 if [ $((retry_count)) != $retry_count ] ; then
80                     echo "ERROR: Invalid option value for -r option ($retry_count)!"
81                     usage=true;
82                 fi
83                 ;;
84             --)
85                 shift
86                 break
87                 ;;
88         esac
89         shift
90     done
91
92     if [ -n "$usage" ] || [ -z "$1" ] ; then
93         if [ -z "$1" ] ; then
94             echo "ERROR: no testcase specified!"
95         fi
96         echo "Usage: vpp-make-test [-a][-d][-f][-g <text>][-j <jobs>][-r <retry count>] <testcase> [<retry_count>]"
97         echo "         -a                Run extended tests"
98         echo "         -d                Run vpp debug image (i.e. with ASSERTS)"
99         echo "         -f                Testcase is a feature set (e.g. tcp)"
100         echo "         -g <text>         Text to grep for in log, FAIL on match."
101         echo "                           Enclose <text> in single quotes when it contains any dashes:"
102         echo "                           e.g.  vpp-make-test -g 'goof-bad-' test_xyz"
103         echo "         -j <# jobs>       Set TEST_JOBS (default = auto) for feature set"
104         echo "         -r <retry count>  Retry Count (default = 100 for individual test | 1 for feature set)"
105         return
106     fi
107
108     if [ $retry_count -le 0 ] ; then
109         retry_count=1
110     fi
111     if [ "$is_feature" == "true" ] ; then
112         run_make_test="make test$all$debug TEST=$1 SANITY=no TEST_JOBS=$jobs"
113     else
114         run_make_test="make test$all$debug TEST=*.*.$1 SANITY=no"
115     fi
116
117     old_pwd=$(pwd)
118     cd $WS_ROOT
119     line="------------------------------------------------------------------------------"
120     test_desc="'$run_make_test'"
121     if [ -n "$grep_for" ] ; then
122         test_desc="$test_desc [grep '$show_grep']"
123     fi
124     for ((i=1; i<=retry_count; i++)) ; do
125         echo -e "\n$line"
126         echo -e "ITERATION [$i/$retry_count]: $test_desc\n$line"
127         result=$($run_make_test)
128         if [ ! -d /tmp/vpp-unittest* ] ; then
129             echo -e "\nERROR: No testcase(s) executed!\n"
130             return
131         fi
132         echo "$result"
133         if [ -n "$grep_for" ] ; then
134             grep_results=$(grep -sHn $grep_for /tmp/vpp-u*/log.txt)
135         fi
136         if [ -n "$(echo $result | grep FAILURE)" ] || [ -n "$grep_results" ] ; then
137             if [ -n "$grep_results" ] ; then
138                 fail="FAIL (grep)"
139             else
140                 fail="FAIL"
141             fi
142             echo -e "\n$line\n$fail [$i/$retry_count]: $test_desc\n$line\n"
143             return
144         fi
145     done
146
147     echo -e "\n$line\nPASS [$((i-1))/$retry_count]: $test_desc\n$line\n"
148     echo -e "Hey $tester, Life is good!!! :D\n"
149     cd $old_pwd
150 }
151
152 # bash function to set up csit python virtual environment
153 csit-env()
154 {
155     if [ -f "$WS_ROOT/VPP_REPO_URL" ] && [ -f "$WS_ROOT/requirements.txt" ]; then
156         if [ -n "$(declare -f deactivate)" ]; then
157             echo "Deactivating Python Virtualenv!"
158             deactivate
159         fi
160         local PIP=pip
161         local setup_framework=$WS_ROOT/resources/libraries/python/SetupFramework.py
162         if [ -n "$(grep pip3 $setup_framework)" ]; then
163             PIP=pip3
164             local VENV_OPTS="-p python3"
165         fi
166         export CSIT_DIR=$WS_ROOT
167         export PYTHONPATH=$CSIT_DIR
168         rm -rf $PYTHONPATH/env && virtualenv $VENV_OPTS $PYTHONPATH/env \
169             && source $PYTHONPATH/env/bin/activate \
170             && $PIP install --upgrade -r $PYTHONPATH/requirements.txt \
171             && $PIP install --upgrade -r $PYTHONPATH/tox-requirements.txt
172     else
173         echo "ERROR: WS_ROOT not set to a CSIT workspace!"
174     fi
175 }
176
177 # bash function to set up jenkins sandbox environment
178 #
179 # See LF Sandbox documentation:
180 #   https://docs.releng.linuxfoundation.org/en/latest/jenkins-sandbox.html
181 #
182 # Prerequisites:
183 #   1. Create jenkins sandbox token and add it to your local jenkins.ini file
184 #      Either specify the location of the init file in $JENKINS_INI or
185 #      JENKINS_INI will be initialized to either
186 #         ~/.config/jenkins_jobs/jenkins.ini
187 #         $WS_ROOT/jenkins.ini
188 #   2. Clone ci-management workspace from gerrit.fd.io
189 #   3. export WS_ROOT=<local ci-management workspace>
190 jjb-sandbox-env()
191 {
192     if [ -z "$WS_ROOT" ] ; then
193         echo "ERROR: WS_ROOT is not set!"
194         return
195     elif [ ! -d "$WS_ROOT/jjb" ] ; then
196         echo "ERROR: WS_ROOT is not set to a ci-management workspace!"
197         return
198     fi
199
200     if [ -n "$(declare -f deactivate)" ]; then
201         echo "Deactivating Python Virtualenv!"
202         deactivate
203     fi
204
205     if [ -z "$JENKINS_INI" ] ; then
206         local user_jenkins_ini="/home/$USER/.config/jenkins_jobs/jenkins.ini"
207         if [ -f "$user_jenkins_ini" ] ; then
208             export JENKINS_INI=$user_jenkins_ini
209         elif [ -f "$WS_ROOT/jenkins.ini" ] ; then
210             export JENKINS_INI="$WS_ROOT/jenkins.ini"
211         else
212             echo "ERROR: Unable to find 'jenkins.ini'!"
213             return
214         fi
215         echo "Exporting JENKINS_INI=$JENKINS_INI"
216     elif [ ! -f "$JENKINS_INI" ] ; then
217         echo "ERROR: file specified in JENKINS_INI ($JENKINS_INI) not found!"
218         return
219     fi
220
221     if [ -n "$(declare -f deactivate)" ]; then
222         echo "Deactivating Python Virtualenv!"
223         deactivate
224     fi
225     cd $WS_ROOT
226     git submodule update --init --recursive
227
228     local VENV_DIR=$WS_ROOT/venv
229     rm -rf $VENV_DIR \
230        && python3 -m venv $VENV_DIR \
231        && source $VENV_DIR/bin/activate \
232        && pip3 install wheel \
233        && pip3 install jenkins-job-builder==3.5.0
234
235     alias jjsb='jenkins-jobs --conf $JENKINS_INI'
236     function jjsb-test() {
237         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
238             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
239             return
240         fi
241         if [ -z "$1" ] ; then
242             echo "Usage: $FUNCNAME <jenkins-job-name>"
243             return
244         fi
245         which jenkins-jobs \
246             && jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
247     }
248     function jjsb-update() {
249         if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
250             echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
251             return
252         fi
253         if [ -z "$1" ] ; then
254             echo "Usage: $FUNCNAME <jenkins-job-name>"
255             return
256         fi
257         which jenkins-jobs \
258             && jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
259     }
260     jenkins-jobs --version
261 }