7290 ZFS test suite needs to control what utilities it can run
[unleashed.git] / usr / src / test / zfs-tests / tests / functional / cli_root / zpool_upgrade / zpool_upgrade.kshlib
blob9a8554648bc08a2ad21d99ff19c89951b4050df0
2 # CDDL HEADER START
4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
19 # CDDL HEADER END
23 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 # Use is subject to license terms.
28 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
29 # Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
32 . $STF_SUITE/include/libtest.shlib
33 . $STF_SUITE/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.cfg
35 # This part of the test suite relies on variables being setup in the
36 # zpool_upgrade.cfg script. Those variables give us details about which
37 # files make up the pool, and what the pool name is.
40 # A function to import a pool from files we have stored in the test suite
41 # We import the pool, and create some random data in the pool.
42 # $1 a version number we can use to get information about the pool
43 function create_old_pool
45         typeset vers=$1
46         typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
47         typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
49         log_note "Creating $pool_name from $pool_files"
50         for pool_file in $pool_files; do
51                 log_must bzcat \
52                     $STF_SUITE/tests/functional/cli_root/zpool_upgrade/blockfiles/$pool_file.bz2 \
53                     >/$TESTPOOL/$pool_file
54         done
55         log_must zpool import -d /$TESTPOOL $pool_name
57         # Put some random contents into the pool
58         for i in {1..1024} ; do
59                 dd if=/dev/urandom of=/$pool_name/random.$i \
60                     count=1 bs=1024 > /dev/null 2>&1
61         done
65 # A function to check the contents of a pool, upgrade it to the current version
66 # and then verify that the data is consistent after upgrading. Note that we're
67 # not using "zpool status -x" to see if the pool is healthy, as it's possible
68 # to also upgrade faulted, or degraded pools.
69 # $1 a version number we can use to get information about the pool
70 function check_upgrade
72         typeset vers=$1
73         typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
74         typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
75         typeset pre_upgrade_checksum
76         typeset post_upgrade_checksum
78         log_note "Checking if we can upgrade from ZFS version $vers"
79         pre_upgrade_checksum=$(check_pool $pool_name pre)
80         log_must zpool upgrade $pool_name
81         post_upgrade_checksum=$(check_pool $pool_name post)
83         log_note "Checking that there are no differences between checksum output"
84         log_must diff $pre_upgrade_checksum $post_upgrade_checksum
85         rm $pre_upgrade_checksum $post_upgrade_checksum
88 # A function to destroy an upgraded pool, plus the files it was based on.
89 # $1 a version number we can use to get information about the pool
90 function destroy_upgraded_pool
92         typeset vers=$1
93         typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
94         typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
96         if poolexists $pool_name; then
97                 log_must zpool destroy $pool_name
98         fi
99         for file in $pool_files; do
100                 rm -f /$TESTPOOL/$file
101         done
104 # This function does a basic sanity check on the pool by computing the
105 # checksums of all files in the pool, echoing the name of the file containing
106 # the checksum results.
107 # $1 the name of the pool
108 # $2 a flag we can use to determine when this check is being performed
109 #    (ie. pre or post pool-upgrade)
110 function check_pool
112         typeset pool=$1
113         typeset flag=$2
114         find /$pool -type f -exec cksum {} + > \
115                 /$TESTPOOL/pool-checksums.$pool.$flag
116         echo /$TESTPOOL/pool-checksums.$pool.$flag
119 # This function simply checks that a pool has a particular version number
120 # as reported by zdb and zpool upgrade -v
121 # $1 the name of the pool
122 # $2 the version of the pool we expect to see
123 function check_poolversion
125         typeset pool=$1
126         typeset vers=$2
127         typeset actual
129         # check version using zdb
130         actual=$(zdb -C $pool | sed -n 's/^.*version: \(.*\)$/\1/p')
131         if [[ $actual != $vers ]] ; then
132                 log_fail "$pool: zdb reported version $actual, expected $vers"
133         fi
135         # check version using zpool upgrade
136         actual=$(zpool upgrade | grep $pool$ | \
137             awk '{print $1}' | sed -e 's/ //g')
138         if [[ $actual != $vers ]] ; then
139                 log_fail "$pool: zpool reported version $actual, expected $vers"
140         fi
143 # A simple function to get a random number between two bounds
144 # probably not the most efficient for large ranges, but it's okay.
145 # Note since we're using $RANDOM, 32767 is the largest number we
146 # can accept as the upper bound.
147 # $1 lower bound
148 # $2 upper bound
149 function random
151         typeset min=$1
152         typeset max=$2
153         typeset rand=0
155         while [[ $rand -lt $min ]] ; do
156                 rand=$(( $RANDOM % $max + 1))
157         done
159         echo $rand