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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26. $STF_SUITE/include/libtest.kshlib
27
28################################################################################
29#
30# __stc_assertion_start
31#
32# ID: zfs_promote_004_pos
33#
34# DESCRIPTION:
35#	'zfs promote' can deal with multi-level clones.
36#
37# STRATEGY:
38#	1. Create multiple snapshots and multi-level clones
39#	2. Promote a clone filesystem
40#	3. Verify the dataset dependency relationships are correct after promotion.
41#
42# TESTABILITY: explicit
43#
44# TEST_AUTOMATION_LEVEL: automated
45#
46# CODING_STATUS: COMPLETED (2006-05-16)
47#
48# __stc_assertion_end
49#
50################################################################################
51
52verify_runnable "both"
53
54function cleanup
55{
56	if snapexists ${c1snap[1]}; then
57		log_must $ZFS promote $clone
58	fi
59
60	typeset ds
61	typeset data
62	for ds in ${snap[*]}; do
63		snapexists $ds && \
64			log_must $ZFS destroy -rR $ds
65	done
66	for data in ${file[*]}; do
67		[[ -e $data ]] && $RM -f $data
68	done
69}
70
71log_assert "'zfs promote' can deal with multi-level clone."
72log_onexit cleanup
73
74fs=$TESTPOOL/$TESTFS
75clone=$TESTPOOL/$TESTCLONE
76clone1=$TESTPOOL/$TESTCLONE1
77
78# Define some arrays here to use loop to reduce code amount
79
80# Array which stores the origin snapshots created in the origin filesystem
81set -A snap "${fs}@$TESTSNAP" "${fs}@$TESTSNAP1" "${fs}@$TESTSNAP2" "${fs}@$TESTSNAP3"
82# Array which stores the snapshots existing in the first clone
83set -A csnap "${clone}@$TESTSNAP3" "${clone}@$TESTSNAP4" "${clone}@$TESTSNAP5"
84# Array which stores the snapshots existing in the second clone after promote operation
85set -A c1snap "${clone1}@$TESTSNAP3" "${clone1}@$TESTSNAP4" "${clone1}@$TESTSNAP5"
86# The data will inject into the origin filesystem
87set -A file "$TESTDIR/$TESTFILE0" "$TESTDIR/$TESTFILE1" "$TESTDIR/$TESTFILE2" \
88		"$TESTDIR/$TESTFILE3"
89cdir=/$TESTPOOL/$TESTCLONE
90# The data will inject into the first clone
91set -A cfile "${cdir}/$CLONEFILE" "${cdir}/$CLONEFILE1" "${cdir}/$CLONEFILE2"
92c1snapdir=/$TESTPOOL/$TESTCLONE1/$(get_snapdir_name)
93# The data which will exist in the snapshot of the second clone filesystem after promote
94set -A c1snapfile "${c1snapdir}/$TESTSNAP3/$CLONEFILE" \
95	"${c1snapdir}/$TESTSNAP4/$CLONEFILE1" \
96	"${c1snapdir}/$TESTSNAP5/$CLONEFILE2"
97
98# setup for promote testing
99typeset -i i=0
100while (( i < 4 )); do
101	log_must $MKFILE $FILESIZE ${file[i]}
102	(( i>0 )) && log_must $RM -f ${file[((i-1))]}
103	log_must $ZFS snapshot ${snap[i]}
104
105	(( i = i + 1 ))
106done
107log_must $ZFS clone ${snap[2]} $clone
108
109log_must $RM -f /$clone/$TESTFILE2
110i=0
111while (( i < 3 )); do
112	log_must $MKFILE $FILESIZE ${cfile[i]}
113	(( i>0 )) && log_must $RM -f ${cfile[(( i-1 ))]}
114	log_must $ZFS snapshot ${csnap[i]}
115
116	(( i = i + 1 ))
117done
118
119log_must $ZFS clone ${csnap[1]} $clone1
120log_must $MKFILE $FILESIZE /$clone1/$CLONEFILE2
121log_must $RM -f /$clone1/$CLONEFILE1
122log_must $ZFS snapshot ${c1snap[2]}
123
124log_must $ZFS promote $clone1
125
126# verify the 'promote' operation
127for ds in ${snap[*]} ${csnap[2]} ${c1snap[*]}; do
128	! snapexists $ds && \
129		log_fail "The snapshot $ds disappear after zfs promote."
130done
131for data in ${c1snapfile[*]}; do
132	[[ ! -e $data ]] && \
133		log_fail "The data file $data loses after zfs promote."
134done
135
136origin_prop=$(get_prop origin $fs)
137[[ "$origin_prop" != "-" ]] && \
138	log_fail "The dependency is not correct for $fs after zfs promote."
139origin_prop=$(get_prop origin $clone)
140[[ "$origin_prop" != "${c1snap[1]}" ]] && \
141	log_fail "The dependency is not correct for $clone after zfs promote."
142origin_prop=$(get_prop origin $clone1)
143[[ "$origin_prop" != "${snap[2]}" ]] && \
144	log_fail "The dependency is not correct for $clone1 after zfs promote."
145
146log_pass "'zfs promote' deal with multi-level clones as expected."
147
148