1
- # Sentinel test suite. Copyright (C) 2014 Salvatore Sanfilippo [email protected]
1
+ # Multi-instance test framework.
2
+ # This is used in order to test Sentinel and Redis Cluster, and provides
3
+ # basic capabilities for spawning and handling N parallel Redis / Sentinel
4
+ # instances.
5
+ #
6
+ # Copyright (C) 2014 Salvatore Sanfilippo [email protected]
2
7
# This softare is released under the BSD License. See the COPYING file for
3
8
# more information.
4
9
5
10
package require Tcl 8.5
6
11
7
12
set tcl_precision 17
8
- source tests /support/redis.tcl
9
- source tests /support/util.tcl
10
- source tests /support/server.tcl
11
- source tests /support/test.tcl
13
+ source .. /support/redis.tcl
14
+ source .. /support/util.tcl
15
+ source .. /support/server.tcl
16
+ source .. /support/test.tcl
12
17
13
18
set ::verbose 0
14
19
set ::pause_on_error 0
@@ -17,45 +22,50 @@ set ::sentinel_instances {}
17
22
set ::redis_instances {}
18
23
set ::sentinel_base_port 20000
19
24
set ::redis_base_port 30000
20
- set ::instances_count 5 ; # How many Sentinels / Instances we use at max
21
25
set ::pids {} ; # We kill everything at exit
22
26
set ::dirs {} ; # We remove all the temp dirs at exit
23
27
set ::run_matching {} ; # If non empty, only tests matching pattern are run.
24
28
25
- if {[catch {cd tests/sentinel- tmp}]} {
26
- puts " tests/sentinel- tmp directory not found."
29
+ if {[catch {cd tmp}]} {
30
+ puts " tmp directory not found."
27
31
puts " Please run this test from the Redis source root."
28
32
exit 1
29
33
}
30
34
31
35
# Spawn a redis or sentinel instance, depending on 'type'.
32
- proc spawn_instance {type base_port count} {
36
+ proc spawn_instance {type base_port count {conf {}} } {
33
37
for {set j 0} {$j < $count } {incr j} {
34
38
set port [find_available_port $base_port ]
35
39
incr base_port
36
40
puts " Starting $type #$j at port $port "
37
41
38
- # Create a directory for this Sentinel .
42
+ # Create a directory for this instance .
39
43
set dirname " ${type} _${j} "
40
44
lappend ::dirs $dirname
41
45
catch {exec rm -rf $dirname }
42
46
file mkdir $dirname
43
47
44
- # Write the Sentinel config file.
48
+ # Write the instance config file.
45
49
set cfgfile [file join $dirname $type .conf]
46
50
set cfg [open $cfgfile w]
47
51
puts $cfg " port $port "
48
52
puts $cfg " dir ./$dirname "
49
53
puts $cfg " logfile log.txt"
54
+ # Add additional config files
55
+ foreach directive $conf {
56
+ puts $cfg $directive
57
+ }
50
58
close $cfg
51
59
52
60
# Finally exec it and remember the pid for later cleanup.
53
61
if {$type eq " redis" } {
54
62
set prgname redis-server
55
- } else {
63
+ } elseif { $type eq " sentinel " } {
56
64
set prgname redis-sentinel
65
+ } else {
66
+ error " Unknown instance type."
57
67
}
58
- set pid [exec ../../src/${prgname} $cfgfile &]
68
+ set pid [exec ../../../ src/${prgname} $cfgfile &]
59
69
lappend ::pids $pid
60
70
61
71
# Check availability
@@ -64,11 +74,13 @@ proc spawn_instance {type base_port count} {
64
74
}
65
75
66
76
# Push the instance into the right list
77
+ set link [redis 127.0.0.1 $port ]
78
+ $link reconnect 1
67
79
lappend ::${type} _instances [list \
68
80
pid $pid \
69
81
host 127.0.0.1 \
70
82
port $port \
71
- link [redis 127.0.0.1 $port ] \
83
+ link $link \
72
84
]
73
85
}
74
86
}
@@ -116,14 +128,6 @@ proc parse_options {} {
116
128
}
117
129
}
118
130
119
- proc main {} {
120
- parse_options
121
- spawn_instance sentinel $::sentinel_base_port $::instances_count
122
- spawn_instance redis $::redis_base_port $::instances_count
123
- run_tests
124
- cleanup
125
- }
126
-
127
131
# If --pause-on-error option was passed at startup this function is called
128
132
# on error in order to give the developer a chance to understand more about
129
133
# the error condition while the instances are still running.
@@ -139,6 +143,14 @@ proc pause_on_error {} {
139
143
set cmd [lindex $argv 0]
140
144
if {$cmd eq {continue }} {
141
145
break
146
+ } elseif {$cmd eq {show-redis-logs}} {
147
+ set count 10
148
+ if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
149
+ foreach_redis_id id {
150
+ puts " === REDIS $id ===="
151
+ puts [exec tail -$count redis_$id /log.txt]
152
+ puts " ---------------------\n "
153
+ }
142
154
} elseif {$cmd eq {show-sentinel-logs}} {
143
155
set count 10
144
156
if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
@@ -182,6 +194,7 @@ proc pause_on_error {} {
182
194
} elseif {$cmd eq {help}} {
183
195
puts " ls List Sentinel and Redis instances."
184
196
puts " show-sentinel-logs \[ N\] Show latest N lines of logs."
197
+ puts " show-redis-logs \[ N\] Show latest N lines of logs."
185
198
puts " S <id> cmd ... arg Call command in Sentinel <id>."
186
199
puts " R <id> cmd ... arg Call command in Redis <id>."
187
200
puts " SI <id> <field> Show Sentinel <id> INFO <field>."
@@ -218,7 +231,7 @@ proc test {descr code} {
218
231
}
219
232
220
233
proc run_tests {} {
221
- set tests [lsort [glob ../sentinel- tests/*]]
234
+ set tests [lsort [glob ../tests/*]]
222
235
foreach test $tests {
223
236
if {$::run_matching ne {} && [string match $::run_matching $test ] == 0} {
224
237
continue
@@ -361,7 +374,7 @@ proc kill_instance {type id} {
361
374
# Return true of the instance of the specified type/id is killed.
362
375
proc instance_is_killed {type id} {
363
376
set pid [get_instance_attrib $type $id pid]
364
- return $pid == -1
377
+ expr { $pid == -1}
365
378
}
366
379
367
380
# Restart an instance previously killed by kill_instance
@@ -377,7 +390,7 @@ proc restart_instance {type id} {
377
390
} else {
378
391
set prgname redis-sentinel
379
392
}
380
- set pid [exec ../../src/${prgname} $cfgfile &]
393
+ set pid [exec ../../../ src/${prgname} $cfgfile &]
381
394
set_instance_attrib $type $id pid $pid
382
395
lappend ::pids $pid
383
396
@@ -387,11 +400,8 @@ proc restart_instance {type id} {
387
400
}
388
401
389
402
# Connect with it with a fresh link
390
- set_instance_attrib $type $id link [redis 127.0.0.1 $port ]
403
+ set link [redis 127.0.0.1 $port ]
404
+ $link reconnect 1
405
+ set_instance_attrib $type $id link $link
391
406
}
392
407
393
- if {[catch main e]} {
394
- puts $::errorInfo
395
- cleanup
396
- exit 1
397
- }
0 commit comments