1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: env001.tcl,v 12.8 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	env001
8# TEST	Test of env remove interface (formerly env_remove).
9proc env001 { } {
10	global errorInfo
11	global errorCode
12
13	source ./include.tcl
14
15	set testfile $testdir/env.db
16	set t1 $testdir/t1
17
18	puts "Env001: Test of environment remove interface."
19	env_cleanup $testdir
20
21	# Try opening without Create flag should error
22	puts "\tEnv001.a: Open without create (should fail)."
23	catch {set env [berkdb_env_noerr -home $testdir]} ret
24	error_check_good env:fail [is_substr $ret "no such file"] 1
25
26	# Now try opening with create
27	puts "\tEnv001.b: Open with create."
28	set env [berkdb_env -create -mode 0644 -home $testdir]
29	error_check_bad env:$testdir $env NULL
30	error_check_good env:$testdir [is_substr $env "env"] 1
31
32	# Make sure that close works.
33	puts "\tEnv001.c: Verify close."
34	error_check_good env:close:$env [$env close] 0
35
36	# Make sure we can reopen.
37	puts "\tEnv001.d: Remove on closed environments."
38	puts "\t\tEnv001.d.1: Verify re-open."
39	set env [berkdb_env -home $testdir]
40	error_check_bad env:$testdir $env NULL
41	error_check_good env:$testdir [is_substr $env "env"] 1
42
43	# remove environment
44	puts "\t\tEnv001.d.2: Close environment."
45	error_check_good env:close [$env close] 0
46	puts "\t\tEnv001.d.3: Try remove with force (should succeed)."
47	error_check_good \
48	    envremove [berkdb envremove -force -home $testdir] 0
49
50	# HP-UX doesn't allow a second handle on an open env.
51	if { $is_hp_test != 1 } {
52		puts "\tEnv001.e: Remove on open environments."
53		puts "\t\tEnv001.e.1: Env is open by single proc,\
54		    remove no force."
55		set env [berkdb_env -create -mode 0644 -home $testdir]
56		error_check_bad env:$testdir $env NULL
57		error_check_good env:$testdir [is_substr $env "env"] 1
58		set stat [catch {berkdb envremove -home $testdir} ret]
59		error_check_good env:remove $stat 1
60		error_check_good env:close [$env close] 0
61	}
62
63	puts \
64	    "\t\tEnv001.e.2: Env is open by single proc, remove with force."
65	if { $is_hp_test != 1 } {
66		set env [berkdb_env_noerr -create -mode 0644 -home $testdir]
67		error_check_bad env:$testdir $env NULL
68		error_check_good env:$testdir [is_substr $env "env"] 1
69		set stat [catch {berkdb envremove -force -home $testdir} ret]
70		error_check_good env:remove(force) $ret 0
71		#
72		# Even though the underlying env is gone, we need to close
73		# the handle.
74		#
75		set stat [catch {$env close} ret]
76		error_check_bad env:close_after_remove $stat 0
77		error_check_good env:close_after_remove \
78		    [is_substr $ret "recovery"] 1
79	}
80
81	puts "\t\tEnv001.e.3: Env is open by 2 procs, remove no force."
82	# should fail
83	set env [berkdb_env -create -mode 0644 -home $testdir]
84	error_check_bad env:$testdir $env NULL
85	error_check_good env:$testdir [is_substr $env "env"] 1
86
87	set f1 [open |$tclsh_path r+]
88	puts $f1 "source $test_path/test.tcl"
89
90	set remote_env [send_cmd $f1 "berkdb_env_noerr -home $testdir"]
91	error_check_good remote:env_open [is_valid_env $remote_env] TRUE
92	# First close our env, but leave remote open
93	error_check_good env:close [$env close] 0
94	catch {berkdb envremove -home $testdir} ret
95	error_check_good envremove:2procs:noforce [is_substr $errorCode EBUSY] 1
96	#
97	# even though it failed, $env is no longer valid, so remove it in
98	# the remote process
99	set remote_close [send_cmd $f1 "$remote_env close"]
100	error_check_good remote_close $remote_close 0
101
102	# exit remote process
103	set err [catch { close $f1 } result]
104	error_check_good close_remote_process $err 0
105
106	puts "\t\tEnv001.e.4: Env is open by 2 procs, remove with force."
107	if { $is_hp_test != 1 } {
108		set env [berkdb_env_noerr -create -mode 0644 -home $testdir]
109		error_check_bad env:$testdir $env NULL
110		error_check_good env:$testdir [is_substr $env "env"] 1
111		set f1 [open |$tclsh_path r+]
112		puts $f1 "source $test_path/test.tcl"
113
114		set remote_env [send_cmd $f1 "berkdb_env -home $testdir"]
115		error_check_good remote:env_open [is_valid_env $remote_env] TRUE
116
117		catch {berkdb envremove -force -home $testdir} ret
118		error_check_good envremove:2procs:force $ret 0
119		#
120		# We still need to close our handle.
121		#
122		set stat [catch {$env close} ret]
123		error_check_bad env:close_after_error $stat 0
124		error_check_good env:close_after_error \
125		    [is_substr $ret recovery] 1
126
127		# Close down remote process
128		set err [catch { close $f1 } result]
129		error_check_good close_remote_process $err 0
130	}
131
132	# Try opening in a different dir
133	puts "\tEnv001.f: Try opening env in another directory."
134	if { [file exists $testdir/NEWDIR] != 1 } {
135		file mkdir $testdir/NEWDIR
136	}
137	set eflags "-create -home $testdir/NEWDIR -mode 0644"
138	set env [eval {berkdb_env} $eflags]
139	error_check_bad env:open $env NULL
140	error_check_good env:close [$env close] 0
141	error_check_good berkdb:envremove \
142	    [berkdb envremove -home $testdir/NEWDIR] 0
143
144	puts "\tEnv001 complete."
145}
146