9faf1049c2b89b62e1026f2a819335be4f074a2f
[vpp.git] / extras / bash / functions.bash
1 # Copyright (c) 2021 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 grep_results
33     local result
34     local fail
35     local i
36     local line
37     local is_feature="false"
38     local retry_count=100
39     local tester=${GERRIT_USER:-$USER}
40     local jobs="auto"
41
42     if [ -z "$WS_ROOT" ] ; then
43         if [ -d "./extras/bash" ] ; then
44             export WS_ROOT="$(pwd)"
45         else
46             echo "ERROR: WS_ROOT is not set!"
47             return
48         fi
49     elif [ ! -d "$WS_ROOT/src/vppinfra" ] ; then
50         echo "ERROR: WS_ROOT is not set to a VPP workspace!"
51         return
52     fi
53
54     options=$(getopt -o "adfg:j:r:" -- "$@")
55     if [ $? -eq 1 ] ; then
56         usage=true
57     else
58         eval set -- $options
59     fi
60     while [ -z "$usage" ] ; do
61         case "$1" in
62             -a)
63                 all="-all"
64                 ;;
65             -d)
66                 debug="-debug"
67                 ;;
68             -f)
69                 is_feature="true"
70                 retry_count=1
71                 ;;
72             -g)
73                 shift
74                 show_grep=$1
75                 grep_for="${1//-/\\-}"
76                 ;;
77             -j)
78                 shift
79                 jobs=$1
80                 if [ $((jobs)) != $jobs ] ; then
81                     echo "ERROR: Invalid option value for -j option ($jobs)!"
82                     usage=true;
83                 fi
84                 ;;
85             -r)
86                 shift
87                 retry_count=$1
88                 if [ $((retry_count)) != $retry_count ] ; then
89                     echo "ERROR: Invalid option value for -r option ($retry_count)!"
90                     usage=true;
91                 fi
92                 ;;
93             --)
94                 shift
95                 break
96                 ;;
97         esac
98         shift
99     done
100
101     if [ -n "$usage" ] || [ -z "$1" ] ; then
102         if [ -z "$1" ] ; then
103             echo "ERROR: no testcase specified!"
104         fi
105         echo "Usage: vpp-make-test [-a][-d][-f][-g <text>][-j <jobs>][-r <retry count>] <testcase> [<retry_count>]"
106         echo "         -a                Run extended tests"
107         echo "         -d                Run vpp debug image (i.e. with ASSERTS)"
108         echo "         -f                Testcase is a feature set (e.g. tcp)"
109         echo "         -g <text>         Text to grep for in log, FAIL on match."
110         echo "                           Enclose <text> in single quotes when it contains any dashes:"
111         echo "                           e.g.  vpp-make-test -g 'goof-bad-' test_xyz"
112         echo "         -j <# jobs>       Set TEST_JOBS (default = auto) for feature set"
113         echo "         -r <retry count>  Retry Count (default = 100 for individual test | 1 for feature set)"
114         return
115     fi
116
117     if [ $retry_count -le 0 ] ; then
118         retry_count=1
119     fi
120     if [ "$is_feature" == "true" ] ; then
121         run_make_test="make test$all$debug TEST=$1 SANITY=no TEST_JOBS=$jobs"
122     else
123         run_make_test="make test$all$debug TEST=*.*.$1 SANITY=no"
124     fi
125
126     old_pwd=$(pwd)
127     cd $WS_ROOT
128     line="------------------------------------------------------------------------------"
129     test_desc="'$run_make_test'"
130     if [ -n "$grep_for" ] ; then
131         test_desc="$test_desc [grep '$show_grep']"
132     fi
133     for ((i=1; i<=retry_count; i++)) ; do
134         echo -e "\n$line"
135         echo -e "ITERATION [$i/$retry_count]: $test_desc\n$line"
136         result=$($run_make_test)
137         if [ ! -d /tmp/vpp-unittest* ] ; then
138             echo -e "\nERROR: No testcase(s) executed!\n"
139             return
140         fi
141         echo "$result"
142         if [ -n "$grep_for" ] ; then
143             grep_results=$(grep -sHn $grep_for /tmp/vpp-u*/log.txt)
144         fi
145         if [ -n "$(echo $result | grep FAILURE)" ] || [ -n "$grep_results" ] ; then
146             if [ -n "$grep_results" ] ; then
147                 fail="FAIL (grep)"
148             else
149                 fail="FAIL"
150             fi
151             echo -e "\n$line\n$fail [$i/$retry_count]: $test_desc\n$line\n"
152             return
153         fi
154     done
155
156     echo -e "\n$line\nPASS [$((i-1))/$retry_count]: $test_desc\n$line\n"
157     echo -e "Hey $tester, Life is good!!! :D\n"
158     cd $old_pwd
159 }
160
161 # bash function to set up csit python virtual environment
162 csit-env()
163 {
164     if [ -f "$WS_ROOT/VPP_REPO_URL" ] && [ -f "$WS_ROOT/requirements.txt" ]; then
165         if [ -n "$(declare -f deactivate)" ]; then
166             echo "Deactivating Python Virtualenv!"
167             deactivate
168         fi
169         local PIP=pip
170         local setup_framework=$WS_ROOT/resources/libraries/python/SetupFramework.py
171         if [ -n "$(grep pip3 $setup_framework)" ]; then
172             PIP=pip3
173             local VENV_OPTS="-p python3"
174         fi
175         export CSIT_DIR=$WS_ROOT
176         export PYTHONPATH=$CSIT_DIR
177         rm -rf $PYTHONPATH/env && virtualenv $VENV_OPTS $PYTHONPATH/env \
178             && source $PYTHONPATH/env/bin/activate \
179             && $PIP install --upgrade -r $PYTHONPATH/requirements.txt \
180             && $PIP install --upgrade -r $PYTHONPATH/tox-requirements.txt
181     else
182         echo "ERROR: WS_ROOT not set to a CSIT workspace!"
183     fi
184 }