andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
| 3 | # Executes a command on the given ONOS instance and matches the output |
Dimitrios Mavrommatis | 30675ca | 2017-11-13 18:26:53 -0800 | [diff] [blame] | 4 | # to the passed after X retries. |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 5 | # First argument is the IP address of the machine to run the command on, |
| 6 | # then you pass the command and it's arguments if needed, then --expect and |
Dimitrios Mavrommatis | 30675ca | 2017-11-13 18:26:53 -0800 | [diff] [blame] | 7 | # after it the string of what the output should be or --retry and the maximum |
| 8 | # retries with 5 seconds delay between them |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 9 | # Example: |
Dimitrios Mavrommatis | 30675ca | 2017-11-13 18:26:53 -0800 | [diff] [blame] | 10 | # onos-execute-expect 1.1.1.1 fooCommand fooParamenter --retry 5 --expect fooOutputString |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 11 | # ----------------------------------------------------------------------------- |
| 12 | |
| 13 | [ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1 |
| 14 | . $ONOS_ROOT/tools/build/envDefaults |
| 15 | |
Jon Hall | fb6009d | 2017-02-15 16:01:17 -0800 | [diff] [blame] | 16 | aux=/tmp/stc/stc-$$.log |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 17 | trap "rm -f $aux 2>/dev/null" EXIT |
Dimitrios Mavrommatis | 30675ca | 2017-11-13 18:26:53 -0800 | [diff] [blame] | 18 | |
| 19 | retry=0 |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 20 | cmd="" |
Dimitrios Mavrommatis | 30675ca | 2017-11-13 18:26:53 -0800 | [diff] [blame] | 21 | expct="" |
| 22 | |
| 23 | for ((i=1; i<=$#; i++)) |
| 24 | do |
| 25 | if [ "${!i}" = "--expect" ]; then |
| 26 | next=$((i+1)) |
| 27 | expct="${!next}" |
| 28 | i=$i+1 |
| 29 | elif [ "${!i}" = "--retry" ]; then |
| 30 | next=$((i+1)) |
| 31 | retry="${!next}" |
| 32 | i=$i+1 |
| 33 | else |
| 34 | cmd="$cmd ${!i}"; |
| 35 | fi |
| 36 | done |
| 37 | |
| 38 | until [ $retry -eq -1 ] |
| 39 | do |
| 40 | onos $cmd > $aux |
| 41 | cat $aux |
| 42 | grep -q $expct $aux && echo "expected value found" && exit 0 |
| 43 | retry=$[$retry-1] |
| 44 | sleep 5 |
| 45 | done |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 46 | exit 1 |