2
0

test_helper.tcl 20 KB

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