1227825Stheraven# vim: filetype=sh
2227825Stheraven#
3227825Stheraven# CDDL HEADER START
4227825Stheraven#
5227825Stheraven# The contents of this file are subject to the terms of the
6227825Stheraven# Common Development and Distribution License (the "License").
7227825Stheraven# You may not use this file except in compliance with the License.
8227825Stheraven#
9227825Stheraven# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10227825Stheraven# or http://www.opensolaris.org/os/licensing.
11227825Stheraven# See the License for the specific language governing permissions
12227825Stheraven# and limitations under the License.
13227825Stheraven#
14227825Stheraven# When distributing Covered Code, include this CDDL HEADER in each
15227825Stheraven# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16227825Stheraven# If applicable, add the following below this CDDL HEADER, with the
17227825Stheraven# fields enclosed by brackets "[]" replaced with your own identifying
18227825Stheraven# information: Portions Copyright [yyyy] [name of copyright owner]
19227825Stheraven#
20227825Stheraven# CDDL HEADER END
21227825Stheraven#
22227825Stheraven
23227825Stheraven#
24227825Stheraven# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25227825Stheraven# Use is subject to license terms.
26227825Stheraven
27227825Stheraven. $STF_SUITE/include/libtest.kshlib
28227825Stheraven
29227825Stheraven#
30227825Stheraven# According to $elements, $prefix and $separator, the function random produce 
31227825Stheraven# the number of $counter combination. 
32227825Stheraven#
33227825Stheraven# $1 elements which is used to get the combination.
34227825Stheraven# $2 prefix is appended to the combination
35227825Stheraven# $3 separator between the combination, such as ' ' or ','
36227825Stheraven# $4 counter is the number of combination which you want to get.
37227825Stheraven#
38227825Stheravenfunction gen_option_str # $elements $prefix $separator $counter
39227825Stheraven{
40227825Stheraven	typeset elements=""
41227825Stheraven	typeset prefix=${2}
42227825Stheraven	typeset separator=${3}
43227825Stheraven	typeset -i counter=${4:-0}
44227825Stheraven	typeset -i i=0
45227825Stheraven	typeset comb_str=""
46227825Stheraven
47227825Stheraven	for e in $1; do
48227825Stheraven		elements[i]="$e"
49227825Stheraven		(( i += 1 ))
50227825Stheraven	done
51227825Stheraven	(( ${#elements[@]} == 0 )) && log_fail "The elements can't be empty."
52227825Stheraven	
53227825Stheraven	typeset -i item=0
54227825Stheraven	typeset -i j=0
55227825Stheraven	typeset -i numb_item=0
56227825Stheraven
57227825Stheraven	# Loop and get the specified number combination strings.
58227825Stheraven	i=0
59227825Stheraven	while (( i < counter )); do
60227825Stheraven		j=0
61227825Stheraven		numb_item=0
62227825Stheraven		comb_str=""
63227825Stheraven
64227825Stheraven		# Get random number items for each combinations.
65227825Stheraven		(( numb_item = ($RANDOM % ${#elements[@]}) + 1 ))
66227825Stheraven
67227825Stheraven		while (( j < numb_item )); do
68227825Stheraven			# Random select elements from the array
69227825Stheraven			(( item = $RANDOM % ${#elements[@]} ))
70227825Stheraven
71227825Stheraven			if (( ${#comb_str} == 0 )); then
72227825Stheraven				comb_str=${elements[item]}
73227825Stheraven			else
74227825Stheraven				comb_str=$comb_str$separator${elements[item]}
75227825Stheraven			fi
76227825Stheraven			(( j += 1 ))
77227825Stheraven		done
78227825Stheraven		
79227825Stheraven		print "$prefix$comb_str"
80227825Stheraven
81227825Stheraven		(( i += 1 ))
82227825Stheraven	done
83227825Stheraven}
84227825Stheraven
85227825Stheraven#
86227825Stheraven# Cleanup the volume snapshot and filesystem snapshot were created for 
87227825Stheraven# this test case.
88227825Stheraven#
89227825Stheravenfunction cleanup
90227825Stheraven{
91227825Stheraven	datasetexists $TESTPOOL/$TESTVOL@$TESTSNAP && \
92227825Stheraven		destroy_snapshot $TESTPOOL/$TESTVOL@$TESTSNAP
93227825Stheraven	datasetexists $TESTPOOL/$TESTFS@$TESTSNAP && \
94227825Stheraven		destroy_snapshot $TESTPOOL/$TESTFS@$TESTSNAP
95227825Stheraven
96227825Stheraven	[[ -e $TESTFILE0 ]] && log_must $RM $TESTFILE0
97227825Stheraven}
98227825Stheraven