]>
Commit | Line | Data |
---|---|---|
1 | set ::global_overrides {} | |
2 | set ::tags {} | |
3 | ||
4 | proc error_and_quit {config_file error} { | |
5 | puts "!!COULD NOT START REDIS-SERVER\n" | |
6 | puts "CONFIGURATION:" | |
7 | puts [exec cat $config_file] | |
8 | puts "\nERROR:" | |
9 | puts [string trim $error] | |
10 | exit 1 | |
11 | } | |
12 | ||
13 | proc check_valgrind_errors stderr { | |
14 | set fd [open $stderr] | |
15 | set buf [read $fd] | |
16 | close $fd | |
17 | ||
18 | if {![regexp -- {ERROR SUMMARY: 0 errors} $buf] || | |
19 | ![regexp -- {definitely lost: 0 bytes} $buf]} { | |
20 | puts "*** VALGRIND ERRORS ***" | |
21 | puts $buf | |
22 | puts "--- press enter to continue ---" | |
23 | gets stdin | |
24 | } | |
25 | } | |
26 | ||
27 | proc kill_server config { | |
28 | # nothing to kill when running against external server | |
29 | if {$::external} return | |
30 | ||
31 | # nevermind if its already dead | |
32 | if {![is_alive $config]} { return } | |
33 | set pid [dict get $config pid] | |
34 | ||
35 | # check for leaks | |
36 | if {![dict exists $config "skipleaks"]} { | |
37 | catch { | |
38 | if {[string match {*Darwin*} [exec uname -a]]} { | |
39 | tags {"leaks"} { | |
40 | test "Check for memory leaks (pid $pid)" { | |
41 | exec leaks $pid | |
42 | } {*0 leaks*} | |
43 | } | |
44 | } | |
45 | } | |
46 | } | |
47 | ||
48 | # kill server and wait for the process to be totally exited | |
49 | while {[is_alive $config]} { | |
50 | if {[incr wait 10] % 1000 == 0} { | |
51 | puts "Waiting for process $pid to exit..." | |
52 | } | |
53 | catch {exec kill $pid} | |
54 | after 10 | |
55 | } | |
56 | ||
57 | # Check valgrind errors if needed | |
58 | if {$::valgrind} { | |
59 | check_valgrind_errors [dict get $config stderr] | |
60 | } | |
61 | } | |
62 | ||
63 | proc is_alive config { | |
64 | set pid [dict get $config pid] | |
65 | if {[catch {exec ps -p $pid} err]} { | |
66 | return 0 | |
67 | } else { | |
68 | return 1 | |
69 | } | |
70 | } | |
71 | ||
72 | proc ping_server {host port} { | |
73 | set retval 0 | |
74 | if {[catch { | |
75 | set fd [socket $::host $::port] | |
76 | fconfigure $fd -translation binary | |
77 | puts $fd "PING\r\n" | |
78 | flush $fd | |
79 | set reply [gets $fd] | |
80 | if {[string range $reply 0 4] eq {+PONG} || | |
81 | [string range $reply 0 3] eq {-ERR}} { | |
82 | set retval 1 | |
83 | } | |
84 | close $fd | |
85 | } e]} { | |
86 | puts "Can't PING server at $host:$port... $e" | |
87 | } | |
88 | return $retval | |
89 | } | |
90 | ||
91 | # doesn't really belong here, but highly coupled to code in start_server | |
92 | proc tags {tags code} { | |
93 | set ::tags [concat $::tags $tags] | |
94 | uplevel 1 $code | |
95 | set ::tags [lrange $::tags 0 end-[llength $tags]] | |
96 | } | |
97 | ||
98 | proc start_server {options {code undefined}} { | |
99 | # If we are runnign against an external server, we just push the | |
100 | # host/port pair in the stack the first time | |
101 | if {$::external} { | |
102 | if {[llength $::servers] == 0} { | |
103 | set srv {} | |
104 | dict set srv "host" $::host | |
105 | dict set srv "port" $::port | |
106 | set client [redis $::host $::port] | |
107 | dict set srv "client" $client | |
108 | $client select 9 | |
109 | ||
110 | # append the server to the stack | |
111 | lappend ::servers $srv | |
112 | } | |
113 | uplevel 1 $code | |
114 | return | |
115 | } | |
116 | ||
117 | # setup defaults | |
118 | set baseconfig "default.conf" | |
119 | set overrides {} | |
120 | set tags {} | |
121 | ||
122 | # parse options | |
123 | foreach {option value} $options { | |
124 | switch $option { | |
125 | "config" { | |
126 | set baseconfig $value } | |
127 | "overrides" { | |
128 | set overrides $value } | |
129 | "tags" { | |
130 | set tags $value | |
131 | set ::tags [concat $::tags $value] } | |
132 | default { | |
133 | error "Unknown option $option" } | |
134 | } | |
135 | } | |
136 | ||
137 | set data [split [exec cat "tests/assets/$baseconfig"] "\n"] | |
138 | set config {} | |
139 | foreach line $data { | |
140 | if {[string length $line] > 0 && [string index $line 0] ne "#"} { | |
141 | set elements [split $line " "] | |
142 | set directive [lrange $elements 0 0] | |
143 | set arguments [lrange $elements 1 end] | |
144 | dict set config $directive $arguments | |
145 | } | |
146 | } | |
147 | ||
148 | # use a different directory every time a server is started | |
149 | dict set config dir [tmpdir server] | |
150 | ||
151 | # start every server on a different port | |
152 | dict set config port [incr ::port] | |
153 | ||
154 | # apply overrides from global space and arguments | |
155 | foreach {directive arguments} [concat $::global_overrides $overrides] { | |
156 | dict set config $directive $arguments | |
157 | } | |
158 | ||
159 | # write new configuration to temporary file | |
160 | set config_file [tmpfile redis.conf] | |
161 | set fp [open $config_file w+] | |
162 | foreach directive [dict keys $config] { | |
163 | puts -nonewline $fp "$directive " | |
164 | puts $fp [dict get $config $directive] | |
165 | } | |
166 | close $fp | |
167 | ||
168 | set stdout [format "%s/%s" [dict get $config "dir"] "stdout"] | |
169 | set stderr [format "%s/%s" [dict get $config "dir"] "stderr"] | |
170 | ||
171 | if {$::valgrind} { | |
172 | exec valgrind src/redis-server $config_file > $stdout 2> $stderr & | |
173 | after 2000 | |
174 | } else { | |
175 | exec src/redis-server $config_file > $stdout 2> $stderr & | |
176 | after 500 | |
177 | } | |
178 | ||
179 | # check that the server actually started | |
180 | if {$code ne "undefined" && ![ping_server $::host $::port]} { | |
181 | error_and_quit $config_file [exec cat $stderr] | |
182 | } | |
183 | ||
184 | # find out the pid | |
185 | while {![info exists pid]} { | |
186 | regexp {^\[(\d+)\]} [exec head -n1 $stdout] _ pid | |
187 | after 100 | |
188 | } | |
189 | ||
190 | # setup properties to be able to initialize a client object | |
191 | set host $::host | |
192 | set port $::port | |
193 | if {[dict exists $config bind]} { set host [dict get $config bind] } | |
194 | if {[dict exists $config port]} { set port [dict get $config port] } | |
195 | ||
196 | # setup config dict | |
197 | dict set srv "config" $config_file | |
198 | dict set srv "pid" $pid | |
199 | dict set srv "host" $host | |
200 | dict set srv "port" $port | |
201 | dict set srv "stdout" $stdout | |
202 | dict set srv "stderr" $stderr | |
203 | ||
204 | # if a block of code is supplied, we wait for the server to become | |
205 | # available, create a client object and kill the server afterwards | |
206 | if {$code ne "undefined"} { | |
207 | set line [exec head -n1 $stdout] | |
208 | if {[string match {*already in use*} $line]} { | |
209 | error_and_quit $config_file $line | |
210 | } | |
211 | ||
212 | while 1 { | |
213 | # check that the server actually started and is ready for connections | |
214 | if {[exec cat $stdout | grep "ready to accept" | wc -l] > 0} { | |
215 | break | |
216 | } | |
217 | after 10 | |
218 | } | |
219 | ||
220 | set client [redis $host $port] | |
221 | dict set srv "client" $client | |
222 | ||
223 | # select the right db when we don't have to authenticate | |
224 | if {![dict exists $config requirepass]} { | |
225 | $client select 9 | |
226 | } | |
227 | ||
228 | # append the server to the stack | |
229 | lappend ::servers $srv | |
230 | ||
231 | # execute provided block | |
232 | set curnum $::testnum | |
233 | catch { uplevel 1 $code } err | |
234 | if {$curnum == $::testnum} { | |
235 | # don't check for leaks when no tests were executed | |
236 | dict set srv "skipleaks" 1 | |
237 | } | |
238 | ||
239 | # pop the server object | |
240 | set ::servers [lrange $::servers 0 end-1] | |
241 | ||
242 | # allow an exception to bubble up the call chain but still kill this | |
243 | # server, because we want to reuse the ports when the tests are re-run | |
244 | if {$err eq "exception"} { | |
245 | puts [format "Logged warnings (pid %d):" [dict get $srv "pid"]] | |
246 | set warnings [warnings_from_file [dict get $srv "stdout"]] | |
247 | if {[string length $warnings] > 0} { | |
248 | puts "$warnings" | |
249 | } else { | |
250 | puts "(none)" | |
251 | } | |
252 | # kill this server without checking for leaks | |
253 | dict set srv "skipleaks" 1 | |
254 | kill_server $srv | |
255 | error "exception" | |
256 | } elseif {[string length $err] > 0} { | |
257 | puts "Error executing the suite, aborting..." | |
258 | puts $err | |
259 | exit 1 | |
260 | } | |
261 | ||
262 | set ::tags [lrange $::tags 0 end-[llength $tags]] | |
263 | kill_server $srv | |
264 | } else { | |
265 | set ::tags [lrange $::tags 0 end-[llength $tags]] | |
266 | set _ $srv | |
267 | } | |
268 | } |