2 # This file and its contents are supplied under the terms of the
3 # Common Development and Distribution License ("CDDL"), version 1.0.
4 # You may only use this file in accordance with the terms of version
7 # A full copy of the text of the CDDL should have accompanied this
8 # source. A copy of the CDDL is also available via the Internet at
9 # http://www.illumos.org/license/CDDL.
13 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
17 # Return 0 if the percentage difference between $a and $b is $percent or
18 # greater. Return 1 if the percentage is lower or if we would divide by
19 # zero. For use like this:
21 # Do $action if the calculated percentage is greater or equal to that passed in:
22 # within_percent A B P && $action
23 # Do $action if the calculated percentage is less than that passed in:
24 # within_percent A B P || $action
26 function within_percent
32 # Set $a or $b to $2 such that a >= b
33 [ 1 -eq $(echo "$2 > $a" | bc) ] && a=$2 || b=$2
35 # Prevent division by 0
36 [[ $a =~ [1-9] ]] || return 1
38 typeset p=$(echo "scale=2; $b * 100 / $a" | bc)
39 log_note "Comparing $a and $b given $percent% (calculated: $p%)"
40 [ 1 -eq $(echo "scale=2; $p >= $percent" | bc) ]
44 # Return 0 if value is within +/-tolerance of target.
45 # Return 1 if value exceeds our tolerance.
48 # Do $action if value is within the tolerance from target passed in:
49 # within_tolerance VAL TAR TOL && $action
50 # Do $action if value surpasses the tolerance from target passed in:
51 # within_tolerance VAL TAR TOL || $action
53 function within_tolerance #value #target #tolerance
59 typeset diff=$((abs(val - target)))
60 log_note "Checking if $val is within +/-$tol of $target (diff: $diff)"
65 # Return 0 if the human readable string of the form <value>[suffix] can
66 # be converted to bytes. Allow suffixes are shown in the table below.
71 typeset value=$(echo "$size" | grep -o '[0-9]\+')
74 *PB|*pb|*P|*p) factor='1024^5' ;;
75 *TB|*tb|*T|*t) factor='1024^4' ;;
76 *GB|*gb|*G|*g) factor='1024^3' ;;
77 *MB|*mb|*M|*m) factor='1024^2' ;;
78 *KB|*kb|*K|*k) factor='1024^1' ;;
79 *B|*b) factor='1024^0' ;;
80 *[!0-9.]*) return 1 ;;
84 echo "$value * ($factor)" | bc
90 # Verify $a is equal to $b, otherwise raise an error specifying
91 # the $type of values being compared
93 function verify_eq # <a> <b> <type>
99 if [[ $a -ne $b ]]; then
100 log_fail "Compared $type should be equal: $a != $b"
105 # Verify $a is not equal to $b, otherwise raise an error specifying
106 # the $type of values being compared
108 function verify_ne # <a> <b> <type>
114 if [[ $a -eq $b ]]; then
115 log_fail "Compared $type should be not equal: $a == $b"
119 # A simple function to get a random number between two bounds (inclusive)
121 # Note since we're using $RANDOM, $min+32767 is the largest number we
122 # can accept as the upper bound.
127 function random_int_between
134 if [[ -z "$3" ]]; then
140 for (( i = 0; i < $count; i++ )); do
141 echo $(( (RANDOM % (max - min + 1)) + min ))