1# vim: filetype=sh
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_get_common.kshlib	1.3	07/03/14 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
33
34#
35# According to $elements, $prefix and $separator, the function random produce
36# the number of $counter combination.
37#
38# $1 elements which is used to get the combination.
39# $2 prefix is appended to the combination
40# $3 separator between the combination, such as ' ' or ','
41# $4 counter is the number of combination which you want to get.
42#
43function gen_option_str # $elements $prefix $separator $counter
44{
45	typeset elements=""
46	typeset prefix=${2}
47	typeset separator=${3}
48	typeset -i counter=${4:-0}
49	typeset -i i=0
50	typeset comb_str=""
51
52	for e in $1; do
53		elements[i]="$e"
54		(( i += 1 ))
55	done
56	(( ${#elements[@]} == 0 )) && log_fail "The elements can't be empty."
57
58	typeset -i item=0
59	typeset -i j=0
60	typeset -i numb_item=0
61
62	# Loop and get the specified number combination strings.
63	i=0
64	while (( i < counter )); do
65		j=0
66		numb_item=0
67		comb_str=""
68
69		# Get random number items for each combinations.
70		(( numb_item = ($RANDOM % ${#elements[@]}) + 1 ))
71
72		while (( j < numb_item )); do
73			# Random select elements from the array
74			(( item = $RANDOM % ${#elements[@]} ))
75
76			if (( ${#comb_str} == 0 )); then
77				comb_str=${elements[item]}
78			else
79				comb_str=$comb_str$separator${elements[item]}
80			fi
81			(( j += 1 ))
82		done
83
84		print "$prefix$comb_str"
85
86		(( i += 1 ))
87	done
88}
89
90#
91# Cleanup the volume snapshot and filesystem snapshot were created for
92# this test case.
93#
94function cleanup
95{
96	datasetexists $TESTPOOL/$TESTVOL@$TESTSNAP && \
97		destroy_snapshot $TESTPOOL/$TESTVOL@$TESTSNAP
98	datasetexists $TESTPOOL/$TESTFS@$TESTSNAP && \
99		destroy_snapshot $TESTPOOL/$TESTFS@$TESTSNAP
100
101	[[ -e $TESTFILE0 ]] && log_must $RM $TESTFILE0
102}
103