From 858dd0a2829769f065cd99fc3facb617e0e78955 Mon Sep 17 00:00:00 2001 From: Srinivasa Vasu Date: Tue, 15 Nov 2022 23:58:08 +0530 Subject: [PATCH] Add YugabyteDB module (#4372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Yugabyte provides two implementations `YSQL` and `YCQL`. This module will provide both `YugabyteDBYSQLContainer` and `YugabyteDBYCQLContainer`. Signed-off-by: Srinivasa Vasu Co-authored-by: EddĂș MelĂ©ndez --- .github/ISSUE_TEMPLATE/bug_report.yaml | 1 + .github/ISSUE_TEMPLATE/enhancement.yaml | 1 + .github/ISSUE_TEMPLATE/feature.yaml | 1 + .github/dependabot.yml | 5 + .github/labeler.yml | 2 + docs/modules/databases/yugabytedb.md | 59 +++++++ modules/yugabytedb/build.gradle | 10 ++ .../containers/YugabyteDBYCQLContainer.java | 167 ++++++++++++++++++ .../containers/YugabyteDBYSQLContainer.java | 150 ++++++++++++++++ .../YugabyteDBYSQLContainerProvider.java | 42 +++++ .../delegate/AbstractYCQLDelegate.java | 27 +++ .../delegate/YugabyteDBYCQLDelegate.java | 57 ++++++ .../strategy/YugabyteDBYCQLWaitStrategy.java | 82 +++++++++ .../strategy/YugabyteDBYSQLWaitStrategy.java | 60 +++++++ ...s.containers.JdbcDatabaseContainerProvider | 1 + .../YugabyteDBYSQLJDBCDriverTest.java | 29 +++ .../junit/yugabytedb/YugabyteDBYCQLTest.java | 122 +++++++++++++ .../junit/yugabytedb/YugabyteDBYSQLTest.java | 98 ++++++++++ .../src/test/resources/init/init_yql.sql | 5 + .../src/test/resources/logback-test.xml | 16 ++ 20 files changed, 935 insertions(+) create mode 100644 docs/modules/databases/yugabytedb.md create mode 100644 modules/yugabytedb/build.gradle create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYCQLContainer.java create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainerProvider.java create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/AbstractYCQLDelegate.java create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java create mode 100644 modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java create mode 100644 modules/yugabytedb/src/main/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider create mode 100644 modules/yugabytedb/src/test/java/org/testcontainers/jdbc/yugabytedb/YugabyteDBYSQLJDBCDriverTest.java create mode 100644 modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java create mode 100644 modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java create mode 100644 modules/yugabytedb/src/test/resources/init/init_yql.sql create mode 100644 modules/yugabytedb/src/test/resources/logback-test.xml diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml index 37609ce1424..406483c2d45 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -50,6 +50,7 @@ body: - ToxiProxy - Trino - Vault + - YugabyteDB validations: required: true - type: input diff --git a/.github/ISSUE_TEMPLATE/enhancement.yaml b/.github/ISSUE_TEMPLATE/enhancement.yaml index 2bbca5db3ae..2f543615dee 100644 --- a/.github/ISSUE_TEMPLATE/enhancement.yaml +++ b/.github/ISSUE_TEMPLATE/enhancement.yaml @@ -50,6 +50,7 @@ body: - ToxiProxy - Trino - Vault + - YugabyteDB validations: required: true - type: textarea diff --git a/.github/ISSUE_TEMPLATE/feature.yaml b/.github/ISSUE_TEMPLATE/feature.yaml index d29d0178c39..85c44eba697 100644 --- a/.github/ISSUE_TEMPLATE/feature.yaml +++ b/.github/ISSUE_TEMPLATE/feature.yaml @@ -50,6 +50,7 @@ body: - ToxiProxy - Trino - Vault + - YugabyteDB - New Module - type: textarea id: problem diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 095257df4c1..121b8ab4b46 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -258,6 +258,11 @@ updates: schedule: interval: "monthly" open-pull-requests-limit: 10 + - package-ecosystem: "gradle" + directory: "/modules/yugabytedb" + schedule: + interval: "monthly" + open-pull-requests-limit: 10 # Examples - package-ecosystem: "gradle" diff --git a/.github/labeler.yml b/.github/labeler.yml index fcbe0744bd8..2aff606e27b 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -87,5 +87,7 @@ - modules/trino/**/* "modules/vault": - modules/vault/**/* +"modules/yugabytedb": + - modules/yugabytedb/**/* "type/docs": - docs/**/*.md diff --git a/docs/modules/databases/yugabytedb.md b/docs/modules/databases/yugabytedb.md new file mode 100644 index 00000000000..4ab3436b57c --- /dev/null +++ b/docs/modules/databases/yugabytedb.md @@ -0,0 +1,59 @@ +# YugabyteDB Module + +!!! note + This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See [our contributing guidelines](/contributing/#incubating-modules) for more information on our incubating modules policy. + +See [Database containers](./index.md) for documentation and usage that is common to all database container types. + +YugabyteDB supports two APIs. +- Yugabyte Structured Query Language [YSQL](https://docs.yugabyte.com/latest/api/ysql/) is a fully-relational API that is built by the PostgreSQL code +- Yugabyte Cloud Query Language [YCQL](https://docs.yugabyte.com/latest/api/ycql/) is a semi-relational SQL API that has its roots in the Cassandra Query Language + +## Usage example + +### YSQL API + + +[Creating a YSQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java) inside_block:creatingYSQLContainer + + + + +[Starting a YSQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java) inside_block:startingYSQLContainer + + + +### YCQL API + + +[Creating a YCQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java) inside_block:creatingYCQLContainer + + + + +[Starting a YCQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java) inside_block:startingYCQLContainer + + + +## Adding this module to your project dependencies + +Add the following dependency to your `pom.xml`/`build.gradle` file: + +=== "Gradle" + ```groovy + testImplementation "org.testcontainers:yugabytedb:{{latest_version}}" + ``` +=== "Maven" + ```xml + + org.testcontainers + yugabytedb + {{latest_version}} + test + + ``` + +!!! hint + Adding this Testcontainers library JAR will not automatically add the Yugabytedb driver JAR to your project. + You should ensure that your project has the Yugabytedb driver as a dependency, if you plan on using it. + Refer to the driver page [YSQL](https://docs.yugabyte.com/latest/integrations/jdbc-driver/) and [YCQL](https://docs.yugabyte.com/latest/reference/drivers/ycql-client-drivers/) for instructions. diff --git a/modules/yugabytedb/build.gradle b/modules/yugabytedb/build.gradle new file mode 100644 index 00000000000..8d0ba6ca3d0 --- /dev/null +++ b/modules/yugabytedb/build.gradle @@ -0,0 +1,10 @@ +description = "Testcontainers :: JDBC :: YugabyteDB" + +dependencies { + api project(':jdbc') + testImplementation project(':jdbc-test') + // YCQL driver + testImplementation 'com.yugabyte:java-driver-core:4.6.0-yb-11' + // YSQL driver + testImplementation 'com.yugabyte:jdbc-yugabytedb:42.3.4' +} diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYCQLContainer.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYCQLContainer.java new file mode 100644 index 00000000000..af20ef9e157 --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYCQLContainer.java @@ -0,0 +1,167 @@ +package org.testcontainers.containers; + +import com.github.dockerjava.api.command.InspectContainerResponse; +import org.testcontainers.containers.delegate.YugabyteDBYCQLDelegate; +import org.testcontainers.containers.strategy.YugabyteDBYCQLWaitStrategy; +import org.testcontainers.ext.ScriptUtils; +import org.testcontainers.utility.DockerImageName; + +import java.net.InetSocketAddress; +import java.time.Duration; +import java.util.Collections; +import java.util.Set; + +/** + * Testcontainers implementation for YugabyteDB YCQL API. + * + * @author srinivasa-vasu + * @see YCQL API + */ +public class YugabyteDBYCQLContainer extends GenericContainer { + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("yugabytedb/yugabyte"); + + private static final Integer YCQL_PORT = 9042; + + private static final Integer MASTER_DASHBOARD_PORT = 7000; + + private static final Integer TSERVER_DASHBOARD_PORT = 9000; + + private static final String ENTRYPOINT = "bin/yugabyted start --background=false"; + + private static final String LOCAL_DC = "datacenter1"; + + private String keyspace; + + private String username; + + private String password; + + private String initScript; + + /** + * @param imageName image name + */ + public YugabyteDBYCQLContainer(final String imageName) { + this(DockerImageName.parse(imageName)); + } + + /** + * @param imageName image name + */ + public YugabyteDBYCQLContainer(final DockerImageName imageName) { + super(imageName); + imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); + withExposedPorts(YCQL_PORT, MASTER_DASHBOARD_PORT, TSERVER_DASHBOARD_PORT); + waitingFor(new YugabyteDBYCQLWaitStrategy(this).withStartupTimeout(Duration.ofSeconds(60))); + withCommand(ENTRYPOINT); + } + + @Override + public Set getLivenessCheckPortNumbers() { + return Collections.singleton(getMappedPort(YCQL_PORT)); + } + + /** + * Configures the environment variables. Setting up these variables would create the + * custom objects. Setting {@link #withKeyspaceName(String)}, + * {@link #withUsername(String)}, {@link #withPassword(String)} these parameters will + * initilaize the database with those custom values + */ + @Override + protected void configure() { + addEnv("YCQL_KEYSPACE", keyspace); + addEnv("YCQL_USER", username); + addEnv("YCQL_PASSWORD", password); + } + + /** + * @param initScript path of the initialization script file + * @return {@link YugabyteDBYCQLContainer} instance + */ + public YugabyteDBYCQLContainer withInitScript(String initScript) { + this.initScript = initScript; + return this; + } + + /** + * Setting this would create the keyspace + * @param keyspace keyspace + * @return {@link YugabyteDBYCQLContainer} instance + */ + public YugabyteDBYCQLContainer withKeyspaceName(final String keyspace) { + this.keyspace = keyspace; + return this; + } + + /** + * Setting this would create the custom user role + * @param username user name + * @return {@link YugabyteDBYCQLContainer} instance + */ + public YugabyteDBYCQLContainer withUsername(final String username) { + this.username = username; + return this; + } + + /** + * Setting this along with {@link #withUsername(String)} would enable authentication + * @param password password + * @return {@link YugabyteDBYCQLContainer} instance + */ + public YugabyteDBYCQLContainer withPassword(final String password) { + this.password = password; + return this; + } + + /** + * Executes the initilization script + * @param containerInfo containerInfo + */ + @Override + protected void containerIsStarted(InspectContainerResponse containerInfo) { + if (this.initScript != null) { + ScriptUtils.runInitScript(new YugabyteDBYCQLDelegate(this), initScript); + } + } + + /** + * Returns a {@link InetSocketAddress} representation of YCQL's contact point info + * @return contactpoint + */ + public InetSocketAddress getContactPoint() { + return new InetSocketAddress(getHost(), getMappedPort(YCQL_PORT)); + } + + /** + * Returns the local datacenter name + * @return localdc name + */ + public String getLocalDc() { + return LOCAL_DC; + } + + /** + * Username getter method + * @return username + */ + public String getUsername() { + return this.username; + } + + /** + * Password getter method + * @return password + */ + public String getPassword() { + return this.password; + } + + /** + * Keyspace getter method + * @return keyspace + */ + public String getKeyspace() { + return this.keyspace; + } +} diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java new file mode 100644 index 00000000000..290269dfe79 --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java @@ -0,0 +1,150 @@ +package org.testcontainers.containers; + +import org.testcontainers.containers.strategy.YugabyteDBYSQLWaitStrategy; +import org.testcontainers.utility.DockerImageName; + +import java.time.Duration; +import java.util.Collections; +import java.util.Set; + +/** + * Testcontainers implementation for YugabyteDB YSQL API. + * + * @author srinivasa-vasu + * @see YSQL API + */ + +public class YugabyteDBYSQLContainer extends JdbcDatabaseContainer { + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("yugabytedb/yugabyte"); + + private static final Integer YSQL_PORT = 5433; + + private static final Integer MASTER_DASHBOARD_PORT = 7000; + + private static final Integer TSERVER_DASHBOARD_PORT = 9000; + + private static final String JDBC_DRIVER_CLASS = "com.yugabyte.Driver"; + + private static final String JDBC_CONNECT_PREFIX = "jdbc:yugabytedb"; + + private static final String ENTRYPOINT = "bin/yugabyted start --background=false"; + + private String database = "yugabyte"; + + private String username = "yugabyte"; + + private String password = "yugabyte"; + + /** + * @param imageName image name + */ + public YugabyteDBYSQLContainer(final String imageName) { + this(DockerImageName.parse(imageName)); + } + + /** + * @param imageName image name + */ + 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))); + withCommand(ENTRYPOINT); + } + + @Override + public Set getLivenessCheckPortNumbers() { + return Collections.singleton(getMappedPort(YSQL_PORT)); + } + + /** + * Configures the environment variables. Setting up these variables would create the + * custom objects. Setting {@link #withDatabaseName(String)}, + * {@link #withUsername(String)}, {@link #withPassword(String)} these parameters will + * initilaize the database with those custom values + */ + + @Override + protected void configure() { + addEnv("YSQL_DB", database); + addEnv("YSQL_USER", username); + addEnv("YSQL_PASSWORD", password); + } + + @Override + public String getDriverClassName() { + return JDBC_DRIVER_CLASS; + } + + @Override + public String getJdbcUrl() { + return ( + JDBC_CONNECT_PREFIX + + "://" + + getHost() + + ":" + + getMappedPort(YSQL_PORT) + + "/" + + database + + constructUrlParameters("?", "&") + ); + } + + @Override + public String getDatabaseName() { + return database; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getTestQueryString() { + return "SELECT 1"; + } + + /** + * Setting this would create the keyspace + * @param database database name + * @return {@link YugabyteDBYSQLContainer} instance + */ + + @Override + public YugabyteDBYSQLContainer withDatabaseName(final String database) { + this.database = database; + return this; + } + + /** + * Setting this would create the custom user role + * @param username user name + * @return {@link YugabyteDBYSQLContainer} instance + */ + + @Override + public YugabyteDBYSQLContainer withUsername(final String username) { + this.username = username; + return this; + } + + /** + * Setting this along with {@link #withUsername(String)} would enable authentication + * @param password password + * @return {@link YugabyteDBYSQLContainer} instance + */ + + @Override + public YugabyteDBYSQLContainer withPassword(final String password) { + this.password = password; + return this; + } +} diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainerProvider.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainerProvider.java new file mode 100644 index 00000000000..71d8b583448 --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainerProvider.java @@ -0,0 +1,42 @@ +package org.testcontainers.containers; + +import org.testcontainers.jdbc.ConnectionUrl; +import org.testcontainers.utility.DockerImageName; + +/** + * YugabyteDB YSQL (Structured Query Language) JDBC container provider + * + * @author srinivasa-vasu + */ +public class YugabyteDBYSQLContainerProvider extends JdbcDatabaseContainerProvider { + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("yugabytedb/yugabyte"); + + private static final String DEFAULT_TAG = "2.14.4.0-b26"; + + private static final String NAME = "yugabyte"; + + private static final String USER_PARAM = "user"; + + private static final String PASSWORD_PARAM = "password"; + + @Override + public boolean supports(String databaseType) { + return databaseType.equals(NAME); + } + + @Override + public JdbcDatabaseContainer newInstance() { + return newInstance(DEFAULT_TAG); + } + + @Override + public JdbcDatabaseContainer newInstance(String tag) { + return new YugabyteDBYSQLContainer(DEFAULT_IMAGE_NAME.withTag(tag)); + } + + @Override + public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) { + return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM); + } +} diff --git a/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/AbstractYCQLDelegate.java b/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/AbstractYCQLDelegate.java new file mode 100644 index 00000000000..c0e566c7ecf --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/AbstractYCQLDelegate.java @@ -0,0 +1,27 @@ +package org.testcontainers.containers.delegate; + +import org.testcontainers.delegate.DatabaseDelegate; + +/** + * An abstract delegate do-nothing class + * + * @author srinivasa-vasu + */ +public abstract class AbstractYCQLDelegate implements DatabaseDelegate { + + @Override + public void execute( + String statement, + String scriptPath, + int lineNumber, + boolean continueOnError, + boolean ignoreFailedDrops + ) { + // do nothing + } + + @Override + public void close() { + // do nothing + } +} 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 new file mode 100644 index 00000000000..e324679713b --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java @@ -0,0 +1,57 @@ +package org.testcontainers.containers.delegate; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.testcontainers.containers.Container.ExecResult; +import org.testcontainers.containers.YugabyteDBYCQLContainer; +import org.testcontainers.ext.ScriptUtils.UncategorizedScriptException; + +import java.util.Collection; + +/** + * Query execution delegate class for YCQL API to delegate init-script statements. This + * invokes the in-built ycqlsh cli within the container to execute the + * statements at one go. It is recommended to use frameworks such as liquibase to manage + * this requirement. This functionality is kept to address the initialization requirements + * from standalone services that can't leverage liquibase or something similar. + * + * @author srinivasa-vasu + * @see YugabyteDBYCQLContainer + */ +@RequiredArgsConstructor +@Slf4j +public final class YugabyteDBYCQLDelegate extends AbstractYCQLDelegate { + + private static final String BIN_PATH = "/home/yugabyte/tserver/bin/ycqlsh"; + + private final YugabyteDBYCQLContainer container; + + @Override + public void execute( + Collection statements, + String scriptPath, + boolean continueOnError, + boolean ignoreFailedDrops + ) { + try { + ExecResult result = container.execInContainer( + BIN_PATH, + "-u", + container.getUsername(), + "-p", + container.getPassword(), + "-k", + container.getKeyspace(), + "-e", + StringUtils.join(statements, ";") + ); + if (result.getExitCode() != 0) { + throw new RuntimeException(result.getStderr()); + } + } catch (Exception e) { + log.debug(e.getMessage(), e); + throw new UncategorizedScriptException(e.getMessage(), e); + } + } +} 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 new file mode 100644 index 00000000000..dd775564941 --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java @@ -0,0 +1,82 @@ +package org.testcontainers.containers.strategy; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.testcontainers.containers.Container.ExecResult; +import org.testcontainers.containers.YugabyteDBYCQLContainer; +import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; +import org.testcontainers.containers.wait.strategy.WaitStrategyTarget; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess; + +/** + * Custom wait strategy for YCQL API. + * + *

+ * Though we can either use HTTP or PORT based wait strategy, when we create a custom + * keyspace/role, it gets executed asynchronously. As the wait on container.start() on a + * specific port wouldn't fully guarantee the custom object execution. It's better to + * check the DB status with this way with a smoke test query that uses the underlying + * custom objects and wait for the operation to complete. + *

+ * + * @author srinivasa-vasu + */ +@RequiredArgsConstructor +@Slf4j +public final class YugabyteDBYCQLWaitStrategy extends AbstractWaitStrategy { + + private static final String YCQL_TEST_QUERY = "SELECT release_version FROM system.local"; + + private static final String BIN_PATH = "/home/yugabyte/tserver/bin/ycqlsh"; + + private final WaitStrategyTarget target; + + @Override + public void waitUntilReady(WaitStrategyTarget target) { + YugabyteDBYCQLContainer container = (YugabyteDBYCQLContainer) target; + AtomicBoolean status = new AtomicBoolean(true); + retryUntilSuccess( + (int) startupTimeout.getSeconds(), + TimeUnit.SECONDS, + () -> { + YugabyteDBYCQLWaitStrategy.this.getRateLimiter() + .doWhenReady(() -> { + try { + ExecResult result = container.execInContainer( + BIN_PATH, + "-u", + container.getUsername(), + "-p", + container.getPassword(), + "-k", + container.getKeyspace(), + "-e", + YCQL_TEST_QUERY + ); + if (result.getExitCode() != 0) { + status.set(false); + log.debug(result.getStderr()); + } + } catch (Exception e) { + status.set(false); + log.debug(e.getMessage(), e); + } finally { + if (!status.getAndSet(true)) { + throw new RuntimeException("container hasn't come up yet"); + } + } + }); + return status; + } + ); + } + + @Override + public void waitUntilReady() { + waitUntilReady(target); + } +} 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 new file mode 100644 index 00000000000..31fa52a4155 --- /dev/null +++ b/modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java @@ -0,0 +1,60 @@ +package org.testcontainers.containers.strategy; + +import lombok.RequiredArgsConstructor; +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.util.concurrent.TimeUnit; + +import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess; + +/** + * Custom wait strategy for YSQL API. + * + *

+ * Though we can either use HTTP or PORT based wait strategy, when we create a custom + * database/role, it gets executed asynchronously. As the wait on container.start() on a + * specific port wouldn't fully guarantee the custom object execution. It's better to + * check the DB status with this way with a smoke test query that uses the underlying + * custom objects and wait for the operation to complete. + *

+ * + * @author srinivasa-vasu + */ +@RequiredArgsConstructor +@Slf4j +public final class YugabyteDBYSQLWaitStrategy extends AbstractWaitStrategy { + + private static final String YSQL_TEST_QUERY = "SELECT 1"; + + private final WaitStrategyTarget target; + + @Override + public void waitUntilReady(WaitStrategyTarget target) { + YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) target; + retryUntilSuccess( + (int) startupTimeout.getSeconds(), + TimeUnit.SECONDS, + () -> { + getRateLimiter() + .doWhenReady(() -> { + try (Connection con = container.createConnection(container.getJdbcUrl())) { + con.createStatement().execute(YSQL_TEST_QUERY); + } catch (SQLException ex) { + log.error("Error connecting to the database", ex); + } + }); + return true; + } + ); + } + + @Override + public void waitUntilReady() { + waitUntilReady(target); + } +} diff --git a/modules/yugabytedb/src/main/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider b/modules/yugabytedb/src/main/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider new file mode 100644 index 00000000000..26e372d51ab --- /dev/null +++ b/modules/yugabytedb/src/main/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider @@ -0,0 +1 @@ +org.testcontainers.containers.YugabyteDBYSQLContainerProvider diff --git a/modules/yugabytedb/src/test/java/org/testcontainers/jdbc/yugabytedb/YugabyteDBYSQLJDBCDriverTest.java b/modules/yugabytedb/src/test/java/org/testcontainers/jdbc/yugabytedb/YugabyteDBYSQLJDBCDriverTest.java new file mode 100644 index 00000000000..09c0240e7db --- /dev/null +++ b/modules/yugabytedb/src/test/java/org/testcontainers/jdbc/yugabytedb/YugabyteDBYSQLJDBCDriverTest.java @@ -0,0 +1,29 @@ +package org.testcontainers.jdbc.yugabytedb; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.testcontainers.jdbc.AbstractJDBCDriverTest; + +import java.util.Arrays; +import java.util.EnumSet; + +/** + * YugabyteDB YSQL API JDBC connectivity driver test class + * + * @author srinivasa-vasu + */ +@RunWith(Parameterized.class) +public class YugabyteDBYSQLJDBCDriverTest extends AbstractJDBCDriverTest { + + @Parameterized.Parameters(name = "{index} - {0}") + public static Iterable data() { + return Arrays.asList( + new Object[][] { + { + "jdbc:tc:yugabyte://hostname/yugabyte?user=yugabyte&password=yugabyte", + EnumSet.noneOf(Options.class), + }, + } + ); + } +} 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 new file mode 100644 index 00000000000..3616b87850b --- /dev/null +++ b/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java @@ -0,0 +1,122 @@ +package org.testcontainers.junit.yugabytedb; + +import com.datastax.oss.driver.api.core.CqlSession; +import com.datastax.oss.driver.api.core.cql.ResultSet; +import org.junit.Test; +import org.testcontainers.containers.YugabyteDBYCQLContainer; +import org.testcontainers.utility.DockerImageName; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * YugabyteDB YCQL API unit test class + * + * @author srinivasa-vasu + */ +public class YugabyteDBYCQLTest { + + private static final String IMAGE_NAME = "yugabytedb/yugabyte:2.14.4.0-b26"; + + private static final DockerImageName YBDB_TEST_IMAGE = DockerImageName.parse(IMAGE_NAME); + + @Test + public void testSmoke() { + try ( + // creatingYCQLContainer { + final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(IMAGE_NAME) + // } + ) { + // startingYCQLContainer { + ycqlContainer.start(); + // } + assertThat(performQuery(ycqlContainer, "SELECT release_version FROM system.local").wasApplied()) + .as("A sample test query succeeds") + .isTrue(); + } + } + + @Test + public void testCustomKeyspace() { + String key = "random"; + try ( + final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(YBDB_TEST_IMAGE) + .withKeyspaceName(key) + ) { + ycqlContainer.start(); + assertThat( + performQuery( + ycqlContainer, + "SELECT keyspace_name FROM system_schema.keyspaces where keyspace_name='" + key + "'" + ) + .one() + .getString(0) + ) + .as("Custom keyspace creation succeeds") + .isEqualTo(key); + } + } + + @Test + public void testAuthenticationEnabled() { + String role = "random"; + try ( + final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(YBDB_TEST_IMAGE) + .withUsername(role) + .withPassword(role) + ) { + ycqlContainer.start(); + assertThat( + performQuery(ycqlContainer, "SELECT role FROM system_auth.roles where role='" + role + "'") + .one() + .getString(0) + ) + .as("Keyspace login with authentication enabled succeeds") + .isEqualTo(role); + } + } + + @Test + public void testAuthenticationDisabled() { + try ( + final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(YBDB_TEST_IMAGE) + .withPassword("") + .withUsername("") + ) { + ycqlContainer.start(); + assertThat(performQuery(ycqlContainer, "SELECT release_version FROM system.local").wasApplied()) + .as("Keyspace login with authentication disabled succeeds") + .isTrue(); + } + } + + @Test + public void testInitScript() { + String key = "random"; + try ( + final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(YBDB_TEST_IMAGE) + .withKeyspaceName(key) + .withUsername(key) + .withPassword(key) + .withInitScript("init/init_yql.sql") + ) { + ycqlContainer.start(); + ResultSet output = performQuery(ycqlContainer, "SELECT greet FROM random.dsql"); + assertThat(output.wasApplied()).as("Statements from a custom script execution succeeds").isTrue(); + assertThat(output.one().getString(0)).as("A record match succeeds").isEqualTo("Hello DSQL"); + } + } + + private ResultSet performQuery(YugabyteDBYCQLContainer ycqlContainer, String cql) { + try ( + CqlSession session = CqlSession + .builder() + .withKeyspace(ycqlContainer.getKeyspace()) + .withAuthCredentials(ycqlContainer.getUsername(), ycqlContainer.getPassword()) + .withLocalDatacenter(ycqlContainer.getLocalDc()) + .addContactPoint(ycqlContainer.getContactPoint()) + .build() + ) { + return session.execute(cql); + } + } +} 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 new file mode 100644 index 00000000000..cb98fcdc922 --- /dev/null +++ b/modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java @@ -0,0 +1,98 @@ +package org.testcontainers.junit.yugabytedb; + +import org.junit.Test; +import org.testcontainers.containers.YugabyteDBYSQLContainer; +import org.testcontainers.db.AbstractContainerDatabaseTest; +import org.testcontainers.utility.DockerImageName; + +import java.sql.SQLException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * YugabyteDB YSQL API unit test class + * + * @author srinivasa-vasu + */ +public class YugabyteDBYSQLTest extends AbstractContainerDatabaseTest { + + private static final String IMAGE_NAME = "yugabytedb/yugabyte:2.14.4.0-b26"; + + private static final DockerImageName YBDB_TEST_IMAGE = DockerImageName.parse(IMAGE_NAME); + + @Test + public void testSmoke() throws SQLException { + try ( + // creatingYSQLContainer { + final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(IMAGE_NAME) + // } + ) { + // startingYSQLContainer { + ysqlContainer.start(); + // } + assertThat(performQuery(ysqlContainer, "SELECT 1").getInt(1)) + .as("A sample test query succeeds") + .isEqualTo(1); + } + } + + @Test + public void testCustomDatabase() throws SQLException { + String key = "random"; + try ( + final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE) + .withDatabaseName(key) + ) { + ysqlContainer.start(); + assertThat(performQuery(ysqlContainer, "SELECT 1").getInt(1)) + .as("A test query on a custom database succeeds") + .isEqualTo(1); + } + } + + @Test + public void testInitScript() throws SQLException { + try ( + final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE) + .withInitScript("init/init_yql.sql") + ) { + ysqlContainer.start(); + assertThat(performQuery(ysqlContainer, "SELECT greet FROM dsql").getString(1)) + .as("A record match succeeds") + .isEqualTo("Hello DSQL"); + } + } + + @Test + public void testWithAdditionalUrlParamInJdbcUrl() { + try ( + final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE) + .withUrlParam("sslmode", "disable") + .withUrlParam("application_name", "yugabyte") + ) { + ysqlContainer.start(); + String jdbcUrl = ysqlContainer.getJdbcUrl(); + assertThat(jdbcUrl) + .contains("?") + .contains("&") + .contains("sslmode=disable") + .contains("application_name=yugabyte") + .as("A JDBC connection string with additional parameter validation succeeds"); + } + } + + @Test + public void testWithCustomRole() throws SQLException { + try ( + final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE) + .withDatabaseName("yugabyte") + .withPassword("yugabyte") + .withUsername("yugabyte") + ) { + ysqlContainer.start(); + assertThat(performQuery(ysqlContainer, "SELECT 1").getInt(1)) + .as("A sample test query with a custom role succeeds") + .isEqualTo(1); + } + } +} diff --git a/modules/yugabytedb/src/test/resources/init/init_yql.sql b/modules/yugabytedb/src/test/resources/init/init_yql.sql new file mode 100644 index 00000000000..31cbaa6a758 --- /dev/null +++ b/modules/yugabytedb/src/test/resources/init/init_yql.sql @@ -0,0 +1,5 @@ +CREATE TABLE dsql( + greet text primary key +); + +INSERT INTO dsql (greet) VALUES ('Hello DSQL'); diff --git a/modules/yugabytedb/src/test/resources/logback-test.xml b/modules/yugabytedb/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..83ef7a1a3ef --- /dev/null +++ b/modules/yugabytedb/src/test/resources/logback-test.xml @@ -0,0 +1,16 @@ + + + + + + %d{HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + +