Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 05c76a8

Browse files
committedMar 7, 2021
Add MariaDB compatibility tests
1 parent 6089601 commit 05c76a8

File tree

4 files changed

+261
-0
lines changed

4 files changed

+261
-0
lines changed
 

‎plugin/trino-mysql/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@
160160
<scope>test</scope>
161161
</dependency>
162162

163+
<!-- org.testcontainers:mariadb depends on the MariaDB driver -->
164+
<dependency>
165+
<groupId>org.mariadb.jdbc</groupId>
166+
<artifactId>mariadb-java-client</artifactId>
167+
<version>2.4.0</version>
168+
<scope>test</scope>
169+
</dependency>
170+
171+
<dependency>
172+
<groupId>org.testcontainers</groupId>
173+
<artifactId>mariadb</artifactId>
174+
<scope>test</scope>
175+
</dependency>
176+
163177
<dependency>
164178
<groupId>org.testcontainers</groupId>
165179
<artifactId>mysql</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 io.trino.plugin.mysql;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import com.google.common.collect.ImmutableMap;
18+
import io.airlift.log.Logger;
19+
import io.airlift.log.Logging;
20+
import io.trino.Session;
21+
import io.trino.plugin.tpch.TpchPlugin;
22+
import io.trino.testing.DistributedQueryRunner;
23+
import io.trino.testing.QueryRunner;
24+
import io.trino.tpch.TpchTable;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import static io.airlift.testing.Closeables.closeAllSuppress;
30+
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
31+
import static io.trino.testing.QueryAssertions.copyTpchTables;
32+
import static io.trino.testing.TestingSession.testSessionBuilder;
33+
34+
public class MariaDBQueryRunner
35+
{
36+
private MariaDBQueryRunner() {}
37+
38+
private static final String TPCH_SCHEMA = "tpch";
39+
40+
public static QueryRunner createMariaDBQueryRunner(TestingMariaDBServer server, TpchTable<?>... tables)
41+
throws Exception
42+
{
43+
return createMariaDBQueryRunner(server, ImmutableMap.of(), ImmutableMap.of(), ImmutableList.copyOf(tables));
44+
}
45+
46+
public static DistributedQueryRunner createMariaDBQueryRunner(
47+
TestingMariaDBServer server,
48+
Map<String, String> extraProperties,
49+
Map<String, String> connectorProperties,
50+
Iterable<TpchTable<?>> tables)
51+
throws Exception
52+
{
53+
DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(createSession())
54+
.setExtraProperties(extraProperties)
55+
.build();
56+
try {
57+
queryRunner.installPlugin(new TpchPlugin());
58+
queryRunner.createCatalog("tpch", "tpch");
59+
60+
connectorProperties = new HashMap<>(ImmutableMap.copyOf(connectorProperties));
61+
connectorProperties.putIfAbsent("connection-url", server.getJdbcUrl());
62+
connectorProperties.putIfAbsent("connection-user", server.getUsername());
63+
connectorProperties.putIfAbsent("connection-password", server.getPassword());
64+
connectorProperties.putIfAbsent("allow-drop-table", "true");
65+
66+
queryRunner.installPlugin(new MySqlPlugin());
67+
queryRunner.createCatalog("mysql", "mysql", connectorProperties);
68+
69+
copyTpchTables(queryRunner, "tpch", TINY_SCHEMA_NAME, createSession(), tables);
70+
71+
return queryRunner;
72+
}
73+
catch (Throwable e) {
74+
closeAllSuppress(e, queryRunner);
75+
throw e;
76+
}
77+
}
78+
79+
private static Session createSession()
80+
{
81+
return testSessionBuilder()
82+
.setCatalog("mysql")
83+
.setSchema(TPCH_SCHEMA)
84+
.build();
85+
}
86+
87+
public static void main(String[] args)
88+
throws Exception
89+
{
90+
Logging.initialize();
91+
92+
DistributedQueryRunner queryRunner = createMariaDBQueryRunner(
93+
new TestingMariaDBServer(),
94+
ImmutableMap.of("http-server.http.port", "8080"),
95+
ImmutableMap.of(),
96+
TpchTable.getTables());
97+
98+
Logger log = Logger.get(MariaDBQueryRunner.class);
99+
log.info("======== SERVER STARTED ========");
100+
log.info("\n====\n%s\n====", queryRunner.getCoordinator().getBaseUrl());
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 io.trino.plugin.mysql;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import io.trino.testing.QueryRunner;
18+
import io.trino.testing.sql.SqlExecutor;
19+
20+
import static io.trino.plugin.mysql.MariaDBQueryRunner.createMariaDBQueryRunner;
21+
22+
public class TestMariaDbCompatibilityTest
23+
extends BaseMySqlConnectorTest
24+
{
25+
private TestingMariaDBServer mariaDBServer;
26+
27+
@Override
28+
protected QueryRunner createQueryRunner()
29+
throws Exception
30+
{
31+
mariaDBServer = closeAfterClass(new TestingMariaDBServer());
32+
return createMariaDBQueryRunner(mariaDBServer, ImmutableMap.of(), ImmutableMap.of(), REQUIRED_TPCH_TABLES);
33+
}
34+
35+
@Override
36+
protected SqlExecutor getMySqlExecutor()
37+
{
38+
return mariaDBServer::execute;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 io.trino.plugin.mysql;
15+
16+
import org.testcontainers.containers.MariaDBContainer;
17+
18+
import java.io.Closeable;
19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
import java.sql.Connection;
22+
import java.sql.DriverManager;
23+
import java.sql.SQLException;
24+
import java.sql.Statement;
25+
26+
import static io.trino.testing.containers.TestContainers.startOrReuse;
27+
import static java.lang.String.format;
28+
29+
public class TestingMariaDBServer
30+
implements AutoCloseable
31+
{
32+
private final MariaDBContainer<?> container;
33+
private final Closeable cleanup;
34+
35+
public TestingMariaDBServer()
36+
{
37+
this("mariadb:10.5.4");
38+
}
39+
40+
public TestingMariaDBServer(String dockerImageName)
41+
{
42+
MariaDBContainer<?> container = new MariaDBContainer<>(dockerImageName);
43+
container = container.withDatabaseName("tpch");
44+
this.container = container;
45+
configureContainer(container);
46+
cleanup = startOrReuse(container);
47+
execute(format("GRANT ALL PRIVILEGES ON *.* TO '%s'", container.getUsername()), "root", container.getPassword());
48+
}
49+
50+
protected void configureContainer(MariaDBContainer<?> container) {}
51+
52+
public Connection createConnection()
53+
throws SQLException
54+
{
55+
return container.createConnection("");
56+
}
57+
58+
public void execute(String sql)
59+
{
60+
execute(sql, getUsername(), getPassword());
61+
}
62+
63+
public void execute(String sql, String user, String password)
64+
{
65+
try (Connection connection = DriverManager.getConnection(getJdbcUrl(), user, password);
66+
Statement statement = connection.createStatement()) {
67+
statement.execute(sql);
68+
}
69+
catch (SQLException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
public String getUsername()
75+
{
76+
return container.getUsername();
77+
}
78+
79+
public String getPassword()
80+
{
81+
return container.getPassword();
82+
}
83+
84+
public String getDatabaseName()
85+
{
86+
return container.getDatabaseName();
87+
}
88+
89+
public String getJdbcUrl()
90+
{
91+
// The connection URL is still using mysql to ensure we test MariaDB compatibility with the MySQL connector
92+
return format("jdbc:mysql://%s:%s?useSSL=false&allowPublicKeyRetrieval=true", container.getContainerIpAddress(), container.getMappedPort(3306));
93+
}
94+
95+
@Override
96+
public void close()
97+
{
98+
try {
99+
cleanup.close();
100+
}
101+
catch (IOException e) {
102+
throw new UncheckedIOException(e);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)
Please sign in to comment.