1#!/bin/ksh -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#
24# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33
34#
35# DESCRIPTION:
36# Verify 'zfs get all' can get all properties for all datasets in the system
37#
38# STRATEGY:
39#	1. Create datasets for testing
40#	2. Issue 'zfs get all' command
41#	3. Verify the command gets all available properties of all datasets
42#
43
44verify_runnable "both"
45
46function cleanup
47{
48	[[ -e $propfile ]] && rm -f $propfile
49
50	datasetexists $clone  && \
51		log_must zfs destroy $clone
52	for snap in $fssnap $volsnap ; do
53		snapexists $snap && \
54			log_must zfs destroy $snap
55	done
56
57	if [[ -n $globalzone ]] ; then
58		for pool in $TESTPOOL1 $TESTPOOL2 $TESTPOOL3; do
59			poolexists $pool && \
60				log_must zpool destroy -f $pool
61		done
62		for file in `ls $TESTDIR1/poolfile*`; do
63			rm -f $file
64		done
65	else
66		for fs in $TESTPOOL/$TESTFS1 $TESTPOOL/$TESTFS2 $TESTPOOL/$TESTFS3; do
67			datasetexists $fs && \
68				log_must zfs destroy -rf $fs
69		done
70	fi
71}
72
73log_assert "Verify the functions of 'zfs get all' work."
74log_onexit cleanup
75
76typeset globalzone=""
77
78if is_global_zone ; then
79	globalzone="true"
80fi
81
82set -A opts "" "-r" "-H" "-p" "-rHp" "-o name" \
83	"-s local,default,temporary,inherited,none" \
84	"-o name -s local,default,temporary,inherited,none" \
85	"-rHp -o name -s local,default,temporary,inherited,none"
86set -A usrprops "a:b=c" "d_1:1_e=0f" "123:456=789"
87
88fs=$TESTPOOL/$TESTFS
89fssnap=$fs@$TESTSNAP
90clone=$TESTPOOL/$TESTCLONE
91volsnap=$TESTPOOL/$TESTVOL@$TESTSNAP
92
93#set user defined properties for $TESTPOOL
94for usrprop in ${usrprops[@]}; do
95	log_must zfs set $usrprop $TESTPOOL
96done
97# create snapshot and clone in $TESTPOOL
98log_must zfs snapshot $fssnap
99log_must zfs clone $fssnap $clone
100log_must zfs snapshot $volsnap
101
102# collect datasets which can be set user defined properties
103usrpropds="$clone $fs"
104
105# collect all datasets which we are creating
106allds="$fs $clone $fssnap $volsnap"
107
108#create pool and datasets to guarantee testing under multiple pools and datasets.
109file=$TESTDIR1/poolfile
110typeset FILESIZE=$MINVDEVSIZE
111(( DFILESIZE = $FILESIZE * 2 ))
112typeset -i VOLSIZE=10485760
113availspace=$(get_prop available $TESTPOOL)
114typeset -i i=0
115
116# make sure 'availspace' is larger then twice of FILESIZE to create a new pool.
117# If any, we only totally create 3 pools for multple datasets testing to limit
118# testing time
119while (( availspace > DFILESIZE )) && (( i < 3 )) ; do
120	(( i += 1 ))
121
122	if [[ -n $globalzone ]] ; then
123		log_must mkfile $FILESIZE ${file}$i
124		eval pool=\$TESTPOOL$i
125		log_must zpool create $pool ${file}$i
126	else
127		eval pool=$TESTPOOL/\$TESTFS$i
128		log_must zfs create $pool
129	fi
130
131	#set user defined properties for testing
132	for usrprop in ${usrprops[@]}; do
133		log_must zfs set $usrprop $pool
134	done
135
136	#create datasets in pool
137	log_must zfs create $pool/$TESTFS
138	log_must zfs snapshot $pool/$TESTFS@$TESTSNAP
139	log_must zfs clone $pool/$TESTFS@$TESTSNAP $pool/$TESTCLONE
140
141	if [[ -n $globalzone ]] ; then
142		log_must zfs create -V $VOLSIZE $pool/$TESTVOL
143	else
144		log_must zfs create $pool/$TESTVOL
145	fi
146
147	ds=`zfs list -H -r -o name -t filesystem,volume $pool`
148	usrpropds="$usrpropds $pool/$TESTFS $pool/$TESTCLONE $pool/$TESTVOL"
149	allds="$allds $pool/$TESTFS $pool/$TESTCLONE $pool/$TESTVOL \
150		$pool/$TESTFS@$TESTSNAP"
151
152	availspace=$(get_prop available $TESTPOOL)
153done
154
155#the expected number of property for each type of dataset in this testing
156typeset -i fspropnum=27
157typeset -i snappropnum=8
158typeset -i volpropnum=15
159propfile=/var/tmp/allpropfile.$$
160
161typeset -i i=0
162typeset -i propnum=0
163typeset -i failflag=0
164while (( i < ${#opts[*]} )); do
165	[[ -e $propfile ]] && rm -f $propfile
166	log_must eval "zfs get ${opts[i]} all >$propfile"
167
168	for ds in $allds; do
169		grep $ds $propfile >/dev/null 2>&1
170		(( $? != 0 )) && \
171			log_fail "There is no property for" \
172				"dataset $ds in 'get all' output."
173
174		propnum=`cat $propfile | awk '{print $1}' | \
175			grep "${ds}$" | wc -l`
176		ds_type=`zfs get -H -o value type $ds`
177		case $ds_type in
178			filesystem )
179				(( propnum < fspropnum )) && \
180				(( failflag += 1 ))
181				;;
182			snapshot )
183				(( propnum < snappropnum )) && \
184				(( failflag += 1 ))
185				;;
186			volume )
187				(( propnum < volpropnum )) && \
188				(( failflag += 1 ))
189				;;
190		esac
191
192		(( failflag != 0 )) && \
193			log_fail " 'zfs get all' fails to get out " \
194				"all properties for dataset $ds."
195
196		(( propnum = 0 ))
197		(( failflag = 0 ))
198	done
199
200	(( i += 1 ))
201done
202
203log_note "'zfs get' can get particular property for all datasets with that property."
204
205function do_particular_prop_test #<property> <suitable datasets>
206{
207	typeset	props="$1"
208	typeset ds="$2"
209
210	for prop in ${commprops[*]}; do
211		ds=`zfs get -H -o name $prop`
212
213		[[ "$ds" != "$allds" ]] && \
214			log_fail "The result datasets are $ds, but all suitable" \
215				"datasets are $allds for the property $prop"
216	done
217}
218
219# Here, we do a testing for user defined properties and the most common properties
220# for all datasets.
221commprop="type creation used referenced compressratio"
222usrprop="a:b d_1:1_e 123:456"
223
224do_particular_prop_test "$commprop" "$allds"
225do_particular_prop_test "$usrprop" "$usrpropds"
226
227log_pass "'zfs get all' works as expected."
228