Merge "support pep8 check"
[doctor.git] / tests / functions-common
1 #!/bin/bash
2
3 # Test if the named environment variable is set and not zero length
4 # is_set env-var
5 function is_set {
6     local var=\$"$1"
7     eval "[[ -n \"$var\" ]]"
8 }
9
10 # Prints backtrace info
11 # filename:lineno:function
12 # backtrace level
13 function backtrace {
14     local level=$1
15     local deep
16     deep=$((${#BASH_SOURCE[@]} - 1))
17     echo "[Call Trace]"
18     while [ $level -le $deep ]; do
19         echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
20         deep=$((deep - 1))
21     done
22 }
23
24 # Prints line number and "message" in error format
25 # err $LINENO "message"
26 function err {
27     local exitcode=$?
28     local xtrace
29     xtrace=$(set +o | grep xtrace)
30     set +o xtrace
31     local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
32     echo $msg 1>&2;
33     if [[ -n ${LOGDIR} ]]; then
34         echo $msg >> "${LOGDIR}/error.log"
35     fi
36     $xtrace
37     return $exitcode
38 }
39
40 # Prints line number and "message" then exits
41 # die $LINENO "message"
42 function die {
43     local exitcode=$?
44     local xtrace
45     xtrace=$(set +o | grep xtrace)
46     set +o xtrace
47     local line=$1; shift
48     if [ $exitcode == 0 ]; then
49         exitcode=1
50     fi
51     backtrace 2
52     err $line "$*"
53     # Give buffers a second to flush
54     sleep 1
55     $xtrace
56     exit $exitcode
57 }
58
59 # Checks an environment variable is not set or has length 0 OR if the
60 # exit code is non-zero and prints "message" and exits
61 # NOTE: env-var is the variable name without a '$'
62 # die_if_not_set $LINENO env-var "message"
63 function die_if_not_set {
64     local exitcode=$?
65     local xtrace
66     xtrace=$(set +o | grep xtrace)
67     set +o xtrace
68     local line=$1; shift
69     local evar=$1; shift
70     if ! is_set $evar || [ $exitcode != 0 ]; then
71         die $line "$*"
72     fi
73     $xtrace
74 }
75
76 # Check the function is defined
77 # die_if_not_defined $LINENO function-name "message"
78 function die_if_not_defined {
79     local xtrace
80     xtrace=$(set +o | grep xtrace)
81     set +o xtrace
82     local line=$1; shift
83     local func_name=$1; shift
84     if ! declare -f "$func_name" > /dev/null; then
85         die $line "$*"
86     fi
87     $xtrace
88 }
89
90 # Wait until the condition is met.
91 # wait_until condition timeout interval
92 function wait_until {
93     local condition="$1"
94     local timeout=$2
95     local interval=$3
96
97     while eval ${condition}
98     do
99         sleep ${interval}
100         timeout=$((${timeout} - ${interval}))
101         if [[ ${timeout} < 0 ]]; then
102             err $LINENO "timed out ($condition)..."
103             return 1
104         fi
105     done
106 }
107
108 # Print IP address of the first vNIC owned by specified VM via virsh
109 # get_first_vnic_ip vm_name
110 function get_first_vnic_ip {
111     local vm_name=$1
112
113     _vnic_mac=$(sudo virsh domiflist $vm_name | \
114         sed -n -e 's/^.*\([0-9a-f]\{2\}\(:[0-9a-f]\{2\}\)\{5\}\).*$/\1/p' | \
115         head -1)
116     die_if_not_set $LINENO _vnic_mac
117     _vnic_ip=$(arp -e | grep $_vnic_mac | awk '{print $1}')
118     die_if_not_set $LINENO _vnic_ip
119     echo $_vnic_ip
120 }