1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	env003
8# TEST	Test DB_TMP_DIR and env name resolution
9# TEST	With an environment path specified using -home, and then again
10# TEST	with it specified by the environment variable DB_HOME:
11# TEST	1) Make sure that the DB_TMP_DIR config file option is respected
12# TEST		a) as a relative pathname.
13# TEST		b) as an absolute pathname.
14# TEST	2) Make sure that the -tmp_dir config option is respected,
15# TEST		again as relative and absolute pathnames.
16# TEST	3) Make sure that if -both- -tmp_dir and a file are present,
17# TEST		only the file is respected (see doc/env/naming.html).
18proc env003 { } {
19	#   env003 is essentially just a small driver that runs
20	# env003_body twice.  First, it supplies a "home" argument
21	# to use with environment opens, and the second time it sets
22	# DB_HOME instead.
23	#   Note that env003_body itself calls env003_run_test to run
24	# the body of the actual test.
25
26	global env
27	source ./include.tcl
28
29	puts "Env003: DB_TMP_DIR test."
30
31	puts "\tEnv003: Running with -home argument to berkdb_env."
32	env003_body "-home $testdir"
33
34	puts "\tEnv003: Running with environment variable DB_HOME set."
35	set env(DB_HOME) $testdir
36	env003_body "-use_environ"
37
38	unset env(DB_HOME)
39
40	puts "\tEnv003: Running with both DB_HOME and -home set."
41	# Should respect -only- -home, so we give it a bogus
42	# environment variable setting.
43	set env(DB_HOME) $testdir/bogus_home
44	env003_body "-use_environ -home $testdir"
45	unset env(DB_HOME)
46}
47
48proc env003_body { home_arg } {
49	source ./include.tcl
50
51	env_cleanup $testdir
52	set tmpdir "tmpfiles_in_here"
53	file mkdir $testdir/$tmpdir
54
55	# Set up full path to $tmpdir for when we test absolute paths.
56	set curdir [pwd]
57	cd $testdir/$tmpdir
58	set fulltmpdir [pwd]
59	cd $curdir
60
61	# Create DB_CONFIG
62	env003_make_config $tmpdir
63
64	# Run the meat of the test.
65	env003_run_test a 1 "relative path, config file" $home_arg \
66		$testdir/$tmpdir
67
68	env003_make_config $fulltmpdir
69
70	# Run the test again
71	env003_run_test a 2 "absolute path, config file" $home_arg \
72		$fulltmpdir
73
74	# Now we try without a config file, but instead with db_config
75	# relative paths
76	env003_run_test b 1 "relative path, db_config" "$home_arg \
77		-tmp_dir $tmpdir -data_dir ." \
78		$testdir/$tmpdir
79
80	# absolute paths
81	env003_run_test b 2 "absolute path, db_config" "$home_arg \
82		-tmp_dir $fulltmpdir -data_dir ." \
83		$fulltmpdir
84
85	# Now, set db_config -and- have a # DB_CONFIG file, and make
86	# sure only the latter is honored.
87
88	file mkdir $testdir/bogus
89	env003_make_config $tmpdir
90
91	env003_run_test c 1 "relative path, both db_config and file" \
92		"$home_arg -tmp_dir $testdir/bogus -data_dir ." \
93		$testdir/$tmpdir
94
95	file mkdir $fulltmpdir/bogus
96	env003_make_config $fulltmpdir
97
98	env003_run_test c 2 "absolute path, both db_config and file" \
99		"$home_arg -tmp_dir $fulltmpdir/bogus -data_dir ." \
100		$fulltmpdir
101}
102
103proc env003_run_test { major minor msg env_args tmp_path} {
104	global testdir
105	global alphabet
106	global errorCode
107
108	puts "\t\tEnv003.$major.$minor: $msg"
109
110	# Create an environment and small-cached in-memory database to
111	# use.
112	set dbenv [eval {berkdb_env -create -home $testdir} $env_args \
113	    {-cachesize {0 50000 1}}]
114	error_check_good env_open [is_valid_env $dbenv] TRUE
115
116	set db [berkdb_open -env $dbenv -create -btree]
117	error_check_good db_open [is_valid_db $db] TRUE
118
119	# Fill the database with more than its cache can fit.
120	#
121	# When CONFIG_TEST is defined, the tempfile is left linked so
122	# we can check for its existence.  Size the data to overfill
123	# the cache--the temp file is created lazily, so it is created
124	# when the cache overflows.
125	#
126	set key "key"
127	set data [repeat $alphabet 2000]
128	error_check_good db_put [$db put $key $data] 0
129
130	# Check for exactly one temp file.
131	set ret [glob -nocomplain $tmp_path/BDB*]
132	error_check_good temp_file_exists [llength $ret] 1
133
134	# Can't remove temp file until db is closed on Windows.
135	error_check_good db_close [$db close] 0
136	fileremove -f $ret
137	error_check_good env_close [$dbenv close] 0
138
139}
140
141proc env003_make_config { tmpdir } {
142	global testdir
143
144	set cid [open $testdir/DB_CONFIG w]
145	puts $cid "set_data_dir ."
146	puts $cid "set_tmp_dir $tmpdir"
147	close $cid
148}
149