test_helper.tcl 20 KB

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