blob: 66658cdda9d35f3c8d0bcc0401a5beb1df21cfdb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#
# 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.
#
#
# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
#
#
# 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
[[ '1' = $(echo "if ($2 > $a) 1" | bc) ]] && a=$2 || b=$2
# Prevent division by 0
[[ $a =~ [1-9] ]] || return 1
typeset p=$(echo "scale=2; $b * 100 / $a" | bc)
log_note "Comparing $a and $b given $percent% (calculated: $p%)"
[[ '1' = $(echo "scale=2; if ($p >= $percent) 1" | bc) ]] && return 0
return 1
}
#
# 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
}
#
# 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
}
|