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