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