Skip to content

Commit

Permalink
refactor: refactor Redis Sentinel setup for improved configuration fl…
Browse files Browse the repository at this point in the history
…exibility

- Refactor `setupRedisSentinelContainer` to accept `masterHost` and `masterPort` as parameters
- Update environment variables in `setupRedisSentinelContainer` to use the provided `masterHost` and `masterPort`
- Add setup for Redis master container in `TestRedisSentinel`
- Add setup for Redis sentinel container in `TestRedisSentinel`
- Update variable names in `TestRedisSentinel` to reflect the new setup process

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jan 18, 2025
1 parent 647d1aa commit 333915d
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func (m mockMessage) Bytes() []byte {
return []byte(m.Message)
}

func setupRedisSentinelContainer(ctx context.Context, t *testing.T) (testcontainers.Container, string) {
func setupRedisSentinelContainer(
ctx context.Context,
t *testing.T,
masterHost string,
masterPort string,
) (testcontainers.Container, string) {
req := testcontainers.ContainerRequest{
Image: "bitnami/redis-sentinel:7.4-debian-12",
ExposedPorts: []string{
Expand All @@ -43,9 +48,10 @@ func setupRedisSentinelContainer(ctx context.Context, t *testing.T) (testcontain
[]string{"redis-cli", "-h", "localhost", "-p", "26379", "ping"},
),
Env: map[string]string{
"REDIS_MASTER_HOST": "redis",
"REDIS_MASTER_SET": "mymaster",
"REDIS_SENTINEL_QUORUM": "1",
"REDIS_MASTER_HOST": masterHost,
"REDIS_MASTER_PORT_NUMBER": masterPort,
"REDIS_MASTER_SET": "mymaster",
"REDIS_SENTINEL_QUORUM": "1",
},
}
redisC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down Expand Up @@ -242,20 +248,29 @@ func TestRedisSentinel(t *testing.T) {
t.Helper()

ctx := context.Background()
redisC, _ := setupRedisSentinelContainer(ctx, t)

// create redis master
redisC, _ := setupRedisContainer(ctx, t)
defer testcontainers.CleanupContainer(t, redisC)
masterPort, err := redisC.MappedPort(ctx, "6379")
assert.NoError(t, err)
masterHost, err := redisC.Host(ctx)
assert.NoError(t, err)

sentinelC, _ := setupRedisSentinelContainer(ctx, t, masterHost, masterPort.Port())
defer testcontainers.CleanupContainer(t, sentinelC)

masterPort, err := redisC.MappedPort(ctx, "26379")
sentinelPort, err := redisC.MappedPort(ctx, "26379")
assert.NoError(t, err)

hostIP, err := redisC.Host(ctx)
sentinelHost, err := redisC.Host(ctx)
assert.NoError(t, err)

m := &mockMessage{
Message: "foo",
}

masterName := fmt.Sprintf("%s:%s", hostIP, masterPort.Port())
masterName := fmt.Sprintf("%s:%s", sentinelHost, sentinelPort.Port())

hosts := []string{masterName}

Expand Down

0 comments on commit 333915d

Please sign in to comment.