2
0

instances.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. # Multi-instance test framework.
  2. # This is used in order to test Sentinel and Redis Cluster, and provides
  3. # basic capabilities for spawning and handling N parallel Redis / Sentinel
  4. # instances.
  5. #
  6. # Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com
  7. # This software is released under the BSD License. See the COPYING file for
  8. # more information.
  9. package require Tcl 8.5
  10. set tcl_precision 17
  11. source ../support/redis.tcl
  12. source ../support/util.tcl
  13. source ../support/server.tcl
  14. source ../support/test.tcl
  15. set ::verbose 0
  16. set ::valgrind 0
  17. set ::tls 0
  18. set ::pause_on_error 0
  19. set ::simulate_error 0
  20. set ::failed 0
  21. set ::sentinel_instances {}
  22. set ::redis_instances {}
  23. set ::sentinel_base_port 20000
  24. set ::redis_base_port 30000
  25. set ::pids {} ; # We kill everything at exit
  26. set ::dirs {} ; # We remove all the temp dirs at exit
  27. set ::run_matching {} ; # If non empty, only tests matching pattern are run.
  28. if {[catch {cd tmp}]} {
  29. puts "tmp directory not found."
  30. puts "Please run this test from the Redis source root."
  31. exit 1
  32. }
  33. # Execute the specified instance of the server specified by 'type', using
  34. # the provided configuration file. Returns the PID of the process.
  35. proc exec_instance {type cfgfile} {
  36. if {$type eq "redis"} {
  37. set prgname redis-server
  38. } elseif {$type eq "sentinel"} {
  39. set prgname redis-sentinel
  40. } else {
  41. error "Unknown instance type."
  42. }
  43. if {$::valgrind} {
  44. set pid [exec valgrind --track-origins=yes --suppressions=../../../src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full ../../../src/${prgname} $cfgfile &]
  45. } else {
  46. set pid [exec ../../../src/${prgname} $cfgfile &]
  47. }
  48. return $pid
  49. }
  50. # Spawn a redis or sentinel instance, depending on 'type'.
  51. proc spawn_instance {type base_port count {conf {}}} {
  52. for {set j 0} {$j < $count} {incr j} {
  53. set port [find_available_port $base_port]
  54. incr base_port
  55. puts "Starting $type #$j at port $port"
  56. # Create a directory for this instance.
  57. set dirname "${type}_${j}"
  58. lappend ::dirs $dirname
  59. catch {exec rm -rf $dirname}
  60. file mkdir $dirname
  61. # Write the instance config file.
  62. set cfgfile [file join $dirname $type.conf]
  63. set cfg [open $cfgfile w]
  64. if {$::tls} {
  65. puts $cfg "tls-port $port"
  66. puts $cfg "tls-replication yes"
  67. puts $cfg "tls-cluster yes"
  68. puts $cfg "port 0"
  69. puts $cfg [format "tls-cert-file %s/../../tls/redis.crt" [pwd]]
  70. puts $cfg [format "tls-key-file %s/../../tls/redis.key" [pwd]]
  71. puts $cfg [format "tls-dh-params-file %s/../../tls/redis.dh" [pwd]]
  72. puts $cfg [format "tls-ca-cert-file %s/../../tls/ca.crt" [pwd]]
  73. puts $cfg "loglevel debug"
  74. } else {
  75. puts $cfg "port $port"
  76. }
  77. puts $cfg "dir ./$dirname"
  78. puts $cfg "logfile log.txt"
  79. # Add additional config files
  80. foreach directive $conf {
  81. puts $cfg $directive
  82. }
  83. close $cfg
  84. # Finally exec it and remember the pid for later cleanup.
  85. set pid [exec_instance $type $cfgfile]
  86. lappend ::pids $pid
  87. # Check availability
  88. if {[server_is_up 127.0.0.1 $port 100] == 0} {
  89. abort_sentinel_test "Problems starting $type #$j: ping timeout"
  90. }
  91. # Push the instance into the right list
  92. set link [redis 127.0.0.1 $port 0 $::tls]
  93. $link reconnect 1
  94. lappend ::${type}_instances [list \
  95. pid $pid \
  96. host 127.0.0.1 \
  97. port $port \
  98. link $link \
  99. ]
  100. }
  101. }
  102. proc log_crashes {} {
  103. set start_pattern {*REDIS BUG REPORT START*}
  104. set logs [glob */log.txt]
  105. foreach log $logs {
  106. set fd [open $log]
  107. set found 0
  108. while {[gets $fd line] >= 0} {
  109. if {[string match $start_pattern $line]} {
  110. puts "\n*** Crash report found in $log ***"
  111. set found 1
  112. }
  113. if {$found} {puts $line}
  114. }
  115. }
  116. }
  117. proc cleanup {} {
  118. puts "Cleaning up..."
  119. log_crashes
  120. foreach pid $::pids {
  121. catch {exec kill -9 $pid}
  122. }
  123. foreach dir $::dirs {
  124. catch {exec rm -rf $dir}
  125. }
  126. }
  127. proc abort_sentinel_test msg {
  128. incr ::failed
  129. puts "WARNING: Aborting the test."
  130. puts ">>>>>>>> $msg"
  131. if {$::pause_on_error} pause_on_error
  132. cleanup
  133. exit 1
  134. }
  135. proc parse_options {} {
  136. for {set j 0} {$j < [llength $::argv]} {incr j} {
  137. set opt [lindex $::argv $j]
  138. set val [lindex $::argv [expr $j+1]]
  139. if {$opt eq "--single"} {
  140. incr j
  141. set ::run_matching "*${val}*"
  142. } elseif {$opt eq "--pause-on-error"} {
  143. set ::pause_on_error 1
  144. } elseif {$opt eq "--fail"} {
  145. set ::simulate_error 1
  146. } elseif {$opt eq {--valgrind}} {
  147. set ::valgrind 1
  148. } elseif {$opt eq {--tls}} {
  149. package require tls 1.6
  150. ::tls::init \
  151. -cafile "$::tlsdir/ca.crt" \
  152. -certfile "$::tlsdir/redis.crt" \
  153. -keyfile "$::tlsdir/redis.key"
  154. set ::tls 1
  155. } elseif {$opt eq "--help"} {
  156. puts "Hello, I'm sentinel.tcl and I run Sentinel unit tests."
  157. puts "\nOptions:"
  158. puts "--single <pattern> Only runs tests specified by pattern."
  159. puts "--pause-on-error Pause for manual inspection on error."
  160. puts "--fail Simulate a test failure."
  161. puts "--valgrind Run with valgrind."
  162. puts "--help Shows this help."
  163. exit 0
  164. } else {
  165. puts "Unknown option $opt"
  166. exit 1
  167. }
  168. }
  169. }
  170. # If --pause-on-error option was passed at startup this function is called
  171. # on error in order to give the developer a chance to understand more about
  172. # the error condition while the instances are still running.
  173. proc pause_on_error {} {
  174. puts ""
  175. puts [colorstr yellow "*** Please inspect the error now ***"]
  176. puts "\nType \"continue\" to resume the test, \"help\" for help screen.\n"
  177. while 1 {
  178. puts -nonewline "> "
  179. flush stdout
  180. set line [gets stdin]
  181. set argv [split $line " "]
  182. set cmd [lindex $argv 0]
  183. if {$cmd eq {continue}} {
  184. break
  185. } elseif {$cmd eq {show-redis-logs}} {
  186. set count 10
  187. if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
  188. foreach_redis_id id {
  189. puts "=== REDIS $id ===="
  190. puts [exec tail -$count redis_$id/log.txt]
  191. puts "---------------------\n"
  192. }
  193. } elseif {$cmd eq {show-sentinel-logs}} {
  194. set count 10
  195. if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
  196. foreach_sentinel_id id {
  197. puts "=== SENTINEL $id ===="
  198. puts [exec tail -$count sentinel_$id/log.txt]
  199. puts "---------------------\n"
  200. }
  201. } elseif {$cmd eq {ls}} {
  202. foreach_redis_id id {
  203. puts -nonewline "Redis $id"
  204. set errcode [catch {
  205. set str {}
  206. append str "@[RI $id tcp_port]: "
  207. append str "[RI $id role] "
  208. if {[RI $id role] eq {slave}} {
  209. append str "[RI $id master_host]:[RI $id master_port]"
  210. }
  211. set str
  212. } retval]
  213. if {$errcode} {
  214. puts " -- $retval"
  215. } else {
  216. puts $retval
  217. }
  218. }
  219. foreach_sentinel_id id {
  220. puts -nonewline "Sentinel $id"
  221. set errcode [catch {
  222. set str {}
  223. append str "@[SI $id tcp_port]: "
  224. append str "[join [S $id sentinel get-master-addr-by-name mymaster]]"
  225. set str
  226. } retval]
  227. if {$errcode} {
  228. puts " -- $retval"
  229. } else {
  230. puts $retval
  231. }
  232. }
  233. } elseif {$cmd eq {help}} {
  234. puts "ls List Sentinel and Redis instances."
  235. puts "show-sentinel-logs \[N\] Show latest N lines of logs."
  236. puts "show-redis-logs \[N\] Show latest N lines of logs."
  237. puts "S <id> cmd ... arg Call command in Sentinel <id>."
  238. puts "R <id> cmd ... arg Call command in Redis <id>."
  239. puts "SI <id> <field> Show Sentinel <id> INFO <field>."
  240. puts "RI <id> <field> Show Sentinel <id> INFO <field>."
  241. puts "continue Resume test."
  242. } else {
  243. set errcode [catch {eval $line} retval]
  244. if {$retval ne {}} {puts "$retval"}
  245. }
  246. }
  247. }
  248. # We redefine 'test' as for Sentinel we don't use the server-client
  249. # architecture for the test, everything is sequential.
  250. proc test {descr code} {
  251. set ts [clock format [clock seconds] -format %H:%M:%S]
  252. puts -nonewline "$ts> $descr: "
  253. flush stdout
  254. if {[catch {set retval [uplevel 1 $code]} error]} {
  255. incr ::failed
  256. if {[string match "assertion:*" $error]} {
  257. set msg [string range $error 10 end]
  258. puts [colorstr red $msg]
  259. if {$::pause_on_error} pause_on_error
  260. puts "(Jumping to next unit after error)"
  261. return -code continue
  262. } else {
  263. # Re-raise, let handler up the stack take care of this.
  264. error $error $::errorInfo
  265. }
  266. } else {
  267. puts [colorstr green OK]
  268. }
  269. }
  270. # Check memory leaks when running on OSX using the "leaks" utility.
  271. proc check_leaks instance_types {
  272. if {[string match {*Darwin*} [exec uname -a]]} {
  273. puts -nonewline "Testing for memory leaks..."; flush stdout
  274. foreach type $instance_types {
  275. foreach_instance_id [set ::${type}_instances] id {
  276. if {[instance_is_killed $type $id]} continue
  277. set pid [get_instance_attrib $type $id pid]
  278. set output {0 leaks}
  279. catch {exec leaks $pid} output
  280. if {[string match {*process does not exist*} $output] ||
  281. [string match {*cannot examine*} $output]} {
  282. # In a few tests we kill the server process.
  283. set output "0 leaks"
  284. } else {
  285. puts -nonewline "$type/$pid "
  286. flush stdout
  287. }
  288. if {![string match {*0 leaks*} $output]} {
  289. puts [colorstr red "=== MEMORY LEAK DETECTED ==="]
  290. puts "Instance type $type, ID $id:"
  291. puts $output
  292. puts "==="
  293. incr ::failed
  294. }
  295. }
  296. }
  297. puts ""
  298. }
  299. }
  300. # Execute all the units inside the 'tests' directory.
  301. proc run_tests {} {
  302. set tests [lsort [glob ../tests/*]]
  303. foreach test $tests {
  304. if {$::run_matching ne {} && [string match $::run_matching $test] == 0} {
  305. continue
  306. }
  307. if {[file isdirectory $test]} continue
  308. puts [colorstr yellow "Testing unit: [lindex [file split $test] end]"]
  309. source $test
  310. check_leaks {redis sentinel}
  311. }
  312. }
  313. # Print a message and exists with 0 / 1 according to zero or more failures.
  314. proc end_tests {} {
  315. if {$::failed == 0} {
  316. puts "GOOD! No errors."
  317. exit 0
  318. } else {
  319. puts "WARNING $::failed test(s) failed."
  320. exit 1
  321. }
  322. }
  323. # The "S" command is used to interact with the N-th Sentinel.
  324. # The general form is:
  325. #
  326. # S <sentinel-id> command arg arg arg ...
  327. #
  328. # Example to ping the Sentinel 0 (first instance): S 0 PING
  329. proc S {n args} {
  330. set s [lindex $::sentinel_instances $n]
  331. [dict get $s link] {*}$args
  332. }
  333. # Like R but to chat with Redis instances.
  334. proc R {n args} {
  335. set r [lindex $::redis_instances $n]
  336. [dict get $r link] {*}$args
  337. }
  338. proc get_info_field {info field} {
  339. set fl [string length $field]
  340. append field :
  341. foreach line [split $info "\n"] {
  342. set line [string trim $line "\r\n "]
  343. if {[string range $line 0 $fl] eq $field} {
  344. return [string range $line [expr {$fl+1}] end]
  345. }
  346. }
  347. return {}
  348. }
  349. proc SI {n field} {
  350. get_info_field [S $n info] $field
  351. }
  352. proc RI {n field} {
  353. get_info_field [R $n info] $field
  354. }
  355. # Iterate over IDs of sentinel or redis instances.
  356. proc foreach_instance_id {instances idvar code} {
  357. upvar 1 $idvar id
  358. for {set id 0} {$id < [llength $instances]} {incr id} {
  359. set errcode [catch {uplevel 1 $code} result]
  360. if {$errcode == 1} {
  361. error $result $::errorInfo $::errorCode
  362. } elseif {$errcode == 4} {
  363. continue
  364. } elseif {$errcode == 3} {
  365. break
  366. } elseif {$errcode != 0} {
  367. return -code $errcode $result
  368. }
  369. }
  370. }
  371. proc foreach_sentinel_id {idvar code} {
  372. set errcode [catch {uplevel 1 [list foreach_instance_id $::sentinel_instances $idvar $code]} result]
  373. return -code $errcode $result
  374. }
  375. proc foreach_redis_id {idvar code} {
  376. set errcode [catch {uplevel 1 [list foreach_instance_id $::redis_instances $idvar $code]} result]
  377. return -code $errcode $result
  378. }
  379. # Get the specific attribute of the specified instance type, id.
  380. proc get_instance_attrib {type id attrib} {
  381. dict get [lindex [set ::${type}_instances] $id] $attrib
  382. }
  383. # Set the specific attribute of the specified instance type, id.
  384. proc set_instance_attrib {type id attrib newval} {
  385. set d [lindex [set ::${type}_instances] $id]
  386. dict set d $attrib $newval
  387. lset ::${type}_instances $id $d
  388. }
  389. # Create a master-slave cluster of the given number of total instances.
  390. # The first instance "0" is the master, all others are configured as
  391. # slaves.
  392. proc create_redis_master_slave_cluster n {
  393. foreach_redis_id id {
  394. if {$id == 0} {
  395. # Our master.
  396. R $id slaveof no one
  397. R $id flushall
  398. } elseif {$id < $n} {
  399. R $id slaveof [get_instance_attrib redis 0 host] \
  400. [get_instance_attrib redis 0 port]
  401. } else {
  402. # Instances not part of the cluster.
  403. R $id slaveof no one
  404. }
  405. }
  406. # Wait for all the slaves to sync.
  407. wait_for_condition 1000 50 {
  408. [RI 0 connected_slaves] == ($n-1)
  409. } else {
  410. fail "Unable to create a master-slaves cluster."
  411. }
  412. }
  413. proc get_instance_id_by_port {type port} {
  414. foreach_${type}_id id {
  415. if {[get_instance_attrib $type $id port] == $port} {
  416. return $id
  417. }
  418. }
  419. fail "Instance $type port $port not found."
  420. }
  421. # Kill an instance of the specified type/id with SIGKILL.
  422. # This function will mark the instance PID as -1 to remember that this instance
  423. # is no longer running and will remove its PID from the list of pids that
  424. # we kill at cleanup.
  425. #
  426. # The instance can be restarted with restart-instance.
  427. proc kill_instance {type id} {
  428. set pid [get_instance_attrib $type $id pid]
  429. set port [get_instance_attrib $type $id port]
  430. if {$pid == -1} {
  431. error "You tried to kill $type $id twice."
  432. }
  433. exec kill -9 $pid
  434. set_instance_attrib $type $id pid -1
  435. set_instance_attrib $type $id link you_tried_to_talk_with_killed_instance
  436. # Remove the PID from the list of pids to kill at exit.
  437. set ::pids [lsearch -all -inline -not -exact $::pids $pid]
  438. # Wait for the port it was using to be available again, so that's not
  439. # an issue to start a new server ASAP with the same port.
  440. set retry 10
  441. while {[incr retry -1]} {
  442. set port_is_free [catch {set s [socket 127.0.01 $port]}]
  443. if {$port_is_free} break
  444. catch {close $s}
  445. after 1000
  446. }
  447. if {$retry == 0} {
  448. error "Port $port does not return available after killing instance."
  449. }
  450. }
  451. # Return true of the instance of the specified type/id is killed.
  452. proc instance_is_killed {type id} {
  453. set pid [get_instance_attrib $type $id pid]
  454. expr {$pid == -1}
  455. }
  456. # Restart an instance previously killed by kill_instance
  457. proc restart_instance {type id} {
  458. set dirname "${type}_${id}"
  459. set cfgfile [file join $dirname $type.conf]
  460. set port [get_instance_attrib $type $id port]
  461. # Execute the instance with its old setup and append the new pid
  462. # file for cleanup.
  463. set pid [exec_instance $type $cfgfile]
  464. set_instance_attrib $type $id pid $pid
  465. lappend ::pids $pid
  466. # Check that the instance is running
  467. if {[server_is_up 127.0.0.1 $port 100] == 0} {
  468. abort_sentinel_test "Problems starting $type #$id: ping timeout"
  469. }
  470. # Connect with it with a fresh link
  471. set link [redis 127.0.0.1 $port 0 $::tls]
  472. $link reconnect 1
  473. set_instance_attrib $type $id link $link
  474. # Make sure the instance is not loading the dataset when this
  475. # function returns.
  476. while 1 {
  477. catch {[$link ping]} retval
  478. if {[string match {*LOADING*} $retval]} {
  479. after 100
  480. continue
  481. } else {
  482. break
  483. }
  484. }
  485. }