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 https://opensource.org/licenses/CDDL-1.0.
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) 2013, 2016 by Delphix. All rights reserved.
29#
30
31#
32# Simple function to get the source of the specified property.
33# If unable to get the property then exits.
34#
35function get_prop_src # property dataset
36{
37        typeset prop=$1
38        typeset dataset=$2
39
40	zfs get -H -o source $prop $dataset ||
41                log_fail "Unable to determine the source of $prop" \
42                        "property for dataset $dataset"
43}
44
45#
46# Function to check the 'source' of a property. The source can
47# either be "default", "local", or "inherited from <parent dataset>".
48#
49# The 'expected src' argument must be either "default", "local", or
50# a dataset name.
51#
52# Returns 0 on success, 1 on failure.
53#
54function verify_prop_src # child_dataset property expected_src
55{
56        typeset target=$1
57        typeset prop=$2
58        typeset expected=$3
59
60        prop_src=$(get_prop_src $prop $target)
61
62	#
63	# Rather than just checking if $prop_src == $expected
64	# we first determine what value $expected should have.
65	# This allows us to catch the case where a property
66	# has a source of "local" but we expected it to be
67	# "default"
68	#
69	if [[ $expected == "default" ]]; then
70		if [[ $prop_src != $expected ]]; then
71			log_note "Property $prop of $target has source"\
72				" $prop_src rather than $expected"
73			return 1
74		fi
75	elif [[ $expected == "local" ]]; then
76		if [[ $prop_src != $expected ]]; then
77			log_note "Property $prop of $target has source"\
78				" $prop_src rather than $expected"
79			return 1
80		fi
81	elif [[ $prop_src != "inherited from $expected" ]]; then
82		log_note "Property $prop of $expected has source $prop_src"\
83			" rather than 'inherited from $expected'"
84                return 1
85	fi
86
87	return 0
88}
89
90#
91# Simple function to set a property to a
92# specified value and verify it has changed
93# correctly.
94#
95function set_n_verify_prop #property value dataset
96{
97	typeset prop=$1
98	typeset prop_val=$2
99	typeset dataset=$3
100
101	zfs set $prop=$prop_val $dataset
102	check_val=$(get_prop $prop $dataset)
103
104	if [[ $check_val != $prop_val ]]; then
105		log_fail "Property $prop of $dataset has value $check_val"\
106			" rather than $prop_val"
107	fi
108}
109