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