7290 ZFS test suite needs to control what utilities it can run
[unleashed.git] / usr / src / test / zfs-tests / tests / functional / cache / cache.kshlib
blob26b56f68e579a26af9c5b19f4cc502ca8bbecea9
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 2009 Sun Microsystems, Inc.  All rights reserved.
24 # Use is subject to license terms.
28 # Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31 . $STF_SUITE/include/libtest.shlib
33 function cleanup
35         if datasetexists $TESTPOOL ; then
36                 log_must zpool destroy -f $TESTPOOL
37         fi
38         if datasetexists $TESTPOOL2 ; then
39                 log_must zpool destroy -f $TESTPOOL2
40         fi
44 # Try zpool status/iostat for given pool
46 # $1 pool
48 function display_status
50         typeset pool=$1
52         typeset -i ret=0
53         zpool status -xv $pool > /dev/null 2>&1
54         ret=$?
56         zpool iostat > /dev/null 2>&1
57         ((ret |= $?))
59         typeset mntpnt=$(get_prop mountpoint $pool)
60         dd if=/dev/random of=$mntpnt/testfile.$$ &
61         typeset pid=$!
63         zpool iostat -v 1 3 > /dev/null
64         ((ret |= $?))
66         kill -9 $pid
68         return $ret
72 # Verify the given cache device have correct type and status
74 # $1 pool name
75 # $2 device name
76 # $3 device status
77 # $4 device type
79 function verify_cache_device
81         typeset pool=$1
82         typeset device=$2
83         typeset status=$3
84         typeset type=$4
86         if [[ -z $pool || -z $device || -z $status ]]; then
87                 log_fail "Usage: verify_cache_device <pool> <device> " \
88                         "<status> [type]"
89         fi
91         #
92         # Get all the cache devices and status table like below
93         #
94         # mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
95         #
96         set -A dev_stat_tab $(zpool status -v $pool | nawk 'BEGIN {start=0} \
97                                 /\tcache/ {start=1}
98                                 /\tmirror/ || /\tspares/ || /^$/ {start=0}
99                                 (start==1) && /\t  (\/|[a-zA-Z])/ \
100                                         {print "stripe:" $1 " " $2}
101                                 (start==1) && /\t    (\/|[a-zA-Z])/ \
102                                         {print "mirror:" $1 " " $2}
103                                 # When hotspare is replacing
104                                 (start==1) && /\t      (\/|[a-zA-Z])/ \
105                                         {print "mirror:" $1 " " $2}'
106                              )
108         typeset -i i=0
109         typeset find=0
110         while (( i < ${#dev_stat_tab[@]} )); do
111                 typeset dev=${dev_stat_tab[$i]}
112                 typeset stat=${dev_stat_tab[((i+1))]}
114                 case $dev in
115                         stripe:$device)
116                                 if [[ "$type" == 'mirror' ]]; then
117                                         log_note "Unexpected type: mirror"
118                                         return 1
119                                 else
120                                         if [[ $stat != $status ]]; then
121                                                 log_note "Status($stat) " \
122                                                         "!= Expected stat($status)"
123                                                 return 1
124                                         fi
125                                         return 0
126                                 fi
127                                 ;;
128                         mirror:$device)
129                                 if [[ -z "$type" || $type == 'stripe' ]]; then
130                                         log_note "Unexpected type: stripe"
131                                         return 1
132                                 else
133                                         if [[ $stat != $status ]]; then
134                                                 log_note "Status($stat) " \
135                                                         "!= Expected stat($status)"
136                                                 return 1
137                                         fi
138                                         return 0
139                                 fi
140                                 ;;
141                 esac
142                 ((i += 2))
143         done
145         log_note "Can not find device: $device"
146         return 1
149 function verify_cache_support
151         zpool upgrade -v | grep "Cache devices" > /dev/null 2>&1
152         return $?