xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zfs_send/zfs_send_006_pos.ksh (revision f012ee0c3db17469b492c2cf757226f3d7b1ebbc)
1#!/bin/ksh
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
19#
20
21. $STF_SUITE/include/libtest.shlib
22#
23# DESCRIPTION:
24#	Verify 'zfs send' can generate valid streams with different options
25#
26# STRATEGY:
27#	1. Create datasets
28#	2. Write some data to the datasets
29#	3. Create a full send streams
30#	4. Receive the send stream
31#	5. Do a dry run with different options and verify the generated size
32#          estimate against the received stream
33#
34
35verify_runnable "both"
36
37function cleanup
38{
39	for ds in $datasets; do
40                datasetexists $ds && zfs destroy -rf $ds
41	done
42}
43
44function cal_percentage
45{
46	typeset value=$1
47	return=$(echo "$PERCENT * $value" | bc)
48	return=$(echo "$return / 100" | bc)
49	echo $return
50}
51
52function get_estimate_size
53{
54	typeset snapshot=$1
55	typeset option=$2
56	typeset base_snapshot=${3:-""}
57	if [[ -z $3 ]];then
58		typeset total_size=$(zfs send $option $snapshot 2>&1 | tail -1)
59	else
60		typeset total_size=$(zfs send $option $base_snapshot $snapshot \
61		     2>&1 | tail -1)
62	fi
63	if [[ $options == *"P"* ]]; then
64		total_size=$(echo "$total_size" | awk '{print $2}')
65	else
66		total_size=$(echo "$total_size" | awk '{print $5}')
67		total_size=${total_size%M}
68		total_size=$(echo "$total_size * $block_count" | bc)
69	fi
70	echo $total_size
71
72}
73
74function verify_size_estimates
75{
76	typeset options=$1
77	typeset file_size=$2
78	typeset refer_diff=$(echo "$refer_size - $estimate_size" | bc)
79	refer_diff=$(echo "$refer_diff / 1" | bc)
80	refer_diff=$(echo "$refer_diff" | nawk '{print ($1 < 0) ? ($1 * -1): $1'})
81	typeset file_diff=$(echo "$file_size - $estimate_size" | bc)
82	file_diff=$(echo "$file_diff / 1" | bc)
83	file_diff=$(echo "$file_diff" | nawk '{print ($1 < 0) ? ($1 * -1):$1'})
84	typeset expected_diff=$(cal_percentage $refer_size)
85
86	[[ -z $refer_diff && -z $file_diff && -z $expected_diff ]] && \
87	    log_fail "zfs send $options failed"
88	[[ $refer_diff -le $expected_diff &&  \
89	    $file_diff -le $expected_diff ]] || \
90	    log_fail "zfs send $options gives wrong size estimates"
91}
92
93log_assert "Verify 'zfs send -nvP' generates valid stream estimates"
94log_onexit cleanup
95typeset -l block_count=0
96typeset -l block_size
97typeset -i PERCENT=1
98
99((block_count=1024*1024))
100
101# create dataset
102log_must zfs create $TESTPOOL/$TESTFS1
103
104# create multiple snapshot for the dataset with data
105for block_size in 64 128 256; do
106	log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS1/file$block_size \
107	    bs=1M count=$block_size
108	log_must zfs snapshot $TESTPOOL/$TESTFS1@snap$block_size
109done
110
111full_snapshot="$TESTPOOL/$TESTFS1@snap64"
112increamental_snapshot="$TESTPOOL/$TESTFS1@snap256"
113
114full_size=$(zfs send $full_snapshot 2>&1 | wc -c)
115increamental_size=$(zfs send $increamental_snapshot 2>&1 | wc -c)
116increamental_send=$(zfs send -i $full_snapshot $increamental_snapshot 2>&1 | wc -c)
117
118log_note "verify zfs send -nv"
119options="-nv"
120refer_size=$(get_prop refer $full_snapshot)
121estimate_size=$(get_estimate_size $full_snapshot $options)
122log_must verify_size_estimates $options $full_size
123
124log_note "verify zfs send -Pnv"
125options="-Pnv"
126
127estimate_size=$(get_estimate_size $full_snapshot $options)
128log_must verify_size_estimates $options $full_size
129
130log_note "verify zfs send -nv for multiple snapshot send"
131options="-nv"
132refer_size=$(get_prop refer $increamental_snapshot)
133
134estimate_size=$(get_estimate_size $increamental_snapshot $options)
135log_must verify_size_estimates $options $increamental_size
136
137log_note "verify zfs send -vPn for multiple snapshot send"
138options="-vPn"
139
140estimate_size=$(get_estimate_size $increamental_snapshot $options)
141log_must verify_size_estimates $options $increamental_size
142
143log_note "verify zfs send -inv for increamental send"
144options="-nvi"
145refer_size=$(get_prop refer $increamental_snapshot)
146deduct_size=$(get_prop refer $full_snapshot)
147refer_size=$(echo "$refer_size - $deduct_size" | bc)
148
149estimate_size=$(get_estimate_size $increamental_snapshot $options $full_snapshot)
150log_must verify_size_estimates $options $increamental_send
151
152log_note "verify zfs send -ivPn for increamental send"
153options="-vPni"
154
155estimate_size=$(get_estimate_size $increamental_snapshot $options $full_snapshot)
156log_must verify_size_estimates $options $increamental_send
157
158log_must zfs destroy -r $TESTPOOL/$TESTFS1
159
160#setup_recursive_send
161datasets="$TESTPOOL/$TESTFS1 $TESTPOOL/$TESTFS1/$TESTFS2
162    $TESTPOOL/$TESTFS1/$TESTFS2/$TESTFS3"
163# create nested datasets
164log_must zfs create -p $TESTPOOL/$TESTFS1/$TESTFS2/$TESTFS3
165
166# verify dataset creation
167for ds in $datasets; do
168        datasetexists $ds || log_fail "Create $ds dataset fail."
169done
170for ds in $datasets; do
171	log_must dd if=/dev/urandom of=/$ds/file64 \
172	    bs=1M count=64
173done
174
175# create recursive nested snapshot
176log_must zfs snapshot -r $TESTPOOL/$TESTFS1@snap64
177for ds in $datasets; do
178        datasetexists $ds@snap64 || log_fail "Create $ds@snap64 snapshot fail."
179done
180recursive_size=$(zfs send -R $full_snapshot 2>&1 | wc -c)
181log_note "verify zfs send -Rnv for recursive send"
182options="-Rnv"
183refer_size=$(get_prop refer $full_snapshot)
184refer_size=$(echo "$refer_size * 3" | bc)
185
186estimate_size=$(get_estimate_size $full_snapshot $options)
187log_must verify_size_estimates $options $recursive_size
188
189log_note "verify zfs send -RvPn for recursive send"
190options="-RvPn"
191estimate_size=$(get_estimate_size $full_snapshot $options)
192log_must verify_size_estimates $options $recursive_size
193
194log_pass "'zfs send' prints the correct size estimates using '-n' and '-P' options."
195