1#! /bin/ksh -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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
34
35#
36# DESCRIPTION:
37#	Verify 'snapshot -r' and 'destroy -r' can correctly create and destroy
38#	snapshot tree respectively.
39#
40# STRATEGY:
41# 1. Use the snapshot -r to create snapshot for top level pool
42# 2. Verify the children snapshots are created correctly.
43# 3. Use destroy -r to destroy the top level snapshot
44# 4. Verify that all children snapshots are destroyed too.
45#
46
47verify_runnable "both"
48
49function cleanup
50{
51	typeset ds
52	typeset snap
53
54	for ds in $ctr/$TESTVOL1 $ctr/$TESTCLONE; do
55		destroy_dataset $ds "-rf"
56	done
57
58	for snap in $ctr/$TESTFS1@$TESTSNAP1 \
59		$snappool $snapvol $snapctr $snapctrvol \
60		$snapctrclone $snapctrfs
61	do
62		snapexists $snap && destroy_dataset $snap "-rf"
63	done
64}
65
66log_assert "Verify snapshot -r can correctly create a snapshot tree."
67log_onexit cleanup
68
69ctr=$TESTPOOL/$TESTCTR
70ctrfs=$ctr/$TESTFS1
71ctrclone=$ctr/$TESTCLONE
72ctrvol=$ctr/$TESTVOL1
73snappool=$SNAPPOOL
74snapfs=$SNAPFS
75snapctr=$ctr@$TESTSNAP
76snapvol=$SNAPFS1
77snapctrvol=$ctrvol@$TESTSNAP
78snapctrclone=$ctrclone@$TESTSNAP
79snapctrfs=$SNAPCTR
80
81#preparation for testing
82log_must zfs snapshot $ctrfs@$TESTSNAP1
83log_must zfs clone $ctrfs@$TESTSNAP1 $ctrclone
84if is_global_zone; then
85	log_must zfs create -V $VOLSIZE $ctrvol
86else
87	log_must zfs create $ctrvol
88fi
89
90log_must zfs snapshot -r $snappool
91block_device_wait
92
93#verify the snapshot -r results
94for snap in $snappool $snapfs $snapvol $snapctr $snapctrvol \
95		$snapctrclone $snapctrfs
96do
97	! snapexists $snap && \
98		log_fail "The snapshot $snap is not created via -r option."
99done
100
101log_note "Verify that destroy -r can destroy the snapshot tree."
102
103log_must zfs destroy -r $snappool
104for snap in $snappool $snapfs $snapvol $snapctr $snapctrvol \
105		$snapctrclone $snapctrfs
106do
107	snapexists $snap && \
108		log_fail "The snapshot $snap is not destroyed correctly."
109done
110
111log_note "Verify that the snapshot with different name should \
112		be not destroyed."
113! snapexists $ctrfs@$TESTSNAP1 && \
114	log_fail "destroy -r incorrectly destroys the snapshot \
115		$ctrfs@$TESTSNAP1."
116
117log_pass  "snapshot|destroy -r with snapshot tree works as expected."
118