X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=tests%2Ffunctions-common;h=53a620b0435d7ebbf0099ddb53ab832b0e043f12;hb=9fd701278bf7d8c5b9451491bcb1a92580adc780;hp=08d489e5d59a94afd1262ab94b772e28c0aa0f8d;hpb=c9d0d2c9fc16f2e989444bc4d7d8fab8060433ba;p=doctor.git diff --git a/tests/functions-common b/tests/functions-common index 08d489e5..53a620b0 100644 --- a/tests/functions-common +++ b/tests/functions-common @@ -4,7 +4,7 @@ # is_set env-var function is_set { local var=\$"$1" - eval "[ -n \"$var\" ]" + eval "[[ -n \"$var\" ]]" } # Prints backtrace info @@ -41,6 +41,8 @@ function err { # die $LINENO "message" function die { local exitcode=$? + local xtrace + xtrace=$(set +o | grep xtrace) set +o xtrace local line=$1; shift if [ $exitcode == 0 ]; then @@ -50,6 +52,7 @@ function die { err $line "$*" # Give buffers a second to flush sleep 1 + $xtrace exit $exitcode } @@ -70,6 +73,20 @@ function die_if_not_set { $xtrace } +# Check the function is defined +# die_if_not_defined $LINENO function-name "message" +function die_if_not_defined { + local xtrace + xtrace=$(set +o | grep xtrace) + set +o xtrace + local line=$1; shift + local func_name=$1; shift + if ! declare -f "$func_name" > /dev/null; then + die $line "$*" + fi + $xtrace +} + # Wait until the condition is met. # wait_until condition timeout interval function wait_until { @@ -87,3 +104,17 @@ function wait_until { fi done } + +# Print IP address of the first vNIC owned by specified VM via virsh +# get_first_vnic_ip vm_name +function get_first_vnic_ip { + local vm_name=$1 + + _vnic_mac=$(sudo virsh domiflist $vm_name | \ + sed -n -e 's/^.*\([0-9a-f]\{2\}\(:[0-9a-f]\{2\}\)\{5\}\).*$/\1/p' | \ + head -1) + die_if_not_set $LINENO _vnic_mac + _vnic_ip=$(arp -e | grep $_vnic_mac | awk '{print $1}') + die_if_not_set $LINENO _vnic_ip + echo $_vnic_ip +}