test_helper.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. # Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
  2. # This software is released under the BSD License. See the COPYING file for
  3. # more information.
  4. package require Tcl 8.5
  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. set ::all_tests {
  12. unit/printver
  13. unit/dump
  14. unit/auth
  15. unit/protocol
  16. unit/keyspace
  17. unit/scan
  18. unit/type/string
  19. unit/type/incr
  20. unit/type/list
  21. unit/type/list-2
  22. unit/type/list-3
  23. unit/type/set
  24. unit/type/zset
  25. unit/type/hash
  26. unit/type/stream
  27. unit/type/stream-cgroups
  28. unit/sort
  29. unit/expire
  30. unit/other
  31. unit/multi
  32. unit/quit
  33. unit/aofrw
  34. integration/block-repl
  35. integration/replication
  36. integration/replication-2
  37. integration/replication-3
  38. integration/replication-4
  39. integration/replication-psync
  40. integration/aof
  41. integration/rdb
  42. integration/convert-zipmap-hash-on-load
  43. integration/logging
  44. integration/psync2
  45. integration/psync2-reg
  46. unit/pubsub
  47. unit/slowlog
  48. unit/scripting
  49. unit/maxmemory
  50. unit/introspection
  51. unit/introspection-2
  52. unit/limits
  53. unit/obuf-limits
  54. unit/bitops
  55. unit/bitfield
  56. unit/geo
  57. unit/memefficiency
  58. unit/hyperloglog
  59. unit/lazyfree
  60. unit/wait
  61. }
  62. # Index to the next test to run in the ::all_tests list.
  63. set ::next_test 0
  64. set ::host 127.0.0.1
  65. set ::port 21111
  66. set ::traceleaks 0
  67. set ::valgrind 0
  68. set ::stack_logging 0
  69. set ::verbose 0
  70. set ::quiet 0
  71. set ::denytags {}
  72. set ::allowtags {}
  73. set ::external 0; # If "1" this means, we are running against external instance
  74. set ::file ""; # If set, runs only the tests in this comma separated list
  75. set ::curfile ""; # Hold the filename of the current suite
  76. set ::accurate 0; # If true runs fuzz tests with more iterations
  77. set ::force_failure 0
  78. set ::timeout 600; # 10 minutes without progresses will quit the test.
  79. set ::last_progress [clock seconds]
  80. set ::active_servers {} ; # Pids of active Redis instances.
  81. # Set to 1 when we are running in client mode. The Redis test uses a
  82. # server-client model to run tests simultaneously. The server instance
  83. # runs the specified number of client instances that will actually run tests.
  84. # The server is responsible of showing the result to the user, and exit with
  85. # the appropriate exit code depending on the test outcome.
  86. set ::client 0
  87. set ::numclients 16
  88. proc execute_tests name {
  89. set path "tests/$name.tcl"
  90. set ::curfile $path
  91. source $path
  92. send_data_packet $::test_server_fd done "$name"
  93. }
  94. # Setup a list to hold a stack of server configs. When calls to start_server
  95. # are nested, use "srv 0 pid" to get the pid of the inner server. To access
  96. # outer servers, use "srv -1 pid" etcetera.
  97. set ::servers {}
  98. proc srv {args} {
  99. set level 0
  100. if {[string is integer [lindex $args 0]]} {
  101. set level [lindex $args 0]
  102. set property [lindex $args 1]
  103. } else {
  104. set property [lindex $args 0]
  105. }
  106. set srv [lindex $::servers end+$level]
  107. dict get $srv $property
  108. }
  109. # Provide easy access to the client for the inner server. It's possible to
  110. # prepend the argument list with a negative level to access clients for
  111. # servers running in outer blocks.
  112. proc r {args} {
  113. set level 0
  114. if {[string is integer [lindex $args 0]]} {
  115. set level [lindex $args 0]
  116. set args [lrange $args 1 end]
  117. }
  118. [srv $level "client"] {*}$args
  119. }
  120. proc reconnect {args} {
  121. set level [lindex $args 0]
  122. if {[string length $level] == 0 || ![string is integer $level]} {
  123. set level 0
  124. }
  125. set srv [lindex $::servers end+$level]
  126. set host [dict get $srv "host"]
  127. set port [dict get $srv "port"]
  128. set config [dict get $srv "config"]
  129. set client [redis $host $port]
  130. dict set srv "client" $client
  131. # select the right db when we don't have to authenticate
  132. if {![dict exists $config "requirepass"]} {
  133. $client select 9
  134. }
  135. # re-set $srv in the servers list
  136. lset ::servers end+$level $srv
  137. }
  138. proc redis_deferring_client {args} {
  139. set level 0
  140. if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
  141. set level [lindex $args 0]
  142. set args [lrange $args 1 end]
  143. }
  144. # create client that defers reading reply
  145. set client [redis [srv $level "host"] [srv $level "port"] 1]
  146. # select the right db and read the response (OK)
  147. $client select 9
  148. $client read
  149. return $client
  150. }
  151. # Provide easy access to INFO properties. Same semantic as "proc r".
  152. proc s {args} {
  153. set level 0
  154. if {[string is integer [lindex $args 0]]} {
  155. set level [lindex $args 0]
  156. set args [lrange $args 1 end]
  157. }
  158. status [srv $level "client"] [lindex $args 0]
  159. }
  160. proc cleanup {} {
  161. if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
  162. flush stdout
  163. catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
  164. catch {exec rm -rf {*}[glob tests/tmp/server.*]}
  165. if {!$::quiet} {puts "OK"}
  166. }
  167. proc test_server_main {} {
  168. cleanup
  169. set tclsh [info nameofexecutable]
  170. # Open a listening socket, trying different ports in order to find a
  171. # non busy one.
  172. set port [find_available_port 11111]
  173. if {!$::quiet} {
  174. puts "Starting test server at port $port"
  175. }
  176. socket -server accept_test_clients -myaddr 127.0.0.1 $port
  177. # Start the client instances
  178. set ::clients_pids {}
  179. set start_port [expr {$::port+100}]
  180. for {set j 0} {$j < $::numclients} {incr j} {
  181. set start_port [find_available_port $start_port]
  182. set p [exec $tclsh [info script] {*}$::argv \
  183. --client $port --port $start_port &]
  184. lappend ::clients_pids $p
  185. incr start_port 10
  186. }
  187. # Setup global state for the test server
  188. set ::idle_clients {}
  189. set ::active_clients {}
  190. array set ::active_clients_task {}
  191. array set ::clients_start_time {}
  192. set ::clients_time_history {}
  193. set ::failed_tests {}
  194. # Enter the event loop to handle clients I/O
  195. after 100 test_server_cron
  196. vwait forever
  197. }
  198. # This function gets called 10 times per second.
  199. proc test_server_cron {} {
  200. set elapsed [expr {[clock seconds]-$::last_progress}]
  201. if {$elapsed > $::timeout} {
  202. set err "\[[colorstr red TIMEOUT]\]: clients state report follows."
  203. puts $err
  204. show_clients_state
  205. kill_clients
  206. force_kill_all_servers
  207. the_end
  208. }
  209. after 100 test_server_cron
  210. }
  211. proc accept_test_clients {fd addr port} {
  212. fconfigure $fd -encoding binary
  213. fileevent $fd readable [list read_from_test_client $fd]
  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. set ::last_progress [clock seconds]
  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. set ::active_clients_task($fd) DONE
  247. } elseif {$status eq {ok}} {
  248. if {!$::quiet} {
  249. puts "\[[colorstr green $status]\]: $data"
  250. }
  251. set ::active_clients_task($fd) "(OK) $data"
  252. } elseif {$status eq {err}} {
  253. set err "\[[colorstr red $status]\]: $data"
  254. puts $err
  255. lappend ::failed_tests $err
  256. set ::active_clients_task($fd) "(ERR) $data"
  257. } elseif {$status eq {exception}} {
  258. puts "\[[colorstr red $status]\]: $data"
  259. kill_clients
  260. force_kill_all_servers
  261. exit 1
  262. } elseif {$status eq {testing}} {
  263. set ::active_clients_task($fd) "(IN PROGRESS) $data"
  264. } elseif {$status eq {server-spawned}} {
  265. lappend ::active_servers $data
  266. } elseif {$status eq {server-killed}} {
  267. set ::active_servers [lsearch -all -inline -not -exact $::active_servers $data]
  268. } else {
  269. if {!$::quiet} {
  270. puts "\[$status\]: $data"
  271. }
  272. }
  273. }
  274. proc show_clients_state {} {
  275. # The following loop is only useful for debugging tests that may
  276. # enter an infinite loop. Commented out normally.
  277. foreach x $::active_clients {
  278. if {[info exist ::active_clients_task($x)]} {
  279. puts "$x => $::active_clients_task($x)"
  280. } else {
  281. puts "$x => ???"
  282. }
  283. }
  284. }
  285. proc kill_clients {} {
  286. foreach p $::clients_pids {
  287. catch {exec kill $p}
  288. }
  289. }
  290. proc force_kill_all_servers {} {
  291. foreach p $::active_servers {
  292. puts "Killing still running Redis server $p"
  293. catch {exec kill -9 $p}
  294. }
  295. }
  296. # A new client is idle. Remove it from the list of active clients and
  297. # if there are still test units to run, launch them.
  298. proc signal_idle_client fd {
  299. # Remove this fd from the list of active clients.
  300. set ::active_clients \
  301. [lsearch -all -inline -not -exact $::active_clients $fd]
  302. if 0 {show_clients_state}
  303. # New unit to process?
  304. if {$::next_test != [llength $::all_tests]} {
  305. if {!$::quiet} {
  306. puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
  307. set ::active_clients_task($fd) "ASSIGNED: $fd ([lindex $::all_tests $::next_test])"
  308. }
  309. set ::clients_start_time($fd) [clock seconds]
  310. send_data_packet $fd run [lindex $::all_tests $::next_test]
  311. lappend ::active_clients $fd
  312. incr ::next_test
  313. } else {
  314. lappend ::idle_clients $fd
  315. if {[llength $::active_clients] == 0} {
  316. the_end
  317. }
  318. }
  319. }
  320. # The the_end function gets called when all the test units were already
  321. # executed, so the test finished.
  322. proc the_end {} {
  323. # TODO: print the status, exit with the rigth exit code.
  324. puts "\n The End\n"
  325. puts "Execution time of different units:"
  326. foreach {time name} $::clients_time_history {
  327. puts " $time seconds - $name"
  328. }
  329. if {[llength $::failed_tests]} {
  330. puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
  331. foreach failed $::failed_tests {
  332. puts "*** $failed"
  333. }
  334. cleanup
  335. exit 1
  336. } else {
  337. puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
  338. cleanup
  339. exit 0
  340. }
  341. }
  342. # The client is not even driven (the test server is instead) as we just need
  343. # to read the command, execute, reply... all this in a loop.
  344. proc test_client_main server_port {
  345. set ::test_server_fd [socket localhost $server_port]
  346. fconfigure $::test_server_fd -encoding binary
  347. send_data_packet $::test_server_fd ready [pid]
  348. while 1 {
  349. set bytes [gets $::test_server_fd]
  350. set payload [read $::test_server_fd $bytes]
  351. foreach {cmd data} $payload break
  352. if {$cmd eq {run}} {
  353. execute_tests $data
  354. } else {
  355. error "Unknown test client command: $cmd"
  356. }
  357. }
  358. }
  359. proc send_data_packet {fd status data} {
  360. set payload [list $status $data]
  361. puts $fd [string length $payload]
  362. puts -nonewline $fd $payload
  363. flush $fd
  364. }
  365. proc print_help_screen {} {
  366. puts [join {
  367. "--valgrind Run the test over valgrind."
  368. "--stack-logging Enable OSX leaks/malloc stack logging."
  369. "--accurate Run slow randomized tests for more iterations."
  370. "--quiet Don't show individual tests."
  371. "--single <unit> Just execute the specified unit (see next option)."
  372. "--list-tests List all the available test units."
  373. "--clients <num> Number of test clients (default 16)."
  374. "--timeout <sec> Test timeout in seconds (default 10 min)."
  375. "--force-failure Force the execution of a test that always fails."
  376. "--help Print this help screen."
  377. } "\n"]
  378. }
  379. # parse arguments
  380. for {set j 0} {$j < [llength $argv]} {incr j} {
  381. set opt [lindex $argv $j]
  382. set arg [lindex $argv [expr $j+1]]
  383. if {$opt eq {--tags}} {
  384. foreach tag $arg {
  385. if {[string index $tag 0] eq "-"} {
  386. lappend ::denytags [string range $tag 1 end]
  387. } else {
  388. lappend ::allowtags $tag
  389. }
  390. }
  391. incr j
  392. } elseif {$opt eq {--valgrind}} {
  393. set ::valgrind 1
  394. } elseif {$opt eq {--stack-logging}} {
  395. if {[string match {*Darwin*} [exec uname -a]]} {
  396. set ::stack_logging 1
  397. }
  398. } elseif {$opt eq {--quiet}} {
  399. set ::quiet 1
  400. } elseif {$opt eq {--host}} {
  401. set ::external 1
  402. set ::host $arg
  403. incr j
  404. } elseif {$opt eq {--port}} {
  405. set ::port $arg
  406. incr j
  407. } elseif {$opt eq {--accurate}} {
  408. set ::accurate 1
  409. } elseif {$opt eq {--force-failure}} {
  410. set ::force_failure 1
  411. } elseif {$opt eq {--single}} {
  412. set ::all_tests $arg
  413. incr j
  414. } elseif {$opt eq {--list-tests}} {
  415. foreach t $::all_tests {
  416. puts $t
  417. }
  418. exit 0
  419. } elseif {$opt eq {--verbose}} {
  420. set ::verbose 1
  421. } elseif {$opt eq {--client}} {
  422. set ::client 1
  423. set ::test_server_port $arg
  424. incr j
  425. } elseif {$opt eq {--clients}} {
  426. set ::numclients $arg
  427. incr j
  428. } elseif {$opt eq {--timeout}} {
  429. set ::timeout $arg
  430. incr j
  431. } elseif {$opt eq {--help}} {
  432. print_help_screen
  433. exit 0
  434. } else {
  435. puts "Wrong argument: $opt"
  436. exit 1
  437. }
  438. }
  439. proc attach_to_replication_stream {} {
  440. set s [socket [srv 0 "host"] [srv 0 "port"]]
  441. fconfigure $s -translation binary
  442. puts -nonewline $s "SYNC\r\n"
  443. flush $s
  444. # Get the count
  445. while 1 {
  446. set count [gets $s]
  447. set prefix [string range $count 0 0]
  448. if {$prefix ne {}} break; # Newlines are allowed as PINGs.
  449. }
  450. if {$prefix ne {$}} {
  451. error "attach_to_replication_stream error. Received '$count' as count."
  452. }
  453. set count [string range $count 1 end]
  454. # Consume the bulk payload
  455. while {$count} {
  456. set buf [read $s $count]
  457. set count [expr {$count-[string length $buf]}]
  458. }
  459. return $s
  460. }
  461. proc read_from_replication_stream {s} {
  462. fconfigure $s -blocking 0
  463. set attempt 0
  464. while {[gets $s count] == -1} {
  465. if {[incr attempt] == 10} return ""
  466. after 100
  467. }
  468. fconfigure $s -blocking 1
  469. set count [string range $count 1 end]
  470. # Return a list of arguments for the command.
  471. set res {}
  472. for {set j 0} {$j < $count} {incr j} {
  473. read $s 1
  474. set arg [::redis::redis_bulk_read $s]
  475. if {$j == 0} {set arg [string tolower $arg]}
  476. lappend res $arg
  477. }
  478. return $res
  479. }
  480. proc assert_replication_stream {s patterns} {
  481. for {set j 0} {$j < [llength $patterns]} {incr j} {
  482. assert_match [lindex $patterns $j] [read_from_replication_stream $s]
  483. }
  484. }
  485. proc close_replication_stream {s} {
  486. close $s
  487. }
  488. # With the parallel test running multiple Redis instances at the same time
  489. # we need a fast enough computer, otherwise a lot of tests may generate
  490. # false positives.
  491. # If the computer is too slow we revert the sequential test without any
  492. # parallelism, that is, clients == 1.
  493. proc is_a_slow_computer {} {
  494. set start [clock milliseconds]
  495. for {set j 0} {$j < 1000000} {incr j} {}
  496. set elapsed [expr [clock milliseconds]-$start]
  497. expr {$elapsed > 200}
  498. }
  499. if {$::client} {
  500. if {[catch { test_client_main $::test_server_port } err]} {
  501. set estr "Executing test client: $err.\n$::errorInfo"
  502. if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
  503. puts $estr
  504. }
  505. exit 1
  506. }
  507. } else {
  508. if {[is_a_slow_computer]} {
  509. puts "** SLOW COMPUTER ** Using a single client to avoid false positives."
  510. set ::numclients 1
  511. }
  512. if {[catch { test_server_main } err]} {
  513. if {[string length $err] > 0} {
  514. # only display error when not generated by the test suite
  515. if {$err ne "exception"} {
  516. puts $::errorInfo
  517. }
  518. exit 1
  519. }
  520. }
  521. }