Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / tcl / test137.tcl
1 # See the file LICENSE for redistribution information.
2 #
3 # Copyright (c) 2011, 2012 Oracle and/or its affiliates.  All rights reserved.
4 #
5 # $Id$
6 #
7 # TEST  test137
8 # TEST  Test Automatic Resource Management. [#16188][#20281]
9 # TEST  Open an environment, and open a database in it.
10 # TEST  Do some operations in the database, including:
11 # TEST    insert, dump, delete
12 # TEST  Close the environment without closing the database.
13 # TEST  Re-open the database and verify the records are the ones we expected.
14
15 proc test137 { method {nentries 1000} {start 0} {skip 0} {use_encrypt 0}
16     {tnum "137"} {envtype "ds"} args } {
17         source ./include.tcl
18         global encrypt
19         global has_crypto
20
21         # This test needs its own env.
22         set eindex [lsearch -exact $args "-env"]
23         if { $eindex != -1 } {
24                 incr eindex
25                 set env [lindex $args $eindex]
26                 puts "Test$tnum skipping for env $env"
27                 return
28         }
29
30         # Skip if use_encrypt and encryption is not supported.
31         if { $use_encrypt && $has_crypto == 0 } {
32                 puts "Skipping test$tnum for non-crypto release."
33                 return
34         }
35
36         # Do not use db encryption flags from the $args -- this test
37         # does its own encryption testing at the env level. 
38         convert_encrypt $args
39         if { $encrypt } {
40                 puts "Test$tnum skipping DB encryption"
41                 return
42         }
43
44         # We can not specify chksum option to opening db when using
45         # an encrypted env.
46         set chksum_indx [lsearch -exact $args "-chksum"]
47         if { $chksum_indx != -1 && $use_encrypt } {
48                 puts "Test$tnum skipping DB chksum"
49                 return
50         }
51
52         set encmsg "non-encrypt"
53         if { $use_encrypt } {
54                 set encmsg "encrypt"
55         }
56
57         set args [convert_args $method $args]
58         set omethod [convert_method $method]
59
60         env_cleanup $testdir
61         set env [test137_openenv $envtype $testdir $use_encrypt]
62         set envargs  "-env $env"
63         set txnenv [string equal $envtype "tds"]
64         if { $txnenv == 1 } {
65                 set nentries 200 
66                 append args " -auto_commit"
67         }
68
69         set testfile test$tnum.db       
70         set t1 $testdir/t1
71         set t2 $testdir/t2
72
73         puts "Test$tnum: $method \
74             ($envtype,$encmsg,$args) $nentries equal key/data pairs"
75
76         set did [open $dict]
77         # The "start" variable determines the record number to start
78         # with, if we're using record numbers.  The "skip" variable
79         # determines the dictionary entry to start with.
80         # In normal use, skip will match start.
81         puts "\tTest$tnum: Starting at $start with dictionary entry $skip"
82         if { $skip != 0 } {
83                 for { set count 0 } { $count < $skip } { incr count } {
84                         gets $did str
85                 }
86         }
87
88         set db [eval {berkdb_open \
89              -create -mode 0644} $envargs $args $omethod $testfile]
90         error_check_good dbopen [is_valid_db $db] TRUE
91
92         set txn ""
93         if { $txnenv == 1 } {
94                 set t [$env txn]
95                 error_check_good txn [is_valid_txn $t $env] TRUE
96                 set txn "-txn $t"
97         }
98
99         puts "\tTest$tnum.a: put loop"
100         # Here is the loop where we put and get each key/data pair
101         set count 0
102         set key_list {}
103         set data_list {}
104         while { [gets $did str] != -1 && $count < $nentries } {
105                 if { [is_record_based $method] == 1 } {
106                         set key [expr $count + 1 + $start]
107                 } else {
108                         set key $str
109                         set str [reverse $str]
110                 }
111                 set ret [eval \
112                     {$db put} $txn {$key [chop_data $method $str]}]
113                 error_check_good db_put $ret 0
114                 lappend key_list $key
115                 lappend data_list $str
116                 incr count
117         }
118         close $did
119
120         puts "\tTest$tnum.b: dump file and close env without closing database"
121         dump_file $db $txn $t1 NONE
122         if { $txnenv == 1 } {
123                 error_check_good txn_commit [$t commit] 0
124         }
125         error_check_good env_close [$env close] 0
126         # Clean the tcl handle only.
127         catch {$db close -handle_only} res
128
129         puts "\tTest$tnum.c: open the env and database again"
130         set env [test137_openenv $envtype $testdir $use_encrypt]
131         set envargs  "-env $env"
132         set db [eval {berkdb_open \
133              -create -mode 0644} $envargs $args $omethod $testfile]
134         error_check_good dbopen [is_valid_db $db] TRUE
135
136         if { $txnenv == 1 } {
137                 set t [$env txn]
138                 error_check_good txn [is_valid_txn $t $env] TRUE
139                 set txn "-txn $t"
140         }
141
142         puts "\tTest$tnum.d: dump file and check"
143         dump_file $db $txn $t2 NONE
144         error_check_good Test$tnum:diff($t1,$t2) \
145             [filecmp $t1 $t2] 0
146
147         puts "\tTest$tnum.e: delete loop"
148         # We delete starting from the last record, since for rrecno, if we 
149         # delete previous records, the ones after will change their keys.
150         for {set i [expr [llength $key_list] - 1]} {$i >= 0} \
151             {incr i -[berkdb random_int 1 5]} {
152                 set key [lindex $key_list $i]
153                 set ret [eval $db del $txn {$key}]
154                 error_check_good db_del $ret 0
155         } 
156
157         puts "\tTest$tnum.f:\
158             dump file and close env without closing database again"
159         dump_file $db $txn $t1 NONE
160         if { $txnenv == 1 } {
161                 error_check_good txn_commit [$t commit] 0
162         }
163         error_check_good env_close [$env close -forcesync] 0
164         catch {$db close -handle_only} res
165
166         # We remove the environment to make sure the regions files
167         # are deleted(including mpool files), so we can verify if
168         # the database has been synced during a force-synced env close.
169         error_check_good env_remove [berkdb envremove -force -home $testdir] 0
170
171         puts "\tTest$tnum.g: open, dump and check again"
172         set env [test137_openenv $envtype $testdir $use_encrypt]
173         set envargs  "-env $env"
174         set db [eval {berkdb_open \
175              -create -mode 0644} $envargs $args $omethod $testfile]
176         error_check_good dbopen [is_valid_db $db] TRUE
177
178         if { $txnenv == 1 } {
179                 set t [$env txn]
180                 error_check_good txn [is_valid_txn $t $env] TRUE
181                 set txn "-txn $t"
182         }
183
184         dump_file $db $txn $t2 NONE
185         error_check_good Test$tnum:diff($t1,$t2) \
186             [filecmp $t1 $t2] 0
187
188         if { $txnenv == 1 } {
189                 error_check_good txn_commit [$t commit] 0
190         }
191         error_check_good db_close [$db close] 0
192         error_check_good env_close [$env close] 0
193 }
194
195 proc test137_openenv {envtype testdir use_encrypt} {
196         global passwd
197         
198         set encargs ""
199         if {$use_encrypt} {
200                 set encargs " -encryptaes $passwd "
201         }
202         switch -exact "$envtype" {
203                 "ds" {
204                         set env [eval berkdb_env_noerr -create -mode 0644 \
205                             $encargs -home $testdir]
206                 }
207                 "cds" {
208                         set env [eval berkdb_env_noerr -create -mode 0644 \
209                             $encargs -cdb -home $testdir]
210                 }
211                 "tds" {
212                         set env [eval berkdb_env_noerr -create -mode 0644 \
213                             $encargs -txn -lock -log -thread -home $testdir]
214                 }
215         }
216         error_check_good env_open [is_valid_env $env] TRUE
217         return $env
218 }