test_helper.tcl 17 KB

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