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