2015-07-01 22:23:09 +00:00
|
|
|
#
|
|
|
|
# This file and its contents are supplied under the terms of the
|
|
|
|
# Common Development and Distribution License ("CDDL"), version 1.0.
|
|
|
|
# You may only use this file in accordance with the terms of version
|
|
|
|
# 1.0 of the CDDL.
|
|
|
|
#
|
|
|
|
# A full copy of the text of the CDDL should have accompanied this
|
|
|
|
# source. A copy of the CDDL is also available via the Internet at
|
|
|
|
# http://www.illumos.org/license/CDDL.
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
2017-04-06 00:18:22 +00:00
|
|
|
# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
|
2015-07-01 22:23:09 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Return 0 if the percentage difference between $a and $b is $percent or
|
|
|
|
# greater. Return 1 if the percentage is lower or if we would divide by
|
|
|
|
# zero. For use like this:
|
|
|
|
#
|
|
|
|
# Do $action if the calculated percentage is greater or equal to that passed in:
|
|
|
|
# within_percent A B P && $action
|
|
|
|
# Do $action if the calculated percentage is less than that passed in:
|
|
|
|
# within_percent A B P || $action
|
|
|
|
#
|
|
|
|
function within_percent
|
|
|
|
{
|
|
|
|
typeset a=$1
|
|
|
|
typeset b=$1
|
|
|
|
typeset percent=$3
|
|
|
|
|
|
|
|
# Set $a or $b to $2 such that a >= b
|
2022-03-23 16:54:07 +00:00
|
|
|
[ 1 -eq $(echo "$2 > $a" | bc) ] && a=$2 || b=$2
|
2015-07-01 22:23:09 +00:00
|
|
|
|
|
|
|
# Prevent division by 0
|
|
|
|
[[ $a =~ [1-9] ]] || return 1
|
|
|
|
|
2017-04-06 00:18:22 +00:00
|
|
|
typeset p=$(echo "scale=2; $b * 100 / $a" | bc)
|
2015-07-01 22:23:09 +00:00
|
|
|
log_note "Comparing $a and $b given $percent% (calculated: $p%)"
|
2022-03-23 16:54:07 +00:00
|
|
|
[ 1 -eq $(echo "scale=2; $p >= $percent" | bc) ]
|
2015-07-01 22:23:09 +00:00
|
|
|
}
|
2017-04-11 21:56:54 +00:00
|
|
|
|
2018-03-07 17:53:04 +00:00
|
|
|
#
|
|
|
|
# Return 0 if value is within +/-tolerance of target.
|
|
|
|
# Return 1 if value exceeds our tolerance.
|
|
|
|
# For use like this:
|
|
|
|
#
|
|
|
|
# Do $action if value is within the tolerance from target passed in:
|
|
|
|
# within_tolerance VAL TAR TOL && $action
|
|
|
|
# Do $action if value surpasses the tolerance from target passed in:
|
|
|
|
# within_tolerance VAL TAR TOL || $action
|
|
|
|
#
|
|
|
|
function within_tolerance #value #target #tolerance
|
|
|
|
{
|
|
|
|
typeset val=$1
|
|
|
|
typeset target=$2
|
|
|
|
typeset tol=$3
|
|
|
|
|
|
|
|
typeset diff=$((abs(val - target)))
|
|
|
|
log_note "Checking if $val is within +/-$tol of $target (diff: $diff)"
|
2022-03-23 16:54:07 +00:00
|
|
|
((diff <= tol))
|
2018-03-07 17:53:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 21:56:54 +00:00
|
|
|
#
|
|
|
|
# Return 0 if the human readable string of the form <value>[suffix] can
|
|
|
|
# be converted to bytes. Allow suffixes are shown in the table below.
|
|
|
|
#
|
|
|
|
function to_bytes
|
|
|
|
{
|
|
|
|
typeset size=$1
|
|
|
|
typeset value=$(echo "$size" | grep -o '[0-9]\+')
|
|
|
|
|
|
|
|
case $size in
|
|
|
|
*PB|*pb|*P|*p) factor='1024^5' ;;
|
|
|
|
*TB|*tb|*T|*t) factor='1024^4' ;;
|
|
|
|
*GB|*gb|*G|*g) factor='1024^3' ;;
|
|
|
|
*MB|*mb|*M|*m) factor='1024^2' ;;
|
|
|
|
*KB|*kb|*K|*k) factor='1024^1' ;;
|
|
|
|
*B|*b) factor='1024^0' ;;
|
|
|
|
*[!0-9.]*) return 1 ;;
|
|
|
|
*) factor='1024^0' ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
echo "$value * ($factor)" | bc
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2017-11-13 17:24:26 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Verify $a is equal to $b, otherwise raise an error specifying
|
|
|
|
# the $type of values being compared
|
|
|
|
#
|
|
|
|
function verify_eq # <a> <b> <type>
|
|
|
|
{
|
|
|
|
typeset a=$1
|
|
|
|
typeset b=$2
|
|
|
|
typeset type=$3
|
|
|
|
|
|
|
|
if [[ $a -ne $b ]]; then
|
|
|
|
log_fail "Compared $type should be equal: $a != $b"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Verify $a is not equal to $b, otherwise raise an error specifying
|
|
|
|
# the $type of values being compared
|
|
|
|
#
|
|
|
|
function verify_ne # <a> <b> <type>
|
|
|
|
{
|
|
|
|
typeset a=$1
|
|
|
|
typeset b=$2
|
|
|
|
typeset type=$3
|
|
|
|
|
|
|
|
if [[ $a -eq $b ]]; then
|
|
|
|
log_fail "Compared $type should be not equal: $a == $b"
|
|
|
|
fi
|
|
|
|
}
|
2020-01-08 17:08:30 +00:00
|
|
|
|
|
|
|
# A simple function to get a random number between two bounds (inclusive)
|
|
|
|
#
|
2023-05-05 16:09:12 +00:00
|
|
|
# Note since we're using $RANDOM, $min+32767 is the largest number we
|
2020-01-08 17:08:30 +00:00
|
|
|
# can accept as the upper bound.
|
|
|
|
#
|
|
|
|
# $1 lower bound
|
|
|
|
# $2 upper bound
|
2023-12-26 20:01:53 +00:00
|
|
|
# [$3 how many]
|
2020-01-08 17:08:30 +00:00
|
|
|
function random_int_between
|
|
|
|
{
|
|
|
|
typeset -i min=$1
|
|
|
|
typeset -i max=$2
|
2023-12-26 20:01:53 +00:00
|
|
|
typeset -i count
|
|
|
|
typeset -i i
|
2020-01-08 17:08:30 +00:00
|
|
|
|
2023-12-26 20:01:53 +00:00
|
|
|
if [[ -z "$3" ]]; then
|
|
|
|
count=1
|
|
|
|
else
|
|
|
|
count=$3
|
|
|
|
fi
|
|
|
|
|
|
|
|
for (( i = 0; i < $count; i++ )); do
|
|
|
|
echo $(( (RANDOM % (max - min + 1)) + min ))
|
|
|
|
done
|
2020-01-08 17:08:30 +00:00
|
|
|
}
|