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 https://opensource.org/licenses/CDDL-1.0.
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		log_must eval "file_write -o create -f ${orig_data[$i]} -b 512 -c 8 >/dev/null 2>&1"
109		log_must zfs snapshot ${orig_snap[$i]}
110		if (( i < 1 )); then
111			log_must eval "zfs send ${orig_snap[$i]} > ${bkup[$i]}"
112		else
113			log_must eval "zfs send -i ${orig_snap[(( i - 1 ))]} \
114				${orig_snap[$i]} > ${bkup[$i]}"
115		fi
116
117		(( i = i + 1 ))
118	done
119
120	log_note "Verifying 'zfs receive <filesystem>' works."
121	i=0
122	while (( i < ${#bkup[*]} )); do
123		if (( i > 0 )); then
124			log_must zfs rollback ${rst_snap[0]}
125		fi
126		log_must eval "zfs receive $rst_fs < ${bkup[$i]}"
127		snapexists ${rst_snap[$i]} || \
128			log_fail "Restoring filesystem fails. ${rst_snap[$i]} not exist"
129		compare_cksum ${orig_data[$i]} ${rst_data[$i]}
130
131		(( i = i + 1 ))
132	done
133
134	log_must zfs destroy -Rf $rst_fs
135
136	log_note "Verifying 'zfs receive <snapshot>' 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_snap[$i]} <${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 -d <filesystem>' works."
153
154	i=0
155	while (( i < ${#bkup[*]} )); do
156		if (( i > 0 )); then
157			log_must zfs rollback ${rst_snap2[0]}
158		fi
159		log_must eval "zfs receive -d -F $rst_root <${bkup[$i]}"
160		snapexists ${rst_snap2[$i]} || \
161			log_fail "Restoring filesystem fails. ${rst_snap2[$i]} not exist"
162		compare_cksum ${orig_data[$i]} ${rst_data2[$i]}
163
164		(( i = i + 1 ))
165	done
166
167	cleanup
168done
169
170log_pass "Verifying 'zfs receive [<filesystem|snapshot>] -d <filesystem>' succeeds."
171