]>
Commit | Line | Data |
---|---|---|
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 | ||
5 | set tcl_precision 17 | |
ab72b483 | 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 | |
98578b57 | 11 | |
13566085 | 12 | set ::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 | |
28 | integration/replication | |
569f84aa | 29 | integration/replication-2 |
30 | integration/replication-3 | |
13566085 | 31 | integration/aof |
5c639226 | 32 | integration/aof-race |
13566085 | 33 | unit/pubsub |
34 | unit/slowlog | |
0681c5ad | 35 | unit/scripting |
243b783f | 36 | unit/maxmemory |
13566085 | 37 | } |
38 | # Index to the next test to run in the ::all_tests list. | |
39 | set ::next_test 0 | |
40 | ||
98578b57 | 41 | set ::host 127.0.0.1 |
24bfb570 | 42 | set ::port 21111 |
e59a64b8 | 43 | set ::traceleaks 0 |
c4669d25 | 44 | set ::valgrind 0 |
322ea972 | 45 | set ::verbose 0 |
6e0e5bed PN |
46 | set ::denytags {} |
47 | set ::allowtags {} | |
7d04fc75 | 48 | set ::external 0; # If "1" this means, we are running against external instance |
9f1ae9ab | 49 | set ::file ""; # If set, runs only the tests in this comma separated list |
6f8a32d5 | 50 | set ::curfile ""; # Hold the filename of the current suite |
524d515f | 51 | set ::accurate 0; # If true runs fuzz tests with more iterations |
04e2410d | 52 | set ::force_failure 0 |
13566085 | 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 | |
98578b57 PN |
61 | |
62 | proc execute_tests name { | |
6f8a32d5 PN |
63 | set path "tests/$name.tcl" |
64 | set ::curfile $path | |
65 | source $path | |
36e790a0 | 66 | send_data_packet $::test_server_fd done "$name" |
98578b57 PN |
67 | } |
68 | ||
1c4114be PN |
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 {} | |
f2dd4769 PN |
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 | } | |
1c4114be PN |
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. | |
98578b57 | 88 | proc r {args} { |
1c4114be PN |
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 | ||
941c9fa2 PN |
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 | ||
5eedc9c6 PN |
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 | ||
1c4114be PN |
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] | |
98578b57 PN |
143 | } |
144 | ||
f166bb1d | 145 | proc cleanup {} { |
c7c16a32 | 146 | puts -nonewline "Cleanup: may take some time... " |
13566085 | 147 | flush stdout |
c4669d25 | 148 | catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]} |
149 | catch {exec rm -rf {*}[glob tests/tmp/server.*]} | |
13566085 | 150 | puts "OK" |
f166bb1d PN |
151 | } |
152 | ||
24bfb570 | 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 | ||
13566085 | 168 | proc test_server_main {} { |
9f1ae9ab | 169 | cleanup |
13566085 | 170 | # Open a listening socket, trying different ports in order to find a |
171 | # non busy one. | |
24bfb570 | 172 | set port [find_available_port 11111] |
173 | puts "Starting test server at port $port" | |
174 | socket -server accept_test_clients $port | |
3fe40d6e | 175 | |
13566085 | 176 | # Start the client instances |
569f84aa | 177 | set ::clients_pids {} |
24bfb570 | 178 | set start_port [expr {$::port+100}] |
13566085 | 179 | for {set j 0} {$j < $::numclients} {incr j} { |
24bfb570 | 180 | set start_port [find_available_port $start_port] |
569f84aa | 181 | set p [exec tclsh8.5 [info script] {*}$::argv \ |
24bfb570 | 182 | --client $port --port $start_port &] |
569f84aa | 183 | lappend ::clients_pids $p |
24bfb570 | 184 | incr start_port 10 |
13566085 | 185 | } |
9f1ae9ab | 186 | |
13566085 | 187 | # Setup global state for the test server |
188 | set ::idle_clients {} | |
189 | set ::active_clients {} | |
36e790a0 | 190 | array set ::clients_start_time {} |
191 | set ::clients_time_history {} | |
04e2410d | 192 | set ::failed_tests {} |
13566085 | 193 | |
194 | # Enter the event loop to handle clients I/O | |
195 | after 100 test_server_cron | |
196 | vwait forever | |
197 | } | |
198 | ||
199 | # This function gets called 10 times per second, for now does nothing but | |
200 | # may be used in the future in order to detect test clients taking too much | |
201 | # time to execute the task. | |
202 | proc test_server_cron {} { | |
203 | } | |
204 | ||
205 | proc accept_test_clients {fd addr port} { | |
206 | fileevent $fd readable [list read_from_test_client $fd] | |
207 | } | |
208 | ||
209 | # This is the readable handler of our test server. Clients send us messages | |
210 | # in the form of a status code such and additional data. Supported | |
211 | # status types are: | |
212 | # | |
213 | # ready: the client is ready to execute the command. Only sent at client | |
214 | # startup. The server will queue the client FD in the list of idle | |
215 | # clients. | |
216 | # testing: just used to signal that a given test started. | |
217 | # ok: a test was executed with success. | |
218 | # err: a test was executed with an error. | |
219 | # exception: there was a runtime exception while executing the test. | |
220 | # done: all the specified test file was processed, this test client is | |
221 | # ready to accept a new task. | |
222 | proc read_from_test_client fd { | |
223 | set bytes [gets $fd] | |
224 | set payload [read $fd $bytes] | |
225 | foreach {status data} $payload break | |
13566085 | 226 | if {$status eq {ready}} { |
82e5dd35 | 227 | puts "\[$status\]: $data" |
13566085 | 228 | signal_idle_client $fd |
229 | } elseif {$status eq {done}} { | |
36e790a0 | 230 | set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}] |
82e5dd35 | 231 | puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)" |
569f84aa | 232 | puts "+++ [expr {[llength $::active_clients]-1}] units still in execution." |
36e790a0 | 233 | lappend ::clients_time_history $elapsed $data |
13566085 | 234 | signal_idle_client $fd |
3744824c | 235 | } elseif {$status eq {ok}} { |
82e5dd35 | 236 | puts "\[[colorstr green $status]\]: $data" |
3744824c | 237 | } elseif {$status eq {err}} { |
04e2410d | 238 | set err "\[[colorstr red $status]\]: $data" |
239 | puts $err | |
240 | lappend ::failed_tests $err | |
569f84aa | 241 | } elseif {$status eq {exception}} { |
242 | puts "\[[colorstr red $status]\]: $data" | |
243 | foreach p $::clients_pids { | |
244 | catch {exec kill -9 $p} | |
9f1ae9ab | 245 | } |
569f84aa | 246 | exit 1 |
daab1599 | 247 | } elseif {$status eq {testing}} { |
248 | # No op | |
9f1ae9ab | 249 | } else { |
82e5dd35 | 250 | puts "\[$status\]: $data" |
9f1ae9ab | 251 | } |
13566085 | 252 | } |
e39c8b50 | 253 | |
13566085 | 254 | # A new client is idle. Remove it from the list of active clients and |
255 | # if there are still test units to run, launch them. | |
256 | proc signal_idle_client fd { | |
257 | # Remove this fd from the list of active clients. | |
258 | set ::active_clients \ | |
259 | [lsearch -all -inline -not -exact $::active_clients $fd] | |
260 | # New unit to process? | |
261 | if {$::next_test != [llength $::all_tests]} { | |
82e5dd35 | 262 | puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"] |
36e790a0 | 263 | set ::clients_start_time($fd) [clock seconds] |
13566085 | 264 | send_data_packet $fd run [lindex $::all_tests $::next_test] |
265 | lappend ::active_clients $fd | |
266 | incr ::next_test | |
9f1ae9ab | 267 | } else { |
13566085 | 268 | lappend ::idle_clients $fd |
269 | if {[llength $::active_clients] == 0} { | |
270 | the_end | |
6f8a32d5 | 271 | } |
9f1ae9ab | 272 | } |
13566085 | 273 | } |
6f8a32d5 | 274 | |
13566085 | 275 | # The the_end funciton gets called when all the test units were already |
276 | # executed, so the test finished. | |
277 | proc the_end {} { | |
278 | # TODO: print the status, exit with the rigth exit code. | |
04e2410d | 279 | puts "\n The End\n" |
36e790a0 | 280 | puts "Execution time of different units:" |
281 | foreach {time name} $::clients_time_history { | |
282 | puts " $time seconds - $name" | |
283 | } | |
04e2410d | 284 | if {[llength $::failed_tests]} { |
121ffc85 | 285 | puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n" |
04e2410d | 286 | foreach failed $::failed_tests { |
287 | puts "*** $failed" | |
288 | } | |
c7c16a32 | 289 | cleanup |
e39c8b50 | 290 | exit 1 |
04e2410d | 291 | } else { |
292 | puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n" | |
c7c16a32 | 293 | cleanup |
04e2410d | 294 | exit 0 |
98578b57 | 295 | } |
98578b57 PN |
296 | } |
297 | ||
13566085 | 298 | # The client is not even driven (the test server is instead) as we just need |
299 | # to read the command, execute, reply... all this in a loop. | |
300 | proc test_client_main server_port { | |
301 | set ::test_server_fd [socket localhost $server_port] | |
302 | send_data_packet $::test_server_fd ready [pid] | |
303 | while 1 { | |
304 | set bytes [gets $::test_server_fd] | |
305 | set payload [read $::test_server_fd $bytes] | |
306 | foreach {cmd data} $payload break | |
307 | if {$cmd eq {run}} { | |
308 | execute_tests $data | |
309 | } else { | |
310 | error "Unknown test client command: $cmd" | |
6f8a32d5 | 311 | } |
98578b57 | 312 | } |
13566085 | 313 | } |
cabe03eb | 314 | |
13566085 | 315 | proc send_data_packet {fd status data} { |
316 | set payload [list $status $data] | |
317 | puts $fd [string length $payload] | |
318 | puts -nonewline $fd $payload | |
319 | flush $fd | |
98578b57 PN |
320 | } |
321 | ||
e4715f00 | 322 | proc print_help_screen {} { |
323 | puts [join { | |
324 | "--valgrind Run the test over valgrind." | |
325 | "--accurate Run slow randomized tests for more iterations." | |
326 | "--single <unit> Just execute the specified unit (see next option)." | |
327 | "--list-tests List all the available test units." | |
328 | "--force-failure Force the execution of a test that always fails." | |
329 | "--help Print this help screen." | |
330 | } "\n"] | |
331 | } | |
332 | ||
73bd6c58 PN |
333 | # parse arguments |
334 | for {set j 0} {$j < [llength $argv]} {incr j} { | |
335 | set opt [lindex $argv $j] | |
336 | set arg [lindex $argv [expr $j+1]] | |
337 | if {$opt eq {--tags}} { | |
338 | foreach tag $arg { | |
339 | if {[string index $tag 0] eq "-"} { | |
340 | lappend ::denytags [string range $tag 1 end] | |
341 | } else { | |
342 | lappend ::allowtags $tag | |
343 | } | |
344 | } | |
345 | incr j | |
4b918769 | 346 | } elseif {$opt eq {--valgrind}} { |
347 | set ::valgrind 1 | |
7d04fc75 | 348 | } elseif {$opt eq {--host}} { |
349 | set ::external 1 | |
350 | set ::host $arg | |
351 | incr j | |
352 | } elseif {$opt eq {--port}} { | |
353 | set ::port $arg | |
354 | incr j | |
524d515f | 355 | } elseif {$opt eq {--accurate}} { |
356 | set ::accurate 1 | |
04e2410d | 357 | } elseif {$opt eq {--force-failure}} { |
358 | set ::force_failure 1 | |
524d515f | 359 | } elseif {$opt eq {--single}} { |
360 | set ::all_tests $arg | |
361 | incr j | |
362 | } elseif {$opt eq {--list-tests}} { | |
363 | foreach t $::all_tests { | |
364 | puts $t | |
365 | } | |
366 | exit 0 | |
13566085 | 367 | } elseif {$opt eq {--client}} { |
368 | set ::client 1 | |
369 | set ::test_server_port $arg | |
370 | incr j | |
524d515f | 371 | } elseif {$opt eq {--help}} { |
e4715f00 | 372 | print_help_screen |
524d515f | 373 | exit 0 |
73bd6c58 PN |
374 | } else { |
375 | puts "Wrong argument: $opt" | |
376 | exit 1 | |
377 | } | |
378 | } | |
379 | ||
13566085 | 380 | if {$::client} { |
381 | if {[catch { test_client_main $::test_server_port } err]} { | |
382 | set estr "Executing test client: $err.\n$::errorInfo" | |
383 | if {[catch {send_data_packet $::test_server_fd exception $estr}]} { | |
384 | puts $estr | |
436f18b6 PN |
385 | } |
386 | exit 1 | |
387 | } | |
13566085 | 388 | } else { |
389 | if {[catch { test_server_main } err]} { | |
390 | if {[string length $err] > 0} { | |
391 | # only display error when not generated by the test suite | |
392 | if {$err ne "exception"} { | |
393 | puts $::errorInfo | |
394 | } | |
395 | exit 1 | |
396 | } | |
397 | } | |
436f18b6 | 398 | } |