Skip to content

Commit fd1dc44

Browse files
committed
Fixed port support
1 parent 1ebae5a commit fd1dc44

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/main/java/com/opentable/db/postgres/embedded/EmbeddedPostgres.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030
import java.util.Objects;
3131
import java.util.Optional;
32+
import java.util.OptionalInt;
3233
import java.util.UUID;
3334

3435
import javax.sql.DataSource;
@@ -47,6 +48,7 @@
4748
* Core class of the library, providing a builder (with reasonable defaults) to wrap
4849
* testcontainers and launch postgres container.
4950
*/
51+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
5052
public class EmbeddedPostgres implements Closeable {
5153
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedPostgres.class);
5254

@@ -74,25 +76,35 @@ public class EmbeddedPostgres implements Closeable {
7476
Map<String, BindMount> bindMounts,
7577
Optional<Network> network,
7678
Optional<String> networkAlias,
79+
OptionalInt fixedPort,
7780
DockerImageName image,
7881
String databaseName
7982
) throws IOException {
80-
this(postgresConfig, localeConfig, bindMounts, network, networkAlias, image, DEFAULT_PG_STARTUP_WAIT, databaseName);
83+
this(postgresConfig, localeConfig, bindMounts, network, networkAlias, fixedPort, image, DEFAULT_PG_STARTUP_WAIT, databaseName);
8184
}
8285

8386
EmbeddedPostgres(Map<String, String> postgresConfig,
8487
Map<String, String> localeConfig,
8588
Map<String, BindMount> bindMounts,
8689
Optional<Network> network,
8790
Optional<String> networkAlias,
91+
OptionalInt fixedPort,
8892
DockerImageName image,
8993
Duration pgStartupWait,
9094
String databaseName
91-
) throws IOException {
95+
) {
9296
LOG.trace("Starting containers with image {}, pgConfig {}, localeConfig {}, bindMounts {}, pgStartupWait {}, dbName {} ", image,
9397
postgresConfig, localeConfig, bindMounts, pgStartupWait, databaseName);
9498
image = image.asCompatibleSubstituteFor(POSTGRES);
95-
this.postgreDBContainer = new PostgreSQLContainer<>(image)
99+
final FixedPostgresSQLContainer pgContainer = new FixedPostgresSQLContainer(image); //NOPMD
100+
fixedPort.ifPresent(p -> {
101+
// This would be exposed via a builder method
102+
LOG.warn("Exposing a fixed port {} which kind of sucks,", p);
103+
pgContainer.addFixedExposedPort(p, POSTGRESQL_PORT);
104+
});
105+
106+
//TODO: generics are still mucked up
107+
this.postgreDBContainer = ((PostgreSQLContainer<?>) pgContainer)
96108
.withDatabaseName(databaseName)
97109
.withUsername(POSTGRES)
98110
.withPassword(POSTGRES)
@@ -225,6 +237,7 @@ public static class Builder {
225237
private DockerImageName image = getDefaultImage();
226238
private String databaseName = POSTGRES;
227239
private Optional<String> networkAlias = Optional.empty();
240+
private OptionalInt fixedPort = OptionalInt.empty();
228241

229242
// See comments at top for the logic.
230243
DockerImageName getDefaultImage() {
@@ -345,6 +358,17 @@ public Builder setImage(DockerImageName image) {
345358
return this;
346359
}
347360

361+
/**
362+
* Force a fixed port on the local side of things. We strongly recommend
363+
* against using this, using ephemeral ports, the default. We reserve the
364+
* right to remove this support.
365+
* @param fixedPort a integer port.
366+
*/
367+
@Deprecated
368+
public void setFixedPort(Integer fixedPort) {
369+
this.fixedPort = OptionalInt.of(fixedPort);
370+
}
371+
348372
/**
349373
* Add the tag to an existing image
350374
* @param tag Tag
@@ -360,7 +384,7 @@ DockerImageName getImage() {
360384
}
361385

362386
public EmbeddedPostgres start() throws IOException {
363-
return new EmbeddedPostgres(config, localeConfig, bindMounts, network, networkAlias, image, pgStartupWait, databaseName);
387+
return new EmbeddedPostgres(config, localeConfig, bindMounts, network, networkAlias, fixedPort, image, pgStartupWait, databaseName);
364388
}
365389

366390
@Override
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 com.opentable.db.postgres.embedded;
15+
16+
import org.testcontainers.containers.PostgreSQLContainer;
17+
import org.testcontainers.utility.DockerImageName;
18+
19+
public class FixedPostgresSQLContainer extends PostgreSQLContainer {
20+
public FixedPostgresSQLContainer(DockerImageName dockerImageName) {
21+
super(dockerImageName);
22+
}
23+
24+
@Override
25+
protected void addFixedExposedPort(int hostPort, int containerPort) {
26+
super.addFixedExposedPort(hostPort, containerPort);
27+
}
28+
}

0 commit comments

Comments
 (0)