server.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. send_data_packet $::test_server_fd server-killing $pid
  50. catch {exec kill $pid}
  51. if {$::valgrind} {
  52. set max_wait 60000
  53. } else {
  54. set max_wait 10000
  55. }
  56. while {[is_alive $config]} {
  57. incr wait 10
  58. if {$wait >= $max_wait} {
  59. puts "Forcing process $pid to exit..."
  60. catch {exec kill -KILL $pid}
  61. } elseif {$wait % 1000 == 0} {
  62. puts "Waiting for process $pid to exit..."
  63. }
  64. after 10
  65. }
  66. # Check valgrind errors if needed
  67. if {$::valgrind} {
  68. check_valgrind_errors [dict get $config stderr]
  69. }
  70. # Remove this pid from the set of active pids in the test server.
  71. send_data_packet $::test_server_fd server-killed $pid
  72. }
  73. proc is_alive config {
  74. set pid [dict get $config pid]
  75. if {[catch {exec ps -p $pid} err]} {
  76. return 0
  77. } else {
  78. return 1
  79. }
  80. }
  81. proc ping_server {host port} {
  82. set retval 0
  83. if {[catch {
  84. if {$::tls} {
  85. set fd [::tls::socket $host $port]
  86. } else {
  87. set fd [socket $host $port]
  88. }
  89. fconfigure $fd -translation binary
  90. puts $fd "PING\r\n"
  91. flush $fd
  92. set reply [gets $fd]
  93. if {[string range $reply 0 0] eq {+} ||
  94. [string range $reply 0 0] eq {-}} {
  95. set retval 1
  96. }
  97. close $fd
  98. } e]} {
  99. if {$::verbose} {
  100. puts -nonewline "."
  101. }
  102. } else {
  103. if {$::verbose} {
  104. puts -nonewline "ok"
  105. }
  106. }
  107. return $retval
  108. }
  109. # Return 1 if the server at the specified addr is reachable by PING, otherwise
  110. # returns 0. Performs a try every 50 milliseconds for the specified number
  111. # of retries.
  112. proc server_is_up {host port retrynum} {
  113. after 10 ;# Use a small delay to make likely a first-try success.
  114. set retval 0
  115. while {[incr retrynum -1]} {
  116. if {[catch {ping_server $host $port} ping]} {
  117. set ping 0
  118. }
  119. if {$ping} {return 1}
  120. after 50
  121. }
  122. return 0
  123. }
  124. # doesn't really belong here, but highly coupled to code in start_server
  125. proc tags {tags code} {
  126. set ::tags [concat $::tags $tags]
  127. uplevel 1 $code
  128. set ::tags [lrange $::tags 0 end-[llength $tags]]
  129. }
  130. # Write the configuration in the dictionary 'config' in the specified
  131. # file name.
  132. proc create_server_config_file {filename config} {
  133. set fp [open $filename w+]
  134. foreach directive [dict keys $config] {
  135. puts -nonewline $fp "$directive "
  136. puts $fp [dict get $config $directive]
  137. }
  138. close $fp
  139. }
  140. proc start_server {options {code undefined}} {
  141. # If we are running against an external server, we just push the
  142. # host/port pair in the stack the first time
  143. if {$::external} {
  144. if {[llength $::servers] == 0} {
  145. set srv {}
  146. dict set srv "host" $::host
  147. dict set srv "port" $::port
  148. set client [redis $::host $::port 0 $::tls]
  149. dict set srv "client" $client
  150. $client select 9
  151. # append the server to the stack
  152. lappend ::servers $srv
  153. }
  154. uplevel 1 $code
  155. return
  156. }
  157. # setup defaults
  158. set baseconfig "default.conf"
  159. set overrides {}
  160. set tags {}
  161. # parse options
  162. foreach {option value} $options {
  163. switch $option {
  164. "config" {
  165. set baseconfig $value }
  166. "overrides" {
  167. set overrides $value }
  168. "tags" {
  169. set tags $value
  170. set ::tags [concat $::tags $value] }
  171. default {
  172. error "Unknown option $option" }
  173. }
  174. }
  175. set data [split [exec cat "tests/assets/$baseconfig"] "\n"]
  176. set config {}
  177. if {$::tls} {
  178. dict set config "tls-cert-file" [format "%s/tests/tls/redis.crt" [pwd]]
  179. dict set config "tls-key-file" [format "%s/tests/tls/redis.key" [pwd]]
  180. dict set config "tls-dh-params-file" [format "%s/tests/tls/redis.dh" [pwd]]
  181. dict set config "tls-ca-cert-file" [format "%s/tests/tls/ca.crt" [pwd]]
  182. dict set config "loglevel" "debug"
  183. }
  184. foreach line $data {
  185. if {[string length $line] > 0 && [string index $line 0] ne "#"} {
  186. set elements [split $line " "]
  187. set directive [lrange $elements 0 0]
  188. set arguments [lrange $elements 1 end]
  189. dict set config $directive $arguments
  190. }
  191. }
  192. # use a different directory every time a server is started
  193. dict set config dir [tmpdir server]
  194. # start every server on a different port
  195. set port [find_available_port $::baseport $::portcount]
  196. if {$::tls} {
  197. dict set config "port" 0
  198. dict set config "tls-port" $port
  199. dict set config "tls-cluster" "yes"
  200. dict set config "tls-replication" "yes"
  201. } else {
  202. dict set config port $port
  203. }
  204. set unixsocket [file normalize [format "%s/%s" [dict get $config "dir"] "socket"]]
  205. dict set config "unixsocket" $unixsocket
  206. # apply overrides from global space and arguments
  207. foreach {directive arguments} [concat $::global_overrides $overrides] {
  208. dict set config $directive $arguments
  209. }
  210. # write new configuration to temporary file
  211. set config_file [tmpfile redis.conf]
  212. create_server_config_file $config_file $config
  213. set stdout [format "%s/%s" [dict get $config "dir"] "stdout"]
  214. set stderr [format "%s/%s" [dict get $config "dir"] "stderr"]
  215. # We need a loop here to retry with different ports.
  216. set server_started 0
  217. while {$server_started == 0} {
  218. if {$::verbose} {
  219. puts -nonewline "=== ($tags) Starting server ${::host}:${port} "
  220. }
  221. send_data_packet $::test_server_fd "server-spawning" "port $port"
  222. if {$::valgrind} {
  223. 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 &]
  224. } elseif ($::stack_logging) {
  225. set pid [exec /usr/bin/env MallocStackLogging=1 MallocLogFile=/tmp/malloc_log.txt src/redis-server $config_file > $stdout 2> $stderr &]
  226. } else {
  227. set pid [exec src/redis-server $config_file > $stdout 2> $stderr &]
  228. }
  229. # Tell the test server about this new instance.
  230. send_data_packet $::test_server_fd server-spawned $pid
  231. # check that the server actually started
  232. # ugly but tries to be as fast as possible...
  233. if {$::valgrind} {set retrynum 1000} else {set retrynum 100}
  234. # Wait for actual startup
  235. set checkperiod 100; # Milliseconds
  236. set maxiter [expr {120*1000/100}] ; # Wait up to 2 minutes.
  237. set port_busy 0
  238. while {![info exists _pid]} {
  239. regexp {PID:\s(\d+)} [exec cat $stdout] _ _pid
  240. after $checkperiod
  241. incr maxiter -1
  242. if {$maxiter == 0} {
  243. start_server_error $config_file "No PID detected in log $stdout"
  244. puts "--- LOG CONTENT ---"
  245. puts [exec cat $stdout]
  246. puts "-------------------"
  247. break
  248. }
  249. # Check if the port is actually busy and the server failed
  250. # for this reason.
  251. if {[regexp {Could not create server TCP} [exec cat $stdout]]} {
  252. set port_busy 1
  253. break
  254. }
  255. }
  256. # Sometimes we have to try a different port, even if we checked
  257. # for availability. Other test clients may grab the port before we
  258. # are able to do it for example.
  259. if {$port_busy} {
  260. puts "Port $port was already busy, trying another port..."
  261. set port [find_available_port $::baseport $::portcount]
  262. if {$::tls} {
  263. dict set config "tls-port" $port
  264. } else {
  265. dict set config port $port
  266. }
  267. create_server_config_file $config_file $config
  268. continue; # Try again
  269. }
  270. if {$code ne "undefined"} {
  271. set serverisup [server_is_up $::host $port $retrynum]
  272. } else {
  273. set serverisup 1
  274. }
  275. if {$::verbose} {
  276. puts ""
  277. }
  278. if {!$serverisup} {
  279. set err {}
  280. append err [exec cat $stdout] "\n" [exec cat $stderr]
  281. start_server_error $config_file $err
  282. return
  283. }
  284. set server_started 1
  285. }
  286. # setup properties to be able to initialize a client object
  287. set port_param [expr $::tls ? {"tls-port"} : {"port"}]
  288. set host $::host
  289. if {[dict exists $config bind]} { set host [dict get $config bind] }
  290. if {[dict exists $config $port_param]} { set port [dict get $config $port_param] }
  291. # setup config dict
  292. dict set srv "config_file" $config_file
  293. dict set srv "config" $config
  294. dict set srv "pid" $pid
  295. dict set srv "host" $host
  296. dict set srv "port" $port
  297. dict set srv "stdout" $stdout
  298. dict set srv "stderr" $stderr
  299. dict set srv "unixsocket" $unixsocket
  300. # if a block of code is supplied, we wait for the server to become
  301. # available, create a client object and kill the server afterwards
  302. if {$code ne "undefined"} {
  303. set line [exec head -n1 $stdout]
  304. if {[string match {*already in use*} $line]} {
  305. error_and_quit $config_file $line
  306. }
  307. if {$::wait_server} {
  308. set msg "server started PID: [dict get $srv "pid"]. press any key to continue..."
  309. puts $msg
  310. read stdin 1
  311. }
  312. while 1 {
  313. # check that the server actually started and is ready for connections
  314. if {[exec grep -i "Ready to accept" | wc -l < $stdout] > 0} {
  315. break
  316. }
  317. after 10
  318. }
  319. # append the server to the stack
  320. lappend ::servers $srv
  321. # connect client (after server dict is put on the stack)
  322. reconnect
  323. # execute provided block
  324. set num_tests $::num_tests
  325. if {[catch { uplevel 1 $code } error]} {
  326. set backtrace $::errorInfo
  327. # Kill the server without checking for leaks
  328. dict set srv "skipleaks" 1
  329. kill_server $srv
  330. # Print warnings from log
  331. puts [format "\nLogged warnings (pid %d):" [dict get $srv "pid"]]
  332. set warnings [warnings_from_file [dict get $srv "stdout"]]
  333. if {[string length $warnings] > 0} {
  334. puts "$warnings"
  335. } else {
  336. puts "(none)"
  337. }
  338. puts ""
  339. error $error $backtrace
  340. }
  341. # Don't do the leak check when no tests were run
  342. if {$num_tests == $::num_tests} {
  343. dict set srv "skipleaks" 1
  344. }
  345. # pop the server object
  346. set ::servers [lrange $::servers 0 end-1]
  347. set ::tags [lrange $::tags 0 end-[llength $tags]]
  348. kill_server $srv
  349. } else {
  350. set ::tags [lrange $::tags 0 end-[llength $tags]]
  351. set _ $srv
  352. }
  353. }