Skip to content

Commit

Permalink
Introduce builder for DockerContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
kokosing committed Sep 11, 2019
1 parent b1e12fa commit d99e5b3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.prestosql.plugin.mysql;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.prestosql.testing.docker.DockerContainer;
import io.prestosql.testing.docker.DockerContainer.HostPortProvider;
Expand All @@ -40,16 +39,15 @@ public class DockerizedMySqlServer

public DockerizedMySqlServer()
{
this.dockerContainer = new DockerContainer(
"mysql:8.0.15",
ImmutableList.of(MYSQL_PORT),
ImmutableMap.of(
this.dockerContainer = DockerContainer.forImage("mysql:8.0.15")
.setPorts(MYSQL_PORT)
.setEnvironment(ImmutableMap.of(
"MYSQL_ROOT_PASSWORD", MYSQL_ROOT_PASSWORD,
"MYSQL_USER", MYSQL_USER,
"MYSQL_PASSWORD", MYSQL_PASSWORD,
"MYSQL_DATABASE", "tpch"),
ImmutableList.of(),
DockerizedMySqlServer::healthCheck);
"MYSQL_DATABASE", "tpch"))
.setHealthCheck(DockerizedMySqlServer::healthCheck)
.start();
}

private static void healthCheck(HostPortProvider hostPortProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.prestosql.plugin.sqlserver;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.prestosql.testing.docker.DockerContainer;
import io.prestosql.testing.docker.DockerContainer.HostPortProvider;
Expand All @@ -37,14 +36,13 @@ public final class TestingSqlServer

public TestingSqlServer()
{
this.dockerContainer = new DockerContainer(
"microsoft/mssql-server-linux:2017-CU13",
ImmutableList.of(SQL_SERVER_PORT),
ImmutableMap.of(
this.dockerContainer = DockerContainer.forImage("microsoft/mssql-server-linux:2017-CU13")
.setPorts(SQL_SERVER_PORT)
.setEnvironment(ImmutableMap.of(
"ACCEPT_EULA", "Y",
"SA_PASSWORD", "SQLServerPass1"),
ImmutableList.of(),
portProvider -> TestingSqlServer.execute(portProvider, "SELECT 1"));
"SA_PASSWORD", "SQLServerPass1"))
.setHealthCheck(portProvider -> TestingSqlServer.execute(portProvider, "SELECT 1"))
.start();
}

public void execute(String sql)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.MoreCollectors.toOptional;
import static com.google.common.primitives.Ints.asList;
import static io.airlift.testing.Closeables.closeAllSuppress;
import static java.lang.String.format;
import static java.time.temporal.ChronoUnit.MINUTES;
Expand All @@ -65,7 +66,7 @@ public final class DockerContainer

private Map<Integer, Integer> hostPorts;

public DockerContainer(String image, List<Integer> ports, Map<String, String> environment, List<String> capabilities, CheckedConsumer<HostPortProvider> healthCheck)
private DockerContainer(String image, List<Integer> ports, Map<String, String> environment, List<String> capabilities, CheckedConsumer<HostPortProvider> healthCheck)
{
this.image = requireNonNull(image, "image is null");
this.environment = ImmutableMap.copyOf(requireNonNull(environment, "environment is null"));
Expand Down Expand Up @@ -253,4 +254,52 @@ public interface HostPortProvider
{
int getHostPort(int containerPort);
}

public static Builder forImage(String image)
{
return new Builder(image);
}

public static class Builder
{
private final String image;
private List<Integer> ports = ImmutableList.of();
private Map<String, String> environment = ImmutableMap.of();
private List<String> capabilities = ImmutableList.of();
private CheckedConsumer<HostPortProvider> healthCheck = hostPortProvider -> {};

private Builder(String image)
{
this.image = requireNonNull(image, "image is null");
}

public Builder setPorts(int... ports)
{
this.ports = ImmutableList.copyOf(asList(requireNonNull(ports, "ports is null")));
return this;
}

public Builder setEnvironment(Map<String, String> environment)
{
this.environment = ImmutableMap.copyOf(requireNonNull(environment, "environment is null"));
return this;
}

public Builder setCapabilities(String... capabilities)
{
this.capabilities = ImmutableList.copyOf(requireNonNull(capabilities, "capabilities is null"));
return this;
}

public Builder setHealthCheck(CheckedConsumer<HostPortProvider> healthCheck)
{
this.healthCheck = requireNonNull(healthCheck, "healthCheck is null");
return this;
}

public DockerContainer start()
{
return new DockerContainer(image, ports, environment, capabilities, healthCheck);
}
}
}

0 comments on commit d99e5b3

Please sign in to comment.