##################################################################
#
# Tests related to file locking
#
# Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de>
#
##################################################################

##################################################################
#
# Initialization
#
##################################################################
INIT {
	global	ARGS host path

	if {[llength $ARGS] < 1} {
		set host "localhost"
	} else {
		set host [lindex $ARGS 0]
	}
	if {[llength $ARGS] < 2} {
		set path "/tmp/nfsbench"
	} else {
		set path [lindex $ARGS 1]
	}

	set mnt [rpc create $host mount 1]
	set nfs [rpc create $host nfs 2]
	set nlm [rpc create $host nlm 2]
	set nlmsvc {rpc svc_create 
}

##################################################################
#
# Test generic mount facilities
#
##################################################################
TEST	exports		Obtain list of exported directories
BODY {
	# TBD
}

##################################################################
TEST	mntlist		Obtain list of mounted file systems
BODY {
	# TBD
}

##################################################################
TEST	mnt0010		Mount remote directory
FAIL	ABORT
BODY {
	set rootfh [$mnt mount $path]
}

##################################################################
TEST	mnt0011		Test whether mount shows up in mount list
COND	mntlist
BODY {
	# TBD
}

##################################################################
TEST	mnt0012		Unmount remote directory
COND	mntlist
FAIL	ABORT
BODY {
	$mnt umount $path
}

##################################################################
TEST	mnt0013		Access remote directory after unmount
FAIL {	WARN "remote directory inaccessible after umnt RPC call" }
BODY {
	$nfs getattr $rootfh
}

##################################################################
TEST	mlistok		Test whether unmounted dir still shows up in mount list
COND	mntlist
BODY {
	# TBD
}

##################################################################
#
# Try mounting and unmounting a single subdirectory
#
##################################################################
TEST	mntsubd		Create a subdirectory
BODY {
	catch {$nfs rmdir $rootfh mnttest}
	set dir [lindex [$nfs mkdir $rootfh mnttest {-mode 0755}] 0]
}

##################################################################
TEST	mnt0101		Mount subdirectory of exported directory
FAIL {	WARN "mountd disallows mounts of sub-directories of exported dirs" }
COND	mntsubd
BODY {
	set tmp [$mnt mount "$path/mnttest"]
	if {$dir != $tmp} {
		WARN "different fh's for different mount points."
		WARN "$dir != $tmp"
	}
}

##################################################################
TEST	mnt0102		Unmount subdirectory
COND	mnt0101
BODY {
	$mnt umount "$path/mnttest"
}

##################################################################
TEST	mnt0103		Test whether unmounted dir can be unmounted
COND	mntsubd && mlistok
FAIL {	WARN "mountd disallows umount of directories not in rmtab" }
BODY {
	$mnt umount "$path/mnttest"
}

##################################################################
TEST	mnt0104		Remove directory
COND	mntsubd
BODY {
	$nfs rmdir $rootfh mnttest
}

##################################################################
TEST	mnt0103		Test whether non-existent dir can be unmounted
COND	mntsubd
FAIL {	WARN "mountd disallows umount of non-existent directories" }
BODY {
	$mnt umount "$path/mnttest"
}

##################################################################
#
# Try mounting and unmounting a bunch of subdirectories
#
##################################################################
TEST	mnt0110		Create subdirectories
COND	mntsubd && mnt0101
BODY {
	set subdirnames {}
	for {set i 0} {$i < 10} {incr i} {
		lappend subdirnames "test$i"
	}
	foreach name $subdirnames {
		catch {$nfs rmdir $rootfh $name}
		$nfs mkdir $rootfh $name {-mode 0755}
	}
}

##################################################################
TEST	mnt0111		Mount and unmount all subdirectories
COND	mnt0110
BODY {
	foreach name $subdirnames {
		$mnt mount "$path/$name";
	}
	foreach name $subdirnames {
		$mnt umount "$path/$name";
	}
}

##################################################################
TEST	mnt0113		Remove all subdirectories
COND	mnt0110
BODY {
	foreach name $subdirnames {
		$nfs rmdir $rootfh $name
	}
}

##################################################################
#
# Check whether we can mount files
#
##################################################################
TEST	mnt0120		Create a file
BODY {
	$nfs create $rootfh mnttest {-mode 0644}
}

##################################################################
TEST	mntfile		Mount the file
COND	mnt0120
BODY {
	set fh [$mnt mount "$path/mnttest"]
	if {[lindex [$nfs getattr $fh] 0] != "REG"} {
		WARN "Something happened to the file when mounting it"
	}
}

##################################################################
TEST	mnt0122		Clean up after file mount test
COND	mnt0120
BODY {
	$nfs remove $rootfh mnttest
	$mnt umount "$path/mnttest"
}
