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_001_pos
34#
35# DESCRIPTION:
36#	Verifying 'zfs receive [<filesystem|snapshot>] -d <filesystem>' works.
37#
38# STRATEGY:
39#	1. Fill in fs with some data
40#	2. Create full and incremental send stream
41#	3. Receive the send stream
42#	4. Verify the restoring results.
43#
44# TESTABILITY: explicit
45#
46# TEST_AUTOMATION_LEVEL: automated
47#
48# CODING_STATUS: COMPLETED (2005-09-06)
49#
50# __stc_assertion_end
51#
52################################################################################
53
54verify_runnable "both"
55
56function cleanup
57{
58	typeset -i i=0
59
60	datasetexists $rst_root && \
61		log_must $ZFS destroy -Rf $rst_root
62	while (( i < 2 )); do
63		snapexists ${orig_snap[$i]} && \
64			log_must $ZFS destroy -f ${orig_snap[$i]}
65		log_must $RM -f ${bkup[$i]}
66
67		(( i = i + 1 ))
68	done
69
70	log_must $RM -rf $TESTDIR1
71}
72
73function recreate_root
74{
75	datasetexists $rst_root && \
76		log_must $ZFS destroy -Rf $rst_root
77	if [[ -d $TESTDIR1 ]] ; then
78		log_must $RM -rf $TESTDIR1
79	fi
80	log_must $ZFS create $rst_root
81	log_must $ZFS set mountpoint=$TESTDIR1 $rst_root
82}
83
84log_assert "Verifying 'zfs receive [<filesystem|snapshot>] -d <filesystem>' works."
85log_onexit cleanup
86
87typeset datasets="$TESTPOOL/$TESTFS $TESTPOOL"
88set -A bkup "$TMPDIR/fullbkup" "$TMPDIR/incbkup"
89orig_sum=""
90rst_sum=""
91rst_root=$TESTPOOL/rst_ctr
92rst_fs=${rst_root}/$TESTFS
93
94for orig_fs in $datasets ; do
95	#
96	# Preparations for testing
97	#
98	recreate_root
99
100	set -A orig_snap "${orig_fs}@init_snap" "${orig_fs}@inc_snap"
101	typeset mntpnt=$(get_prop mountpoint ${orig_fs})
102	set -A orig_data "${mntpnt}/$TESTFILE1" "${mntpnt}/$TESTFILE2"
103
104	typeset relative_path=""
105	if [[ ${orig_fs} == *"/"* ]]; then
106		relative_path=${orig_fs#*/}
107	fi
108
109	typeset leaf_fs=${rst_root}/${relative_path}
110	leaf_fs=${leaf_fs%/}
111	rst_snap=${leaf_fs}@snap
112
113	set -A rst_snap "$rst_root/$TESTFS@init_snap" "$rst_root/$TESTFS@inc_snap"
114	set -A rst_snap2 "${leaf_fs}@init_snap" "${leaf_fs}@inc_snap"
115	set -A rst_data "$TESTDIR1/$TESTFS/$TESTFILE1" "$TESTDIR1/$TESTFS/$TESTFILE2"
116	set -A rst_data2 "$TESTDIR1/${relative_path}/$TESTFILE1" "$TESTDIR1/${relative_path}/$TESTFILE2"
117
118	typeset -i i=0
119	while (( i < ${#orig_snap[*]} )); do
120		log_must $FILE_WRITE -o create -f ${orig_data[$i]} \
121			-b $BLOCK_SIZE -c $WRITE_COUNT
122		log_must $ZFS snapshot ${orig_snap[$i]}
123		if (( i < 1 )); then
124			log_must eval "$ZFS send ${orig_snap[$i]} > ${bkup[$i]}"
125		else
126			log_must eval "$ZFS send -i ${orig_snap[(( i - 1 ))]} \
127				${orig_snap[$i]} > ${bkup[$i]}"
128		fi
129
130		(( i = i + 1 ))
131	done
132
133	log_note "Verifying 'zfs receive <filesystem>' works."
134	i=0
135	while (( i < ${#bkup[*]} )); do
136		if (( i > 0 )); then
137			log_must $ZFS rollback ${rst_snap[0]}
138		fi
139		log_must eval "$ZFS receive $rst_fs < ${bkup[$i]}"
140		snapexists ${rst_snap[$i]} || \
141			log_fail "Restoring filesystem fails. ${rst_snap[$i]} not exist"
142		compare_cksum ${orig_data[$i]} ${rst_data[$i]}
143
144		(( i = i + 1 ))
145	done
146
147	log_must $ZFS destroy -Rf $rst_fs
148
149	log_note "Verifying 'zfs receive <snapshot>' works."
150	i=0
151	while (( i < ${#bkup[*]} )); do
152		if (( i > 0 )); then
153			log_must $ZFS rollback ${rst_snap[0]}
154		fi
155		log_must eval "$ZFS receive ${rst_snap[$i]} <${bkup[$i]}"
156		snapexists ${rst_snap[$i]} || \
157			log_fail "Restoring filesystem fails. ${rst_snap[$i]} not exist"
158		compare_cksum ${orig_data[$i]} ${rst_data[$i]}
159
160		(( i = i + 1 ))
161	done
162
163	log_must $ZFS destroy -Rf $rst_fs
164
165	log_note "Verfiying 'zfs receive -d <filesystem>' works."
166
167	i=0
168	while (( i < ${#bkup[*]} )); do
169		if (( i > 0 )); then
170			log_must $ZFS rollback ${rst_snap2[0]}
171		fi
172		log_must eval "$ZFS receive -d -F $rst_root <${bkup[$i]}"
173		snapexists ${rst_snap2[$i]} || \
174			log_fail "Restoring filesystem fails. ${rst_snap2[$i]} not exist"
175		compare_cksum ${orig_data[$i]} ${rst_data2[$i]}
176
177		(( i = i + 1 ))
178	done
179
180	cleanup
181done
182
183log_pass "Verifying 'zfs receive [<filesystem|snapshot>] -d <filesystem>' succeeds."
184