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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016 by Delphix. All rights reserved.
30# Copyright (c) 2021 Matt Fiddaman
31#
32
33. $STF_SUITE/tests/functional/cli_root/zfs_get/zfs_get_common.kshlib
34. $STF_SUITE/tests/functional/cli_root/zfs_get/zfs_get_list_d.kshlib
35
36#
37# DESCRIPTION:
38# Setting the valid option and properties, 'zfs get' should return the
39# correct property value.
40#
41# STRATEGY:
42# 1. Create pool, filesystem, volume, snapshot, and bookmark.
43# 2. Setting valid parameter, 'zfs get' should succeed.
44# 3. Compare the output property name with the original input property.
45#
46
47verify_runnable "both"
48
49typeset options=("" "-p" "-r" "-H")
50
51typeset -i i=${#options[*]}
52typeset -i j=0
53while ((j<${#depth_options[*]}));
54do
55	options[$i]=-"${depth_options[$j]}"
56	((j+=1))
57	((i+=1))
58done
59
60typeset zfs_props=("type" used available creation volsize referenced \
61    compressratio mounted origin recordsize quota reservation mountpoint \
62    sharenfs checksum compression atime devices exec readonly setuid zoned \
63    snapdir aclmode aclinherit canmount primarycache secondarycache \
64    usedbychildren usedbydataset usedbyrefreservation usedbysnapshots \
65    version clones)
66
67typeset userquota_props=(userquota@root groupquota@root userused@root \
68    groupused@root)
69typeset all_props=("${zfs_props[@]}" "${userquota_props[@]}")
70typeset dataset=($TESTPOOL/$TESTCTR $TESTPOOL/$TESTFS $TESTPOOL/$TESTVOL \
71	$TESTPOOL/$TESTFS@$TESTSNAP $TESTPOOL/$TESTVOL@$TESTSNAP
72	$TESTPOOL/$TESTFS@$TESTSNAP1 $TESTPOOL/$TESTCLONE)
73
74typeset bookmark_props=(creation)
75typeset bookmark=($TESTPOOL/$TESTFS#$TESTBKMARK $TESTPOOL/$TESTVOL#$TESTBKMARK)
76
77#
78# According to dataset and option, checking if 'zfs get' return correct
79# property information.
80#
81# $1 dataset
82# $2 properties which are expected to output into $TESTDIR/$TESTFILE0
83# $3 option
84#
85function check_return_value
86{
87	typeset dst=$1
88	typeset props=$2
89	typeset opt=$3
90	typeset -i found=0
91	typeset p
92
93	for p in $props; do
94		found=0
95
96		while read line; do
97			typeset item
98			item=$(echo $line | awk '{print $2}' 2>&1)
99
100			if [[ $item == $p ]]; then
101				((found += 1))
102				cols=$(echo $line | awk '{print NF}')
103				break
104			fi
105		done < $TESTDIR/$TESTFILE0
106
107		if ((found == 0)); then
108			log_fail "'zfs get $opt $props $dst' return " \
109			    "error message.'$p' haven't been found."
110		elif [[ "$opt" == "-p" ]] && ((cols != 4)); then
111			log_fail "'zfs get $opt $props $dst' returned " \
112			    "$cols columns instead of 4."
113		fi
114	done
115
116	log_note "SUCCESS: 'zfs get $opt $prop $dst'."
117}
118
119log_assert "Setting the valid options and properties 'zfs get' should return " \
120    "the correct property value."
121log_onexit cleanup
122
123# Create filesystem and volume's snapshot
124create_snapshot $TESTPOOL/$TESTFS $TESTSNAP
125create_snapshot $TESTPOOL/$TESTVOL $TESTSNAP
126
127# Create second snapshot and clone it
128create_snapshot $TESTPOOL/$TESTFS $TESTSNAP1
129create_clone $TESTPOOL/$TESTFS@$TESTSNAP1 $TESTPOOL/$TESTCLONE
130
131# Create filesystem and volume's bookmark
132create_bookmark $TESTPOOL/$TESTFS $TESTSNAP $TESTBKMARK
133create_bookmark $TESTPOOL/$TESTVOL $TESTSNAP $TESTBKMARK
134
135typeset -i i=0
136while ((i < ${#dataset[@]})); do
137	for opt in "${options[@]}"; do
138		for prop in ${all_props[@]}; do
139			eval "zfs get $opt $prop ${dataset[i]} > \
140			    $TESTDIR/$TESTFILE0"
141			ret=$?
142			if [[ $ret != 0 ]]; then
143				log_fail "zfs get returned: $ret"
144			fi
145			check_return_value ${dataset[i]} "$prop" "$opt"
146		done
147	done
148	((i += 1))
149done
150
151i=0
152while ((i < ${#bookmark[@]})); do
153	for opt in "${options[@]}"; do
154		for prop in ${bookmark_props[@]}; do
155			eval "zfs get $opt $prop ${bookmark[i]} > \
156			    $TESTDIR/$TESTFILE0"
157			ret=$?
158			if [[ $ret != 0 ]]; then
159				log_fail "zfs get returned: $ret"
160			fi
161			check_return_value ${bookmark[i]} "$prop" "$opt"
162		done
163	done
164	((i += 1))
165done
166
167log_pass "Setting the valid options to dataset, it should succeed and return " \
168    "valid value. 'zfs get' pass."
169