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