Dave Wallace | d7b9af4 | 2021-01-14 15:03:07 -0500 | [diff] [blame] | 1 | # Copyright (c) 2021 Cisco and/or its affiliates. |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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 | |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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 |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 28 | local show_grep |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 29 | local run_make_test |
| 30 | local old_pwd |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 31 | local test_desc |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 32 | local is_feature="false" |
| 33 | local retry_count=100 |
| 34 | local tester=${GERRIT_USER:-$USER} |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 35 | local jobs="auto" |
Andrew Yourtchenko | be360ee | 2020-04-29 16:48:24 +0000 | [diff] [blame] | 36 | |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 37 | if [ -z "$WS_ROOT" ] ; then |
| 38 | echo "ERROR: WS_ROOT is not set!" |
| 39 | return |
Andrew Yourtchenko | be360ee | 2020-04-29 16:48:24 +0000 | [diff] [blame] | 40 | elif [ ! -d "$WS_ROOT/src/vppinfra" ] ; then |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 41 | echo "ERROR: WS_ROOT is not set to a VPP workspace!" |
| 42 | return |
| 43 | fi |
Andrew Yourtchenko | be360ee | 2020-04-29 16:48:24 +0000 | [diff] [blame] | 44 | |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 45 | options=$(getopt -o "adfg:j:r:" -- "$@") |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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 | ;; |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 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 | ;; |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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 |
Andrew Yourtchenko | be360ee | 2020-04-29 16:48:24 +0000 | [diff] [blame] | 91 | |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 92 | if [ -n "$usage" ] || [ -z "$1" ] ; then |
| 93 | if [ -z "$1" ] ; then |
| 94 | echo "ERROR: no testcase specified!" |
| 95 | fi |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 96 | echo "Usage: vpp-make-test [-a][-d][-f][-g <text>][-j <jobs>][-r <retry count>] <testcase> [<retry_count>]" |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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" |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 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)" |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 105 | return |
| 106 | fi |
| 107 | |
| 108 | if [ $retry_count -le 0 ] ; then |
| 109 | retry_count=1 |
| 110 | fi |
| 111 | if [ "$is_feature" == "true" ] ; then |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 112 | run_make_test="make test$all$debug TEST=$1 SANITY=no TEST_JOBS=$jobs" |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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="------------------------------------------------------------------------------" |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 120 | test_desc="'$run_make_test'" |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 121 | if [ -n "$grep_for" ] ; then |
Dave Wallace | b4f073c | 2019-12-18 14:06:31 -0500 | [diff] [blame] | 122 | test_desc="$test_desc [grep '$show_grep']" |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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 |
Andrew Yourtchenko | be360ee | 2020-04-29 16:48:24 +0000 | [diff] [blame] | 146 | |
Dave Wallace | 535fdba | 2019-11-19 18:49:49 -0500 | [diff] [blame] | 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 | } |
Andrew Yourtchenko | be360ee | 2020-04-29 16:48:24 +0000 | [diff] [blame] | 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 | } |