2
0

server.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. set ::global_overrides {}
  2. set ::tags {}
  3. set ::valgrind_errors {}
  4. proc start_server_error {config_file error} {
  5. set err {}
  6. append err "Can't start the Redis server\n"
  7. append err "CONFIGURATION:"
  8. append err [exec cat $config_file]
  9. append err "\nERROR:"
  10. append err [string trim $error]
  11. send_data_packet $::test_server_fd err $err
  12. }
  13. proc check_valgrind_errors stderr {
  14. set fd [open $stderr]
  15. set buf [read $fd]
  16. close $fd
  17. if {[regexp -- { at 0x} $buf] ||
  18. (![regexp -- {definitely lost: 0 bytes} $buf] &&
  19. ![regexp -- {no leaks are possible} $buf])} {
  20. send_data_packet $::test_server_fd err "Valgrind error: $buf\n"
  21. }
  22. }
  23. proc kill_server config {
  24. # nothing to kill when running against external server
  25. if {$::external} return
  26. # nevermind if its already dead
  27. if {![is_alive $config]} { return }
  28. set pid [dict get $config pid]
  29. # check for leaks
  30. if {![dict exists $config "skipleaks"]} {
  31. catch {
  32. if {[string match {*Darwin*} [exec uname -a]]} {
  33. tags {"leaks"} {
  34. test "Check for memory leaks (pid $pid)" {
  35. set output {0 leaks}
  36. catch {exec leaks $pid} output
  37. if {[string match {*process does not exist*} $output] ||
  38. [string match {*cannot examine*} $output]} {
  39. # In a few tests we kill the server process.
  40. set output "0 leaks"
  41. }
  42. set output
  43. } {*0 leaks*}
  44. }
  45. }
  46. }
  47. }
  48. # kill server and wait for the process to be totally exited
  49. catch {exec kill $pid}
  50. if {$::valgrind} {
  51. set max_wait 60000
  52. } else {
  53. set max_wait 10000
  54. }
  55. while {[is_alive $config]} {
  56. incr wait 10
  57. if {$wait >= $max_wait} {
  58. puts "Forcing process $pid to exit..."
  59. catch {exec kill -KILL $pid}
  60. } elseif {$wait % 1000 == 0} {
  61. puts "Waiting for process $pid to exit..."
  62. }
  63. after 10
  64. }
  65. # Check valgrind errors if needed
  66. if {$::valgrind} {
  67. check_valgrind_errors [dict get $config stderr]
  68. }
  69. # Remove this pid from the set of active pids in the test server.
  70. send_data_packet $::test_server_fd server-killed $pid
  71. }
  72. proc is_alive config {
  73. set pid [dict get $config pid]
  74. if {[catch {exec ps -p $pid} err]} {
  75. return 0
  76. } else {
  77. return 1
  78. }
  79. }
  80. proc ping_server {host port} {
  81. set retval 0
  82. if {[catch {
  83. if {$::tls} {
  84. set fd [::tls::socket $host $port]
  85. } else {
  86. set fd [socket $host $port]
  87. }
  88. fconfigure $fd -translation binary
  89. puts $fd "PING\r\n"
  90. flush $fd
  91. set reply [gets $fd]
  92. if {[string range $reply 0 0] eq {+} ||
  93. [string range $reply 0 0] eq {-}} {
  94. set retval 1
  95. }
  96. close $fd
  97. } e]} {
  98. if {$::verbose} {
  99. puts -nonewline "."
  100. }
  101. } else {
  102. if {$::verbose} {
  103. puts -nonewline "ok"
  104. }
  105. }
  106. return $retval
  107. }
  108. # Return 1 if the server at the specified addr is reachable by PING, otherwise
  109. # returns 0. Performs a try every 50 milliseconds for the specified number
  110. # of retries.
  111. proc server_is_up {host port retrynum} {
  112. after 10 ;# Use a small delay to make likely a first-try success.
  113. set retval 0
  114. while {[incr retrynum -1]} {
  115. if {[catch {ping_server $host $port} ping]} {
  116. set ping 0
  117. }
  118. if {$ping} {return 1}
  119. after 50
  120. }
  121. return 0
  122. }
  123. # doesn't really belong here, but highly coupled to code in start_server
  124. proc tags {tags code} {
  125. set ::tags [concat $::tags $tags]
  126. uplevel 1 $code
  127. set ::tags [lrange $::tags 0 end-[llength $tags]]
  128. }
  129. proc start_server {options {code undefined}} {
  130. # If we are running against an external server, we just push the
  131. # host/port pair in the stack the first time
  132. if {$::external} {
  133. if {[llength $::servers] == 0} {
  134. set srv {}
  135. dict set srv "host" $::host
  136. dict set srv "port" $::port
  137. set client [redis $::host $::port 0 $::tls]
  138. dict set srv "client" $client
  139. $client select 9
  140. # append the server to the stack
  141. lappend ::servers $srv
  142. }
  143. uplevel 1 $code
  144. return
  145. }
  146. # setup defaults
  147. set baseconfig "default.conf"
  148. set overrides {}
  149. set tags {}
  150. # parse options
  151. foreach {option value} $options {
  152. switch $option {
  153. "config" {
  154. set baseconfig $value }
  155. "overrides" {
  156. set overrides $value }
  157. "tags" {
  158. set tags $value
  159. set ::tags [concat $::tags $value] }
  160. default {
  161. error "Unknown option $option" }
  162. }
  163. }
  164. set data [split [exec cat "tests/assets/$baseconfig"] "\n"]
  165. set config {}
  166. if {$::tls} {
  167. dict set config "tls-cert-file" [format "%s/tests/tls/redis.crt" [pwd]]
  168. dict set config "tls-key-file" [format "%s/tests/tls/redis.key" [pwd]]
  169. dict set config "tls-dh-params-file" [format "%s/tests/tls/redis.dh" [pwd]]
  170. dict set config "tls-ca-cert-file" [format "%s/tests/tls/ca.crt" [pwd]]
  171. dict set config "loglevel" "debug"
  172. }
  173. foreach line $data {
  174. if {[string length $line] > 0 && [string index $line 0] ne "#"} {
  175. set elements [split $line " "]
  176. set directive [lrange $elements 0 0]
  177. set arguments [lrange $elements 1 end]
  178. dict set config $directive $arguments
  179. }
  180. }
  181. # use a different directory every time a server is started
  182. dict set config dir [tmpdir server]
  183. # start every server on a different port
  184. set ::port [find_available_port [expr {$::port+1}]]
  185. if {$::tls} {
  186. dict set config "port" 0
  187. dict set config "tls-port" $::port
  188. dict set config "tls-cluster" "yes"
  189. dict set config "tls-replication" "yes"
  190. } else {
  191. dict set config port $::port
  192. }
  193. set unixsocket [file normalize [format "%s/%s" [dict get $config "dir"] "socket"]]
  194. dict set config "unixsocket" $unixsocket
  195. # apply overrides from global space and arguments
  196. foreach {directive arguments} [concat $::global_overrides $overrides] {
  197. dict set config $directive $arguments
  198. }
  199. # write new configuration to temporary file
  200. set config_file [tmpfile redis.conf]
  201. set fp [open $config_file w+]
  202. foreach directive [dict keys $config] {
  203. puts -nonewline $fp "$directive "
  204. puts $fp [dict get $config $directive]
  205. }
  206. close $fp
  207. set stdout [format "%s/%s" [dict get $config "dir"] "stdout"]
  208. set stderr [format "%s/%s" [dict get $config "dir"] "stderr"]
  209. if {$::valgrind} {
  210. set pid [exec valgrind --track-origins=yes --suppressions=src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full src/redis-server $config_file > $stdout 2> $stderr &]
  211. } elseif ($::stack_logging) {
  212. set pid [exec /usr/bin/env MallocStackLogging=1 MallocLogFile=/tmp/malloc_log.txt src/redis-server $config_file > $stdout 2> $stderr &]
  213. } else {
  214. set pid [exec src/redis-server $config_file > $stdout 2> $stderr &]
  215. }
  216. # Tell the test server about this new instance.
  217. send_data_packet $::test_server_fd server-spawned $pid
  218. # check that the server actually started
  219. # ugly but tries to be as fast as possible...
  220. if {$::valgrind} {set retrynum 1000} else {set retrynum 100}
  221. if {$::verbose} {
  222. puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
  223. }
  224. if {$code ne "undefined"} {
  225. set serverisup [server_is_up $::host $::port $retrynum]
  226. } else {
  227. set serverisup 1
  228. }
  229. if {$::verbose} {
  230. puts ""
  231. }
  232. if {!$serverisup} {
  233. set err {}
  234. append err [exec cat $stdout] "\n" [exec cat $stderr]
  235. start_server_error $config_file $err
  236. return
  237. }
  238. # Wait for actual startup
  239. while {![info exists _pid]} {
  240. regexp {PID:\s(\d+)} [exec cat $stdout] _ _pid
  241. after 100
  242. }
  243. # setup properties to be able to initialize a client object
  244. set port_param [expr $::tls ? {"tls-port"} : {"port"}]
  245. set host $::host
  246. set port $::port
  247. if {[dict exists $config bind]} { set host [dict get $config bind] }
  248. if {[dict exists $config $port_param]} { set port [dict get $config $port_param] }
  249. # setup config dict
  250. dict set srv "config_file" $config_file
  251. dict set srv "config" $config
  252. dict set srv "pid" $pid
  253. dict set srv "host" $host
  254. dict set srv "port" $port
  255. dict set srv "stdout" $stdout
  256. dict set srv "stderr" $stderr
  257. dict set srv "unixsocket" $unixsocket
  258. # if a block of code is supplied, we wait for the server to become
  259. # available, create a client object and kill the server afterwards
  260. if {$code ne "undefined"} {
  261. set line [exec head -n1 $stdout]
  262. if {[string match {*already in use*} $line]} {
  263. error_and_quit $config_file $line
  264. }
  265. if {$::wait_server} {
  266. set msg "server started PID: [dict get $srv "pid"]. press any key to continue..."
  267. puts $msg
  268. read stdin 1
  269. }
  270. while 1 {
  271. # check that the server actually started and is ready for connections
  272. if {[exec grep -i "Ready to accept" | wc -l < $stdout] > 0} {
  273. break
  274. }
  275. after 10
  276. }
  277. # append the server to the stack
  278. lappend ::servers $srv
  279. # connect client (after server dict is put on the stack)
  280. reconnect
  281. # execute provided block
  282. set num_tests $::num_tests
  283. if {[catch { uplevel 1 $code } error]} {
  284. set backtrace $::errorInfo
  285. # Kill the server without checking for leaks
  286. dict set srv "skipleaks" 1
  287. kill_server $srv
  288. # Print warnings from log
  289. puts [format "\nLogged warnings (pid %d):" [dict get $srv "pid"]]
  290. set warnings [warnings_from_file [dict get $srv "stdout"]]
  291. if {[string length $warnings] > 0} {
  292. puts "$warnings"
  293. } else {
  294. puts "(none)"
  295. }
  296. puts ""
  297. error $error $backtrace
  298. }
  299. # Don't do the leak check when no tests were run
  300. if {$num_tests == $::num_tests} {
  301. dict set srv "skipleaks" 1
  302. }
  303. # pop the server object
  304. set ::servers [lrange $::servers 0 end-1]
  305. set ::tags [lrange $::tags 0 end-[llength $tags]]
  306. kill_server $srv
  307. } else {
  308. set ::tags [lrange $::tags 0 end-[llength $tags]]
  309. set _ $srv
  310. }
  311. }