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