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 2009 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: snapshot_015_pos
35#
36# DESCRIPTION:
37#	Verify snapshot can be created or destroy via mkdir or rm
38#	in $(get_snapdir_name).
39#
40# STRATEGY:
41#	1. Verify make directories only successfully in $(get_snapdir_name).
42#	2. Verify snapshot can be created and destroy via mkdir and remove
43#	directories in $(get_snapdir_name).
44#	3. Verify rollback to previous snapshot can succeed.
45#	4. Verify remove directory in snapdir can destroy snapshot.
46#
47# TESTABILITY: explicit
48#
49# TEST_AUTOMATION_LEVEL: automated
50#
51# CODING_STATUS: COMPLETED (2006-10-17)
52#
53# __stc_assertion_end
54#
55################################################################################
56
57verify_runnable "both"
58
59function cleanup
60{
61	typeset -i i=0
62	while ((i < snap_cnt)); do
63		typeset snap=$fs@snap.$i
64		datasetexists $snap && log_must $ZFS destroy -f $snap
65
66		((i += 1))
67	done
68}
69
70$ZFS 2>&1 | $GREP "allow" > /dev/null
71(($? != 0)) && log_unsupported
72
73log_assert "Verify snapshot can be created via mkdir in $(get_snapdir_name)."
74log_onexit cleanup
75
76[[ $os_name == "FreeBSD" ]] && \
77	log_uninitiated "Directory operations on the $(get_snapdir_name) directory are not yet supported in FreeBSD"
78
79fs=$TESTPOOL/$TESTFS
80# Verify all the other directories are readonly.
81mntpnt=$(get_prop mountpoint $fs)
82snapdir=$mntpnt/.zfs
83set -A ro_dirs "$snapdir" "$snapdir/snap" "$snapdir/snapshot"
84for dir in ${ro_dirs[@]}; do
85	if [[ -d $dir ]]; then
86		log_mustnot $RM -rf $dir
87		log_mustnot $TOUCH $dir/testfile
88	else
89		log_mustnot $MKDIR $dir
90	fi
91done
92
93# Verify snapshot can be created via mkdir in $(get_snapdir_name)
94typeset -i snap_cnt=5
95typeset -i cnt=0
96while ((cnt < snap_cnt)); do
97	testfile=$mntpnt/testfile.$cnt
98	log_must $MKFILE 1M $testfile
99	log_must $MKDIR $mntpnt/$(get_snapdir_name)/snap.$cnt
100	if ! datasetexists $fs@snap.$cnt ; then
101		log_fail "ERROR: $fs@snap.$cnt should exists."
102	fi
103
104	((cnt += 1))
105done
106
107# Verify rollback to previous snapshot succeed.
108((cnt = RANDOM % snap_cnt))
109log_must $ZFS rollback -r $fs@snap.$cnt
110
111typeset -i i=0
112while ((i < snap_cnt)); do
113	testfile=$mntpnt/testfile.$i
114	if ((i <= cnt)); then
115		if [[ ! -f $testfile ]]; then
116			log_fail "ERROR: $testfile should exists."
117		fi
118	else
119		if [[ -f $testfile ]]; then
120			log_fail "ERROR: $testfile should not exists."
121		fi
122	fi
123
124	((i += 1))
125done
126
127# Verify remove directory in snapdir can destroy snapshot.
128log_must $RMDIR $mntpnt/$(get_snapdir_name)/snap.$cnt
129log_mustnot datasetexists $fs@snap.$cnt
130
131log_pass "Verify snapshot can be created via mkdir in $(get_snapdir_name) passed."
132