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 2020 Attila Fülöp <attila@fueloep.org>
25#
26
27. $STF_SUITE/include/libtest.shlib
28
29typeset NEWPOOL="${TESTPOOL}split"
30typeset STR_DRYRUN="would create '$NEWPOOL' with the following layout:"
31typeset VDEV_PREFIX="$TEST_BASE_DIR/filedev"
32
33#
34# DESCRIPTION:
35# 'zpool split -n <pool> <newpool> [<vdev> ...]' can display the correct
36# configuration
37#
38# STRATEGY:
39# 1. Create a mirrored storage pool, split -n and verify the output is as
40#    expected.
41#
42
43typeset -a dev=(
44	"${VDEV_PREFIX}00" "${VDEV_PREFIX}01" "${VDEV_PREFIX}02"
45	"${VDEV_PREFIX}03" "${VDEV_PREFIX}04" "${VDEV_PREFIX}05"
46	"${VDEV_PREFIX}06" "${VDEV_PREFIX}07" "${VDEV_PREFIX}08"
47	"${VDEV_PREFIX}09" "${VDEV_PREFIX}10" "${VDEV_PREFIX}11"
48)
49
50typeset -a tests=(
51    # Test for hole.
52    (
53	tree="mirror '${dev[0]}' '${dev[1]}' log mirror '${dev[2]}' '${dev[3]}' \
54	    special mirror '${dev[4]}' '${dev[5]}'"
55
56	devs=""
57	want="$STR_DRYRUN
58
59	$NEWPOOL
60	  ${dev[1]}
61	special
62	  ${dev[5]}"
63    )
64    (
65	tree="mirror '${dev[0]}' '${dev[1]}' log mirror '${dev[2]}' '${dev[3]}' \
66	    special mirror '${dev[4]}' '${dev[5]}'"
67
68	devs="'${dev[0]}' '${dev[4]}'"
69	want="$STR_DRYRUN
70
71	$NEWPOOL
72	  ${dev[0]}
73	special
74	  ${dev[4]}"
75    )
76
77	# Full set of vdev types.
78    (
79	tree="mirror '${dev[0]}' '${dev[1]}'
80	    dedup mirror '${dev[2]}' '${dev[3]}' \
81	    special mirror '${dev[4]}' '${dev[5]}' \
82	    cache '${dev[6]}' '${dev[7]}' \
83	    spare '${dev[8]}' '${dev[9]}'\
84		log mirror '${dev[10]}' '${dev[11]}'"
85
86	devs=""
87	want="$STR_DRYRUN
88
89	$NEWPOOL
90	  ${dev[1]}
91	dedup
92	  ${dev[3]}
93	special
94	  ${dev[5]}"
95    )
96    (
97	tree="mirror '${dev[0]}' '${dev[1]}'
98	    dedup mirror '${dev[2]}' '${dev[3]}' \
99	    special mirror '${dev[4]}' '${dev[5]}' \
100	    cache '${dev[6]}' '${dev[7]}' \
101	    spare '${dev[8]}' '${dev[9]}'\
102		log mirror '${dev[10]}' '${dev[11]}'"
103
104	devs="'${dev[0]}' '${dev[2]}' '${dev[4]}'"
105	want="$STR_DRYRUN
106
107	$NEWPOOL
108	  ${dev[0]}
109	dedup
110	  ${dev[2]}
111	special
112	  ${dev[4]}"
113    )
114)
115
116verify_runnable "global"
117
118function cleanup
119{
120	destroy_pool "$TESTPOOL"
121	rm -f "$VDEV_PREFIX"*
122}
123
124log_assert \
125"'zpool split -n <pool> <newpool> [<vdev>]...' can display the configuration"
126
127log_onexit cleanup
128
129# Create needed file vdevs.
130for (( i=0; i < ${#dev[@]}; i+=1 )); do
131	log_must truncate -s $SPA_MINDEVSIZE "${dev[$i]}"
132done
133
134# Foreach test create pool, add -n devices and check output.
135for (( i=0; i < ${#tests[@]}; i+=1 )); do
136	typeset tree="${tests[$i].tree}"
137	typeset devs="${tests[$i].devs}"
138	typeset want="${tests[$i].want}"
139
140	log_must eval zpool create "$TESTPOOL" $tree
141	log_must poolexists "$TESTPOOL"
142	typeset out="$(log_must eval "zpool split -n \
143	    '$TESTPOOL' '$NEWPOOL' $devs" | sed /^SUCCESS/d)"
144
145	if [[ "$out" != "$want" ]]; then
146		log_fail "Got:\n" "$out" "\nbut expected:\n" "$want"
147	fi
148	log_must destroy_pool "$TESTPOOL"
149done
150
151log_pass \
152"'zpool split -n <pool> <newpool> [<vdev>]...' displays config correctly."
153