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