4
4
"fmt"
5
5
"net"
6
6
"os"
7
- "os/exec"
8
7
"path/filepath"
9
8
"strconv"
10
9
"sync"
@@ -28,12 +27,12 @@ const (
28
27
29
28
const (
30
29
sentinelName = "go-redis-test"
31
- sentinelMasterPort = "9123 "
32
- sentinelSlave1Port = "9124 "
33
- sentinelSlave2Port = "9125 "
34
- sentinelPort1 = "9126 "
35
- sentinelPort2 = "9127 "
36
- sentinelPort3 = "9128 "
30
+ sentinelMasterPort = "9121 "
31
+ sentinelSlave1Port = "9122 "
32
+ sentinelSlave2Port = "9123 "
33
+ sentinelPort1 = "26379 "
34
+ sentinelPort2 = "26380 "
35
+ sentinelPort3 = "26381 "
37
36
)
38
37
39
38
var (
52
51
processes map [string ]* redisProcess
53
52
54
53
ringShard1 , ringShard2 , ringShard3 * redis.Client
55
- sentinelMaster , sentinelSlave1 , sentinelSlave2 * redisProcess
56
- sentinel1 , sentinel2 , sentinel3 * redisProcess
54
+ sentinelMaster , sentinelSlave1 , sentinelSlave2 * redis. Client
55
+ sentinel1 , sentinel2 , sentinel3 * redis. Client
57
56
)
58
57
59
58
var cluster = & clusterScenario {
@@ -118,7 +117,7 @@ var _ = BeforeSuite(func() {
118
117
}
119
118
120
119
if ! RECluster && ! RCEDocker {
121
- sentinelMaster , err = startRedis (sentinelMasterPort )
120
+ sentinelMaster , err = connectTo (sentinelMasterPort )
122
121
Expect (err ).NotTo (HaveOccurred ())
123
122
124
123
sentinel1 , err = startSentinel (sentinelPort1 , sentinelName , sentinelMasterPort )
@@ -130,15 +129,14 @@ var _ = BeforeSuite(func() {
130
129
sentinel3 , err = startSentinel (sentinelPort3 , sentinelName , sentinelMasterPort )
131
130
Expect (err ).NotTo (HaveOccurred ())
132
131
133
- sentinelSlave1 , err = startRedis (
134
- sentinelSlave1Port , "--slaveof" , "127.0.0.1" , sentinelMasterPort )
132
+ sentinelSlave1 , err = connectTo (sentinelSlave1Port )
135
133
Expect (err ).NotTo (HaveOccurred ())
136
-
137
- sentinelSlave2 , err = startRedis (
138
- sentinelSlave2Port , "--slaveof" , "127.0.0.1" , sentinelMasterPort )
134
+ err = sentinelSlave1 .SlaveOf (ctx , "127.0.0.1" , sentinelMasterPort ).Err ()
139
135
Expect (err ).NotTo (HaveOccurred ())
140
136
141
- err = startCluster (ctx , cluster )
137
+ sentinelSlave2 , err = connectTo (sentinelSlave2Port )
138
+ Expect (err ).NotTo (HaveOccurred ())
139
+ err = sentinelSlave2 .SlaveOf (ctx , "127.0.0.1" , sentinelMasterPort ).Err ()
142
140
Expect (err ).NotTo (HaveOccurred ())
143
141
} else {
144
142
redisPort = redisStackPort
@@ -154,6 +152,28 @@ var _ = BeforeSuite(func() {
154
152
ringShard3 , err = connectTo (ringShard3Port )
155
153
Expect (err ).NotTo (HaveOccurred ())
156
154
155
+ sentinelMaster , err = connectTo (sentinelMasterPort )
156
+ Expect (err ).NotTo (HaveOccurred ())
157
+
158
+ sentinel1 , err = startSentinel (sentinelPort1 , sentinelName , sentinelMasterPort )
159
+ Expect (err ).NotTo (HaveOccurred ())
160
+
161
+ sentinel2 , err = startSentinel (sentinelPort2 , sentinelName , sentinelMasterPort )
162
+ Expect (err ).NotTo (HaveOccurred ())
163
+
164
+ sentinel3 , err = startSentinel (sentinelPort3 , sentinelName , sentinelMasterPort )
165
+ Expect (err ).NotTo (HaveOccurred ())
166
+
167
+ sentinelSlave1 , err = connectTo (sentinelSlave1Port )
168
+ Expect (err ).NotTo (HaveOccurred ())
169
+ err = sentinelSlave1 .SlaveOf (ctx , "127.0.0.1" , sentinelMasterPort ).Err ()
170
+ Expect (err ).NotTo (HaveOccurred ())
171
+
172
+ sentinelSlave2 , err = connectTo (sentinelSlave2Port )
173
+ Expect (err ).NotTo (HaveOccurred ())
174
+ err = sentinelSlave2 .SlaveOf (ctx , "127.0.0.1" , sentinelMasterPort ).Err ()
175
+ Expect (err ).NotTo (HaveOccurred ())
176
+
157
177
// populate cluster node information
158
178
Expect (configureClusterTopology (ctx , cluster )).NotTo (HaveOccurred ())
159
179
}
@@ -310,15 +330,6 @@ func eventually(fn func() error, timeout time.Duration) error {
310
330
}
311
331
}
312
332
313
- func execCmd (name string , args ... string ) (* os.Process , error ) {
314
- cmd := exec .Command (name , args ... )
315
- if testing .Verbose () {
316
- cmd .Stdout = os .Stdout
317
- cmd .Stderr = os .Stderr
318
- }
319
- return cmd .Process , cmd .Start ()
320
- }
321
-
322
333
func connectTo (port string ) (* redis.Client , error ) {
323
334
client := redis .NewClient (& redis.Options {
324
335
Addr : ":" + port ,
@@ -379,52 +390,9 @@ func redisDir(port string) (string, error) {
379
390
return dir , nil
380
391
}
381
392
382
- func startRedis (port string , args ... string ) (* redisProcess , error ) {
383
- dir , err := redisDir (port )
384
- if err != nil {
385
- return nil , err
386
- }
387
-
388
- if err := exec .Command ("cp" , "-f" , redisServerConf , dir ).Run (); err != nil {
389
- return nil , err
390
- }
391
-
392
- baseArgs := []string {filepath .Join (dir , "redis.conf" ), "--port" , port , "--dir" , dir , "--enable-module-command" , "yes" }
393
- process , err := execCmd (redisServerBin , append (baseArgs , args ... )... )
394
- if err != nil {
395
- return nil , err
396
- }
397
-
398
- client , err := connectTo (port )
399
- if err != nil {
400
- process .Kill ()
401
- return nil , err
402
- }
403
-
404
- p := & redisProcess {process , client }
405
- registerProcess (port , p )
406
- return p , nil
407
- }
408
-
409
- func startSentinel (port , masterName , masterPort string ) (* redisProcess , error ) {
410
- dir , err := redisDir (port )
411
- if err != nil {
412
- return nil , err
413
- }
414
-
415
- sentinelConf := filepath .Join (dir , "sentinel.conf" )
416
- if err := os .WriteFile (sentinelConf , nil , 0o644 ); err != nil {
417
- return nil , err
418
- }
419
-
420
- process , err := execCmd (redisServerBin , sentinelConf , "--sentinel" , "--port" , port , "--dir" , dir )
421
- if err != nil {
422
- return nil , err
423
- }
424
-
393
+ func startSentinel (port , masterName , masterPort string ) (* redis.Client , error ) {
425
394
client , err := connectTo (port )
426
395
if err != nil {
427
- process .Kill ()
428
396
return nil , err
429
397
}
430
398
@@ -438,14 +406,11 @@ func startSentinel(port, masterName, masterPort string) (*redisProcess, error) {
438
406
} {
439
407
client .Process (ctx , cmd )
440
408
if err := cmd .Err (); err != nil {
441
- process .Kill ()
442
409
return nil , fmt .Errorf ("%s failed: %w" , cmd , err )
443
410
}
444
411
}
445
412
446
- p := & redisProcess {process , client }
447
- registerProcess (port , p )
448
- return p , nil
413
+ return client , nil
449
414
}
450
415
451
416
//------------------------------------------------------------------------------
0 commit comments