1f38cb554SJohn Wren Kennedy#
2f38cb554SJohn Wren Kennedy# CDDL HEADER START
3f38cb554SJohn Wren Kennedy#
4f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
5f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
6f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
7f38cb554SJohn Wren Kennedy#
8f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
10f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
11f38cb554SJohn Wren Kennedy# and limitations under the License.
12f38cb554SJohn Wren Kennedy#
13f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
14f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
16f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
17f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
18f38cb554SJohn Wren Kennedy#
19f38cb554SJohn Wren Kennedy# CDDL HEADER END
20f38cb554SJohn Wren Kennedy#
21f38cb554SJohn Wren Kennedy
22f38cb554SJohn Wren Kennedy#
23f38cb554SJohn Wren Kennedy# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24f38cb554SJohn Wren Kennedy# Use is subject to license terms.
25f38cb554SJohn Wren Kennedy#
26f38cb554SJohn Wren Kennedy
27f38cb554SJohn Wren Kennedy#
28*1d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29f38cb554SJohn Wren Kennedy#
30f38cb554SJohn Wren Kennedy
31f38cb554SJohn Wren Kennedy#
32f38cb554SJohn Wren Kennedy# Simple function to get the source of the specified property.
33f38cb554SJohn Wren Kennedy# If unable to get the property then exits.
34f38cb554SJohn Wren Kennedy#
35f38cb554SJohn Wren Kennedyfunction get_prop_src # property dataset
36f38cb554SJohn Wren Kennedy{
37f38cb554SJohn Wren Kennedy        typeset prop_val
38f38cb554SJohn Wren Kennedy        typeset prop=$1
39f38cb554SJohn Wren Kennedy        typeset dataset=$2
40f38cb554SJohn Wren Kennedy
41*1d32ba66SJohn Wren Kennedy	prop_val=`zfs get -H -o source $prop $dataset`
42f38cb554SJohn Wren Kennedy
43f38cb554SJohn Wren Kennedy        if [[ $? -ne 0 ]]; then
44f38cb554SJohn Wren Kennedy                log_fail "Unable to determine the source of $prop " \
45f38cb554SJohn Wren Kennedy                        "property for dataset $dataset"
46f38cb554SJohn Wren Kennedy        else
47f38cb554SJohn Wren Kennedy                echo $prop_val
48f38cb554SJohn Wren Kennedy        fi
49f38cb554SJohn Wren Kennedy}
50f38cb554SJohn Wren Kennedy
51f38cb554SJohn Wren Kennedy#
52f38cb554SJohn Wren Kennedy# Function to check the 'source' of a property. The source can
53f38cb554SJohn Wren Kennedy# either be "default", "local", or "inherited from <parent dataset>".
54f38cb554SJohn Wren Kennedy#
55f38cb554SJohn Wren Kennedy# The 'expected src' argument must be either "default", "local", or
56f38cb554SJohn Wren Kennedy# a dataset name.
57f38cb554SJohn Wren Kennedy#
58f38cb554SJohn Wren Kennedy# Returns 0 on success, 1 on failure.
59f38cb554SJohn Wren Kennedy#
60f38cb554SJohn Wren Kennedyfunction verify_prop_src # child_dataset property expected_src
61f38cb554SJohn Wren Kennedy{
62f38cb554SJohn Wren Kennedy        typeset target=$1
63f38cb554SJohn Wren Kennedy        typeset prop=$2
64f38cb554SJohn Wren Kennedy        typeset expected=$3
65f38cb554SJohn Wren Kennedy
66f38cb554SJohn Wren Kennedy        prop_src=`get_prop_src $prop $target`
67f38cb554SJohn Wren Kennedy
68f38cb554SJohn Wren Kennedy	#
69f38cb554SJohn Wren Kennedy	# Rather than just checking if $prop_src == $expected
70f38cb554SJohn Wren Kennedy	# we first determine what value $expected should have.
71f38cb554SJohn Wren Kennedy	# This allows us to catch the case where a property
72f38cb554SJohn Wren Kennedy	# has a source of "local" but we expected it to be
73f38cb554SJohn Wren Kennedy	# "default"
74f38cb554SJohn Wren Kennedy	#
75f38cb554SJohn Wren Kennedy	if [[ $expected == "default" ]]; then
76f38cb554SJohn Wren Kennedy		if [[ $prop_src != $expected ]]; then
77f38cb554SJohn Wren Kennedy			log_note "Property $prop of $target has source"\
78f38cb554SJohn Wren Kennedy				" $prop_src rather than $expected"
79f38cb554SJohn Wren Kennedy			return 1
80f38cb554SJohn Wren Kennedy		fi
81f38cb554SJohn Wren Kennedy	elif [[ $expected == "local" ]]; then
82f38cb554SJohn Wren Kennedy		if [[ $prop_src != $expected ]]; then
83f38cb554SJohn Wren Kennedy			log_note "Property $prop of $target has source"\
84f38cb554SJohn Wren Kennedy				" $prop_src rather than $expected"
85f38cb554SJohn Wren Kennedy			return 1
86f38cb554SJohn Wren Kennedy		fi
87f38cb554SJohn Wren Kennedy	elif [[ $prop_src != "inherited from $expected" ]]; then
88f38cb554SJohn Wren Kennedy		log_note "Property $prop of $expected has source $prop_src"\
89f38cb554SJohn Wren Kennedy			" rather than 'inherited from $expected'"
90f38cb554SJohn Wren Kennedy                return 1
91f38cb554SJohn Wren Kennedy	fi
92f38cb554SJohn Wren Kennedy
93f38cb554SJohn Wren Kennedy	return 0
94f38cb554SJohn Wren Kennedy}
95f38cb554SJohn Wren Kennedy
96f38cb554SJohn Wren Kennedy#
97f38cb554SJohn Wren Kennedy# Simple function to set a property to a
98f38cb554SJohn Wren Kennedy# specified value and verify it has changed
99f38cb554SJohn Wren Kennedy# correctly.
100f38cb554SJohn Wren Kennedy#
101f38cb554SJohn Wren Kennedyfunction set_n_verify_prop #property value dataset
102f38cb554SJohn Wren Kennedy{
103f38cb554SJohn Wren Kennedy	typeset prop=$1
104f38cb554SJohn Wren Kennedy	typeset prop_val=$2
105f38cb554SJohn Wren Kennedy	typeset dataset=$3
106f38cb554SJohn Wren Kennedy
107*1d32ba66SJohn Wren Kennedy	zfs set $prop=$prop_val $dataset
108f38cb554SJohn Wren Kennedy	check_val=`get_prop $prop $dataset`
109f38cb554SJohn Wren Kennedy
110f38cb554SJohn Wren Kennedy	if [[ $check_val != $prop_val ]]; then
111f38cb554SJohn Wren Kennedy		log_fail "Property $prop of $dataset has value $check_val"\
112f38cb554SJohn Wren Kennedy			" rather than $prop_val"
113f38cb554SJohn Wren Kennedy	fi
114f38cb554SJohn Wren Kennedy}
115