1#!/usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zfs_get_004_pos.ksh	1.3	07/02/06 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
33
34#################################################################################
35#
36# __stc_assertion_start
37#
38# ID: zfs_get_004_pos
39#
40# DESCRIPTION:
41# Verify 'zfs get all' can get all properties for all datasets in the system
42#
43# STRATEGY:
44#	1. Create datasets for testing
45#	2. Issue 'zfs get all' command
46#	3. Verify the command gets all available properties of all datasets
47#
48# TESTABILITY: explicit
49#
50# TEST_AUTOMATION_LEVEL: automated
51#
52# CODING_STATUS: COMPLETED (2006-08-31)
53#
54# __stc_assertion_end
55#
56################################################################################
57
58verify_runnable "both"
59
60#check 'zfs get all' supportability with the installed OS version
61$ZFS get all >/dev/null 2>&1
62(( $? != 0 )) && log_unsupported "ZFS get all option is unsupported."
63
64
65function cleanup
66{
67	[[ -e $propfile ]] && $RM -f $propfile
68
69	datasetexists $clone  && \
70		log_must $ZFS destroy $clone
71	for snap in $fssnap $volsnap ; do
72		snapexists $snap && \
73			log_must $ZFS destroy $snap
74	done
75
76	if [[ -n $globalzone ]] ; then
77		for pool in $TESTPOOL1 $TESTPOOL2 $TESTPOOL3; do
78			poolexists $pool && \
79				log_must $ZPOOL destroy -f $pool
80		done
81		for file in `$LS $TESTDIR1/poolfile*`; do
82			$RM -f $file
83		done
84	else
85		for fs in $TESTPOOL/$TESTFS1 $TESTPOOL/$TESTFS2 $TESTPOOL/$TESTFS3; do
86			datasetexists $fs && \
87				log_must $ZFS destroy -rf $fs
88		done
89	fi
90}
91
92log_assert "Verify the functions of 'zfs get all' work."
93log_onexit cleanup
94
95typeset globalzone=""
96
97if is_global_zone ; then
98	globalzone="true"
99fi
100
101set -A opts "" "-r" "-H" "-p" "-rHp" "-o name" \
102	"-s local,default,temporary,inherited,none" \
103	"-o name -s local,default,temporary,inherited,none" \
104	"-rHp -o name -s local,default,temporary,inherited,none"
105set -A usrprops "a:b=c" "d_1:1_e=0f" "123:456=789"
106
107fs=$TESTPOOL/$TESTFS
108fssnap=$fs@$TESTSNAP
109clone=$TESTPOOL/$TESTCLONE
110volsnap=$TESTPOOL/$TESTVOL@$TESTSNAP
111
112#set user defined properties for $TESTPOOL
113for usrprop in ${usrprops[@]}; do
114	log_must $ZFS set $usrprop $TESTPOOL
115done
116# create snapshot and clone in $TESTPOOL
117log_must $ZFS snapshot $fssnap
118log_must $ZFS clone $fssnap $clone
119log_must $ZFS snapshot $volsnap
120
121# collect datasets which can be set user defined properties
122usrpropds="$clone $fs"
123
124# collect all datasets which we are creating
125allds="$fs $clone $fssnap $volsnap"
126
127#create pool and datasets to guarantee testing under multiple pools and datasets.
128file=$TESTDIR1/poolfile
129typeset -i FILESIZE=104857600    #100M
130(( DFILESIZE = FILESIZE * 2 ))   # double of FILESIZE
131typeset -i VOLSIZE=10485760      #10M
132availspace=$(get_prop available $TESTPOOL)
133typeset -i i=0
134
135# make sure 'availspace' is larger then twice of FILESIZE to create a new pool.
136# If any, we only totally create 3 pools for multple datasets testing to limit
137# testing time
138while (( availspace > DFILESIZE )) && (( i < 3 )) ; do
139	(( i += 1 ))
140
141	if [[ -n $globalzone ]] ; then
142		log_must create_vdevs ${file}$i
143		eval pool=\$TESTPOOL$i
144		log_must $ZPOOL create $pool ${file}$i
145	else
146		eval pool=$TESTPOOL/\$TESTFS$i
147		log_must $ZFS create $pool
148	fi
149
150	#set user defined properties for testing
151	for usrprop in ${usrprops[@]}; do
152		log_must $ZFS set $usrprop $pool
153	done
154
155	#create datasets in pool
156	log_must $ZFS create $pool/$TESTFS
157	log_must $ZFS snapshot $pool/$TESTFS@$TESTSNAP
158	log_must $ZFS clone $pool/$TESTFS@$TESTSNAP $pool/$TESTCLONE
159
160	if [[ -n $globalzone ]] ; then
161		log_must $ZFS create -V $VOLSIZE $pool/$TESTVOL
162	else
163		log_must $ZFS create $pool/$TESTVOL
164	fi
165
166	ds=`$ZFS list -H -r -o name -t filesystem,volume $pool`
167	usrpropds="$usrpropds $pool/$TESTFS $pool/$TESTCLONE $pool/$TESTVOL"
168	allds="$allds $pool/$TESTFS $pool/$TESTCLONE $pool/$TESTVOL \
169		$pool/$TESTFS@$TESTSNAP"
170
171	availspace=$(get_prop available $TESTPOOL)
172done
173
174#the expected number of property for each type of dataset in this testing
175typeset -i fspropnum=27
176typeset -i snappropnum=8
177typeset -i volpropnum=15
178propfile=$TMPDIR/allpropfile.${TESTCASE_ID}
179
180typeset -i i=0
181typeset -i propnum=0
182typeset -i failflag=0
183while (( i < ${#opts[*]} )); do
184	[[ -e $propfile ]] && $RM -f $propfile
185	log_must eval "$ZFS get ${opts[i]} all >$propfile"
186
187	for ds in $allds; do
188		$GREP $ds $propfile >/dev/null 2>&1
189		(( $? != 0 )) && \
190			log_fail "There is no property for" \
191				"dataset $ds in 'get all' output."
192
193		propnum=`$CAT $propfile | $AWK '{print $1}' | \
194			$GREP "${ds}$" | $WC -l`
195		ds_type=`$ZFS get -H -o value type $ds`
196		case $ds_type in
197			filesystem )
198				(( propnum < fspropnum )) && \
199				(( failflag += 1 ))
200				;;
201			snapshot )
202				(( propnum < snappropnum )) && \
203				(( failflag += 1 ))
204				;;
205			volume )
206				(( propnum < volpropnum )) && \
207				(( failflag += 1 ))
208				;;
209		esac
210
211		(( failflag != 0 )) && \
212			log_fail " 'zfs get all' fails to get out " \
213				"all properties for dataset $ds."
214
215		(( propnum = 0 ))
216		(( failflag = 0 ))
217	done
218
219	(( i += 1 ))
220done
221
222log_note "'zfs get' can get particular property for all datasets with that property."
223
224function do_particular_prop_test #<property> <suitable datasets>
225{
226	typeset	props="$1"
227	typeset ds="$2"
228
229	for prop in ${commprops[*]}; do
230		ds=`$ZFS get -H -o name $prop`
231
232		[[ "$ds" != "$allds" ]] && \
233			log_fail "The result datasets are $ds, but all suitable" \
234				"datasets are $allds for the property $prop"
235	done
236}
237
238# Here, we do a testing for user defined properties and the most common properties
239# for all datasets.
240commprop="type creation used referenced compressratio"
241usrprop="a:b d_1:1_e 123:456"
242
243do_particular_prop_test "$commprop" "$allds"
244do_particular_prop_test "$usrprop" "$usrpropds"
245
246log_pass "'zfs get all' works as expected."
247