xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zfs_get/zfs_get_common.kshlib (revision b6805bf78d2bbbeeaea8909a05623587b42d58b3)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27. $STF_SUITE/include/libtest.shlib
28
29#
30# According to $elements, $prefix and $separator, the function random produce
31# the number of $counter combination.
32#
33# $1 elements which is used to get the combination.
34# $2 prefix is appended to the combination
35# $3 separator between the combination, such as ' ' or ','
36# $4 counter is the number of combination which you want to get.
37#
38function gen_option_str # $elements $prefix $separator $counter
39{
40	typeset elements=""
41	typeset prefix=${2}
42	typeset separator=${3}
43	typeset -i counter=${4:-0}
44	typeset -i i=0
45	typeset comb_str=""
46
47	for e in $1; do
48		elements[i]="$e"
49		(( i += 1 ))
50	done
51	(( ${#elements[@]} == 0 )) && log_fail "The elements can't be empty."
52
53	typeset -i item=0
54	typeset -i j=0
55	typeset -i numb_item=0
56
57	# Loop and get the specified number combination strings.
58	i=0
59	while (( i < counter )); do
60		j=0
61		numb_item=0
62		comb_str=""
63
64		# Get random number items for each combinations.
65		(( numb_item = ($RANDOM % ${#elements[@]}) + 1 ))
66
67		while (( j < numb_item )); do
68			# Random select elements from the array
69			(( item = $RANDOM % ${#elements[@]} ))
70
71			if (( ${#comb_str} == 0 )); then
72				comb_str=${elements[item]}
73			else
74				comb_str=$comb_str$separator${elements[item]}
75			fi
76			(( j += 1 ))
77		done
78
79		echo "$prefix$comb_str"
80
81		(( i += 1 ))
82	done
83}
84
85#
86# Cleanup the volume snapshot and filesystem snapshot were created for
87# this test case.
88#
89function cleanup
90{
91	datasetexists $TESTPOOL/$TESTVOL@$TESTSNAP && \
92		destroy_snapshot $TESTPOOL/$TESTVOL@$TESTSNAP
93	datasetexists $TESTPOOL/$TESTFS@$TESTSNAP && \
94		destroy_snapshot $TESTPOOL/$TESTFS@$TESTSNAP
95
96	[[ -e $TESTFILE0 ]] && log_must $RM $TESTFILE0
97}
98