5767 fix several problems with zfs test suite
[unleashed.git] / usr / src / test / zfs-tests / tests / functional / cli_root / zfs_set / zfs_set_common.kshlib
blob5c4363b19ea6d767b684aff9e6ebfde33552a970
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) 2014 by Delphix. All rights reserved.
31 . $STF_SUITE/include/libtest.shlib
33 set -A VALID_NAME_CHAR a b c d e f g h i j k l m n o p q r s t u v w x y z \
34     0 1 2 3 4 5 6 7 8 9 ':' '-' '.' '_'
35 set -A INVALID_NAME_CHAR A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
36     '`' '~' '!' '@' '#' '$' '%' '^' '&' '(' ')' '+' '=' '|' "\\" '{' '[' ']' \
37     '}' ';' '"' '<' ',' '>' '?' '/' ' '
38 set -A ALL_CHAR ${VALID_NAME_CHAR[*]} ${INVALID_NAME_CHAR[*]}
41 # Firstly, set the property value to dataset. Then checking if the property
42 # value is equal with the expected value, according to the expected result.
44 # $1 property value
45 # $2 property name
46 # $3 dataset
47 # $4 expected result
49 function set_n_check_prop
51         typeset expect_value=$1
52         typeset prop=$2
53         typeset dataset=$3
54         typeset expect_result=${4:-true}
56         typeset old_value=""
57         typeset cur_value=""
59         [[ -n $prop ]] && old_value=$(get_prop $prop $dataset)
61         if [[ $expect_result == true ]]; then
62                 [[ -z $prop || -z $dataset ]] && \
63                         log_fail "property or dataset isn't defined."
65                 log_must $ZFS set $prop=$expect_value $dataset
66                 if [[ $expect_value == "gzip-6" ]]; then
67                         expect_value="gzip"
68                 fi
70                 [[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
72                 case $prop in
73                         reservation|reserv|quota )
74                                 if [[ $expect_value == "none" ]]; then
75                                         [[ $cur_value != "0" ]] && \
76                                                 log_fail "The '$dataset' '$prop' value \
77                                                 '$cur_value' is not expected."
78                                 elif [[ $cur_value != $expect_value ]]; then
79                                         log_fail "The '$dataset' '$prop' value '$cur_value' \
80                                         does not equal the expected value '$expect_value'."
81                                 fi
82                                 ;;
83                         * )
84                                 if [[ $cur_value != $expect_value ]]; then
85                                         log_fail "The '$dataset' '$prop' value '$cur_value' \
86                                         does not equal the expected value '$expect_value'."
87                                 fi
88                                 ;;
89                 esac
91         else
92                 log_mustnot $ZFS set $prop=$expect_value $dataset
94                 [[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
96                 if [[ "$expect_value" != "" && "$cur_value" != "$old_value" ]];
97                 then
98                         log_fail "The '$dataset' '$prop' value '$cur_value' \
99                                 should equal with '$old_value'."
100                 fi
101         fi
105 # Cleanup all the user properties of the pool and the dataset reside it.
107 # $1 pool name
109 function cleanup_user_prop
111         typeset pool=$1
112         typeset dtst=$($ZFS list -H -r -o name -t filesystem,volume $pool)
114         typeset user_prop
115         for dt in $dtst; do
116                 user_prop=$($ZFS get -H -o property all $dtst | grep ":")
118                 typeset prop
119                 for prop in $user_prop; do
120                         $ZFS inherit $prop $dt
121                         (($? != 0)) && log_must $ZFS inherit $prop $dt
122                 done
123         done
127 # Random select charactor from the specified charactor set and combine into a
128 # random string
130 # $1 character set name
131 # $2 String length
133 function random_string
135         typeset char_set=${1:-VALID_NAME_CHAR}
136         typeset -i len=${2:-5}
138         eval typeset -i count=\${#$char_set[@]}
140         # No consumers want an empty string.
141         ((len == 0)) && len=3
143         typeset str
144         typeset -i i=0
145         while ((i < len)); do
146                 typeset -i ind
147                 ((ind = RANDOM % count))
148                 eval str=\${str}\${$char_set[\$ind]}
150                 ((i += 1))
151         done
153         $ECHO "$str"
157 # Get vaild user defined property name
159 # $1 user defined property name length
161 function valid_user_property
163         typeset -i sumlen=${1:-10}
164         ((sumlen < 2 )) && sumlen=2
165         typeset -i len
166         ((len = RANDOM % sumlen))
167         typeset part1 part2
169         while true; do
170                 part1="$(random_string VALID_NAME_CHAR $len)"
171                 if [[ "$part1" == "-"* ]]; then
172                         continue
173                 fi
174                 break
175         done
176         ((len = sumlen - (len + 1)))
178         while true; do
179                 part2="$(random_string VALID_NAME_CHAR $len)"
180                 if [[ -z $part1 && -z $part2 ]]; then
181                         continue
182                 fi
183                 break
184         done
186         $ECHO "${part1}:${part2}"
190 # Get invaild user defined property name
192 # $1 user defined property name length
194 function invalid_user_property
196         typeset -i sumlen=${1:-10}
197         ((sumlen == 0)) && sumlen=1
198         typeset -i len
199         ((len = RANDOM % sumlen))
201         typeset part1 part2
202         while true; do
203                 part1="$(random_string VALID_NAME_CHAR $len)"
204                 ((len = sumlen - len))
205                 part2="$(random_string INVALID_NAME_CHAR $len)"
207                 # Avoid $part1 is *:* and $part2 is "=*"
208                 if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
209                         continue
210                 fi
211                 break
212         done
214         $ECHO "${part1}${part2}"
218 # Get user property value
220 # $1 user defined property name length
222 function user_property_value
224         typeset -i len=${1:-100}
226         typeset value=$(random_string ALL_CHAR $len)
228         $ECHO "$value"
232 # Check if the user property is identical to the expected value.
234 # $1 dataset
235 # $2 user property
236 # $3 expected value
238 function check_user_prop
240         typeset dtst=$1
241         typeset user_prop="$2"
242         typeset expect_value="$3"
243         typeset value=$($ZFS get -p -H -o value "$user_prop" $dtst 2>&1)
245         if [[ "$expect_value" == "$value" ]]; then
246                 return 0
247         else
248                 return 1
249         fi
253 # Get source of the dataset
255 function get_source
257         typeset prop=$1
258         typeset dataset=$2
259         typeset source
261         source=$($ZFS get -H -o source $prop $dataset)
262         if (($? != 0)); then
263                 log_fail "Unable to get $prop source for dataset $dataset"
264         fi
266         $ECHO "$source"