#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or https://opensource.org/licenses/CDDL-1.0.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
#

#
# Simple function to get the source of the specified property.
# If unable to get the property then exits.
#
function get_prop_src # property dataset
{
        typeset prop=$1
        typeset dataset=$2

	zfs get -H -o source $prop $dataset ||
                log_fail "Unable to determine the source of $prop" \
                        "property for dataset $dataset"
}

#
# Function to check the 'source' of a property. The source can
# either be "default", "local", or "inherited from <parent dataset>".
#
# The 'expected src' argument must be either "default", "local", or
# a dataset name.
#
# Returns 0 on success, 1 on failure.
#
function verify_prop_src # child_dataset property expected_src
{
        typeset target=$1
        typeset prop=$2
        typeset expected=$3

        prop_src=$(get_prop_src $prop $target)

	#
	# Rather than just checking if $prop_src == $expected
	# we first determine what value $expected should have.
	# This allows us to catch the case where a property
	# has a source of "local" but we expected it to be
	# "default"
	#
	if [[ $expected == "default" ]]; then
		if [[ $prop_src != $expected ]]; then
			log_note "Property $prop of $target has source"\
				" $prop_src rather than $expected"
			return 1
		fi
	elif [[ $expected == "local" ]]; then
		if [[ $prop_src != $expected ]]; then
			log_note "Property $prop of $target has source"\
				" $prop_src rather than $expected"
			return 1
		fi
	elif [[ $prop_src != "inherited from $expected" ]]; then
		log_note "Property $prop of $expected has source $prop_src"\
			" rather than 'inherited from $expected'"
                return 1
	fi

	return 0
}

#
# Simple function to set a property to a
# specified value and verify it has changed
# correctly.
#
function set_n_verify_prop #property value dataset
{
	typeset prop=$1
	typeset prop_val=$2
	typeset dataset=$3

	zfs set $prop=$prop_val $dataset
	check_val=$(get_prop $prop $dataset)

	if [[ $check_val != $prop_val ]]; then
		log_fail "Property $prop of $dataset has value $check_val"\
			" rather than $prop_val"
	fi
}
