From f8d55b95a5ed825d4fb50cc8e0e03edf25bd44e3 Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Wed, 8 Jan 2020 12:08:30 -0500 Subject: [PATCH] ZTS: Eliminate functions named 'random' The name overlaps with a command needed by FreeBSD. There is also no sense having two 'random' functions that do nearly the same thing, so consolidate to just the more general one and name it 'random_int_between'. Reviewed-by: John Kennedy Reviewed-by: Kjeld Schouten Reviewed-by: Igor Kozhukhov Reviewed-by: Brian Behlendorf Reviewed-by: George Melikov Signed-off-by: Ryan Moeller Closes #9820 --- tests/zfs-tests/include/libtest.shlib | 9 -------- tests/zfs-tests/include/math.shlib | 22 +++++++++++++++++++ .../cli_root/zfs_diff/zfs_diff_timestamp.ksh | 2 +- .../zpool_upgrade/zpool_upgrade.kshlib | 19 ---------------- .../zpool_upgrade/zpool_upgrade_008_pos.ksh | 3 ++- .../functional/redacted_send/redacted.kshlib | 2 +- .../redundancy/redundancy_001_pos.ksh | 3 ++- .../redundancy/redundancy_002_pos.ksh | 3 ++- .../redundancy/redundancy_003_pos.ksh | 3 ++- .../redundancy/redundancy_004_neg.ksh | 3 ++- 10 files changed, 34 insertions(+), 35 deletions(-) diff --git a/tests/zfs-tests/include/libtest.shlib b/tests/zfs-tests/include/libtest.shlib index 7e9dd52f11..e2274d1b17 100644 --- a/tests/zfs-tests/include/libtest.shlib +++ b/tests/zfs-tests/include/libtest.shlib @@ -3349,15 +3349,6 @@ function get_min echo $min } -# -# Generate a random number between 1 and the argument. -# -function random -{ - typeset max=$1 - echo $(( ($RANDOM % $max) + 1 )) -} - # Write data that can be compressed into a directory function write_compressible { diff --git a/tests/zfs-tests/include/math.shlib b/tests/zfs-tests/include/math.shlib index 0c3508ec2f..692a2a45ba 100644 --- a/tests/zfs-tests/include/math.shlib +++ b/tests/zfs-tests/include/math.shlib @@ -119,3 +119,25 @@ function verify_ne # log_fail "Compared $type should be not equal: $a == $b" fi } + +# A simple function to get a random number between two bounds (inclusive) +# +# Probably not the most efficient for large ranges, but it's okay. +# +# Note since we're using $RANDOM, 32767 is the largest number we +# can accept as the upper bound. +# +# $1 lower bound +# $2 upper bound +function random_int_between +{ + typeset -i min=$1 + typeset -i max=$2 + typeset -i rand=0 + + while [[ $rand -lt $min ]] ; do + rand=$(( $RANDOM % $max + 1)) + done + + echo $rand +} diff --git a/tests/zfs-tests/tests/functional/cli_root/zfs_diff/zfs_diff_timestamp.ksh b/tests/zfs-tests/tests/functional/cli_root/zfs_diff/zfs_diff_timestamp.ksh index 5efaff8cd5..a4cedca49c 100755 --- a/tests/zfs-tests/tests/functional/cli_root/zfs_diff/zfs_diff_timestamp.ksh +++ b/tests/zfs-tests/tests/functional/cli_root/zfs_diff/zfs_diff_timestamp.ksh @@ -50,7 +50,7 @@ function create_random # while (( i < count )); do log_must touch "$fspath/file$i" - sleep $(random 3) + sleep $(random_int_between 1 3) (( i = i + 1 )) done } diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib b/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib index 7b018da1b6..783ae54e71 100644 --- a/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib @@ -138,22 +138,3 @@ function check_poolversion log_fail "$pool: zpool reported version $actual, expected $vers" fi } - -# A simple function to get a random number between two bounds -# probably not the most efficient for large ranges, but it's okay. -# Note since we're using $RANDOM, 32767 is the largest number we -# can accept as the upper bound. -# $1 lower bound -# $2 upper bound -function random -{ - typeset min=$1 - typeset max=$2 - typeset rand=0 - - while [[ $rand -lt $min ]] ; do - rand=$(( $RANDOM % $max + 1)) - done - - echo $rand -} diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade_008_pos.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade_008_pos.ksh index 173d7f68c8..d930919652 100755 --- a/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade_008_pos.ksh +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade_008_pos.ksh @@ -30,6 +30,7 @@ # Copyright 2015 Nexenta Systems, Inc. All rights reserved. # +. $STF_SUITE/include/libtest.shlib . $STF_SUITE/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib # @@ -67,7 +68,7 @@ MAX_VER=15 for ver_old in $VERSIONS; do typeset -n pool_name=ZPOOL_VERSION_${ver_old}_NAME - typeset ver_new=$(random $ver_old $MAX_VER) + typeset -i ver_new=$(random_int_between $ver_old $MAX_VER) create_old_pool $ver_old log_must zpool upgrade -V $ver_new $pool_name > /dev/null diff --git a/tests/zfs-tests/tests/functional/redacted_send/redacted.kshlib b/tests/zfs-tests/tests/functional/redacted_send/redacted.kshlib index d3d219bb9b..5150ec5d8b 100644 --- a/tests/zfs-tests/tests/functional/redacted_send/redacted.kshlib +++ b/tests/zfs-tests/tests/functional/redacted_send/redacted.kshlib @@ -100,7 +100,7 @@ function setup_holes log_must zfs create $sendfs/manyrm for i in {1..256}; do log_must stride_dd -i /dev/urandom -o $mntpnt/manyrm/f$i -b 512 \ - -c $(random 100) -s $(random 4) + -c $(random_int_between 1 100) -s $(random_int_between 1 4) done log_must zfs snapshot $sendfs/manyrm@snap diff --git a/tests/zfs-tests/tests/functional/redundancy/redundancy_001_pos.ksh b/tests/zfs-tests/tests/functional/redundancy/redundancy_001_pos.ksh index b5557f1f7e..90d14f6001 100755 --- a/tests/zfs-tests/tests/functional/redundancy/redundancy_001_pos.ksh +++ b/tests/zfs-tests/tests/functional/redundancy/redundancy_001_pos.ksh @@ -29,6 +29,7 @@ # Copyright (c) 2013 by Delphix. All rights reserved. # +. $STF_SUITE/include/libtest.shlib . $STF_SUITE/tests/functional/redundancy/redundancy.kshlib # @@ -50,7 +51,7 @@ verify_runnable "global" log_assert "Verify raidz pool can withstand one device is failing." log_onexit cleanup -typeset -i cnt=$(random 2 5) +typeset -i cnt=$(random_int_between 2 5) setup_test_env $TESTPOOL raidz $cnt # diff --git a/tests/zfs-tests/tests/functional/redundancy/redundancy_002_pos.ksh b/tests/zfs-tests/tests/functional/redundancy/redundancy_002_pos.ksh index b16687dbe8..74bda19990 100755 --- a/tests/zfs-tests/tests/functional/redundancy/redundancy_002_pos.ksh +++ b/tests/zfs-tests/tests/functional/redundancy/redundancy_002_pos.ksh @@ -29,6 +29,7 @@ # Copyright (c) 2013 by Delphix. All rights reserved. # +. $STF_SUITE/include/libtest.shlib . $STF_SUITE/tests/functional/redundancy/redundancy.kshlib # @@ -50,7 +51,7 @@ verify_runnable "global" log_assert "Verify raidz2 pool can withstand two devices are failing." log_onexit cleanup -typeset -i cnt=$(random 3 5) +typeset -i cnt=$(random_int_between 3 5) setup_test_env $TESTPOOL raidz2 $cnt # diff --git a/tests/zfs-tests/tests/functional/redundancy/redundancy_003_pos.ksh b/tests/zfs-tests/tests/functional/redundancy/redundancy_003_pos.ksh index a1ca2cb765..b7b791b248 100755 --- a/tests/zfs-tests/tests/functional/redundancy/redundancy_003_pos.ksh +++ b/tests/zfs-tests/tests/functional/redundancy/redundancy_003_pos.ksh @@ -29,6 +29,7 @@ # Copyright (c) 2013 by Delphix. All rights reserved. # +. $STF_SUITE/include/libtest.shlib . $STF_SUITE/tests/functional/redundancy/redundancy.kshlib # @@ -50,7 +51,7 @@ verify_runnable "global" log_assert "Verify mirrored pool can withstand N-1 devices are failing or missing." log_onexit cleanup -typeset -i cnt=$(random 2 5) +typeset -i cnt=$(random_int_between 2 5) setup_test_env $TESTPOOL mirror $cnt typeset -i i=1 diff --git a/tests/zfs-tests/tests/functional/redundancy/redundancy_004_neg.ksh b/tests/zfs-tests/tests/functional/redundancy/redundancy_004_neg.ksh index cb4603af80..7ee51051ea 100755 --- a/tests/zfs-tests/tests/functional/redundancy/redundancy_004_neg.ksh +++ b/tests/zfs-tests/tests/functional/redundancy/redundancy_004_neg.ksh @@ -29,6 +29,7 @@ # Copyright (c) 2013, 2016 by Delphix. All rights reserved. # +. $STF_SUITE/include/libtest.shlib . $STF_SUITE/tests/functional/redundancy/redundancy.kshlib # @@ -50,7 +51,7 @@ verify_runnable "global" log_assert "Verify striped pool have no data redundancy." log_onexit cleanup -typeset -i cnt=$(random 2 5) +typeset -i cnt=$(random_int_between 2 5) setup_test_env $TESTPOOL "" $cnt damage_devs $TESTPOOL 1 "keep_label"