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