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#
24# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28
29#
30# Create or recover a set of test environment which include ctr, vol, fs,
31# snap & clone. It looks like the following.
32#
33# pool
34#    |ctr
35#    |  |fs
36#    |  | |fssnap
37#    |  |vol
38#    |     |volsnap
39#    |fsclone
40#    |volclone
41#
42# $1 indicate which dependent dataset need be created. Such as 'snap', 'clone'.
43#
44function setup_testenv #[dtst]
45{
46	typeset dtst=$1
47
48	if ! datasetexists $CTR; then
49		log_must $ZFS create $CTR
50	fi
51	if ! datasetexists $FS; then
52		log_must $ZFS create $FS
53	fi
54	# Volume test is only availible on globle zone
55	if ! datasetexists $VOL && is_global_zone; then
56		log_must $ZFS create -V $VOLSIZE $VOL
57		log_must $NEWFS /dev/zvol/$VOL
58
59		if [[ ! -d $TESTDIR1 ]]; then
60			log_must $MKDIR $TESTDIR1
61		fi
62		log_must $MOUNT /dev/zvol/$VOL $TESTDIR1
63	fi
64
65	if [[ $dtst == snap || $dtst == clone ]]; then
66		if ! datasetexists $FSSNAP; then
67			log_must $ZFS snapshot $FSSNAP
68		fi
69		if ! datasetexists $VOLSNAP && is_global_zone; then
70			log_must $ZFS snapshot $VOLSNAP
71		fi
72	fi
73
74	if [[ $dtst == clone ]]; then
75		if ! datasetexists $FSCLONE; then
76			log_must $ZFS clone $FSSNAP $FSCLONE
77		fi
78		if ! datasetexists $VOLCLONE && is_global_zone; then
79			log_must $ZFS clone $VOLSNAP $VOLCLONE
80		fi
81	fi
82}
83
84function make_dir_busy
85{
86	typeset dir=$1
87	typeset dirfiltered=$(echo $dir | sed -Ee 's,[/\.],_,g')
88
89	OLDPWD=$(pwd)
90	cd $dir
91	# Sleep for long enough for the test to have run through.  Note that
92	# even if the test itself changes directory, sleep will still be on it.
93	$SLEEP $STF_TIMEOUT &
94	eval SLEEP_PID_${dirfiltered}=$!
95	pid=$(eval echo \$SLEEP_PID_${dirfiltered})
96	cd ${OLDPWD}
97	log_note "Sleeping while on ${dir} in pid $pid"
98}
99
100function make_dir_unbusy
101{
102	typeset dir=$1
103	typeset dirfiltered=$(echo $dir | sed -Ee 's,[/\.],_,g')
104	typeset pid=$(eval echo \$SLEEP_PID_${dirfiltered})
105
106	# Safeguard in case this is used incorrectly.
107	[[ -z "$pid" ]] && log_fail "make_dir_unbusy called without busy?"
108	$KILL -15 $pid
109	eval SLEEP_PID_${dirfiltered}=""
110	log_note "Unbusied ${dir}"
111}
112
113# Clean up the testing environment
114#
115function cleanup_testenv
116{
117	if [[ $STF_EXITCODE -eq $STF_FAIL ]]; then
118		$ECHO "Testcase failed; dataset listing follows:"
119		$ZFS list -t all -r $TESTPOOL
120	fi
121	if (( ${#init_dir} != 0 )); then
122		cd $init_dir
123		init_dir=""
124	fi
125	if is_global_zone && ismounted "$TESTDIR1" "ufs" ; then
126		log_must $UMOUNT -f $TESTDIR1
127	fi
128	if [[ -d $TESTDIR1 ]]; then
129		log_must $RM -rf $TESTDIR1
130	fi
131
132	[[ -n "$SLEEP_PID" ]] && $KILL -15 $SLEEP_PID
133
134	if datasetexists $CTR; then
135		log_must $ZFS destroy -Rf $CTR
136	fi
137}
138
139#
140# Delete volume and related datasets from list, if the test cases was
141# runing in local zone. Then check them are existed or non-exists.
142#
143# $1   function name
144# $2-n datasets name
145#
146function check_dataset
147{
148	typeset funname=$1
149	typeset newlist=""
150	typeset dtst
151	shift
152
153	for dtst in "$@"; do
154		# Volume and related stuff are unvailable in local zone
155		if ! is_global_zone; then
156			if [[ $dtst == $VOL || $dtst == $VOLSNAP || \
157				$dtst == $VOLCLONE ]]
158			then
159				continue
160			fi
161		fi
162		newlist="$newlist $dtst"
163	done
164
165	if (( ${#newlist} != 0 )); then
166		log_must eval "$funname $newlist"
167	fi
168}
169