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