diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java index c988d842bd1..86a3010fda2 100644 --- a/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java @@ -57,7 +57,7 @@ public YugabyteDBYSQLContainer(final DockerImageName imageName) { super(imageName); imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); withExposedPorts(YSQL_PORT, MASTER_DASHBOARD_PORT, TSERVER_DASHBOARD_PORT); - waitingFor(new YugabyteDBYSQLWaitStrategy(this).withStartupTimeout(Duration.ofSeconds(60))); + waitingFor(new YugabyteDBYSQLWaitStrategy().withStartupTimeout(Duration.ofSeconds(60))); withCommand(ENTRYPOINT); } @@ -154,4 +154,9 @@ public YugabyteDBYSQLContainer withPassword(final String password) { this.password = password; return this; } + + @Override + protected void waitUntilContainerStarted() { + getWaitStrategy().waitUntilReady(this); + } } diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java index cadb83f66e0..68998538fd4 100644 --- a/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java @@ -33,9 +33,20 @@ public void execute( boolean continueOnError, boolean ignoreFailedDrops ) { + final String containerInterfaceIP = container + .getContainerInfo() + .getNetworkSettings() + .getNetworks() + .entrySet() + .stream() + .findFirst() + .get() + .getValue() + .getIpAddress(); try { ExecResult result = container.execInContainer( BIN_PATH, + containerInterfaceIP, "-u", container.getUsername(), "-p", diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java index 89d93a5cb84..9b9d24a4c71 100644 --- a/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java @@ -37,6 +37,16 @@ public final class YugabyteDBYCQLWaitStrategy extends AbstractWaitStrategy { public void waitUntilReady(WaitStrategyTarget target) { YugabyteDBYCQLContainer container = (YugabyteDBYCQLContainer) target; AtomicBoolean status = new AtomicBoolean(true); + final String containerInterfaceIP = container + .getContainerInfo() + .getNetworkSettings() + .getNetworks() + .entrySet() + .stream() + .findFirst() + .get() + .getValue() + .getIpAddress(); retryUntilSuccess( (int) startupTimeout.getSeconds(), TimeUnit.SECONDS, @@ -46,6 +56,7 @@ public void waitUntilReady(WaitStrategyTarget target) { try { ExecResult result = container.execInContainer( BIN_PATH, + containerInterfaceIP, "-u", container.getUsername(), "-p", diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java index 690f8a97bc9..b69505435b7 100644 --- a/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java @@ -4,10 +4,10 @@ import lombok.extern.slf4j.Slf4j; import org.testcontainers.containers.YugabyteDBYSQLContainer; import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; -import org.testcontainers.containers.wait.strategy.WaitStrategyTarget; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; import java.util.concurrent.TimeUnit; import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess; @@ -27,32 +27,26 @@ @Slf4j public final class YugabyteDBYSQLWaitStrategy extends AbstractWaitStrategy { - private static final String YSQL_TEST_QUERY = "SELECT 1"; - - private final WaitStrategyTarget target; + private static final String YSQL_EXTENDED_PROBE = + "CREATE TEMP TABLE IF NOT EXISTS YB_SAMPLE(k int, v int, primary key(k, v))"; @Override - public void waitUntilReady(WaitStrategyTarget target) { - YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) target; + public void waitUntilReady() { + YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) waitStrategyTarget; retryUntilSuccess( (int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> { getRateLimiter() .doWhenReady(() -> { - try (Connection con = container.createConnection(container.getJdbcUrl())) { - con.createStatement().execute(YSQL_TEST_QUERY); + try (Connection con = container.createConnection(""); Statement stmt = con.createStatement()) { + stmt.execute(YSQL_EXTENDED_PROBE); } catch (SQLException ex) { - log.error("Error connecting to the database", ex); + throw new RuntimeException(ex); } }); return true; } ); } - - @Override - public void waitUntilReady() { - waitUntilReady(target); - } } diff --git a/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java b/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java index 5026048ab85..0318ddb5243 100644 --- a/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java +++ b/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java @@ -15,6 +15,8 @@ public class YugabyteDBYCQLTest { private static final String IMAGE_NAME = "yugabytedb/yugabyte:2.14.4.0-b26"; + private static final String IMAGE_NAME_2_18 = "yugabytedb/yugabyte:2.18.3.0-b75"; + private static final DockerImageName YBDB_TEST_IMAGE = DockerImageName.parse(IMAGE_NAME); @Test @@ -106,6 +108,14 @@ public void testInitScript() { } } + @Test + public void shouldStartWhenContainerIpIsUsedInWaitStrategy() { + try (final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(IMAGE_NAME_2_18)) { + ycqlContainer.start(); + assertThat(performQuery(ycqlContainer, "SELECT release_version FROM system.local").wasApplied()).isTrue(); + } + } + private ResultSet performQuery(YugabyteDBYCQLContainer ycqlContainer, String cql) { try ( CqlSession session = CqlSession diff --git a/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java b/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java index 2cf0b0b15c1..73cbc82d8ff 100644 --- a/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java +++ b/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java @@ -95,4 +95,20 @@ public void testWithCustomRole() throws SQLException { .isEqualTo(1); } } + + @Test + public void testWaitStrategy() throws SQLException { + try (final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE)) { + ysqlContainer.start(); + assertThat(performQuery(ysqlContainer, "SELECT 1").getInt(1)) + .as("A sample test query succeeds") + .isEqualTo(1); + assertThat( + performQuery(ysqlContainer, "SELECT EXISTS (SELECT FROM pg_tables WHERE tablename = 'YB_SAMPLE')") + .getBoolean(1) + ) + .as("yb_sample table does not exists") + .isEqualTo(false); + } + } }