-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new ClickHouseContainer implementation (#7403)
New implementation supports `withUsername`, `withPassword`, `withDatabaseName` and `withUrlParam()`.
- Loading branch information
1 parent
8c49699
commit fb2fbbc
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
modules/clickhouse/src/main/java/org/testcontainers/clickhouse/ClickHouseContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.testcontainers.clickhouse; | ||
|
||
import org.testcontainers.containers.JdbcDatabaseContainer; | ||
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.time.Duration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Testcontainers implementation for ClickHouse. | ||
*/ | ||
public class ClickHouseContainer extends JdbcDatabaseContainer<ClickHouseContainer> { | ||
|
||
private static final String NAME = "clickhouse"; | ||
|
||
private static final DockerImageName CLICKHOUSE_IMAGE_NAME = DockerImageName.parse("clickhouse/clickhouse-server"); | ||
|
||
private static final Integer HTTP_PORT = 8123; | ||
|
||
private static final Integer NATIVE_PORT = 9000; | ||
|
||
private static final String DRIVER_CLASS_NAME = "com.clickhouse.jdbc.ClickHouseDriver"; | ||
|
||
private static final String JDBC_URL_PREFIX = "jdbc:" + NAME + "://"; | ||
|
||
private static final String TEST_QUERY = "SELECT 1"; | ||
|
||
private String databaseName = "default"; | ||
|
||
private String username = "default"; | ||
|
||
private String password = ""; | ||
|
||
public ClickHouseContainer(String dockerImageName) { | ||
this(DockerImageName.parse(dockerImageName)); | ||
} | ||
|
||
public ClickHouseContainer(final DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
dockerImageName.assertCompatibleWith(CLICKHOUSE_IMAGE_NAME); | ||
|
||
addExposedPorts(HTTP_PORT, NATIVE_PORT); | ||
this.waitStrategy = | ||
new HttpWaitStrategy() | ||
.forStatusCode(200) | ||
.forResponsePredicate("Ok."::equals) | ||
.withStartupTimeout(Duration.ofMinutes(1)); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
withEnv("CLICKHOUSE_DB", this.databaseName); | ||
withEnv("CLICKHOUSE_USER", this.username); | ||
withEnv("CLICKHOUSE_PASSWORD", this.password); | ||
} | ||
|
||
@Override | ||
public Set<Integer> getLivenessCheckPortNumbers() { | ||
return new HashSet<>(getMappedPort(HTTP_PORT)); | ||
} | ||
|
||
@Override | ||
public String getDriverClassName() { | ||
return DRIVER_CLASS_NAME; | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return ( | ||
JDBC_URL_PREFIX + | ||
getHost() + | ||
":" + | ||
getMappedPort(HTTP_PORT) + | ||
"/" + | ||
this.databaseName + | ||
constructUrlParameters("?", "&") | ||
); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public String getTestQueryString() { | ||
return TEST_QUERY; | ||
} | ||
|
||
@Override | ||
public ClickHouseContainer withUsername(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClickHouseContainer withPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClickHouseContainer withDatabaseName(String databaseName) { | ||
this.databaseName = databaseName; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
modules/clickhouse/src/test/java/org/testcontainers/clickhouse/ClickHouseContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.testcontainers.clickhouse; | ||
|
||
import org.junit.Test; | ||
import org.testcontainers.db.AbstractContainerDatabaseTest; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ClickHouseContainerTest extends AbstractContainerDatabaseTest { | ||
|
||
@Test | ||
public void testSimple() throws SQLException { | ||
try (ClickHouseContainer clickhouse = new ClickHouseContainer("clickhouse/clickhouse-server:21.9.2-alpine")) { | ||
clickhouse.start(); | ||
|
||
ResultSet resultSet = performQuery(clickhouse, "SELECT 1"); | ||
|
||
int resultSetInt = resultSet.getInt(1); | ||
assertThat(resultSetInt).isEqualTo(1); | ||
} | ||
} | ||
|
||
@Test | ||
public void customCredentialsWithUrlParams() throws SQLException { | ||
try ( | ||
ClickHouseContainer clickhouse = new ClickHouseContainer("clickhouse/clickhouse-server:21.9.2-alpine") | ||
.withUsername("test") | ||
.withPassword("test") | ||
.withDatabaseName("test") | ||
.withUrlParam("max_result_rows", "5") | ||
) { | ||
clickhouse.start(); | ||
|
||
ResultSet resultSet = performQuery( | ||
clickhouse, | ||
"SELECT value FROM system.settings where name='max_result_rows'" | ||
); | ||
|
||
int resultSetInt = resultSet.getInt(1); | ||
assertThat(resultSetInt).isEqualTo(5); | ||
} | ||
} | ||
} |