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/tests/cli_root/cli_common.kshlib
28
29#################################################################################
30#
31# __stc_assertion_start
32#
33# ID: zfs_receive_008_pos
34#
35# DESCRIPTION:
36#	Verifying 'zfs receive -vn [<filesystem|snapshot>]
37#		   and zfs receive -vn -d <filesystem>'
38#
39# STRATEGY:
40#	1. Fill in fs with some data
41#	2. Create full and incremental send stream
42#	3. run zfs receive with -v option
43#	3. Dryrun zfs receive with -vn option
44#	3. Dryrun zfs receive with -vn -d option
45#	4. Verify receive output and result
46#
47# TESTABILITY: explicit
48#
49# TEST_AUTOMATION_LEVEL: automated
50#
51# CODING_STATUS: COMPLETED (2007-06-14)
52#
53# __stc_assertion_end
54#
55################################################################################
56function cleanup
57{
58	for dset in $rst_snap $rst_fs $orig_snap; do
59		if datasetexists $dset; then
60			log_must $ZFS destroy -fr $dset
61		fi
62	done
63
64	for file in $fbackup $mnt_file $tmp_out; do
65		if [[ -f $file ]]; then
66			log_must $RM -f $file
67		fi
68	done
69
70	if datasetexists $TESTPOOL/$TESTFS; then
71		log_must $ZFS destroy -Rf $TESTPOOL/$TESTFS
72		log_must $ZFS create $TESTPOOL/$TESTFS
73		log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
74	fi
75}
76
77verify_runnable "both"
78log_assert "Verifying 'zfs receive -vn [<filesystem|snapshot>] " \
79		"and zfs receive -vn -d <filesystem>'"
80
81log_onexit cleanup
82
83typeset datasets="$TESTPOOL/$TESTFS $TESTPOOL"
84typeset rst_fs=$TESTPOOL/$TESTFS/$TESTFS
85typeset fbackup=$TMPDIR/fbackup.${TESTCASE_ID}
86typeset tmp_out=$TMPDIR/tmpout.${TESTCASE_ID}
87
88for orig_fs in $datasets ; do
89	typeset rst_snap=$rst_fs@snap
90	typeset orig_snap=$orig_fs@snap
91	typeset verb_msg="receiving full stream of ${orig_snap} into ${rst_snap}"
92	typeset dryrun_msg="would receive full stream of ${orig_snap} into ${rst_snap}"
93
94	if ! datasetexists $orig_fs; then
95		log_must $ZFS create $orig_fs
96	fi
97
98	typeset mntpnt
99	mntpnt=$(get_prop mountpoint $orig_fs)
100	if [[ $? -ne 0 ]] ; then
101		log_fail "get_prop mountpoint $orig_fs failed"
102	fi
103
104	typeset mnt_file=$mntpnt/file1
105
106	log_must $MKFILE 100m $mnt_file
107	log_must $ZFS snapshot $orig_snap
108	log_must eval "$ZFS send $orig_snap > $fbackup"
109
110	for opt in "-v"  "-vn"; do
111		if datasetexists $rst_fs; then
112			log_must $ZFS destroy -fr $rst_fs
113		fi
114		log_note "Check ZFS receive $opt [<filesystem|snapshot>]"
115		log_must eval "$ZFS receive $opt $rst_fs < $fbackup > $tmp_out 2>&1"
116		if [[ $opt == "-v" ]]; then
117			log_must eval "$GREP \"$verb_msg\" $tmp_out >/dev/null 2>&1"
118			if ! datasetexists $rst_snap; then
119				log_fail "dataset was not received, even though the"\
120					" -v flag was used."
121			fi
122		else
123			log_must eval "$GREP \"$dryrun_msg\" $tmp_out >/dev/null 2>&1"
124			if datasetexists $rst_snap; then
125				log_fail "dataset was received, even though the -nv"\
126					" flag was used."
127			fi
128		fi
129	done
130
131	log_note "Check ZFS receive -vn -d <filesystem>"
132	if ! datasetexists $rst_fs; then
133		log_must $ZFS create $rst_fs
134	fi
135	log_must eval "$ZFS receive -vn -d -F $rst_fs <$fbackup >$tmp_out 2>&1"
136	typeset relative_path=""
137	if [[ ${orig_fs} == *"/"* ]]; then
138		relative_path=${orig_fs#*/}
139	fi
140
141	typeset leaf_fs=${rst_fs}/${relative_path}
142	leaf_fs=${leaf_fs%/}
143	rst_snap=${leaf_fs}@snap
144	dryrun_msg="would receive full stream of ${orig_snap} into ${rst_snap}"
145
146	log_must eval "$GREP \"$dryrun_msg\" $tmp_out > /dev/null 2>&1"
147
148	if datasetexists $rst_snap; then
149		log_fail "dataset $rst_snap should not existed."
150	fi
151	log_must $ZFS destroy -Rf $rst_fs
152
153	cleanup
154done
155
156log_pass "zfs receive -vn [<filesystem|snapshot>] and " \
157	"zfs receive -vn -d <filesystem>' succeed."
158