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