Skip to content

Commit a85547b

Browse files
committed
Add Presto container
1 parent d013889 commit a85547b

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

modules/jdbc-test/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repositories {
77
dependencies {
88
compile project(':mysql')
99
compile project(':postgresql')
10+
compile project(':presto')
1011
compile project(':cockroachdb')
1112
compile project(':mariadb')
1213
compile project(':oracle-xe')
@@ -16,6 +17,7 @@ dependencies {
1617

1718
testCompile 'com.google.guava:guava:18.0'
1819
testCompile 'org.postgresql:postgresql:42.0.0'
20+
testCompile 'io.prestosql:presto-jdbc:326'
1921
testCompile 'mysql:mysql-connector-java:8.0.14'
2022
testCompile 'org.mariadb.jdbc:mariadb-java-client:1.4.6'
2123
testCompile 'com.oracle:ojdbc6:12.1.0.1-atlassian-hosted'

modules/jdbc-test/src/test/java/org/testcontainers/jdbc/JDBCDriverTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static Iterable<Object[]> data() {
5353
{"jdbc:tc:postgresql:9.6.8://hostname/databasename?user=someuser&password=somepwd", EnumSet.of(Options.JDBCParams)},
5454
{"jdbc:tc:postgis://hostname/databasename?user=someuser&password=somepwd", EnumSet.of(Options.JDBCParams)},
5555
{"jdbc:tc:postgis:9.6://hostname/databasename?user=someuser&password=somepwd", EnumSet.of(Options.JDBCParams)},
56+
{"jdbc:tc:presto:326://hostname/", EnumSet.of(Options.PmdKnownBroken)},
5657
{"jdbc:tc:mysql:5.6://hostname/databasename?TC_MY_CNF=somepath/mysql_conf_override", EnumSet.of(Options.CustomIniFile)},
5758
{"jdbc:tc:mariadb://hostname/databasename", EnumSet.noneOf(Options.class)},
5859
{"jdbc:tc:mariadb://hostname/databasename?user=someuser&TC_INITSCRIPT=somepath/init_mariadb.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},

modules/presto/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
description = "Testcontainers :: JDBC :: Presto"
2+
3+
dependencies {
4+
compile project(':jdbc')
5+
6+
testCompile 'io.prestosql:presto-jdbc:326'
7+
testCompile 'commons-dbutils:commons-dbutils:1.7'
8+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.testcontainers.containers;
2+
3+
import com.google.common.base.Strings;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
6+
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
import java.time.Duration;
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
import static com.google.common.base.Strings.isNullOrEmpty;
14+
import static com.google.common.base.Strings.nullToEmpty;
15+
import static java.lang.String.format;
16+
import static java.time.temporal.ChronoUnit.SECONDS;
17+
18+
public class PrestoContainer<SELF extends PrestoContainer<SELF>> extends JdbcDatabaseContainer<SELF> {
19+
public static final String NAME = "presto";
20+
public static final String IMAGE = "prestosql/presto";
21+
public static final String DEFAULT_TAG = "326";
22+
23+
public static final Integer PRESTO_PORT = 8080;
24+
25+
private String username = "test";
26+
private String password = "";
27+
private String catalog = null;
28+
29+
public PrestoContainer() {
30+
this(IMAGE + ":" + DEFAULT_TAG);
31+
}
32+
33+
public PrestoContainer(final String dockerImageName) {
34+
super(dockerImageName);
35+
this.waitStrategy = new LogMessageWaitStrategy()
36+
.withRegEx(".*io.prestosql.server.PrestoServer\\s+======== SERVER STARTED ========.*")
37+
.withStartupTimeout(Duration.of(60, SECONDS));
38+
}
39+
40+
@NotNull
41+
@Override
42+
protected Set<Integer> getLivenessCheckPorts() {
43+
return new HashSet<>(getMappedPort(PRESTO_PORT));
44+
}
45+
46+
@Override
47+
protected void configure() {
48+
addExposedPort(PRESTO_PORT);
49+
}
50+
51+
@Override
52+
public String getDriverClassName() {
53+
return "io.prestosql.jdbc.PrestoDriver";
54+
}
55+
56+
@Override
57+
public String getJdbcUrl() {
58+
return format("jdbc:presto://%s:%s/%s", getContainerIpAddress(), getMappedPort(PRESTO_PORT), nullToEmpty(catalog));
59+
}
60+
61+
@Override
62+
public String getUsername() {
63+
return username;
64+
}
65+
66+
@Override
67+
public String getPassword() {
68+
return password;
69+
}
70+
71+
@Override
72+
public String getDatabaseName() {
73+
return catalog;
74+
}
75+
76+
@Override
77+
public String getTestQueryString() {
78+
return "SELECT 1";
79+
}
80+
81+
@Override
82+
public SELF withUsername(final String username) {
83+
this.username = username;
84+
return self();
85+
}
86+
87+
@Override
88+
public SELF withPassword(final String password) {
89+
// ignored, Presto does not support password authentication without TLS
90+
return self();
91+
}
92+
93+
@Override
94+
public SELF withDatabaseName(String dbName) {
95+
this.catalog = dbName;
96+
return self();
97+
}
98+
99+
@Override
100+
protected void waitUntilContainerStarted() {
101+
getWaitStrategy().waitUntilReady(this);
102+
}
103+
104+
public Connection createConnection() throws SQLException, NoDriverFoundException {
105+
return createConnection("");
106+
}
107+
108+
@Override
109+
protected void runInitScriptIfRequired() {
110+
try {
111+
// TODO remove when upgrading to 327
112+
Thread.sleep(5000);
113+
} catch (InterruptedException e) {
114+
Thread.currentThread().interrupt();
115+
throw new RuntimeException("Interrupted");
116+
}
117+
super.runInitScriptIfRequired();
118+
}
119+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.testcontainers.containers;
2+
3+
import org.testcontainers.jdbc.ConnectionUrl;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* Factory for Presto containers.
9+
*/
10+
public class PrestoContainerProvider extends JdbcDatabaseContainerProvider {
11+
12+
public static final String USER_PARAM = "user";
13+
public static final String PASSWORD_PARAM = "password";
14+
15+
@Override
16+
public boolean supports(String databaseType) {
17+
return databaseType.equals(PrestoContainer.NAME);
18+
}
19+
20+
@Override
21+
public JdbcDatabaseContainer newInstance() {
22+
return newInstance(PrestoContainer.DEFAULT_TAG);
23+
}
24+
25+
@Override
26+
public JdbcDatabaseContainer newInstance(String tag) {
27+
return new PrestoContainer(PrestoContainer.IMAGE + ":" + tag);
28+
}
29+
30+
@Override
31+
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl) {
32+
return newInstanceFromConnectionUrl(connectionUrl, USER_PARAM, PASSWORD_PARAM);
33+
}
34+
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.testcontainers.containers.PrestoContainerProvider
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.testcontainers.containers;
15+
16+
import org.junit.Test;
17+
18+
import java.sql.Connection;
19+
import java.sql.ResultSet;
20+
import java.sql.Statement;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertTrue;
25+
26+
/**
27+
* @author findepi
28+
*/
29+
public class PrestoContainerTest {
30+
@Test
31+
public void testSimple() throws Exception {
32+
try (PrestoContainer<?> prestoContainer = new PrestoContainer<>()) {
33+
prestoContainer.start();
34+
try (Connection connection = prestoContainer.createConnection();
35+
Statement statement = connection.createStatement();
36+
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT node_version FROM system.runtime.nodes")) {
37+
assertTrue("No result", resultSet.next());
38+
assertEquals("Presto version", PrestoContainer.DEFAULT_TAG, resultSet.getString("node_version"));
39+
}
40+
}
41+
}
42+
43+
@Test
44+
public void testSpecificVersion() throws Exception {
45+
String prestoVersion = "325";
46+
try (PrestoContainer<?> prestoContainer = new PrestoContainer<>("prestosql/presto:" + prestoVersion)) {
47+
prestoContainer.start();
48+
try (Connection connection = prestoContainer.createConnection();
49+
Statement statement = connection.createStatement();
50+
ResultSet resultSet = statement.executeQuery("SELECT DISTINCT node_version FROM system.runtime.nodes")) {
51+
assertTrue("No result", resultSet.next());
52+
assertEquals("Presto version", "325", resultSet.getString("node_version"));
53+
}
54+
}
55+
}
56+
57+
58+
@Test
59+
public void testInitScript() throws Exception {
60+
try (PrestoContainer<?> prestoContainer = new PrestoContainer<>()) {
61+
prestoContainer.withInitScript("initial.sql");
62+
prestoContainer.start();
63+
try (Connection connection = prestoContainer.createConnection();
64+
Statement statement = connection.createStatement();
65+
ResultSet resultSet = statement.executeQuery("SELECT a FROM memory.default.test_table")) {
66+
assertTrue("No result", resultSet.next());
67+
assertEquals("Value", 12345678909324L, resultSet.getObject("a"));
68+
assertFalse("Too many result", resultSet.next());
69+
}
70+
}
71+
}
72+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE memory.default.test_table(a bigint);
2+
INSERT INTO memory.default.test_table(a) VALUES (12345678909324);

0 commit comments

Comments
 (0)