1#!/usr/local/bin/ksh93 -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. $STF_SUITE/include/libtest.kshlib
28. $STF_SUITE/tests/cli_root/zfs_destroy/zfs_destroy_common.kshlib
29
30#################################################################################
31#
32# __stc_assertion_start
33#
34# ID: zfs_destroy_001_pos
35#
36# DESCRIPTION:
37#	'zfs destroy -r|-rf|-R|-Rf <fs|ctr|vol|snap>' should recursively destroy
38#	all children and clones based on options.
39#
40# STRATEGY:
41#	1. Create test environment according to options. There are three test
42#	models can be created. Only ctr, fs & vol; with snap; with clone.
43#	2. According to option, make the dataset busy or not.
44#	3. Run 'zfs destroy [-rRf] <dataset>'
45#	4. According to dataset and option, check if get the expected results.
46#
47# TESTABILITY: explicit
48#
49# TEST_AUTOMATION_LEVEL: automated
50#
51# CODING_STATUS: COMPLETED (2005-07-22)
52#
53# __stc_assertion_end
54#
55################################################################################
56
57verify_runnable "both"
58
59#
60# According to parameters, 1st, create suitable testing environment. 2nd,
61# run 'zfs destroy $opt <dataset>'. 3rd, check the system status.
62#
63# $1 option of 'zfs destroy'
64# $2 dataset will be destroied.
65#
66function test_n_check
67{
68	typeset opt=$1
69	typeset dtst=$2
70
71	if ! is_global_zone ; then
72		if [[ $dtst == $VOL || $dtst == $VOLSNAP ]]; then
73			log_note "UNSUPPORTED: Volume are unavailable in LZ."
74			return
75		fi
76	fi
77
78	# '-f' has no effect on non-filesystems
79	if [[ $opt == -f ]]; then
80		if [[ $dtst != $FS || $dtst != $CTR ]]; then
81			log_note "UNSUPPORTED: '-f ' is only available for FS."
82			return
83		fi
84	fi
85
86	# Clean the test environment and make it clear.
87	if datasetexists $CTR; then
88		log_must $ZFS destroy -Rf $CTR
89	fi
90
91	# According to option create test compatible environment.
92	case $opt in
93		-r|-rf) setup_testenv snap ;;
94		-R|-Rf) setup_testenv clone ;;
95		-f) 	setup_testenv ;;
96		*)	log_fail "Incorrect option: '$opt'." ;;
97	esac
98
99	#
100	# According to different dataset type, create busy condition when try to
101	# destroy this dataset.
102	#
103	typeset mpt_dir
104	case $dtst in
105		$CTR|$FS)
106			if [[ $opt == *f* ]]; then
107				mpt_dir=$(get_prop mountpoint $FS)
108				make_dir_busy $mpt_dir
109				log_mustnot $ZFS destroy -rR $dtst
110				make_dir_unbusy $mpt_dir
111			fi
112			;;
113		$VOL)
114			if [[ $opt == *f* ]]; then
115				make_dir_busy $TESTDIR1
116				log_mustnot $ZFS destroy -rR $dtst
117				make_dir_unbusy $TESTDIR1
118			fi
119			;;
120		$FSSNAP)
121			if [[ $opt == *f* ]]; then
122				mpt_dir=$(snapshot_mountpoint $dtst)
123				init_dir=$PWD
124				make_dir_busy $mpt_dir
125				log_must $ZFS destroy -rR $dtst
126				log_must $ZFS snapshot $dtst
127				make_dir_unbusy $mpt_dir
128			fi
129			;;
130		$VOLSNAP)
131			if [[ $opt == *f* ]]; then
132				mpt_dir=$TESTDIR1
133				init_dir=$PWD
134				make_dir_busy $mpt_dir
135				log_must $ZFS destroy -rR $dtst
136				log_must $ZFS snapshot $dtst
137				make_dir_unbusy $mpt_dir
138			fi
139			;;
140		*)	log_fail "Unsupported dataset: '$dtst'."
141	esac
142
143	# Firstly, umount ufs filesystem which was created by zfs volume.
144	if is_global_zone; then
145		log_must $UMOUNT -f $TESTDIR1
146	fi
147	# Invoke 'zfs destroy [-rRf] <dataset>'
148	log_must $ZFS destroy $opt $dtst
149
150	case $dtst in
151		$CTR)	check_dataset datasetnonexists \
152					$CTR $FS $VOL $FSSNAP $VOLSNAP
153			if [[ $opt == *R* ]]; then
154				check_dataset datasetnonexists \
155					$FSCLONE $VOLCLONE
156			fi
157			;;
158		$FS)	check_dataset datasetexists $CTR $VOL
159			check_dataset datasetnonexists $FS
160			if [[ $opt != -f ]]; then
161				check_dataset datasetexists $VOLSNAP
162				check_dataset datasetnonexists $FSSNAP
163			fi
164			if [[ $opt == *R* ]]; then
165				check_dataset datasetexists $VOLCLONE
166				check_dataset datasetnonexists $FSCLONE
167			fi
168			;;
169		$VOL)	check_dataset datasetexists $CTR $FS $FSSNAP
170			check_dataset datasetnonexists $VOL $VOLSNAP
171			if [[ $opt == *R* ]]; then
172				check_dataset datasetexists $FSCLONE
173				check_dataset datasetnonexists $VOLCLONE
174			fi
175			;;
176		$FSSNAP)
177			check_dataset datasetexists $CTR $FS $VOL $VOLSNAP
178			check_dataset datasetnonexists $FSSNAP
179			if [[ $opt == *R* ]]; then
180				check_dataset datasetexists $VOLCLONE
181				check_dataset datasetnonexists $FSCLONE
182			fi
183			;;
184		$VOLSNAP)
185			check_dataset datasetexists $CTR $FS $VOL $FSSNAP
186			check_dataset datasetnonexists $VOLSNAP
187			if [[ $opt == *R* ]]; then
188				check_dataset datasetexists $FSCLONE
189				check_dataset datasetnonexists $VOLCLONE
190			fi
191			;;
192	esac
193
194	log_note "'$ZFS destroy $opt $dtst' passed."
195}
196
197log_assert "'zfs destroy -r|-R|-f|-rf|-Rf <fs|ctr|vol|snap>' should " \
198	"recursively destroy all children."
199log_onexit cleanup_testenv
200
201typeset dtst=""
202typeset opt=""
203for dtst in $CTR $FS $VOL $FSSNAP $VOLSNAP; do
204	for opt in "-r" "-R" "-f" "-rf" "-Rf"; do
205		log_note "Starting test: $ZFS destroy $opt $dtst"
206		test_n_check $opt $dtst
207	done
208done
209
210log_pass "'zfs destroy -r|-R|-f|-rf|-Rf <fs|ctr|vol|snap>' passed."
211