default log level is now notice.
[redis.git] / tests / test_helper.tcl
1 # Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
2 # This softare is released under the BSD License. See the COPYING file for
3 # more information.
4
5 set tcl_precision 17
6 source tests/support/redis.tcl
7 source tests/support/server.tcl
8 source tests/support/tmpfile.tcl
9 source tests/support/test.tcl
10 source tests/support/util.tcl
11
12 set ::all_tests {
13 unit/printver
14 unit/auth
15 unit/protocol
16 unit/basic
17 unit/type/list
18 unit/type/list-2
19 unit/type/list-3
20 unit/type/set
21 unit/type/zset
22 unit/type/hash
23 unit/sort
24 unit/expire
25 unit/other
26 unit/cas
27 unit/quit
28 integration/replication
29 integration/replication-2
30 integration/replication-3
31 integration/aof
32 unit/pubsub
33 unit/slowlog
34 unit/scripting
35 unit/maxmemory
36 }
37 # Index to the next test to run in the ::all_tests list.
38 set ::next_test 0
39
40 set ::host 127.0.0.1
41 set ::port 21111
42 set ::traceleaks 0
43 set ::valgrind 0
44 set ::verbose 0
45 set ::quiet 0
46 set ::denytags {}
47 set ::allowtags {}
48 set ::external 0; # If "1" this means, we are running against external instance
49 set ::file ""; # If set, runs only the tests in this comma separated list
50 set ::curfile ""; # Hold the filename of the current suite
51 set ::accurate 0; # If true runs fuzz tests with more iterations
52 set ::force_failure 0
53
54 # Set to 1 when we are running in client mode. The Redis test uses a
55 # server-client model to run tests simultaneously. The server instance
56 # runs the specified number of client instances that will actually run tests.
57 # The server is responsible of showing the result to the user, and exit with
58 # the appropriate exit code depending on the test outcome.
59 set ::client 0
60 set ::numclients 16
61
62 proc execute_tests name {
63 set path "tests/$name.tcl"
64 set ::curfile $path
65 source $path
66 send_data_packet $::test_server_fd done "$name"
67 }
68
69 # Setup a list to hold a stack of server configs. When calls to start_server
70 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
71 # outer servers, use "srv -1 pid" etcetera.
72 set ::servers {}
73 proc srv {args} {
74 set level 0
75 if {[string is integer [lindex $args 0]]} {
76 set level [lindex $args 0]
77 set property [lindex $args 1]
78 } else {
79 set property [lindex $args 0]
80 }
81 set srv [lindex $::servers end+$level]
82 dict get $srv $property
83 }
84
85 # Provide easy access to the client for the inner server. It's possible to
86 # prepend the argument list with a negative level to access clients for
87 # servers running in outer blocks.
88 proc r {args} {
89 set level 0
90 if {[string is integer [lindex $args 0]]} {
91 set level [lindex $args 0]
92 set args [lrange $args 1 end]
93 }
94 [srv $level "client"] {*}$args
95 }
96
97 proc reconnect {args} {
98 set level [lindex $args 0]
99 if {[string length $level] == 0 || ![string is integer $level]} {
100 set level 0
101 }
102
103 set srv [lindex $::servers end+$level]
104 set host [dict get $srv "host"]
105 set port [dict get $srv "port"]
106 set config [dict get $srv "config"]
107 set client [redis $host $port]
108 dict set srv "client" $client
109
110 # select the right db when we don't have to authenticate
111 if {![dict exists $config "requirepass"]} {
112 $client select 9
113 }
114
115 # re-set $srv in the servers list
116 set ::servers [lreplace $::servers end+$level 1 $srv]
117 }
118
119 proc redis_deferring_client {args} {
120 set level 0
121 if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
122 set level [lindex $args 0]
123 set args [lrange $args 1 end]
124 }
125
126 # create client that defers reading reply
127 set client [redis [srv $level "host"] [srv $level "port"] 1]
128
129 # select the right db and read the response (OK)
130 $client select 9
131 $client read
132 return $client
133 }
134
135 # Provide easy access to INFO properties. Same semantic as "proc r".
136 proc s {args} {
137 set level 0
138 if {[string is integer [lindex $args 0]]} {
139 set level [lindex $args 0]
140 set args [lrange $args 1 end]
141 }
142 status [srv $level "client"] [lindex $args 0]
143 }
144
145 proc cleanup {} {
146 if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
147 flush stdout
148 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
149 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
150 if {!$::quiet} {puts "OK"}
151 }
152
153 proc find_available_port start {
154 for {set j $start} {$j < $start+1024} {incr j} {
155 if {[catch {
156 set fd [socket 127.0.0.1 $start]
157 }]} {
158 return $start
159 } else {
160 close $fd
161 }
162 }
163 if {$j == $start+1024} {
164 error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
165 }
166 }
167
168 proc test_server_main {} {
169 cleanup
170 # Open a listening socket, trying different ports in order to find a
171 # non busy one.
172 set port [find_available_port 11111]
173 if {!$::quiet} {
174 puts "Starting test server at port $port"
175 }
176 socket -server accept_test_clients $port
177
178 # Start the client instances
179 set ::clients_pids {}
180 set start_port [expr {$::port+100}]
181 for {set j 0} {$j < $::numclients} {incr j} {
182 set start_port [find_available_port $start_port]
183 set p [exec tclsh8.5 [info script] {*}$::argv \
184 --client $port --port $start_port &]
185 lappend ::clients_pids $p
186 incr start_port 10
187 }
188
189 # Setup global state for the test server
190 set ::idle_clients {}
191 set ::active_clients {}
192 array set ::clients_start_time {}
193 set ::clients_time_history {}
194 set ::failed_tests {}
195
196 # Enter the event loop to handle clients I/O
197 after 100 test_server_cron
198 vwait forever
199 }
200
201 # This function gets called 10 times per second, for now does nothing but
202 # may be used in the future in order to detect test clients taking too much
203 # time to execute the task.
204 proc test_server_cron {} {
205 }
206
207 proc accept_test_clients {fd addr port} {
208 fileevent $fd readable [list read_from_test_client $fd]
209 }
210
211 # This is the readable handler of our test server. Clients send us messages
212 # in the form of a status code such and additional data. Supported
213 # status types are:
214 #
215 # ready: the client is ready to execute the command. Only sent at client
216 # startup. The server will queue the client FD in the list of idle
217 # clients.
218 # testing: just used to signal that a given test started.
219 # ok: a test was executed with success.
220 # err: a test was executed with an error.
221 # exception: there was a runtime exception while executing the test.
222 # done: all the specified test file was processed, this test client is
223 # ready to accept a new task.
224 proc read_from_test_client fd {
225 set bytes [gets $fd]
226 set payload [read $fd $bytes]
227 foreach {status data} $payload break
228 if {$status eq {ready}} {
229 if {!$::quiet} {
230 puts "\[$status\]: $data"
231 }
232 signal_idle_client $fd
233 } elseif {$status eq {done}} {
234 set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
235 set all_tests_count [llength $::all_tests]
236 set running_tests_count [expr {[llength $::active_clients]-1}]
237 set completed_tests_count [expr {$::next_test-$running_tests_count}]
238 puts "\[$completed_tests_count/$all_tests_count [colorstr yellow $status]\]: $data ($elapsed seconds)"
239 lappend ::clients_time_history $elapsed $data
240 signal_idle_client $fd
241 } elseif {$status eq {ok}} {
242 if {!$::quiet} {
243 puts "\[[colorstr green $status]\]: $data"
244 }
245 } elseif {$status eq {err}} {
246 set err "\[[colorstr red $status]\]: $data"
247 puts $err
248 lappend ::failed_tests $err
249 } elseif {$status eq {exception}} {
250 puts "\[[colorstr red $status]\]: $data"
251 foreach p $::clients_pids {
252 catch {exec kill -9 $p}
253 }
254 exit 1
255 } elseif {$status eq {testing}} {
256 # No op
257 } else {
258 if {!$::quiet} {
259 puts "\[$status\]: $data"
260 }
261 }
262 }
263
264 # A new client is idle. Remove it from the list of active clients and
265 # if there are still test units to run, launch them.
266 proc signal_idle_client fd {
267 # Remove this fd from the list of active clients.
268 set ::active_clients \
269 [lsearch -all -inline -not -exact $::active_clients $fd]
270 # New unit to process?
271 if {$::next_test != [llength $::all_tests]} {
272 if {!$::quiet} {
273 puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
274 }
275 set ::clients_start_time($fd) [clock seconds]
276 send_data_packet $fd run [lindex $::all_tests $::next_test]
277 lappend ::active_clients $fd
278 incr ::next_test
279 } else {
280 lappend ::idle_clients $fd
281 if {[llength $::active_clients] == 0} {
282 the_end
283 }
284 }
285 }
286
287 # The the_end funciton gets called when all the test units were already
288 # executed, so the test finished.
289 proc the_end {} {
290 # TODO: print the status, exit with the rigth exit code.
291 puts "\n The End\n"
292 puts "Execution time of different units:"
293 foreach {time name} $::clients_time_history {
294 puts " $time seconds - $name"
295 }
296 if {[llength $::failed_tests]} {
297 puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
298 foreach failed $::failed_tests {
299 puts "*** $failed"
300 }
301 cleanup
302 exit 1
303 } else {
304 puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
305 cleanup
306 exit 0
307 }
308 }
309
310 # The client is not even driven (the test server is instead) as we just need
311 # to read the command, execute, reply... all this in a loop.
312 proc test_client_main server_port {
313 set ::test_server_fd [socket localhost $server_port]
314 send_data_packet $::test_server_fd ready [pid]
315 while 1 {
316 set bytes [gets $::test_server_fd]
317 set payload [read $::test_server_fd $bytes]
318 foreach {cmd data} $payload break
319 if {$cmd eq {run}} {
320 execute_tests $data
321 } else {
322 error "Unknown test client command: $cmd"
323 }
324 }
325 }
326
327 proc send_data_packet {fd status data} {
328 set payload [list $status $data]
329 puts $fd [string length $payload]
330 puts -nonewline $fd $payload
331 flush $fd
332 }
333
334 proc print_help_screen {} {
335 puts [join {
336 "--valgrind Run the test over valgrind."
337 "--accurate Run slow randomized tests for more iterations."
338 "--quiet Don't show individual tests."
339 "--single <unit> Just execute the specified unit (see next option)."
340 "--list-tests List all the available test units."
341 "--force-failure Force the execution of a test that always fails."
342 "--help Print this help screen."
343 } "\n"]
344 }
345
346 # parse arguments
347 for {set j 0} {$j < [llength $argv]} {incr j} {
348 set opt [lindex $argv $j]
349 set arg [lindex $argv [expr $j+1]]
350 if {$opt eq {--tags}} {
351 foreach tag $arg {
352 if {[string index $tag 0] eq "-"} {
353 lappend ::denytags [string range $tag 1 end]
354 } else {
355 lappend ::allowtags $tag
356 }
357 }
358 incr j
359 } elseif {$opt eq {--valgrind}} {
360 set ::valgrind 1
361 } elseif {$opt eq {--quiet}} {
362 set ::quiet 1
363 } elseif {$opt eq {--host}} {
364 set ::external 1
365 set ::host $arg
366 incr j
367 } elseif {$opt eq {--port}} {
368 set ::port $arg
369 incr j
370 } elseif {$opt eq {--accurate}} {
371 set ::accurate 1
372 } elseif {$opt eq {--force-failure}} {
373 set ::force_failure 1
374 } elseif {$opt eq {--single}} {
375 set ::all_tests $arg
376 incr j
377 } elseif {$opt eq {--list-tests}} {
378 foreach t $::all_tests {
379 puts $t
380 }
381 exit 0
382 } elseif {$opt eq {--client}} {
383 set ::client 1
384 set ::test_server_port $arg
385 incr j
386 } elseif {$opt eq {--help}} {
387 print_help_screen
388 exit 0
389 } else {
390 puts "Wrong argument: $opt"
391 exit 1
392 }
393 }
394
395 if {$::client} {
396 if {[catch { test_client_main $::test_server_port } err]} {
397 set estr "Executing test client: $err.\n$::errorInfo"
398 if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
399 puts $estr
400 }
401 exit 1
402 }
403 } else {
404 if {[catch { test_server_main } err]} {
405 if {[string length $err] > 0} {
406 # only display error when not generated by the test suite
407 if {$err ne "exception"} {
408 puts $::errorInfo
409 }
410 exit 1
411 }
412 }
413 }