1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# TEST	env002
8# TEST	Test of DB_LOG_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 set_lg_dir option is respected
12# TEST		a) as a relative pathname.
13# TEST		b) as an absolute pathname.
14# TEST	2) Make sure that the DB_LOG_DIR db_config argument is respected,
15# TEST		again as relative and absolute pathnames.
16# TEST	3) Make sure that if -both- db_config and a file are present,
17# TEST		only the file is respected (see doc/env/naming.html).
18proc env002 { } {
19	#   env002 is essentially just a small driver that runs
20	# env002_body--formerly the entire test--twice;  once, it
21	# supplies a "home" argument to use with environment opens,
22	# and the second time it sets DB_HOME instead.
23	#   Note that env002_body itself calls env002_run_test to run
24	# the body of the actual test and check for the presence
25	# of logs.  The nesting, I hope, makes this test's structure simpler.
26
27	global env
28	source ./include.tcl
29
30	puts "Env002: set_lg_dir test."
31
32	puts "\tEnv002: Running with -home argument to berkdb_env."
33	env002_body "-home $testdir"
34
35	puts "\tEnv002: Running with environment variable DB_HOME set."
36	set env(DB_HOME) $testdir
37	env002_body "-use_environ"
38
39	unset env(DB_HOME)
40
41	puts "\tEnv002: Running with both DB_HOME and -home set."
42	# Should respect -only- -home, so we give it a bogus
43	# environment variable setting.
44	set env(DB_HOME) $testdir/bogus_home
45	env002_body "-use_environ -home $testdir"
46	unset env(DB_HOME)
47
48}
49
50proc env002_body { home_arg } {
51	source ./include.tcl
52
53	env_cleanup $testdir
54	set logdir "logs_in_here"
55
56	file mkdir $testdir/$logdir
57
58	# Set up full path to $logdir for when we test absolute paths.
59	set curdir [pwd]
60	cd $testdir/$logdir
61	set fulllogdir [pwd]
62	cd $curdir
63
64	env002_make_config $logdir
65
66	# Run the meat of the test.
67	env002_run_test a 1 "relative path, config file" $home_arg \
68		$testdir/$logdir
69
70	env_cleanup $testdir
71
72	file mkdir $fulllogdir
73	env002_make_config $fulllogdir
74
75	# Run the test again
76	env002_run_test a 2 "absolute path, config file" $home_arg \
77		$fulllogdir
78
79	env_cleanup $testdir
80
81	# Now we try without a config file, but instead with db_config
82	# relative paths
83	file mkdir $testdir/$logdir
84	env002_run_test b 1 "relative path, db_config" "$home_arg \
85		-log_dir $logdir -data_dir ." \
86		$testdir/$logdir
87
88	env_cleanup $testdir
89
90	# absolute
91	file mkdir $fulllogdir
92	env002_run_test b 2 "absolute path, db_config" "$home_arg \
93		-log_dir $fulllogdir -data_dir ." \
94		$fulllogdir
95
96	env_cleanup $testdir
97
98	# Now, set db_config -and- have a # DB_CONFIG file, and make
99	# sure only the latter is honored.
100
101	file mkdir $testdir/$logdir
102	env002_make_config $logdir
103
104	# note that we supply a -nonexistent- log dir to db_config
105	env002_run_test c 1 "relative path, both db_config and file" \
106		"$home_arg -log_dir $testdir/bogus \
107		-data_dir ." $testdir/$logdir
108	env_cleanup $testdir
109
110	file mkdir $fulllogdir
111	env002_make_config $fulllogdir
112
113	# note that we supply a -nonexistent- log dir to db_config
114	env002_run_test c 2 "relative path, both db_config and file" \
115		"$home_arg -log_dir $fulllogdir/bogus \
116		-data_dir ." $fulllogdir
117}
118
119proc env002_run_test { major minor msg env_args log_path} {
120	global testdir
121	set testfile "env002.db"
122
123	puts "\t\tEnv002.$major.$minor: $msg"
124
125	# Create an environment, with logging, and scribble some
126	# stuff in a [btree] database in it.
127	# puts [concat {berkdb_env -create -log -private} $env_args]
128	set dbenv [eval {berkdb_env -create -log -private} $env_args]
129	error_check_good env_open [is_valid_env $dbenv] TRUE
130	set db [berkdb_open -env $dbenv -create -btree -mode 0644 $testfile]
131	error_check_good db_open [is_valid_db $db] TRUE
132
133	set key "some_key"
134	set data "some_data"
135
136	error_check_good db_put \
137		[$db put $key [chop_data btree $data]] 0
138
139	error_check_good db_close [$db close] 0
140	error_check_good env_close [$dbenv close] 0
141
142	# Now make sure the log file is where we want it to be.
143	error_check_good db_exists [file exists $testdir/$testfile] 1
144	error_check_good log_exists \
145		[file exists $log_path/log.0000000001] 1
146}
147
148proc env002_make_config { logdir } {
149	global testdir
150
151	set cid [open $testdir/DB_CONFIG w]
152	puts $cid "set_data_dir ."
153	puts $cid "set_lg_dir $logdir"
154	close $cid
155}
156