xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zfs_set/zfs_set_common.kshlib (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
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#
28# Copyright (c) 2014 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32
33set -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 ':' '-' '.' '_'
35set -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    '}' ';' '"' '<' ',' '>' '?' '/' ' '
38set -A ALL_CHAR ${VALID_NAME_CHAR[*]} ${INVALID_NAME_CHAR[*]}
39
40#
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.
43#
44# $1 property value
45# $2 property name
46# $3 dataset
47# $4 expected result
48#
49function set_n_check_prop
50{
51	typeset expect_value=$1
52	typeset prop=$2
53	typeset dataset=$3
54	typeset expect_result=${4:-true}
55
56	typeset old_value=""
57	typeset cur_value=""
58
59	[[ -n $prop ]] && old_value=$(get_prop $prop $dataset)
60
61	if [[ $expect_result == true ]]; then
62		[[ -z $prop || -z $dataset ]] && \
63			log_fail "property or dataset isn't defined."
64
65		log_must $ZFS set $prop=$expect_value $dataset
66		if [[ $expect_value == "gzip-6" ]]; then
67			expect_value="gzip"
68		fi
69
70		[[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
71
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
90
91	else
92		log_mustnot $ZFS set $prop=$expect_value $dataset
93
94		[[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
95
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
102}
103
104#
105# Cleanup all the user properties of the pool and the dataset reside it.
106#
107# $1 pool name
108#
109function cleanup_user_prop
110{
111	typeset pool=$1
112	typeset dtst=$($ZFS list -H -r -o name -t filesystem,volume $pool)
113
114	typeset user_prop
115	for dt in $dtst; do
116		user_prop=$($ZFS get -H -o property all $dtst | grep ":")
117
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
124}
125
126#
127# Random select charactor from the specified charactor set and combine into a
128# random string
129#
130# $1 character set name
131# $2 String length
132#
133function random_string
134{
135	typeset char_set=${1:-VALID_NAME_CHAR}
136	typeset -i len=${2:-5}
137
138	eval typeset -i count=\${#$char_set[@]}
139
140	# No consumers want an empty string.
141	((len == 0)) && len=3
142
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]}
149
150		((i += 1))
151	done
152
153	$ECHO "$str"
154}
155
156#
157# Get vaild user defined property name
158#
159# $1 user defined property name length
160#
161function valid_user_property
162{
163	typeset -i sumlen=${1:-10}
164	((sumlen < 2 )) && sumlen=2
165	typeset -i len
166	((len = RANDOM % sumlen))
167	typeset part1 part2
168
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)))
177
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
185
186	$ECHO "${part1}:${part2}"
187}
188
189#
190# Get invaild user defined property name
191#
192# $1 user defined property name length
193#
194function invalid_user_property
195{
196	typeset -i sumlen=${1:-10}
197	((sumlen == 0)) && sumlen=1
198	typeset -i len
199	((len = RANDOM % sumlen))
200
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)"
206
207		# Avoid $part1 is *:* and $part2 is "=*"
208		if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
209			continue
210		fi
211		break
212	done
213
214	$ECHO "${part1}${part2}"
215}
216
217#
218# Get user property value
219#
220# $1 user defined property name length
221#
222function user_property_value
223{
224	typeset -i len=${1:-100}
225
226	typeset value=$(random_string ALL_CHAR $len)
227
228	$ECHO "$value"
229}
230
231#
232# Check if the user property is identical to the expected value.
233#
234# $1 dataset
235# $2 user property
236# $3 expected value
237#
238function check_user_prop
239{
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)
244
245	if [[ "$expect_value" == "$value" ]]; then
246		return 0
247	else
248		return 1
249	fi
250}
251
252#
253# Get source of the dataset
254#
255function get_source
256{
257	typeset prop=$1
258	typeset dataset=$2
259	typeset source
260
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
265
266	$ECHO "$source"
267}
268