test_helper.tcl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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/info
  19. unit/type/string
  20. unit/type/incr
  21. unit/type/list
  22. unit/type/list-2
  23. unit/type/list-3
  24. unit/type/set
  25. unit/type/zset
  26. unit/type/hash
  27. unit/type/stream
  28. unit/type/stream-cgroups
  29. unit/sort
  30. unit/expire
  31. unit/other
  32. unit/multi
  33. unit/quit
  34. unit/aofrw
  35. unit/acl
  36. unit/latency-monitor
  37. integration/block-repl
  38. integration/replication
  39. integration/replication-2
  40. integration/replication-3
  41. integration/replication-4
  42. integration/replication-psync
  43. integration/replication-buffer
  44. integration/aof
  45. integration/rdb
  46. integration/corrupt-dump
  47. integration/corrupt-dump-fuzzer
  48. integration/convert-zipmap-hash-on-load
  49. integration/convert-ziplist-hash-on-load
  50. integration/convert-ziplist-zset-on-load
  51. integration/logging
  52. integration/psync2
  53. integration/psync2-reg
  54. integration/psync2-pingoff
  55. integration/psync2-master-restart
  56. integration/failover
  57. integration/redis-cli
  58. integration/redis-benchmark
  59. integration/dismiss-mem
  60. unit/pubsub
  61. unit/slowlog
  62. unit/scripting
  63. unit/maxmemory
  64. unit/introspection
  65. unit/introspection-2
  66. unit/limits
  67. unit/obuf-limits
  68. unit/bitops
  69. unit/bitfield
  70. unit/geo
  71. unit/memefficiency
  72. unit/hyperloglog
  73. unit/lazyfree
  74. unit/wait
  75. unit/pause
  76. unit/querybuf
  77. unit/pendingquerybuf
  78. unit/tls
  79. unit/tracking
  80. unit/oom-score-adj
  81. unit/shutdown
  82. unit/networking
  83. unit/cluster
  84. unit/client-eviction
  85. }
  86. # Index to the next test to run in the ::all_tests list.
  87. set ::next_test 0
  88. set ::host 127.0.0.1
  89. set ::port 6379; # port for external server
  90. set ::baseport 21111; # initial port for spawned redis servers
  91. set ::portcount 8000; # we don't wanna use more than 10000 to avoid collision with cluster bus ports
  92. set ::traceleaks 0
  93. set ::valgrind 0
  94. set ::durable 0
  95. set ::tls 0
  96. set ::stack_logging 0
  97. set ::verbose 0
  98. set ::quiet 0
  99. set ::denytags {}
  100. set ::skiptests {}
  101. set ::skipunits {}
  102. set ::no_latency 0
  103. set ::allowtags {}
  104. set ::only_tests {}
  105. set ::single_tests {}
  106. set ::run_solo_tests {}
  107. set ::skip_till ""
  108. set ::external 0; # If "1" this means, we are running against external instance
  109. set ::file ""; # If set, runs only the tests in this comma separated list
  110. set ::curfile ""; # Hold the filename of the current suite
  111. set ::accurate 0; # If true runs fuzz tests with more iterations
  112. set ::force_failure 0
  113. set ::timeout 1200; # 20 minutes without progresses will quit the test.
  114. set ::last_progress [clock seconds]
  115. set ::active_servers {} ; # Pids of active Redis instances.
  116. set ::dont_clean 0
  117. set ::wait_server 0
  118. set ::stop_on_failure 0
  119. set ::dump_logs 0
  120. set ::loop 0
  121. set ::tlsdir "tests/tls"
  122. set ::singledb 0
  123. set ::cluster_mode 0
  124. set ::ignoreencoding 0
  125. set ::ignoredigest 0
  126. # Set to 1 when we are running in client mode. The Redis test uses a
  127. # server-client model to run tests simultaneously. The server instance
  128. # runs the specified number of client instances that will actually run tests.
  129. # The server is responsible of showing the result to the user, and exit with
  130. # the appropriate exit code depending on the test outcome.
  131. set ::client 0
  132. set ::numclients 16
  133. # This function is called by one of the test clients when it receives
  134. # a "run" command from the server, with a filename as data.
  135. # It will run the specified test source file and signal it to the
  136. # test server when finished.
  137. proc execute_test_file __testname {
  138. set path "tests/$__testname.tcl"
  139. set ::curfile $path
  140. source $path
  141. send_data_packet $::test_server_fd done "$__testname"
  142. }
  143. # This function is called by one of the test clients when it receives
  144. # a "run_code" command from the server, with a verbatim test source code
  145. # as argument, and an associated name.
  146. # It will run the specified code and signal it to the test server when
  147. # finished.
  148. proc execute_test_code {__testname filename code} {
  149. set ::curfile $filename
  150. eval $code
  151. send_data_packet $::test_server_fd done "$__testname"
  152. }
  153. # Setup a list to hold a stack of server configs. When calls to start_server
  154. # are nested, use "srv 0 pid" to get the pid of the inner server. To access
  155. # outer servers, use "srv -1 pid" etcetera.
  156. set ::servers {}
  157. proc srv {args} {
  158. set level 0
  159. if {[string is integer [lindex $args 0]]} {
  160. set level [lindex $args 0]
  161. set property [lindex $args 1]
  162. } else {
  163. set property [lindex $args 0]
  164. }
  165. set srv [lindex $::servers end+$level]
  166. dict get $srv $property
  167. }
  168. # Provide easy access to the client for the inner server. It's possible to
  169. # prepend the argument list with a negative level to access clients for
  170. # servers running in outer blocks.
  171. proc r {args} {
  172. set level 0
  173. if {[string is integer [lindex $args 0]]} {
  174. set level [lindex $args 0]
  175. set args [lrange $args 1 end]
  176. }
  177. [srv $level "client"] {*}$args
  178. }
  179. proc reconnect {args} {
  180. set level [lindex $args 0]
  181. if {[string length $level] == 0 || ![string is integer $level]} {
  182. set level 0
  183. }
  184. set srv [lindex $::servers end+$level]
  185. set host [dict get $srv "host"]
  186. set port [dict get $srv "port"]
  187. set config [dict get $srv "config"]
  188. set client [redis $host $port 0 $::tls]
  189. if {[dict exists $srv "client"]} {
  190. set old [dict get $srv "client"]
  191. $old close
  192. }
  193. dict set srv "client" $client
  194. # select the right db when we don't have to authenticate
  195. if {![dict exists $config "requirepass"] && !$::singledb} {
  196. $client select 9
  197. }
  198. # re-set $srv in the servers list
  199. lset ::servers end+$level $srv
  200. }
  201. proc redis_deferring_client {args} {
  202. set level 0
  203. if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
  204. set level [lindex $args 0]
  205. set args [lrange $args 1 end]
  206. }
  207. # create client that defers reading reply
  208. set client [redis [srv $level "host"] [srv $level "port"] 1 $::tls]
  209. # select the right db and read the response (OK)
  210. if {!$::singledb} {
  211. $client select 9
  212. $client read
  213. } else {
  214. # For timing/symmetry with the above select
  215. $client ping
  216. $client read
  217. }
  218. return $client
  219. }
  220. proc redis_client {args} {
  221. set level 0
  222. if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
  223. set level [lindex $args 0]
  224. set args [lrange $args 1 end]
  225. }
  226. # create client that defers reading reply
  227. set client [redis [srv $level "host"] [srv $level "port"] 0 $::tls]
  228. # select the right db and read the response (OK), or at least ping
  229. # the server if we're in a singledb mode.
  230. if {$::singledb} {
  231. $client ping
  232. } else {
  233. $client select 9
  234. }
  235. return $client
  236. }
  237. # Provide easy access to INFO properties. Same semantic as "proc r".
  238. proc s {args} {
  239. set level 0
  240. if {[string is integer [lindex $args 0]]} {
  241. set level [lindex $args 0]
  242. set args [lrange $args 1 end]
  243. }
  244. status [srv $level "client"] [lindex $args 0]
  245. }
  246. # Test wrapped into run_solo are sent back from the client to the
  247. # test server, so that the test server will send them again to
  248. # clients once the clients are idle.
  249. proc run_solo {name code} {
  250. if {$::numclients == 1 || $::loop || $::external} {
  251. # run_solo is not supported in these scenarios, just run the code.
  252. eval $code
  253. return
  254. }
  255. send_data_packet $::test_server_fd run_solo [list $name $::curfile $code]
  256. }
  257. proc cleanup {} {
  258. if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
  259. flush stdout
  260. catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
  261. catch {exec rm -rf {*}[glob tests/tmp/server.*]}
  262. if {!$::quiet} {puts "OK"}
  263. }
  264. proc test_server_main {} {
  265. cleanup
  266. set tclsh [info nameofexecutable]
  267. # Open a listening socket, trying different ports in order to find a
  268. # non busy one.
  269. set clientport [find_available_port [expr {$::baseport - 32}] 32]
  270. if {!$::quiet} {
  271. puts "Starting test server at port $clientport"
  272. }
  273. socket -server accept_test_clients -myaddr 127.0.0.1 $clientport
  274. # Start the client instances
  275. set ::clients_pids {}
  276. if {$::external} {
  277. set p [exec $tclsh [info script] {*}$::argv \
  278. --client $clientport &]
  279. lappend ::clients_pids $p
  280. } else {
  281. set start_port $::baseport
  282. set port_count [expr {$::portcount / $::numclients}]
  283. for {set j 0} {$j < $::numclients} {incr j} {
  284. set p [exec $tclsh [info script] {*}$::argv \
  285. --client $clientport --baseport $start_port --portcount $port_count &]
  286. lappend ::clients_pids $p
  287. incr start_port $port_count
  288. }
  289. }
  290. # Setup global state for the test server
  291. set ::idle_clients {}
  292. set ::active_clients {}
  293. array set ::active_clients_task {}
  294. array set ::clients_start_time {}
  295. set ::clients_time_history {}
  296. set ::failed_tests {}
  297. # Enter the event loop to handle clients I/O
  298. after 100 test_server_cron
  299. vwait forever
  300. }
  301. # This function gets called 10 times per second.
  302. proc test_server_cron {} {
  303. set elapsed [expr {[clock seconds]-$::last_progress}]
  304. if {$elapsed > $::timeout} {
  305. set err "\[[colorstr red TIMEOUT]\]: clients state report follows."
  306. puts $err
  307. lappend ::failed_tests $err
  308. show_clients_state
  309. kill_clients
  310. force_kill_all_servers
  311. the_end
  312. }
  313. after 100 test_server_cron
  314. }
  315. proc accept_test_clients {fd addr port} {
  316. fconfigure $fd -encoding binary
  317. fileevent $fd readable [list read_from_test_client $fd]
  318. }
  319. # This is the readable handler of our test server. Clients send us messages
  320. # in the form of a status code such and additional data. Supported
  321. # status types are:
  322. #
  323. # ready: the client is ready to execute the command. Only sent at client
  324. # startup. The server will queue the client FD in the list of idle
  325. # clients.
  326. # testing: just used to signal that a given test started.
  327. # ok: a test was executed with success.
  328. # err: a test was executed with an error.
  329. # skip: a test was skipped by skipfile or individual test options.
  330. # ignore: a test was skipped by a group tag.
  331. # exception: there was a runtime exception while executing the test.
  332. # done: all the specified test file was processed, this test client is
  333. # ready to accept a new task.
  334. proc read_from_test_client fd {
  335. set bytes [gets $fd]
  336. set payload [read $fd $bytes]
  337. foreach {status data} $payload break
  338. set ::last_progress [clock seconds]
  339. if {$status eq {ready}} {
  340. if {!$::quiet} {
  341. puts "\[$status\]: $data"
  342. }
  343. signal_idle_client $fd
  344. } elseif {$status eq {done}} {
  345. set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
  346. set all_tests_count [llength $::all_tests]
  347. set running_tests_count [expr {[llength $::active_clients]-1}]
  348. set completed_tests_count [expr {$::next_test-$running_tests_count}]
  349. puts "\[$completed_tests_count/$all_tests_count [colorstr yellow $status]\]: $data ($elapsed seconds)"
  350. lappend ::clients_time_history $elapsed $data
  351. signal_idle_client $fd
  352. set ::active_clients_task($fd) "(DONE) $data"
  353. } elseif {$status eq {ok}} {
  354. if {!$::quiet} {
  355. puts "\[[colorstr green $status]\]: $data"
  356. }
  357. set ::active_clients_task($fd) "(OK) $data"
  358. } elseif {$status eq {skip}} {
  359. if {!$::quiet} {
  360. puts "\[[colorstr yellow $status]\]: $data"
  361. }
  362. } elseif {$status eq {ignore}} {
  363. if {!$::quiet} {
  364. puts "\[[colorstr cyan $status]\]: $data"
  365. }
  366. } elseif {$status eq {err}} {
  367. set err "\[[colorstr red $status]\]: $data"
  368. puts $err
  369. lappend ::failed_tests $err
  370. set ::active_clients_task($fd) "(ERR) $data"
  371. if {$::stop_on_failure} {
  372. puts -nonewline "(Test stopped, press enter to resume the tests)"
  373. flush stdout
  374. gets stdin
  375. }
  376. } elseif {$status eq {exception}} {
  377. puts "\[[colorstr red $status]\]: $data"
  378. kill_clients
  379. force_kill_all_servers
  380. exit 1
  381. } elseif {$status eq {testing}} {
  382. set ::active_clients_task($fd) "(IN PROGRESS) $data"
  383. } elseif {$status eq {server-spawning}} {
  384. set ::active_clients_task($fd) "(SPAWNING SERVER) $data"
  385. } elseif {$status eq {server-spawned}} {
  386. lappend ::active_servers $data
  387. set ::active_clients_task($fd) "(SPAWNED SERVER) pid:$data"
  388. } elseif {$status eq {server-killing}} {
  389. set ::active_clients_task($fd) "(KILLING SERVER) pid:$data"
  390. } elseif {$status eq {server-killed}} {
  391. set ::active_servers [lsearch -all -inline -not -exact $::active_servers $data]
  392. set ::active_clients_task($fd) "(KILLED SERVER) pid:$data"
  393. } elseif {$status eq {run_solo}} {
  394. lappend ::run_solo_tests $data
  395. } else {
  396. if {!$::quiet} {
  397. puts "\[$status\]: $data"
  398. }
  399. }
  400. }
  401. proc show_clients_state {} {
  402. # The following loop is only useful for debugging tests that may
  403. # enter an infinite loop.
  404. foreach x $::active_clients {
  405. if {[info exist ::active_clients_task($x)]} {
  406. puts "$x => $::active_clients_task($x)"
  407. } else {
  408. puts "$x => ???"
  409. }
  410. }
  411. }
  412. proc kill_clients {} {
  413. foreach p $::clients_pids {
  414. catch {exec kill $p}
  415. }
  416. }
  417. proc force_kill_all_servers {} {
  418. foreach p $::active_servers {
  419. puts "Killing still running Redis server $p"
  420. catch {exec kill -9 $p}
  421. }
  422. }
  423. proc lpop {listVar {count 1}} {
  424. upvar 1 $listVar l
  425. set ele [lindex $l 0]
  426. set l [lrange $l 1 end]
  427. set ele
  428. }
  429. proc lremove {listVar value} {
  430. upvar 1 $listVar var
  431. set idx [lsearch -exact $var $value]
  432. set var [lreplace $var $idx $idx]
  433. }
  434. # A new client is idle. Remove it from the list of active clients and
  435. # if there are still test units to run, launch them.
  436. proc signal_idle_client fd {
  437. # Remove this fd from the list of active clients.
  438. set ::active_clients \
  439. [lsearch -all -inline -not -exact $::active_clients $fd]
  440. # New unit to process?
  441. if {$::next_test != [llength $::all_tests]} {
  442. if {!$::quiet} {
  443. puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
  444. set ::active_clients_task($fd) "ASSIGNED: $fd ([lindex $::all_tests $::next_test])"
  445. }
  446. set ::clients_start_time($fd) [clock seconds]
  447. send_data_packet $fd run [lindex $::all_tests $::next_test]
  448. lappend ::active_clients $fd
  449. incr ::next_test
  450. if {$::loop && $::next_test == [llength $::all_tests]} {
  451. set ::next_test 0
  452. }
  453. } elseif {[llength $::run_solo_tests] != 0 && [llength $::active_clients] == 0} {
  454. if {!$::quiet} {
  455. puts [colorstr bold-white "Testing solo test"]
  456. set ::active_clients_task($fd) "ASSIGNED: $fd solo test"
  457. }
  458. set ::clients_start_time($fd) [clock seconds]
  459. send_data_packet $fd run_code [lpop ::run_solo_tests]
  460. lappend ::active_clients $fd
  461. } else {
  462. lappend ::idle_clients $fd
  463. set ::active_clients_task($fd) "SLEEPING, no more units to assign"
  464. if {[llength $::active_clients] == 0} {
  465. the_end
  466. }
  467. }
  468. }
  469. # The the_end function gets called when all the test units were already
  470. # executed, so the test finished.
  471. proc the_end {} {
  472. # TODO: print the status, exit with the right exit code.
  473. puts "\n The End\n"
  474. puts "Execution time of different units:"
  475. foreach {time name} $::clients_time_history {
  476. puts " $time seconds - $name"
  477. }
  478. if {[llength $::failed_tests]} {
  479. puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
  480. foreach failed $::failed_tests {
  481. puts "*** $failed"
  482. }
  483. if {!$::dont_clean} cleanup
  484. exit 1
  485. } else {
  486. puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
  487. if {!$::dont_clean} cleanup
  488. exit 0
  489. }
  490. }
  491. # The client is not even driven (the test server is instead) as we just need
  492. # to read the command, execute, reply... all this in a loop.
  493. proc test_client_main server_port {
  494. set ::test_server_fd [socket localhost $server_port]
  495. fconfigure $::test_server_fd -encoding binary
  496. send_data_packet $::test_server_fd ready [pid]
  497. while 1 {
  498. set bytes [gets $::test_server_fd]
  499. set payload [read $::test_server_fd $bytes]
  500. foreach {cmd data} $payload break
  501. if {$cmd eq {run}} {
  502. execute_test_file $data
  503. } elseif {$cmd eq {run_code}} {
  504. foreach {name filename code} $data break
  505. execute_test_code $name $filename $code
  506. } else {
  507. error "Unknown test client command: $cmd"
  508. }
  509. }
  510. }
  511. proc send_data_packet {fd status data} {
  512. set payload [list $status $data]
  513. puts $fd [string length $payload]
  514. puts -nonewline $fd $payload
  515. flush $fd
  516. }
  517. proc print_help_screen {} {
  518. puts [join {
  519. "--valgrind Run the test over valgrind."
  520. "--durable suppress test crashes and keep running"
  521. "--stack-logging Enable OSX leaks/malloc stack logging."
  522. "--accurate Run slow randomized tests for more iterations."
  523. "--quiet Don't show individual tests."
  524. "--single <unit> Just execute the specified unit (see next option). This option can be repeated."
  525. "--verbose Increases verbosity."
  526. "--list-tests List all the available test units."
  527. "--only <test> Just execute tests that match <test> regexp. This option can be repeated."
  528. "--skip-till <unit> Skip all units until (and including) the specified one."
  529. "--skipunit <unit> Skip one unit."
  530. "--clients <num> Number of test clients (default 16)."
  531. "--timeout <sec> Test timeout in seconds (default 10 min)."
  532. "--force-failure Force the execution of a test that always fails."
  533. "--config <k> <v> Extra config file argument."
  534. "--skipfile <file> Name of a file containing test names or regexp patterns that should be skipped (one per line)."
  535. "--skiptest <test> Test name or regexp pattern to skip. This option can be repeated."
  536. "--tags <tags> Run only tests having specified tags or not having '-' prefixed tags."
  537. "--dont-clean Don't delete redis log files after the run."
  538. "--no-latency Skip latency measurements and validation by some tests."
  539. "--stop Blocks once the first test fails."
  540. "--loop Execute the specified set of tests forever."
  541. "--wait-server Wait after server is started (so that you can attach a debugger)."
  542. "--dump-logs Dump server log on test failure."
  543. "--tls Run tests in TLS mode."
  544. "--host <addr> Run tests against an external host."
  545. "--port <port> TCP port to use against external host."
  546. "--baseport <port> Initial port number for spawned redis servers."
  547. "--portcount <num> Port range for spawned redis servers."
  548. "--singledb Use a single database, avoid SELECT."
  549. "--cluster-mode Run tests in cluster protocol compatible mode."
  550. "--ignore-encoding Don't validate object encoding."
  551. "--ignore-digest Don't use debug digest validations."
  552. "--help Print this help screen."
  553. } "\n"]
  554. }
  555. # parse arguments
  556. for {set j 0} {$j < [llength $argv]} {incr j} {
  557. set opt [lindex $argv $j]
  558. set arg [lindex $argv [expr $j+1]]
  559. if {$opt eq {--tags}} {
  560. foreach tag $arg {
  561. if {[string index $tag 0] eq "-"} {
  562. lappend ::denytags [string range $tag 1 end]
  563. } else {
  564. lappend ::allowtags $tag
  565. }
  566. }
  567. incr j
  568. } elseif {$opt eq {--config}} {
  569. set arg2 [lindex $argv [expr $j+2]]
  570. lappend ::global_overrides $arg
  571. lappend ::global_overrides $arg2
  572. incr j 2
  573. } elseif {$opt eq {--skipfile}} {
  574. incr j
  575. set fp [open $arg r]
  576. set file_data [read $fp]
  577. close $fp
  578. set ::skiptests [split $file_data "\n"]
  579. } elseif {$opt eq {--skiptest}} {
  580. lappend ::skiptests $arg
  581. incr j
  582. } elseif {$opt eq {--valgrind}} {
  583. set ::valgrind 1
  584. } elseif {$opt eq {--stack-logging}} {
  585. if {[string match {*Darwin*} [exec uname -a]]} {
  586. set ::stack_logging 1
  587. }
  588. } elseif {$opt eq {--quiet}} {
  589. set ::quiet 1
  590. } elseif {$opt eq {--tls}} {
  591. package require tls 1.6
  592. set ::tls 1
  593. ::tls::init \
  594. -cafile "$::tlsdir/ca.crt" \
  595. -certfile "$::tlsdir/client.crt" \
  596. -keyfile "$::tlsdir/client.key"
  597. } elseif {$opt eq {--host}} {
  598. set ::external 1
  599. set ::host $arg
  600. incr j
  601. } elseif {$opt eq {--port}} {
  602. set ::port $arg
  603. incr j
  604. } elseif {$opt eq {--baseport}} {
  605. set ::baseport $arg
  606. incr j
  607. } elseif {$opt eq {--portcount}} {
  608. set ::portcount $arg
  609. incr j
  610. } elseif {$opt eq {--accurate}} {
  611. set ::accurate 1
  612. } elseif {$opt eq {--force-failure}} {
  613. set ::force_failure 1
  614. } elseif {$opt eq {--single}} {
  615. lappend ::single_tests $arg
  616. incr j
  617. } elseif {$opt eq {--only}} {
  618. lappend ::only_tests $arg
  619. incr j
  620. } elseif {$opt eq {--skipunit}} {
  621. lappend ::skipunits $arg
  622. incr j
  623. } elseif {$opt eq {--skip-till}} {
  624. set ::skip_till $arg
  625. incr j
  626. } elseif {$opt eq {--list-tests}} {
  627. foreach t $::all_tests {
  628. puts $t
  629. }
  630. exit 0
  631. } elseif {$opt eq {--verbose}} {
  632. set ::verbose 1
  633. } elseif {$opt eq {--client}} {
  634. set ::client 1
  635. set ::test_server_port $arg
  636. incr j
  637. } elseif {$opt eq {--clients}} {
  638. set ::numclients $arg
  639. incr j
  640. } elseif {$opt eq {--durable}} {
  641. set ::durable 1
  642. } elseif {$opt eq {--dont-clean}} {
  643. set ::dont_clean 1
  644. } elseif {$opt eq {--no-latency}} {
  645. set ::no_latency 1
  646. } elseif {$opt eq {--wait-server}} {
  647. set ::wait_server 1
  648. } elseif {$opt eq {--dump-logs}} {
  649. set ::dump_logs 1
  650. } elseif {$opt eq {--stop}} {
  651. set ::stop_on_failure 1
  652. } elseif {$opt eq {--loop}} {
  653. set ::loop 1
  654. } elseif {$opt eq {--timeout}} {
  655. set ::timeout $arg
  656. incr j
  657. } elseif {$opt eq {--singledb}} {
  658. set ::singledb 1
  659. } elseif {$opt eq {--cluster-mode}} {
  660. set ::cluster_mode 1
  661. set ::singledb 1
  662. } elseif {$opt eq {--ignore-encoding}} {
  663. set ::ignoreencoding 1
  664. } elseif {$opt eq {--ignore-digest}} {
  665. set ::ignoredigest 1
  666. } elseif {$opt eq {--help}} {
  667. print_help_screen
  668. exit 0
  669. } else {
  670. puts "Wrong argument: $opt"
  671. exit 1
  672. }
  673. }
  674. set filtered_tests {}
  675. # Set the filtered tests to be the short list (single_tests) if exists.
  676. # Otherwise, we start filtering all_tests
  677. if {[llength $::single_tests] > 0} {
  678. set filtered_tests $::single_tests
  679. } else {
  680. set filtered_tests $::all_tests
  681. }
  682. # If --skip-till option was given, we populate the list of single tests
  683. # to run with everything *after* the specified unit.
  684. if {$::skip_till != ""} {
  685. set skipping 1
  686. foreach t $::all_tests {
  687. if {$skipping == 1} {
  688. lremove filtered_tests $t
  689. }
  690. if {$t == $::skip_till} {
  691. set skipping 0
  692. }
  693. }
  694. if {$skipping} {
  695. puts "test $::skip_till not found"
  696. exit 0
  697. }
  698. }
  699. # If --skipunits option was given, we populate the list of single tests
  700. # to run with everything *not* in the skipunits list.
  701. if {[llength $::skipunits] > 0} {
  702. foreach t $::all_tests {
  703. if {[lsearch $::skipunits $t] != -1} {
  704. lremove filtered_tests $t
  705. }
  706. }
  707. }
  708. # Override the list of tests with the specific tests we want to run
  709. # in case there was some filter, that is --single, -skipunit or --skip-till options.
  710. if {[llength $filtered_tests] < [llength $::all_tests]} {
  711. set ::all_tests $filtered_tests
  712. }
  713. proc attach_to_replication_stream {} {
  714. r config set repl-ping-replica-period 3600
  715. if {$::tls} {
  716. set s [::tls::socket [srv 0 "host"] [srv 0 "port"]]
  717. } else {
  718. set s [socket [srv 0 "host"] [srv 0 "port"]]
  719. }
  720. fconfigure $s -translation binary
  721. puts -nonewline $s "SYNC\r\n"
  722. flush $s
  723. # Get the count
  724. while 1 {
  725. set count [gets $s]
  726. set prefix [string range $count 0 0]
  727. if {$prefix ne {}} break; # Newlines are allowed as PINGs.
  728. }
  729. if {$prefix ne {$}} {
  730. error "attach_to_replication_stream error. Received '$count' as count."
  731. }
  732. set count [string range $count 1 end]
  733. # Consume the bulk payload
  734. while {$count} {
  735. set buf [read $s $count]
  736. set count [expr {$count-[string length $buf]}]
  737. }
  738. return $s
  739. }
  740. proc read_from_replication_stream {s} {
  741. fconfigure $s -blocking 0
  742. set attempt 0
  743. while {[gets $s count] == -1} {
  744. if {[incr attempt] == 10} return ""
  745. after 100
  746. }
  747. fconfigure $s -blocking 1
  748. set count [string range $count 1 end]
  749. # Return a list of arguments for the command.
  750. set res {}
  751. for {set j 0} {$j < $count} {incr j} {
  752. read $s 1
  753. set arg [::redis::redis_bulk_read $s]
  754. if {$j == 0} {set arg [string tolower $arg]}
  755. lappend res $arg
  756. }
  757. return $res
  758. }
  759. proc assert_replication_stream {s patterns} {
  760. for {set j 0} {$j < [llength $patterns]} {incr j} {
  761. assert_match [lindex $patterns $j] [read_from_replication_stream $s]
  762. }
  763. }
  764. proc close_replication_stream {s} {
  765. close $s
  766. r config set repl-ping-replica-period 10
  767. return
  768. }
  769. # With the parallel test running multiple Redis instances at the same time
  770. # we need a fast enough computer, otherwise a lot of tests may generate
  771. # false positives.
  772. # If the computer is too slow we revert the sequential test without any
  773. # parallelism, that is, clients == 1.
  774. proc is_a_slow_computer {} {
  775. set start [clock milliseconds]
  776. for {set j 0} {$j < 1000000} {incr j} {}
  777. set elapsed [expr [clock milliseconds]-$start]
  778. expr {$elapsed > 200}
  779. }
  780. if {$::client} {
  781. if {[catch { test_client_main $::test_server_port } err]} {
  782. set estr "Executing test client: $err.\n$::errorInfo"
  783. if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
  784. puts $estr
  785. }
  786. exit 1
  787. }
  788. } else {
  789. if {[is_a_slow_computer]} {
  790. puts "** SLOW COMPUTER ** Using a single client to avoid false positives."
  791. set ::numclients 1
  792. }
  793. if {[catch { test_server_main } err]} {
  794. if {[string length $err] > 0} {
  795. # only display error when not generated by the test suite
  796. if {$err ne "exception"} {
  797. puts $::errorInfo
  798. }
  799. exit 1
  800. }
  801. }
  802. }