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 2008 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_rollback/zfs_rollback_common.kshlib
29
30################################################################################
31#
32# __stc_assertion_start
33#
34# ID: zfs_rollback_001_pos
35#
36# DESCRIPTION:
37# 	'zfs rollback -r|-rf|-R|-Rf' will recursively destroy any snapshots
38#	more recent than the one specified.
39#
40# STRATEGY:
41#	1. Create pool, fs & volume.
42#	2. Separately create three snapshots or clones for fs & volume
43#	3. Roll back to the second snapshot and check the results.
44#	4. Create the third snapshot or clones for fs & volume again.
45#	5. Roll back to the first snapshot and check the results.
46#	6. Separately create two snapshots for fs & volume.
47#	7. Roll back to the first snapshot and check the results.
48#
49# TESTABILITY: explicit
50#
51# TEST_AUTOMATION_LEVEL: automated
52#
53# CODING_STATUS: COMPLETED (2005-07-29)
54#
55# __stc_assertion_end
56#
57################################################################################
58
59verify_runnable "both"
60
61log_assert "'zfs rollback -r|-rf|-R|-Rf' will recursively destroy any " \
62	"snapshots more recent than the one specified."
63log_onexit cleanup_env
64
65#
66# Create suitable test environment and run 'zfs rollback', then compare with
67# expected value to check the system status.
68#
69# $1 option.
70# $2 the number of snapshots or clones.
71# $3 the number of snapshot point which we want to rollback.
72#
73function test_n_check #opt num_snap_clone num_rollback
74{
75	typeset opt=$1
76	typeset -i cnt=$2
77	typeset -i pointcnt=$3
78	typeset dtst
79
80	(( cnt > 3 || pointcnt > cnt )) && \
81		log_fail "Unsupported testing condition."
82
83	# Clean up the test environment
84	datasetexists $FS && log_must $ZFS destroy -Rf $FS
85	if datasetexists $VOL; then
86		$MOUNT | grep -q "/dev/zvol/$VOL" > /dev/null 2>&1
87		(( $? == 0 )) && log_must $UMOUNT -f $TESTDIR1
88
89		log_must $ZFS destroy -Rf $VOL
90	fi
91
92	# Create specified test environment
93	case $opt in
94		*r*) setup_snap_env $cnt ;;
95		*R*) setup_clone_env $cnt ;;
96	esac
97
98	all_snap="$TESTSNAP $TESTSNAP1 $TESTSNAP2"
99	all_clone="$TESTCLONE $TESTCLONE1 $TESTCLONE2"
100	typeset snap_point
101	typeset exist_snap
102	typeset exist_clone
103	case $pointcnt in
104		1) snap_point=$TESTSNAP
105		   exist_snap=$TESTSNAP
106		   [[ $opt == *R* ]] && exist_clone=$TESTCLONE
107		   ;;
108		2) snap_point=$TESTSNAP1
109		   exist_snap="$TESTSNAP $TESTSNAP1"
110		   [[ $opt == *R* ]] && exist_clone="$TESTCLONE $TESTCLONE1"
111		   ;;
112	esac
113
114	typeset snap
115	for dtst in $FS $VOL; do
116		# Volume is not available in Local Zone.
117		if [[ $dtst == $VOL ]]; then
118			if ! is_global_zone; then
119				break
120			fi
121		fi
122		if [[ $opt == *f* ]]; then
123			# To write data to the mountpoint directory,
124			write_mountpoint_dir $dtst
125			opt=${opt%f}
126		fi
127
128		if [[ $dtst == $VOL ]]; then
129			log_must $UMOUNT -f $TESTDIR1
130			log_must $ZFS rollback $opt $dtst@$snap_point
131			log_must $MOUNT \
132				/dev/zvol/$TESTPOOL/$TESTVOL $TESTDIR1
133		else
134			log_must $ZFS rollback $opt $dtst@$snap_point
135		fi
136
137		for snap in $all_snap; do
138			if [[ " $exist_snap " == *" $snap "* ]]; then
139				log_must datasetexists $dtst@$snap
140			else
141				log_must datasetnonexists $dtst@$snap
142			fi
143		done
144		for clone in $all_clone; do
145			if [[ " $exist_clone " == *" $clone "* ]]; then
146				log_must datasetexists $dtst$clone
147			else
148				log_must datasetnonexists $dtst$clone
149			fi
150		done
151
152		check_files $dtst@$snap_point
153	done
154}
155
156typeset opt
157for opt in "-r" "-rf" "-R" "-Rf"; do
158	#
159	# Currently, the test case was limited to create and rollback
160	# in three snapshots
161	#
162	log_note "Create 3 snapshots, rollback to the 2nd snapshot " \
163		"using $opt."
164	test_n_check "$opt" 3 2
165
166	log_note "Create 3 snapshots and rollback to the 1st snapshot " \
167		"using $opt."
168	test_n_check "$opt" 3 1
169
170	log_note "Create 2 snapshots and rollback to the 1st snapshot " \
171		"using $opt."
172	test_n_check "$opt" 2 1
173done
174
175log_pass "'zfs rollback -r|-rf|-R|-Rf' recursively destroy any snapshots more "\
176	"recent than the one specified passed."
177