Skip to content

Fixed port #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* limitations under the License.
*/
package com.opentable.db.postgres.embedded;

// trigger github action test

import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;

Expand All @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.UUID;

import javax.sql.DataSource;
Expand Down Expand Up @@ -65,7 +66,7 @@ public class EmbeddedPostgres implements Closeable {
static final String DOCKER_DEFAULT_TAG = "13-alpine";
// Note you can override any of these defaults explicitly in the builder.

private final PostgreSQLContainer<?> postgreDBContainer;
private final PostgreSQLContainer<?> postgreSQLContainer;

private final UUID instanceId = UUID.randomUUID();

Expand All @@ -74,14 +75,23 @@ public class EmbeddedPostgres implements Closeable {
Map<String, BindMount> bindMounts,
Optional<Network> network,
Optional<String> networkAlias,
OptionalInt fixedPort,
DockerImageName image,
Duration pgStartupWait,
String databaseName
) throws IOException {
LOG.trace("Starting containers with image {}, pgConfig {}, localeConfig {}, bindMounts {}, pgStartupWait {}, dbName {} ", image,
postgresConfig, localeConfig, bindMounts, pgStartupWait, databaseName);
) {
if (LOG.isTraceEnabled()) {
LOG.trace("Starting containers with image {}, pgConfig {}, localeConfig {}, " +
"bindMounts {}, pgStartupWait {}, dbName {} ", image,
postgresConfig, localeConfig, bindMounts, pgStartupWait, databaseName);
}
image = image.asCompatibleSubstituteFor(POSTGRES);
this.postgreDBContainer = new PostgreSQLContainer<>(image)
final FixedPostgresSQLContainer<?> pgContainer = new FixedPostgresSQLContainer<>(image); //NOPMD
fixedPort.ifPresent(p -> {
LOG.warn("Exposing a fixed port {} which is NOT recommended.", p);
pgContainer.addFixedExposedPort(p, POSTGRESQL_PORT);
});
this.postgreSQLContainer = pgContainer
.withDatabaseName(databaseName)
.withUsername(POSTGRES)
.withPassword(POSTGRES)
Expand All @@ -92,11 +102,11 @@ public class EmbeddedPostgres implements Closeable {
.withEnv("POSTGRES_HOST_AUTH_METHOD", "trust");
final List<String> cmd = new ArrayList<>(Collections.singletonList(POSTGRES));
cmd.addAll(createConfigOptions(postgresConfig));
postgreDBContainer.setCommand(cmd.toArray(new String[0]));
processBindMounts(postgreDBContainer, bindMounts);
network.ifPresent(postgreDBContainer::withNetwork);
networkAlias.ifPresent(postgreDBContainer::withNetworkAliases);
postgreDBContainer.start();
postgreSQLContainer.setCommand(cmd.toArray(new String[0]));
processBindMounts(postgreSQLContainer, bindMounts);
network.ifPresent(postgreSQLContainer::withNetwork);
networkAlias.ifPresent(postgreSQLContainer::withNetworkAliases);
postgreSQLContainer.start();
}

private void processBindMounts(PostgreSQLContainer<?> postgreDBContainer, Map<String, BindMount> bindMounts) {
Expand Down Expand Up @@ -125,19 +135,19 @@ private List<String> createInitOptions(final Map<String, String> localeConfig) {
}

public DataSource getTemplateDatabase() {
return getDatabase(postgreDBContainer.getUsername(), "template1");
return getDatabase(postgreSQLContainer.getUsername(), "template1");
}

public DataSource getTemplateDatabase(Map<String, String> properties) {
return getDatabase(postgreDBContainer.getUsername(), "template1", properties);
return getDatabase(postgreSQLContainer.getUsername(), "template1", properties);
}

public DataSource getPostgresDatabase() {
return getDatabase(postgreDBContainer.getUsername(), postgreDBContainer.getDatabaseName());
return getDatabase(postgreSQLContainer.getUsername(), postgreSQLContainer.getDatabaseName());
}

public DataSource getPostgresDatabase(Map<String, String> properties) {
return getDatabase(postgreDBContainer.getUsername(), postgreDBContainer.getDatabaseName(), properties);
return getDatabase(postgreSQLContainer.getUsername(), postgreSQLContainer.getDatabaseName(), properties);
}

public DataSource getDatabase(String userName, String dbName) {
Expand All @@ -147,10 +157,10 @@ public DataSource getDatabase(String userName, String dbName) {
public DataSource getDatabase(String userName, String dbName, Map<String, String> properties) {
final PGSimpleDataSource ds = new PGSimpleDataSource();

ds.setURL(postgreDBContainer.getJdbcUrl());
ds.setURL(postgreSQLContainer.getJdbcUrl());
ds.setDatabaseName(dbName);
ds.setUser(userName);
ds.setPassword(postgreDBContainer.getPassword());
ds.setPassword(postgreSQLContainer.getPassword());

properties.forEach((propertyKey, propertyValue) -> {
try {
Expand All @@ -169,22 +179,22 @@ public DataSource getDatabase(String userName, String dbName, Map<String, String
*/
public String getJdbcUrl(String dbName) {
try {
return JdbcUrlUtils.replaceDatabase(postgreDBContainer.getJdbcUrl(), dbName);
return JdbcUrlUtils.replaceDatabase(postgreSQLContainer.getJdbcUrl(), dbName);
} catch (URISyntaxException e) {
return null;
}
}

public String getHost() {
return postgreDBContainer.getContainerIpAddress();
return postgreSQLContainer.getContainerIpAddress();
}
public int getPort() {
return postgreDBContainer.getMappedPort(POSTGRESQL_PORT);
return postgreSQLContainer.getMappedPort(POSTGRESQL_PORT);
}

@Override
public void close() throws IOException {
postgreDBContainer.close();
postgreSQLContainer.close();
}

public static EmbeddedPostgres start() throws IOException {
Expand All @@ -196,11 +206,11 @@ public static EmbeddedPostgres.Builder builder() {
}

public String getUserName() {
return postgreDBContainer.getUsername();
return postgreSQLContainer.getUsername();
}

public String getPassword() {
return postgreDBContainer.getPassword();
return postgreSQLContainer.getPassword();
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
Expand All @@ -215,6 +225,7 @@ public static class Builder {
private DockerImageName image = getDefaultImage();
private String databaseName = POSTGRES;
private Optional<String> networkAlias = Optional.empty();
private OptionalInt fixedPort = OptionalInt.empty();

// See comments at top for the logic.
DockerImageName getDefaultImage() {
Expand Down Expand Up @@ -335,6 +346,17 @@ public Builder setImage(DockerImageName image) {
return this;
}

/**
* Force a fixed port on the local side of things. We strongly recommend
* against using this, using ephemeral ports, the default. We reserve the
* right to remove this support.
* @param fixedPort a integer port.
*/
@Deprecated
public void setFixedPort(Integer fixedPort) {
this.fixedPort = OptionalInt.of(fixedPort);
}

/**
* Add the tag to an existing image
* @param tag Tag
Expand All @@ -350,7 +372,7 @@ DockerImageName getImage() {
}

public EmbeddedPostgres start() throws IOException {
return new EmbeddedPostgres(config, localeConfig, bindMounts, network, networkAlias, image, pgStartupWait, databaseName);
return new EmbeddedPostgres(config, localeConfig, bindMounts, network, networkAlias, fixedPort, image, pgStartupWait, databaseName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 com.opentable.db.postgres.embedded;

import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

/**
* This class exists only to expose the addFixedExposedPort option.
* Otherwise we'd use PostgreSQLContainer directly.
* @param <SELF> boring generics for fluent api
*/
public class FixedPostgresSQLContainer<SELF extends PostgreSQLContainer<SELF>> extends PostgreSQLContainer<SELF> {
public FixedPostgresSQLContainer(DockerImageName dockerImageName) {
super(dockerImageName);
}

@Override
protected void addFixedExposedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort);
}
}