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' = $(echo "if ($2 > $a) 1 else 0" | 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' = $(echo "scale=2; if ($p >= $percent) 1 else 0" | bc) ]] && \
47 # Return 0 if value is within +/-tolerance of target.
48 # Return 1 if value exceeds our tolerance.
51 # Do $action if value is within the tolerance from target passed in:
52 # within_tolerance VAL TAR TOL && $action
53 # Do $action if value surpasses the tolerance from target passed in:
54 # within_tolerance VAL TAR TOL || $action
56 function within_tolerance #value #target #tolerance
62 typeset diff=$((abs(val - target)))
63 log_note "Checking if $val is within +/-$tol of $target (diff: $diff)"
64 ((diff <= tol)) && return 0
70 # Return 0 if the human readable string of the form <value>[suffix] can
71 # be converted to bytes. Allow suffixes are shown in the table below.
76 typeset value=$(echo "$size" | grep -o '[0-9]\+')
79 *PB|*pb|*P|*p) factor='1024^5' ;;
80 *TB|*tb|*T|*t) factor='1024^4' ;;
81 *GB|*gb|*G|*g) factor='1024^3' ;;
82 *MB|*mb|*M|*m) factor='1024^2' ;;
83 *KB|*kb|*K|*k) factor='1024^1' ;;
84 *B|*b) factor='1024^0' ;;
85 *[!0-9.]*) return 1 ;;
89 echo "$value * ($factor)" | bc
95 # Verify $a is equal to $b, otherwise raise an error specifying
96 # the $type of values being compared
98 function verify_eq # <a> <b> <type>
104 if [[ $a -ne $b ]]; then
105 log_fail "Compared $type should be equal: $a != $b"
110 # Verify $a is not equal to $b, otherwise raise an error specifying
111 # the $type of values being compared
113 function verify_ne # <a> <b> <type>
119 if [[ $a -eq $b ]]; then
120 log_fail "Compared $type should be not equal: $a == $b"
124 # A simple function to get a random number between two bounds (inclusive)
126 # Probably not the most efficient for large ranges, but it's okay.
128 # Note since we're using $RANDOM, 32767 is the largest number we
129 # can accept as the upper bound.
133 function random_int_between
139 while [[ $rand -lt $min ]] ; do
140 rand=$(( $RANDOM % $max + 1))