Skip to content

Commit

Permalink
Allow to reuse few testing servers
Browse files Browse the repository at this point in the history
No plans to reuse containers in CI. This is only for development
purposes. Notice that it not always bring much value and some test do
not play well with reusing as they have side effects. Use it wisely on
your own responsibility.

Locally starting TestingMySqlServer and loading tpch data there takes
over a minute, with reusing it is barely noticeable.

Allow to reuse:
 - TestingSqlServer
 - TestingMySqlServer
  • Loading branch information
kokosing committed Feb 6, 2021
1 parent 4fe08ad commit fbd1de0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@

import org.testcontainers.containers.MySQLContainer;

import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import static io.trino.testing.containers.TestContainers.startOrReuse;
import static java.lang.String.format;
import static org.testcontainers.containers.MySQLContainer.MYSQL_PORT;

public class TestingMySqlServer
implements AutoCloseable
{
private final MySQLContainer<?> container;
private final Closeable cleanup;

public TestingMySqlServer()
{
Expand All @@ -46,7 +51,7 @@ public TestingMySqlServer(String dockerImageName, boolean globalTransactionEnabl
container = container.withCommand("--gtid-mode=ON", "--enforce-gtid-consistency=ON");
}
this.container = container;
container.start();
cleanup = startOrReuse(container);
execute(format("GRANT ALL PRIVILEGES ON *.* TO '%s'", container.getUsername()), "root", container.getPassword());
}

Expand Down Expand Up @@ -89,6 +94,11 @@ public String getJdbcUrl()
@Override
public void close()
{
container.close();
try {
cleanup.close();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
4 changes: 4 additions & 0 deletions plugin/trino-phoenix/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
import org.testcontainers.containers.MSSQLServerContainer;
import org.testcontainers.utility.DockerImageName;

import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.UUID;

import static io.trino.testing.containers.TestContainers.startOrReuse;
import static java.lang.String.format;

public final class TestingSqlServer
Expand All @@ -30,6 +34,7 @@ public final class TestingSqlServer
private final MSSQLServerContainer<?> container;
private final boolean snapshotIsolationEnabled;
private final String databaseName;
private Closeable cleanup = () -> {};

public TestingSqlServer()
{
Expand Down Expand Up @@ -57,7 +62,7 @@ public void execute(String sql)

public void start()
{
container.start();
cleanup = startOrReuse(container);
setUpDatabase();
}

Expand Down Expand Up @@ -91,6 +96,11 @@ private void setUpDatabase()
@Override
public void close()
{
container.close();
try {
cleanup.close();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
5 changes: 5 additions & 0 deletions testing/trino-testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@
<artifactId>jdbi3-core</artifactId>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 io.trino.testing.containers;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.Closeable;

import static com.google.common.base.Preconditions.checkState;
import static java.lang.Boolean.parseBoolean;
import static java.lang.System.getenv;

public final class TestContainers
{
// To reuse container set TESTCONTAINERS_REUSE_ENABLE=true environment variable.
// TESTCONTAINERS_REUSE_ENABLE is an environment variable defined in testcontainers library.
private static final boolean TESTCONTAINERS_REUSE_ENABLE = parseBoolean(getenv("TESTCONTAINERS_REUSE_ENABLE"));

private TestContainers() {}

// You should not close the container directly if you want to reuse it.
// Instead you should close closeable returned by {@link this::startOrReuse}
public static Closeable startOrReuse(GenericContainer<?> container)
{
boolean reuse = TestcontainersConfiguration.getInstance().environmentSupportsReuse();
checkState(reuse == TESTCONTAINERS_REUSE_ENABLE, "Unable to enable or disable container reuse");

container.withReuse(TESTCONTAINERS_REUSE_ENABLE);
container.start();
if (reuse) {
return () -> {};
}
return container::stop;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ public void testWrongType()
{
QueryAssert queryAssert = assertThat(query("SELECT X'001234'"));
assertThatThrownBy(() -> queryAssert.matches("VALUES '001234'"))
.hasMessageContaining("[Output types] \n" +
"Expecting:\n" +
" <[varbinary]>\n" +
"to be equal to:\n" +
" <[varchar(6)]>");
.hasMessageContaining("[Output types] expected:<[var[char(6)]]> but was:<[var[binary]]>");
}

@Test
Expand Down

0 comments on commit fbd1de0

Please sign in to comment.