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