xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zfs_set/zfs_set_common.kshlib (revision b6805bf78d2bbbeeaea8909a05623587b42d58b3)
1#
2# CDDL HEADER START
3#
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.
7#
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.
12#
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]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27. $STF_SUITE/include/libtest.shlib
28
29set -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 \
30    0 1 2 3 4 5 6 7 8 9 ':' '-' '.' '_'
31set -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 \
32    '`' '~' '!' '@' '#' '$' '%' '^' '&' '(' ')' '+' '=' '|' "\\" '{' '[' ']' \
33    '}' ';' '"' '<' ',' '>' '?' '/' ' '
34set -A ALL_CHAR ${VALID_NAME_CHAR[*]} ${INVALID_NAME_CHAR[*]}
35
36#
37# Firstly, set the property value to dataset. Then checking if the property
38# value is equal with the expected value, according to the expected result.
39#
40# $1 property value
41# $2 property name
42# $3 dataset
43# $4 expected result
44#
45function set_n_check_prop
46{
47	typeset expect_value=$1
48	typeset prop=$2
49	typeset dataset=$3
50	typeset expect_result=${4:-true}
51
52	typeset old_value=""
53	typeset cur_value=""
54
55	[[ -n $prop ]] && old_value=$(get_prop $prop $dataset)
56
57	if [[ $expect_result == true ]]; then
58		[[ -z $prop || -z $dataset ]] && \
59			log_fail "property or dataset isn't defined."
60
61		log_must $ZFS set $prop=$expect_value $dataset
62		if [[ $expect_value == "gzip-6" ]]; then
63			expect_value="gzip"
64		fi
65
66		[[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
67
68		case $prop in
69			reservation|reserv|quota )
70				if [[ $expect_value == "none" ]]; then
71					[[ $cur_value != "0" ]] && \
72						log_fail "The '$dataset' '$prop' value \
73						'$cur_value' is not expected."
74				elif [[ $cur_value != $expect_value ]]; then
75					log_fail "The '$dataset' '$prop' value '$cur_value' \
76					does not equal the expected value '$expect_value'."
77				fi
78				;;
79			* )
80				if [[ $cur_value != $expect_value ]]; then
81					log_fail "The '$dataset' '$prop' value '$cur_value' \
82					does not equal the expected value '$expect_value'."
83				fi
84				;;
85		esac
86
87	else
88		log_mustnot $ZFS set $prop=$expect_value $dataset
89
90		[[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
91
92		if [[ "$expect_value" != "" && "$cur_value" != "$old_value" ]];
93		then
94			log_fail "The '$dataset' '$prop' value '$cur_value' \
95				should equal with '$old_value'."
96		fi
97	fi
98}
99
100#
101# Cleanup all the user properties of the pool and the dataset reside it.
102#
103# $1 pool name
104#
105function cleanup_user_prop
106{
107	typeset pool=$1
108	typeset dtst=$($ZFS list -H -r -o name -t filesystem,volume $pool)
109
110	typeset user_prop
111	for dt in $dtst; do
112		user_prop=$($ZFS get -H -o property all $dtst | grep ":")
113
114		typeset prop
115		for prop in $user_prop; do
116			$ZFS inherit $prop $dt
117			(($? != 0)) && log_must $ZFS inherit $prop $dt
118		done
119	done
120}
121
122#
123# Random select charactor from the specified charactor set and combine into a
124# random string
125#
126# $1 character set name
127# $2 String length
128#
129function random_string
130{
131	typeset char_set=${1:-VALID_NAME_CHAR}
132	typeset -i len=${2:-5}
133
134	eval typeset -i count=\${#$char_set[@]}
135
136	typeset str
137	typeset -i i=0
138	while ((i < len)); do
139		typeset -i ind
140		((ind = RANDOM % count))
141		eval str=\${str}\${$char_set[\$ind]}
142
143		((i += 1))
144	done
145
146	$ECHO "$str"
147}
148
149#
150# Get vaild user defined property name
151#
152# $1 user defined property name length
153#
154function valid_user_property
155{
156	typeset -i sumlen=${1:-10}
157	((sumlen < 2 )) && sumlen=2
158	typeset -i len
159	((len = RANDOM % sumlen))
160	typeset part1 part2
161
162	while true; do
163		part1="$(random_string VALID_NAME_CHAR $len)"
164		if [[ "$part1" == "-"* ]]; then
165			continue
166		fi
167		break
168	done
169	((len = sumlen - (len + 1)))
170
171	while true; do
172		part2="$(random_string VALID_NAME_CHAR $len)"
173		if [[ -z $part1 && -z $part2 ]]; then
174			continue
175		fi
176		break
177	done
178
179	$ECHO "${part1}:${part2}"
180}
181
182#
183# Get invaild user defined property name
184#
185# $1 user defined property name length
186#
187function invalid_user_property
188{
189	typeset -i sumlen=${1:-10}
190	((sumlen == 0)) && sumlen=1
191	typeset -i len
192	((len = RANDOM % sumlen))
193
194	typeset part1 part2
195	while true; do
196		part1="$(random_string VALID_NAME_CHAR $len)"
197		((len = sumlen - len))
198		part2="$(random_string INVALID_NAME_CHAR $len)"
199
200		# Avoid $part1 is *:* and $part2 is "=*"
201		if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
202			continue
203		fi
204		break
205	done
206
207	$ECHO "${part1}${part2}"
208}
209
210#
211# Get user property value
212#
213# $1 user defined property name length
214#
215function user_property_value
216{
217	typeset -i len=${1:-100}
218
219	typeset value=$(random_string ALL_CHAR $len)
220
221	$ECHO "$value"
222}
223
224#
225# Check if the user property is identical to the expected value.
226#
227# $1 dataset
228# $2 user property
229# $3 expected value
230#
231function check_user_prop
232{
233	typeset dtst=$1
234	typeset user_prop="$2"
235	typeset expect_value="$3"
236	typeset value=$($ZFS get -p -H -o value "$user_prop" $dtst 2>&1)
237
238	if [[ "$expect_value" == "$value" ]]; then
239		return 0
240	else
241		return 1
242	fi
243}
244
245#
246# Get source of the dataset
247#
248function get_source
249{
250	typeset prop=$1
251	typeset dataset=$2
252	typeset source
253
254	source=$($ZFS get -H -o source $prop $dataset)
255        if (($? != 0)); then
256                log_fail "Unable to get $prop source for dataset $dataset"
257        fi
258
259	$ECHO "$source"
260}
261