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 https://opensource.org/licenses/CDDL-1.0.
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-point snapshots.
38#
39# STRATEGY:
40#	1. Create multiple snapshots and a clone to a middle point snapshot
41#	2. Promote the clone filesystem
42#	3. Verify the origin filesystem and promoted filesystem include
43#	   correct datasets separated by the clone point.
44#
45
46verify_runnable "both"
47
48function cleanup
49{
50	if snapexists ${csnap[2]}; then
51		log_must zfs promote $fs
52	fi
53
54	typeset ds
55	typeset data
56	for ds in ${snap[*]}; do
57		snapexists $ds && destroy_dataset $ds -rR
58	done
59	for data in ${file[*]}; do
60		[[ -e $data ]] && rm -f $data
61	done
62
63}
64
65log_assert "'zfs promote' can deal with multi-point snapshots."
66log_onexit cleanup
67
68fs=$TESTPOOL/$TESTFS
69clone=$TESTPOOL/$TESTCLONE
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 clone after promote operation
76set -A csnap "${clone}@$TESTSNAP" "${clone}@$TESTSNAP1" "${clone}@$TESTSNAP2" \
77	"${clone}@$TESTSNAP3"
78# The data will inject into the origin filesystem
79set -A file "$TESTDIR/$TESTFILE0" "$TESTDIR/$TESTFILE1" "$TESTDIR/$TESTFILE2" \
80		"$TESTDIR/$TESTFILE3"
81snapdir=$TESTDIR/.zfs/snapshot
82# The data which will exist in the snapshot after creation of snapshot
83set -A snapfile "$snapdir/$TESTSNAP/$TESTFILE0" "$snapdir/$TESTSNAP1/$TESTFILE1" \
84	"$snapdir/$TESTSNAP2/$TESTFILE2" "$snapdir/$TESTSNAP3/$TESTFILE3"
85csnapdir=/$clone/.zfs/snapshot
86# The data which will exist in the snapshot of clone filesystem after promote
87set -A csnapfile "${csnapdir}/$TESTSNAP/$TESTFILE0" "${csnapdir}/$TESTSNAP1/$TESTFILE1" \
88	"${csnapdir}/$TESTSNAP2/$TESTFILE2"
89
90# setup for promote testing
91typeset -i i=0
92while (( i < 4 )); do
93	log_must mkfile $FILESIZE ${file[i]}
94	(( i>0 )) && log_must rm -f ${file[((i-1))]}
95	log_must zfs snapshot ${snap[i]}
96
97	(( i = i + 1 ))
98done
99log_must zfs clone ${snap[2]} $clone
100log_must mkfile $FILESIZE /$clone/$CLONEFILE
101log_must rm -f /$clone/$TESTFILE2
102log_must zfs snapshot ${csnap[3]}
103
104log_must zfs promote $clone
105
106# verify the 'promote' operation
107for ds in ${snap[3]} ${csnap[*]}; do
108	! snapexists $ds && \
109		log_fail "The snapshot $ds disappear after zfs promote."
110done
111for data in ${csnapfile[*]} $TESTDIR/$TESTFILE3 /$clone/$CLONEFILE; do
112	[[ ! -e $data ]] && \
113		log_fail "The data file $data loses after zfs promote."
114done
115
116for ds in ${snap[0]} ${snap[1]} ${snap[2]}; do
117	snapexists $ds && \
118		log_fail "zfs promote cannot promote the snapshot $ds."
119done
120for data in ${snapfile[0]} ${snapfile[1]} ${snapfile[2]}; do
121	[[ -e $data ]] && \
122		log_fail "zfs promote cannot promote the data $data."
123done
124
125origin_prop=$(get_prop origin $fs)
126[[ "$origin_prop" != "${csnap[2]}" ]] && \
127	log_fail "The dependency is not correct for $fs after zfs promote."
128origin_prop=$(get_prop origin $clone)
129[[ "$origin_prop" != "-" ]] && \
130	log_fail "The dependency is not correct for $clone after zfs promote."
131
132log_pass "'zfs promote' deal with multi-point snapshots as expected."
133
134