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