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 http://www.opensolaris.org/os/licensing.
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 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy.cfg
33
34#
35# Create or recover a set of test environment which include ctr, vol, fs,
36# snap & clone. It looks like the following.
37#
38# pool
39#    |ctr
40#    |  |fs
41#    |  | |fssnap
42#    |  |vol
43#    |     |volsnap
44#    |fsclone
45#    |volclone
46#
47# $1 indicate which dependent dataset need be created. Such as 'snap', 'clone'.
48#
49function setup_testenv #[dtst]
50{
51	typeset dtst=$1
52
53	if ! datasetexists $CTR; then
54		log_must zfs create $CTR
55	fi
56	if ! datasetexists $FS; then
57		log_must zfs create $FS
58	fi
59	# Volume test is only availible on globle zone
60	if ! datasetexists $VOL && is_global_zone; then
61		log_must zfs create -V $VOLSIZE $VOL
62
63		echo "y" | newfs /dev/zvol/dsk/$VOL > /dev/null 2>&1
64		if (( $? == 0 )); then
65			log_note "SUCCESS: newfs /dev/zvol/dsk/$VOL>/dev/null"
66		else
67			log_fail "newfs /dev/zvol/dsk/$VOL > /dev/null"
68		fi
69
70		if [[ ! -d $TESTDIR1 ]]; then
71			log_must mkdir $TESTDIR1
72		fi
73		log_must mount /dev/zvol/dsk/$VOL $TESTDIR1
74	fi
75
76	if [[ $dtst == snap || $dtst == clone ]]; then
77		if ! datasetexists $FSSNAP; then
78			log_must zfs snapshot $FSSNAP
79		fi
80		if ! datasetexists $VOLSNAP && is_global_zone; then
81			log_must zfs snapshot $VOLSNAP
82		fi
83	fi
84
85	if [[ $dtst == clone ]]; then
86		if ! datasetexists $FSCLONE; then
87			log_must zfs clone $FSSNAP $FSCLONE
88		fi
89		if ! datasetexists $VOLCLONE && is_global_zone; then
90			log_must zfs clone $VOLSNAP $VOLCLONE
91		fi
92	fi
93}
94
95# Clean up the testing environment
96#
97function cleanup_testenv
98{
99	if is_global_zone && ismounted "$TESTDIR1" "ufs" ; then
100		log_must umount -f $TESTDIR1
101	fi
102	if [[ -d $TESTDIR1 ]]; then
103		log_must rm -rf $TESTDIR1
104	fi
105
106	pkill mkbusy
107
108	if datasetexists $CTR; then
109		log_must zfs destroy -Rf $CTR
110	fi
111}
112
113#
114# Delete volume and related datasets from list, if the test cases was
115# runing in local zone. Then check them are existed or non-exists.
116#
117# $1   function name
118# $2-n datasets name
119#
120function check_dataset
121{
122	typeset funname=$1
123	typeset newlist=""
124	typeset dtst
125	shift
126
127	for dtst in "$@"; do
128		# Volume and related stuff are unvailable in local zone
129		if ! is_global_zone; then
130			if [[ $dtst == $VOL || $dtst == $VOLSNAP || \
131				$dtst == $VOLCLONE ]]
132			then
133				continue
134			fi
135		fi
136		newlist="$newlist $dtst"
137	done
138
139	if (( ${#newlist} != 0 )); then
140		# Run each item in $newlist individually so on failure, the
141		# probelmatic dataset is listed in the logs.
142		for i in $newlist; do
143			log_must $funname $i
144		done
145	fi
146}
147