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