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