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 CrateDB testcontainers #6790

Merged
merged 11 commits into from
Mar 31, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ body:
- CockroachDB
- Consul
- Couchbase
- CrateDB
- DB2
- Dynalite
- Elasticsearch
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/enhancement.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ body:
- CockroachDB
- Consul
- Couchbase
- CrateDB
- DB2
- Dynalite
- Elasticsearch
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ body:
- Cassandra
- Clickhouse
- CockroachDB
- CrateDB
- Consul
- Couchbase
- DB2
Expand Down
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ updates:
schedule:
interval: "monthly"
open-pull-requests-limit: 10
- package-ecosystem: "gradle"
directory: "/modules/cratedb"
schedule:
interval: "monthly"
open-pull-requests-limit: 10
- package-ecosystem: "gradle"
directory: "/modules/database-commons"
schedule:
Expand Down
2 changes: 2 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- modules/consul/**/*
"modules/couchbase":
- modules/couchbase/**/*
"modules/cratedb":
- modules/cratedb/**/*
"modules/db2":
- modules/db2/**/*
"modules/dynalite":
Expand Down
3 changes: 3 additions & 0 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ labels:
- name: modules/couchbase
color: '#006b75'

- name: modules/cratedb
color: '#006b75'

- name: modules/db2
color: '#006b75'

Expand Down
25 changes: 25 additions & 0 deletions docs/modules/databases/cratedb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# CrateDB Module

See [Database containers](./index.md) for documentation and usage that is common to all relational database container types.

## Adding this module to your project dependencies

Add the following dependency to your `pom.xml`/`build.gradle` file:

=== "Gradle"
```groovy
testImplementation "org.testcontainers:cratedb:{{latest_version}}"
```
=== "Maven"
```xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cratedb</artifactId>
<version>{{latest_version}}</version>
<scope>test</scope>
</dependency>
```

!!! hint
Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency.

4 changes: 4 additions & 0 deletions docs/modules/databases/jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database

`jdbc:tc:cockroach:v21.2.3:///databasename`

#### Using CrateDB

`jdbc:tc:cratedb:5.2.3//localhost:5432/crate`

#### Using TiDB

`jdbc:tc:tidb:v6.1.0:///databasename`
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ nav:
- modules/databases/cockroachdb.md
- modules/databases/couchbase.md
- modules/databases/clickhouse.md
- modules/databases/cratedb.md
- modules/databases/db2.md
- modules/databases/dynalite.md
- modules/databases/influxdb.md
Expand Down
13 changes: 13 additions & 0 deletions modules/cratedb/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description = "Testcontainers :: JDBC :: CrateDB"

dependencies {
annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
compileOnly 'com.google.auto.service:auto-service:1.0.1'

api project(':jdbc')

testImplementation project(':jdbc-test')
testImplementation 'org.postgresql:postgresql:42.5.4'

compileOnly 'org.jetbrains:annotations:24.0.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.testcontainers.cratedb;

import org.jetbrains.annotations.NotNull;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

import java.util.Set;

public class CrateDBContainer extends JdbcDatabaseContainer<CrateDBContainer> {

static final String NAME = "cratedb";

static final String IMAGE = "crate";

static final String DEFAULT_TAG = "5.2.5";

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("crate");

static final Integer CRATEDB_PG_PORT = 5432;

static final Integer CRATEDB_HTTP_PORT = 4200;

private String databaseName = "crate";

private String username = "crate";

private String password = "crate";

public CrateDBContainer(final String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

public CrateDBContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

this.waitStrategy = Wait.forHttp("/").forPort(CRATEDB_HTTP_PORT).forStatusCode(200);

addExposedPort(CRATEDB_PG_PORT);
addExposedPort(CRATEDB_HTTP_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return super.getLivenessCheckPorts();
}

@Override
public String getDriverClassName() {
return "org.postgresql.Driver";
}

@Override
public String getJdbcUrl() {
String additionalUrlParams = constructUrlParameters("?", "&");
return (
"jdbc:postgresql://" +
getHost() +
":" +
getMappedPort(CRATEDB_PG_PORT) +
"/" +
databaseName +
additionalUrlParams
);
}

@Override
public String getDatabaseName() {
return databaseName;
}

@Override
public String getUsername() {
return username;
}

@Override
public String getPassword() {
return password;
}

@Override
public String getTestQueryString() {
return "SELECT 1";
}

@Override
public CrateDBContainer withDatabaseName(final String databaseName) {
this.databaseName = databaseName;
return self();
}

@Override
public CrateDBContainer withUsername(final String username) {
this.username = username;
return self();
}

@Override
public CrateDBContainer withPassword(final String password) {
this.password = password;
return self();
}

@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.testcontainers.cratedb;

import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.JdbcDatabaseContainerProvider;
import org.testcontainers.jdbc.ConnectionUrl;
import org.testcontainers.utility.DockerImageName;

/**
* Factory for CrateDB containers using PostgreSQL JDBC driver.
*/
public class CrateDBContainerProvider extends JdbcDatabaseContainerProvider {

public static final String USER_PARAM = "user";

public static final String PASSWORD_PARAM = "password";

@Override
public boolean supports(String databaseType) {
return databaseType.equals(CrateDBContainer.NAME);
}

@Override
public JdbcDatabaseContainer newInstance() {
return newInstance(CrateDBContainer.DEFAULT_TAG);
}

@Override
public JdbcDatabaseContainer newInstance(String tag) {
return new CrateDBContainer(DockerImageName.parse(CrateDBContainer.IMAGE).withTag(tag));
}

@Override
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) {
return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.testcontainers.cratedb.CrateDBContainerProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.testcontainers;

import org.testcontainers.utility.DockerImageName;

public interface CrateDBTestImages {
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
DockerImageName CRATEDB_TEST_IMAGE = DockerImageName.parse("crate:5.2.5");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.testcontainers.jdbc.cratedb;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.jdbc.AbstractJDBCDriverTest;

import java.util.Arrays;
import java.util.EnumSet;

@RunWith(Parameterized.class)
public class CrateDBJDBCDriverTest extends AbstractJDBCDriverTest {

@Parameterized.Parameters(name = "{index} - {0}")
public static Iterable<Object[]> data() {
return Arrays.asList(
new Object[][] {
{ "jdbc:tc:cratedb:5.2.3://hostname/crate?user=crate&password=somepwd", EnumSet.noneOf(Options.class) },
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.testcontainers.junit.cratedb;

import org.junit.Test;
import org.testcontainers.CrateDBTestImages;
import org.testcontainers.cratedb.CrateDBContainer;
import org.testcontainers.db.AbstractContainerDatabaseTest;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.LogManager;

import static org.assertj.core.api.Assertions.assertThat;

public class SimpleCrateDBTest extends AbstractContainerDatabaseTest {
static {
// Postgres JDBC driver uses JUL; disable it to avoid annoying, irrelevant, stderr logs during connection testing
LogManager.getLogManager().getLogger("").setLevel(Level.OFF);
}

@Test
public void testSimple() throws SQLException {
try (CrateDBContainer cratedb = new CrateDBContainer(CrateDBTestImages.CRATEDB_TEST_IMAGE)) {
cratedb.start();

ResultSet resultSet = performQuery(cratedb, "SELECT 1");
int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(cratedb);
}
}

@Test
public void testCommandOverride() throws SQLException {
try (
CrateDBContainer cratedb = new CrateDBContainer(CrateDBTestImages.CRATEDB_TEST_IMAGE)
.withCommand("crate -C cluster.name=testcontainers")
) {
cratedb.start();

ResultSet resultSet = performQuery(cratedb, "select name from sys.cluster");
String result = resultSet.getString(1);
assertThat(result).as("cluster name should be overriden").isEqualTo("testcontainers");
}
}

@Test
public void testExplicitInitScript() throws SQLException {
try (
CrateDBContainer cratedb = new CrateDBContainer(CrateDBTestImages.CRATEDB_TEST_IMAGE)
.withInitScript("somepath/init_cratedb.sql")
) {
cratedb.start();

ResultSet resultSet = performQuery(cratedb, "SELECT foo FROM bar");

String firstColumnValue = resultSet.getString(1);
assertThat(firstColumnValue).as("Value from init script should equal real value").isEqualTo("hello world");
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(CrateDBContainer cratedb) {
assertThat(cratedb.getExposedPorts()).containsExactly(5432, 4200);
assertThat(cratedb.getLivenessCheckPortNumbers())
.containsExactlyInAnyOrder(cratedb.getMappedPort(5432), cratedb.getMappedPort(4200));
}
}
16 changes: 16 additions & 0 deletions modules/cratedb/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE bar (
foo STRING
);

INSERT INTO bar (foo) VALUES ('hello world');
REFRESH TABLE bar;