functions.bash revision 535fdba5
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
19# Bash function to run vpp 'make test' testcases
20# repeatedly, stopping on test failure or when
21# a test log contains the optionally specified text
22vpp-make-test()
23{
24    local options
25    local usage
26    local all
27    local debug
28    local grep_for
29    local run_make_test
30    local old_pwd
31    local is_feature="false"
32    local retry_count=100
33    local tester=${GERRIT_USER:-$USER}
34    
35    if [ -z "$WS_ROOT" ] ; then
36        echo "ERROR: WS_ROOT is not set!"
37        return
38    elif [ -z "$(find $WS_ROOT -type d -name vppinfra)" ] ; then
39        echo "ERROR: WS_ROOT is not set to a VPP workspace!"
40        return
41    fi
42    
43    options=$(getopt -o "adfg:r:" -- "$@")
44    if [ $? -eq 1 ] ; then
45        usage=true
46    else
47        eval set -- $options
48    fi
49    while [ -z "$usage" ] ; do
50        case "$1" in
51            -a)
52                all="-all"
53                ;;
54            -d)
55                debug="-debug"
56                ;;
57            -f)
58                is_feature="true"
59                retry_count=1
60                ;;
61            -g)
62                shift
63                show_grep=$1
64                grep_for="${1//-/\\-}"
65                ;;
66            -r)
67                shift
68                retry_count=$1
69                if [ $((retry_count)) != $retry_count ] ; then
70                    echo "ERROR: Invalid option value for -r option ($retry_count)!"
71                    usage=true;
72                fi
73                ;;
74            --)
75                shift
76                break
77                ;;
78        esac
79        shift
80    done
81    
82    if [ -n "$usage" ] || [ -z "$1" ] ; then
83        if [ -z "$1" ] ; then
84            echo "ERROR: no testcase specified!"
85        fi
86        echo "Usage: vpp-make-test [-a][-d][-f][-g <text>][-r <retry count>] <testcase> [<retry_count>]"
87        echo "         -a                Run extended tests"
88        echo "         -d                Run vpp debug image (i.e. with ASSERTS)"
89        echo "         -f                Testcase is a feature set (e.g. tcp)"
90        echo "         -g <text>         Text to grep for in log, FAIL on match."
91        echo "                           Enclose <text> in single quotes when it contains any dashes:"
92        echo "                           e.g.  vpp-make-test -g 'goof-bad-' test_xyz"
93        echo "         -r <retry count>  Retry Count (default = 100 for individual | 1 for feature)"
94        return
95    fi
96
97    if [ $retry_count -le 0 ] ; then
98        retry_count=1
99    fi
100    if [ "$is_feature" == "true" ] ; then
101        run_make_test="make test$all$debug TEST=$1 SANITY=no TEST_JOBS=auto"
102    else
103        run_make_test="make test$all$debug TEST=*.*.$1 SANITY=no"
104    fi
105
106    old_pwd=$(pwd)
107    cd $WS_ROOT
108    line="------------------------------------------------------------------------------"
109    local test_desc="'$run_make_test'"
110    if [ -n "$grep_for" ] ; then
111        test_desc="$test_desc [grep $show_grep]"
112    fi
113    for ((i=1; i<=retry_count; i++)) ; do
114        echo -e "\n$line"
115        echo -e "ITERATION [$i/$retry_count]: $test_desc\n$line"
116        result=$($run_make_test)
117        if [ ! -d /tmp/vpp-unittest* ] ; then
118            echo -e "\nERROR: No testcase(s) executed!\n"
119            return
120        fi
121        echo "$result"
122        if [ -n "$grep_for" ] ; then
123            grep_results=$(grep -sHn $grep_for /tmp/vpp-u*/log.txt)
124        fi
125        if [ -n "$(echo $result | grep FAILURE)" ] || [ -n "$grep_results" ] ; then
126            if [ -n "$grep_results" ] ; then
127                fail="FAIL (grep)"
128            else
129                fail="FAIL"
130            fi
131            echo -e "\n$line\n$fail [$i/$retry_count]: $test_desc\n$line\n"
132            return
133        fi
134    done
135    
136    echo -e "\n$line\nPASS [$((i-1))/$retry_count]: $test_desc\n$line\n"
137    echo -e "Hey $tester, Life is good!!! :D\n"
138    cd $old_pwd
139}
140export -f vpp-make-test
141