Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YugabyteDB module #4372

Merged
merged 18 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Expand Up @@ -50,6 +50,7 @@ body:
- ToxiProxy
- Trino
- Vault
- YugabyteDB
validations:
required: true
- type: input
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/enhancement.yaml
Expand Up @@ -50,6 +50,7 @@ body:
- ToxiProxy
- Trino
- Vault
- YugabyteDB
validations:
required: true
- type: textarea
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature.yaml
Expand Up @@ -50,6 +50,7 @@ body:
- ToxiProxy
- Trino
- Vault
- YugabyteDB
- New Module
- type: textarea
id: problem
Expand Down
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions .github/labeler.yml
Expand Up @@ -87,5 +87,7 @@
- modules/trino/**/*
"modules/vault":
- modules/vault/**/*
"modules/yugabytedb":
- modules/yugabytedb/**/*
"type/docs":
- docs/**/*.md
59 changes: 59 additions & 0 deletions 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

<!--codeinclude-->
[Creating a YSQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java) inside_block:creatingYSQLContainer
<!--/codeinclude-->


<!--codeinclude-->
[Starting a YSQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java) inside_block:startingYSQLContainer
<!--/codeinclude-->


### YCQL API

<!--codeinclude-->
[Creating a YCQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java) inside_block:creatingYCQLContainer
<!--/codeinclude-->


<!--codeinclude-->
[Starting a YCQL container](../../../modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java) inside_block:startingYCQLContainer
<!--/codeinclude-->


## 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
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>yugabytedb</artifactId>
<version>{{latest_version}}</version>
<scope>test</scope>
</dependency>
```

!!! 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.
10 changes: 10 additions & 0 deletions 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'
}
@@ -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 <a href="https://docs.yugabyte.com/stable/api/ycql/">YCQL API</a>
*/
public class YugabyteDBYCQLContainer extends GenericContainer<YugabyteDBYCQLContainer> {

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<Integer> 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;
}
}