-
-
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.
- Loading branch information
Showing
8 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,8 @@ | ||
description = "Testcontainers :: JDBC :: Presto" | ||
|
||
dependencies { | ||
compile project(':jdbc') | ||
|
||
testCompile 'io.prestosql:presto-jdbc:326' | ||
testCompile 'commons-dbutils:commons-dbutils:1.7' | ||
} |
123 changes: 123 additions & 0 deletions
123
modules/presto/src/main/java/org/testcontainers/containers/PrestoContainer.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,123 @@ | ||
package org.testcontainers.containers; | ||
|
||
import com.google.common.base.Strings; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.testcontainers.containers.wait.LogMessageWaitStrategy; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.time.Duration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
import static com.google.common.base.Strings.nullToEmpty; | ||
import static java.lang.String.format; | ||
import static java.time.temporal.ChronoUnit.SECONDS; | ||
|
||
public class PrestoContainer<SELF extends PrestoContainer<SELF>> extends JdbcDatabaseContainer<SELF> { | ||
public static final String NAME = "presto"; | ||
public static final String IMAGE = "prestosql/presto"; | ||
public static final String DEFAULT_TAG = "326"; | ||
|
||
public static final Integer PRESTO_PORT = 8080; | ||
|
||
private String username = "test"; | ||
private String catalog = null; | ||
|
||
public PrestoContainer() { | ||
this(IMAGE + ":" + DEFAULT_TAG); | ||
} | ||
|
||
public PrestoContainer(final String dockerImageName) { | ||
super(dockerImageName); | ||
this.waitStrategy = new LogMessageWaitStrategy() | ||
.withRegEx(".*io.prestosql.server.PrestoServer\\s+======== SERVER STARTED ========.*") | ||
.withStartupTimeout(Duration.of(60, SECONDS)); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
protected Set<Integer> getLivenessCheckPorts() { | ||
return new HashSet<>(getMappedPort(PRESTO_PORT)); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
addExposedPort(PRESTO_PORT); | ||
} | ||
|
||
@Override | ||
public String getDriverClassName() { | ||
return "io.prestosql.jdbc.PrestoDriver"; | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() { | ||
return format("jdbc:presto://%s:%s/%s", getContainerIpAddress(), getMappedPort(PRESTO_PORT), nullToEmpty(catalog)); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getDatabaseName() { | ||
return catalog; | ||
} | ||
|
||
@Override | ||
public String getTestQueryString() { | ||
return "SELECT 1"; | ||
} | ||
|
||
@Override | ||
public SELF withUsername(final String username) { | ||
this.username = username; | ||
return self(); | ||
} | ||
|
||
/** | ||
* @deprecated This operation is not supported. | ||
*/ | ||
@Override | ||
@Deprecated | ||
public SELF withPassword(final String password) { | ||
// ignored, Presto does not support password authentication without TLS | ||
// TODO: make JDBCDriverTest not pass a password unconditionally and remove this method | ||
return self(); | ||
} | ||
|
||
@Override | ||
public SELF withDatabaseName(String dbName) { | ||
this.catalog = dbName; | ||
return self(); | ||
} | ||
|
||
@Override | ||
protected void waitUntilContainerStarted() { | ||
getWaitStrategy().waitUntilReady(this); | ||
} | ||
|
||
public Connection createConnection() throws SQLException, NoDriverFoundException { | ||
return createConnection(""); | ||
} | ||
|
||
@Override | ||
protected void runInitScriptIfRequired() { | ||
try { | ||
// TODO remove when upgrading to 327 | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException("Interrupted"); | ||
} | ||
super.runInitScriptIfRequired(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
modules/presto/src/main/java/org/testcontainers/containers/PrestoContainerProvider.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,35 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.jdbc.ConnectionUrl; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Factory for Presto containers. | ||
*/ | ||
public class PrestoContainerProvider 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(PrestoContainer.NAME); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance() { | ||
return newInstance(PrestoContainer.DEFAULT_TAG); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(String tag) { | ||
return new PrestoContainer(PrestoContainer.IMAGE + ":" + tag); | ||
} | ||
|
||
@Override | ||
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) { | ||
return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/services/org.testcontainers.containers.JdbcDatabaseContainerProvider
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 @@ | ||
org.testcontainers.containers.PrestoContainerProvider |
72 changes: 72 additions & 0 deletions
72
modules/presto/src/test/java/org/testcontainers/containers/PrestoContainerTest.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,72 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.testcontainers.containers; | ||
|
||
import org.junit.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* @author findepi | ||
*/ | ||
public class PrestoContainerTest { | ||
@Test | ||
public void testSimple() throws Exception { | ||
try (PrestoContainer<?> prestoContainer = new PrestoContainer<>()) { | ||
prestoContainer.start(); | ||
try (Connection connection = prestoContainer.createConnection(); | ||
Statement statement = connection.createStatement(); | ||
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT node_version FROM system.runtime.nodes")) { | ||
assertTrue("No result", resultSet.next()); | ||
assertEquals("Presto version", PrestoContainer.DEFAULT_TAG, resultSet.getString("node_version")); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testSpecificVersion() throws Exception { | ||
String prestoVersion = "325"; | ||
try (PrestoContainer<?> prestoContainer = new PrestoContainer<>("prestosql/presto:" + prestoVersion)) { | ||
prestoContainer.start(); | ||
try (Connection connection = prestoContainer.createConnection(); | ||
Statement statement = connection.createStatement(); | ||
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT node_version FROM system.runtime.nodes")) { | ||
assertTrue("No result", resultSet.next()); | ||
assertEquals("Presto version", "325", resultSet.getString("node_version")); | ||
} | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void testInitScript() throws Exception { | ||
try (PrestoContainer<?> prestoContainer = new PrestoContainer<>()) { | ||
prestoContainer.withInitScript("initial.sql"); | ||
prestoContainer.start(); | ||
try (Connection connection = prestoContainer.createConnection(); | ||
Statement statement = connection.createStatement(); | ||
ResultSet resultSet = statement.executeQuery("SELECT a FROM memory.default.test_table")) { | ||
assertTrue("No result", resultSet.next()); | ||
assertEquals("Value", 12345678909324L, resultSet.getObject("a")); | ||
assertFalse("Too many result", resultSet.next()); | ||
} | ||
} | ||
} | ||
} |
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,2 @@ | ||
CREATE TABLE memory.default.test_table(a bigint); | ||
INSERT INTO memory.default.test_table(a) VALUES (12345678909324); |