I recently learned of testcontainers, so I wanted to use it for an integration test cases, for a Spring Boot application.
I have tried many combinations to achieve this, but it isn't working / starting. For example, the below is the latest code I tried:
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
public class RedisSentinelIntegrationTest {
private static final int REDIS_MASTER_PORT = 6379;
private static final int REDIS_SENTINEL_PORT_1 = 26379;
private static final int REDIS_SENTINEL_PORT_2 = 26380;
@Test
void testRedisSentinel() {
Network network = Network.newNetwork();
// Redis Master
GenericContainer<?> redisMaster = new GenericContainer<>("redis:6.0.12")
.withExposedPorts(REDIS_MASTER_PORT)
.withNetwork(network)
.waitingFor(Wait.forListeningPort());
redisMaster.start();
// Redis Sentinel 1
GenericContainer<?> redisSentinel1 = new GenericContainer<>("redis:6.0.12")
.withExposedPorts(REDIS_SENTINEL_PORT_1)
.withNetwork(network)
.withCommand("redis-server", "--sentinel", "yes", "--sentinel-announce-ip", "redis-sentinel-1", "--sentinel-announce-port", String.valueOf(REDIS_SENTINEL_PORT_1))
.waitingFor(Wait.forListeningPort());
redisSentinel1.start();
// Redis Sentinel 2
GenericContainer<?> redisSentinel2 = new GenericContainer<>("redis:6.0.12")
.withExposedPorts(REDIS_SENTINEL_PORT_2)
.withNetwork(network)
.withCommand("redis-server", "--sentinel", "yes", "--sentinel-announce-ip", "redis-sentinel-2", "--sentinel-announce-port", String.valueOf(REDIS_SENTINEL_PORT_2))
.waitingFor(Wait.forListeningPort());
redisSentinel2.start();
String sentinelUrl1 = "redis://" + redisSentinel1.getContainerIpAddress() + ":" + redisSentinel1.getMappedPort(REDIS_SENTINEL_PORT_1);
String sentinelUrl2 = "redis://" + redisSentinel2.getContainerIpAddress() + ":" + redisSentinel2.getMappedPort(REDIS_SENTINEL_PORT_2);
// .. do something
// Stop the containers
redisMaster.stop();
redisSentinel1.stop();
redisSentinel2.stop();
}
}
But to my dismay it never works; isn't testcontainers supposed to ease life? I tried their official website, but again they haven't given any such example.
How can I use Redis testcontainers to run in sentinel mode and successfully run it? What issues might I have in the set up?