1#!/bin/ksh -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
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/tests/functional/cli_root/cli_common.kshlib
33
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
45verify_runnable "both"
46
47function cleanup
48{
49	typeset -i i=0
50
51	datasetexists $rst_root && destroy_dataset $rst_root -Rf
52	while (( i < 2 )); do
53		snapexists ${orig_snap[$i]} && destroy_dataset ${orig_snap[$i]} -f
54		log_must rm -f ${bkup[$i]}
55
56		(( i = i + 1 ))
57	done
58
59	log_must rm -rf $TESTDIR1
60}
61
62function recreate_root
63{
64	datasetexists $rst_root && destroy_dataset $rst_root -Rf
65	if [[ -d $TESTDIR1 ]] ; then
66		log_must rm -rf $TESTDIR1
67	fi
68	log_must zfs create $rst_root
69	log_must zfs set mountpoint=$TESTDIR1 $rst_root
70}
71
72log_assert "Verifying 'zfs receive [<filesystem|snapshot>] -d <filesystem>' works."
73log_onexit cleanup
74
75typeset datasets="$TESTPOOL/$TESTFS $TESTPOOL"
76set -A bkup "$TEST_BASE_DIR/fullbkup" "$TEST_BASE_DIR/incbkup"
77orig_sum=""
78rst_sum=""
79rst_root=$TESTPOOL/rst_ctr
80rst_fs=${rst_root}/$TESTFS
81
82for orig_fs in $datasets ; do
83	#
84	# Preparations for testing
85	#
86	recreate_root
87
88	set -A orig_snap "${orig_fs}@init_snap" "${orig_fs}@inc_snap"
89	typeset mntpnt=$(get_prop mountpoint ${orig_fs})
90	set -A orig_data "${mntpnt}/$TESTFILE1" "${mntpnt}/$TESTFILE2"
91
92	typeset relative_path=""
93	if [[ ${orig_fs} == *"/"* ]]; then
94		relative_path=${orig_fs#*/}
95	fi
96
97	typeset leaf_fs=${rst_root}/${relative_path}
98	leaf_fs=${leaf_fs%/}
99	rst_snap=${leaf_fs}@snap
100
101	set -A rst_snap "$rst_root/$TESTFS@init_snap" "$rst_root/$TESTFS@inc_snap"
102	set -A rst_snap2 "${leaf_fs}@init_snap" "${leaf_fs}@inc_snap"
103	set -A rst_data "$TESTDIR1/$TESTFS/$TESTFILE1" "$TESTDIR1/$TESTFS/$TESTFILE2"
104	set -A rst_data2 "$TESTDIR1/${relative_path}/$TESTFILE1" "$TESTDIR1/${relative_path}/$TESTFILE2"
105
106	typeset -i i=0
107	while (( i < ${#orig_snap[*]} )); do
108		file_write -o create -f ${orig_data[$i]} -b 512 \
109		    -c 8 >/dev/null 2>&1
110		(( $? != 0 )) && \
111			log_fail "Writing data into zfs filesystem fails."
112		log_must zfs snapshot ${orig_snap[$i]}
113		if (( i < 1 )); then
114			log_must eval "zfs send ${orig_snap[$i]} > ${bkup[$i]}"
115		else
116			log_must eval "zfs send -i ${orig_snap[(( i - 1 ))]} \
117				${orig_snap[$i]} > ${bkup[$i]}"
118		fi
119
120		(( i = i + 1 ))
121	done
122
123	log_note "Verifying 'zfs receive <filesystem>' works."
124	i=0
125	while (( i < ${#bkup[*]} )); do
126		if (( i > 0 )); then
127			log_must zfs rollback ${rst_snap[0]}
128		fi
129		log_must eval "zfs receive $rst_fs < ${bkup[$i]}"
130		snapexists ${rst_snap[$i]} || \
131			log_fail "Restoring filesystem fails. ${rst_snap[$i]} not exist"
132		compare_cksum ${orig_data[$i]} ${rst_data[$i]}
133
134		(( i = i + 1 ))
135	done
136
137	log_must zfs destroy -Rf $rst_fs
138
139	log_note "Verifying 'zfs receive <snapshot>' works."
140	i=0
141	while (( i < ${#bkup[*]} )); do
142		if (( i > 0 )); then
143			log_must zfs rollback ${rst_snap[0]}
144		fi
145		log_must eval "zfs receive ${rst_snap[$i]} <${bkup[$i]}"
146		snapexists ${rst_snap[$i]} || \
147			log_fail "Restoring filesystem fails. ${rst_snap[$i]} not exist"
148		compare_cksum ${orig_data[$i]} ${rst_data[$i]}
149
150		(( i = i + 1 ))
151	done
152
153	log_must zfs destroy -Rf $rst_fs
154
155	log_note "Verifying 'zfs receive -d <filesystem>' works."
156
157	i=0
158	while (( i < ${#bkup[*]} )); do
159		if (( i > 0 )); then
160			log_must zfs rollback ${rst_snap2[0]}
161		fi
162		log_must eval "zfs receive -d -F $rst_root <${bkup[$i]}"
163		snapexists ${rst_snap2[$i]} || \
164			log_fail "Restoring filesystem fails. ${rst_snap2[$i]} not exist"
165		compare_cksum ${orig_data[$i]} ${rst_data2[$i]}
166
167		(( i = i + 1 ))
168	done
169
170	cleanup
171done
172
173log_pass "Verifying 'zfs receive [<filesystem|snapshot>] -d <filesystem>' succeeds."
174