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