Skip to content

Commit eef369b

Browse files
committed
Sentinel framework new layout back ported 2.8.
Functionally the same, but makes cherry picking simpler.
1 parent f392c77 commit eef369b

File tree

10 files changed

+67
-35
lines changed

10 files changed

+67
-35
lines changed

runtest-sentinel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ then
1111
echo "You need tcl 8.5 or newer in order to run the Redis Sentinel test"
1212
exit 1
1313
fi
14-
$TCLSH tests/sentinel.tcl $*
14+
$TCLSH tests/sentinel/run.tcl $*

tests/sentinel.tcl renamed to tests/instances.tcl

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
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]
27
# This softare is released under the BSD License. See the COPYING file for
38
# more information.
49

510
package require Tcl 8.5
611

712
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
1217

1318
set ::verbose 0
1419
set ::pause_on_error 0
@@ -17,45 +22,50 @@ set ::sentinel_instances {}
1722
set ::redis_instances {}
1823
set ::sentinel_base_port 20000
1924
set ::redis_base_port 30000
20-
set ::instances_count 5 ; # How many Sentinels / Instances we use at max
2125
set ::pids {} ; # We kill everything at exit
2226
set ::dirs {} ; # We remove all the temp dirs at exit
2327
set ::run_matching {} ; # If non empty, only tests matching pattern are run.
2428

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."
2731
puts "Please run this test from the Redis source root."
2832
exit 1
2933
}
3034

3135
# 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 {}}} {
3337
for {set j 0} {$j < $count} {incr j} {
3438
set port [find_available_port $base_port]
3539
incr base_port
3640
puts "Starting $type #$j at port $port"
3741

38-
# Create a directory for this Sentinel.
42+
# Create a directory for this instance.
3943
set dirname "${type}_${j}"
4044
lappend ::dirs $dirname
4145
catch {exec rm -rf $dirname}
4246
file mkdir $dirname
4347

44-
# Write the Sentinel config file.
48+
# Write the instance config file.
4549
set cfgfile [file join $dirname $type.conf]
4650
set cfg [open $cfgfile w]
4751
puts $cfg "port $port"
4852
puts $cfg "dir ./$dirname"
4953
puts $cfg "logfile log.txt"
54+
# Add additional config files
55+
foreach directive $conf {
56+
puts $cfg $directive
57+
}
5058
close $cfg
5159

5260
# Finally exec it and remember the pid for later cleanup.
5361
if {$type eq "redis"} {
5462
set prgname redis-server
55-
} else {
63+
} elseif {$type eq "sentinel"} {
5664
set prgname redis-sentinel
65+
} else {
66+
error "Unknown instance type."
5767
}
58-
set pid [exec ../../src/${prgname} $cfgfile &]
68+
set pid [exec ../../../src/${prgname} $cfgfile &]
5969
lappend ::pids $pid
6070

6171
# Check availability
@@ -64,11 +74,13 @@ proc spawn_instance {type base_port count} {
6474
}
6575

6676
# Push the instance into the right list
77+
set link [redis 127.0.0.1 $port]
78+
$link reconnect 1
6779
lappend ::${type}_instances [list \
6880
pid $pid \
6981
host 127.0.0.1 \
7082
port $port \
71-
link [redis 127.0.0.1 $port] \
83+
link $link \
7284
]
7385
}
7486
}
@@ -116,14 +128,6 @@ proc parse_options {} {
116128
}
117129
}
118130

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-
127131
# If --pause-on-error option was passed at startup this function is called
128132
# on error in order to give the developer a chance to understand more about
129133
# the error condition while the instances are still running.
@@ -139,6 +143,14 @@ proc pause_on_error {} {
139143
set cmd [lindex $argv 0]
140144
if {$cmd eq {continue}} {
141145
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+
}
142154
} elseif {$cmd eq {show-sentinel-logs}} {
143155
set count 10
144156
if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
@@ -182,6 +194,7 @@ proc pause_on_error {} {
182194
} elseif {$cmd eq {help}} {
183195
puts "ls List Sentinel and Redis instances."
184196
puts "show-sentinel-logs \[N\] Show latest N lines of logs."
197+
puts "show-redis-logs \[N\] Show latest N lines of logs."
185198
puts "S <id> cmd ... arg Call command in Sentinel <id>."
186199
puts "R <id> cmd ... arg Call command in Redis <id>."
187200
puts "SI <id> <field> Show Sentinel <id> INFO <field>."
@@ -218,7 +231,7 @@ proc test {descr code} {
218231
}
219232

220233
proc run_tests {} {
221-
set tests [lsort [glob ../sentinel-tests/*]]
234+
set tests [lsort [glob ../tests/*]]
222235
foreach test $tests {
223236
if {$::run_matching ne {} && [string match $::run_matching $test] == 0} {
224237
continue
@@ -361,7 +374,7 @@ proc kill_instance {type id} {
361374
# Return true of the instance of the specified type/id is killed.
362375
proc instance_is_killed {type id} {
363376
set pid [get_instance_attrib $type $id pid]
364-
return $pid == -1
377+
expr {$pid == -1}
365378
}
366379

367380
# Restart an instance previously killed by kill_instance
@@ -377,7 +390,7 @@ proc restart_instance {type id} {
377390
} else {
378391
set prgname redis-sentinel
379392
}
380-
set pid [exec ../../src/${prgname} $cfgfile &]
393+
set pid [exec ../../../src/${prgname} $cfgfile &]
381394
set_instance_attrib $type $id pid $pid
382395
lappend ::pids $pid
383396

@@ -387,11 +400,8 @@ proc restart_instance {type id} {
387400
}
388401

389402
# 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
391406
}
392407

393-
if {[catch main e]} {
394-
puts $::errorInfo
395-
cleanup
396-
exit 1
397-
}

tests/sentinel/run.tcl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sentinel test suite. Copyright (C) 2014 Salvatore Sanfilippo [email protected]
2+
# This softare is released under the BSD License. See the COPYING file for
3+
# more information.
4+
5+
cd tests/sentinel
6+
source ../instances.tcl
7+
8+
set ::instances_count 5 ; # How many instances we use at max.
9+
10+
proc main {} {
11+
parse_options
12+
spawn_instance sentinel $::sentinel_base_port $::instances_count
13+
spawn_instance redis $::redis_base_port $::instances_count
14+
run_tests
15+
cleanup
16+
}
17+
18+
if {[catch main e]} {
19+
puts $::errorInfo
20+
cleanup
21+
exit 1
22+
}

tests/sentinel-tests/00-base.tcl renamed to tests/sentinel/tests/00-base.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Check the basic monitoring and failover capabilities.
22

3-
source "../sentinel-tests/includes/init-tests.tcl"
3+
source "../tests/includes/init-tests.tcl"
44

55
if {$::simulate_error} {
66
test "This test will fail" {

tests/sentinel-tests/01-conf-update.tcl renamed to tests/sentinel/tests/01-conf-update.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Test Sentinel configuration consistency after partitions heal.
22

3-
source "../sentinel-tests/includes/init-tests.tcl"
3+
source "../tests/includes/init-tests.tcl"
44

55
test "We can failover with Sentinel 1 crashed" {
66
set old_port [RI $master_id tcp_port]

tests/sentinel-tests/02-slaves-reconf.tcl renamed to tests/sentinel/tests/02-slaves-reconf.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# 2) That partitioned slaves point to new master when they are partitioned
66
# away during failover and return at a latter time.
77

8-
source "../sentinel-tests/includes/init-tests.tcl"
8+
source "../tests/includes/init-tests.tcl"
99

1010
proc 03_test_slaves_replication {} {
1111
uplevel 1 {
File renamed without changes.

0 commit comments

Comments
 (0)