]> git.saurik.com Git - redis.git/blame - tests/support/server.tcl
Merge pull request #74 from kmerenkov/issue_620
[redis.git] / tests / support / server.tcl
CommitLineData
6e0e5bed
PN
1set ::global_overrides {}
2set ::tags {}
cabe03eb 3set ::valgrind_errors {}
6e0e5bed 4
98578b57
PN
5proc error_and_quit {config_file error} {
6 puts "!!COULD NOT START REDIS-SERVER\n"
7 puts "CONFIGURATION:"
8 puts [exec cat $config_file]
9 puts "\nERROR:"
10 puts [string trim $error]
11 exit 1
12}
13
c4669d25 14proc check_valgrind_errors stderr {
15 set fd [open $stderr]
16 set buf [read $fd]
17 close $fd
18
19 if {![regexp -- {ERROR SUMMARY: 0 errors} $buf] ||
cabe03eb 20 (![regexp -- {definitely lost: 0 bytes} $buf] &&
21 ![regexp -- {no leaks are possible} $buf])} {
4c378d7f 22 send_data_packet $::test_server_fd err "Valgrind error: $buf\n"
c4669d25 23 }
24}
25
4fb6d00c 26proc kill_server config {
7d04fc75 27 # nothing to kill when running against external server
28 if {$::external} return
29
53cbf66c
PN
30 # nevermind if its already dead
31 if {![is_alive $config]} { return }
4fb6d00c
PN
32 set pid [dict get $config pid]
33
239515bc 34 # check for leaks
436f18b6
PN
35 if {![dict exists $config "skipleaks"]} {
36 catch {
37 if {[string match {*Darwin*} [exec uname -a]]} {
5a9fcb87
PN
38 tags {"leaks"} {
39 test "Check for memory leaks (pid $pid)" {
40 exec leaks $pid
41 } {*0 leaks*}
42 }
436f18b6 43 }
239515bc
PN
44 }
45 }
46
4fb6d00c 47 # kill server and wait for the process to be totally exited
53cbf66c
PN
48 while {[is_alive $config]} {
49 if {[incr wait 10] % 1000 == 0} {
50 puts "Waiting for process $pid to exit..."
4fb6d00c 51 }
f166bb1d 52 catch {exec kill $pid}
4fb6d00c
PN
53 after 10
54 }
c4669d25 55
56 # Check valgrind errors if needed
57 if {$::valgrind} {
58 check_valgrind_errors [dict get $config stderr]
59 }
4fb6d00c
PN
60}
61
53cbf66c
PN
62proc is_alive config {
63 set pid [dict get $config pid]
64 if {[catch {exec ps -p $pid} err]} {
65 return 0
66 } else {
67 return 1
68 }
69}
70
c4669d25 71proc ping_server {host port} {
72 set retval 0
73 if {[catch {
74 set fd [socket $::host $::port]
75 fconfigure $fd -translation binary
76 puts $fd "PING\r\n"
77 flush $fd
78 set reply [gets $fd]
79 if {[string range $reply 0 4] eq {+PONG} ||
80 [string range $reply 0 3] eq {-ERR}} {
81 set retval 1
82 }
83 close $fd
84 } e]} {
6f8a32d5
PN
85 if {$::verbose} {
86 puts -nonewline "."
87 }
08f55b78 88 } else {
6f8a32d5
PN
89 if {$::verbose} {
90 puts -nonewline "ok"
91 }
c4669d25 92 }
93 return $retval
94}
95
6e0e5bed
PN
96# doesn't really belong here, but highly coupled to code in start_server
97proc tags {tags code} {
98 set ::tags [concat $::tags $tags]
99 uplevel 1 $code
100 set ::tags [lrange $::tags 0 end-[llength $tags]]
101}
102
9e5d2e8b 103proc start_server {options {code undefined}} {
7d04fc75 104 # If we are runnign against an external server, we just push the
105 # host/port pair in the stack the first time
106 if {$::external} {
107 if {[llength $::servers] == 0} {
108 set srv {}
109 dict set srv "host" $::host
110 dict set srv "port" $::port
111 set client [redis $::host $::port]
112 dict set srv "client" $client
113 $client select 9
114
115 # append the server to the stack
116 lappend ::servers $srv
117 }
118 uplevel 1 $code
119 return
120 }
121
9e5d2e8b
PN
122 # setup defaults
123 set baseconfig "default.conf"
124 set overrides {}
6e0e5bed 125 set tags {}
9e5d2e8b
PN
126
127 # parse options
128 foreach {option value} $options {
129 switch $option {
6e0e5bed
PN
130 "config" {
131 set baseconfig $value }
132 "overrides" {
133 set overrides $value }
134 "tags" {
135 set tags $value
136 set ::tags [concat $::tags $value] }
137 default {
138 error "Unknown option $option" }
9e5d2e8b
PN
139 }
140 }
141
142 set data [split [exec cat "tests/assets/$baseconfig"] "\n"]
98578b57
PN
143 set config {}
144 foreach line $data {
145 if {[string length $line] > 0 && [string index $line 0] ne "#"} {
146 set elements [split $line " "]
147 set directive [lrange $elements 0 0]
148 set arguments [lrange $elements 1 end]
149 dict set config $directive $arguments
150 }
151 }
152
153 # use a different directory every time a server is started
154 dict set config dir [tmpdir server]
155
47bebf15
PN
156 # start every server on a different port
157 dict set config port [incr ::port]
158
f166bb1d 159 # apply overrides from global space and arguments
9e5d2e8b 160 foreach {directive arguments} [concat $::global_overrides $overrides] {
98578b57
PN
161 dict set config $directive $arguments
162 }
163
164 # write new configuration to temporary file
165 set config_file [tmpfile redis.conf]
166 set fp [open $config_file w+]
167 foreach directive [dict keys $config] {
168 puts -nonewline $fp "$directive "
169 puts $fp [dict get $config $directive]
170 }
171 close $fp
172
173 set stdout [format "%s/%s" [dict get $config "dir"] "stdout"]
174 set stderr [format "%s/%s" [dict get $config "dir"] "stderr"]
c4669d25 175
176 if {$::valgrind} {
4b918769 177 exec valgrind --suppressions=src/valgrind.sup src/redis-server $config_file > $stdout 2> $stderr &
c4669d25 178 } else {
e2641e09 179 exec src/redis-server $config_file > $stdout 2> $stderr &
c4669d25 180 }
98578b57
PN
181
182 # check that the server actually started
08f55b78 183 # ugly but tries to be as fast as possible...
5ab1461f 184 set retrynum 100
08f55b78 185 set serverisup 0
186
6f8a32d5
PN
187 if {$::verbose} {
188 puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
189 }
190
08f55b78 191 after 10
192 if {$code ne "undefined"} {
193 while {[incr retrynum -1]} {
194 catch {
195 if {[ping_server $::host $::port]} {
196 set serverisup 1
197 }
198 }
199 if {$serverisup} break
200 after 50
201 }
202 } else {
203 set serverisup 1
204 }
6f8a32d5
PN
205
206 if {$::verbose} {
207 puts ""
208 }
08f55b78 209
210 if {!$serverisup} {
98578b57
PN
211 error_and_quit $config_file [exec cat $stderr]
212 }
213
98578b57 214 # find out the pid
c4669d25 215 while {![info exists pid]} {
72dff2c0 216 regexp {\[(\d+)\]} [exec cat $stdout] _ pid
c4669d25 217 after 100
218 }
98578b57 219
53cbf66c 220 # setup properties to be able to initialize a client object
98578b57
PN
221 set host $::host
222 set port $::port
223 if {[dict exists $config bind]} { set host [dict get $config bind] }
224 if {[dict exists $config port]} { set port [dict get $config port] }
98578b57 225
4fb6d00c 226 # setup config dict
941c9fa2
PN
227 dict set srv "config_file" $config_file
228 dict set srv "config" $config
1c4114be
PN
229 dict set srv "pid" $pid
230 dict set srv "host" $host
231 dict set srv "port" $port
232 dict set srv "stdout" $stdout
233 dict set srv "stderr" $stderr
4fb6d00c 234
53cbf66c
PN
235 # if a block of code is supplied, we wait for the server to become
236 # available, create a client object and kill the server afterwards
98578b57 237 if {$code ne "undefined"} {
53cbf66c
PN
238 set line [exec head -n1 $stdout]
239 if {[string match {*already in use*} $line]} {
240 error_and_quit $config_file $line
241 }
242
243 while 1 {
244 # check that the server actually started and is ready for connections
245 if {[exec cat $stdout | grep "ready to accept" | wc -l] > 0} {
246 break
247 }
248 after 10
249 }
250
1c4114be
PN
251 # append the server to the stack
252 lappend ::servers $srv
941c9fa2
PN
253
254 # connect client (after server dict is put on the stack)
255 reconnect
256
98578b57 257 # execute provided block
6f8a32d5
PN
258 set num_tests $::num_tests
259 if {[catch { uplevel 1 $code } error]} {
260 set backtrace $::errorInfo
261
262 # Kill the server without checking for leaks
263 dict set srv "skipleaks" 1
264 kill_server $srv
265
266 # Print warnings from log
267 puts [format "\nLogged warnings (pid %d):" [dict get $srv "pid"]]
268 set warnings [warnings_from_file [dict get $srv "stdout"]]
269 if {[string length $warnings] > 0} {
270 puts "$warnings"
271 } else {
272 puts "(none)"
273 }
274 puts ""
275
276 error $error $backtrace
a53ebb4c
PN
277 }
278
6f8a32d5
PN
279 # Don't do the leak check when no tests were run
280 if {$num_tests == $::num_tests} {
6e0e5bed
PN
281 dict set srv "skipleaks" 1
282 }
98578b57 283
1c4114be
PN
284 # pop the server object
285 set ::servers [lrange $::servers 0 end-1]
436f18b6 286
f6fa411d 287 set ::tags [lrange $::tags 0 end-[llength $tags]]
436f18b6 288 kill_server $srv
98578b57 289 } else {
f6fa411d 290 set ::tags [lrange $::tags 0 end-[llength $tags]]
1c4114be 291 set _ $srv
98578b57
PN
292 }
293}