Skip to content

Commit a3dc09a

Browse files
author
admitrov
committed
feat: add configurable network alias for PostgreSQL container
- Add `embedded.postgresql.networkAlias` property (default: 'postgresql.testcontainer.docker') - Use configured network alias in Artifactory system.yaml JDBC URL - Add test to verify network alias configuration in system.yaml - Update README documentation with new property
1 parent d968891 commit a3dc09a

8 files changed

Lines changed: 49 additions & 8 deletions

File tree

embedded-artifactory/README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Configure the database via `embedded.postgresql.*` properties:
3131
* `embedded.postgresql.user` `(default is 'postgresql')`
3232
* `embedded.postgresql.password` `(default is 'letmein')`
3333
* `embedded.postgresql.database` `(default is 'test_db')`
34+
* `embedded.postgresql.networkAlias` `(default is 'postgresql.testcontainer.docker')`
3435
* `embedded.postgresql.dockerImage` `(default is 'postgres:18-alpine')`
3536

3637
Example override in `bootstrap.properties`:

embedded-artifactory/src/main/java/com/playtika/testcontainer/artifactory/EmbeddedArtifactoryBootstrapConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public GenericContainer<?> artifactory(ConfigurableEnvironment environment,
8989
WaitStrategy artifactoryWaitStrategy,
9090
Network network) {
9191

92-
String systemYaml = getSystemYaml(postgresqlProperties,postgreSQLContainer);
92+
String systemYaml = getSystemYaml(postgresqlProperties, postgreSQLContainer);
9393

9494
GenericContainer<?> container =
9595
new GenericContainer<>(ContainerUtils.getDockerImageName(properties))
@@ -108,10 +108,10 @@ public GenericContainer<?> artifactory(ConfigurableEnvironment environment,
108108
return container;
109109
}
110110

111-
private static @NonNull String getSystemYaml(PostgreSQLProperties postgresqlProperties,
112-
PostgreSQLContainer postgreSQLContainer) {
111+
static @NonNull String getSystemYaml(PostgreSQLProperties postgresqlProperties,
112+
PostgreSQLContainer postgreSQLContainer) {
113113
String jdbcUrl = "jdbc:postgresql://%s:%d/%s"
114-
.formatted(postgreSQLContainer.getNetwork(), POSTGRESQL_PORT, postgresqlProperties.getDatabase());
114+
.formatted(postgresqlProperties.getNetworkAlias(), POSTGRESQL_PORT, postgresqlProperties.getDatabase());
115115

116116
return """
117117
shared:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.playtika.testcontainer.artifactory;
2+
3+
import com.playtika.testcontainer.postgresql.PostgreSQLProperties;
4+
import org.junit.jupiter.api.Test;
5+
import org.testcontainers.postgresql.PostgreSQLContainer;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class EmbeddedArtifactoryBootstrapConfigurationSystemYamlTest {
10+
11+
@Test
12+
void shouldUseConfiguredPostgresqlNetworkAliasInSystemYaml() {
13+
PostgreSQLProperties postgresqlProperties = new PostgreSQLProperties();
14+
postgresqlProperties.setDatabase("artifactory");
15+
postgresqlProperties.setNetworkAlias("postgresql.internal");
16+
17+
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:18-alpine")
18+
.withNetworkAliases("postgresql.internal")
19+
.withUsername("artifactory")
20+
.withPassword("secret");
21+
22+
String systemYaml = EmbeddedArtifactoryBootstrapConfiguration.getSystemYaml(
23+
postgresqlProperties,
24+
postgreSQLContainer);
25+
26+
assertThat(systemYaml)
27+
.contains("url: \"jdbc:postgresql://postgresql.internal:5432/artifactory\"")
28+
.contains("username: \"artifactory\"")
29+
.contains("password: \"secret\"");
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
embedded.postgresql.user=artifactory
2+
embedded.postgresql.password=artifactory
3+
embedded.postgresql.database=artifactory

embedded-postgresql/README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* `embedded.postgresql.database`
2323
* `embedded.postgresql.user`
2424
* `embedded.postgresql.password`
25+
* `embedded.postgresql.networkAlias` `(default is 'postgresql.testcontainer.docker')`
2526
* `embedded.postgresql.initScriptPath` `(default is null)`
2627
* `embedded.postgresql.startupLogCheckRegex` `(default is null)`
2728
* `embedded.postgresql.mountVolumes` `(default is empty list)`

embedded-postgresql/src/main/java/com/playtika/testcontainer/postgresql/EmbeddedPostgreSQLBootstrapConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
@EnableConfigurationProperties(PostgreSQLProperties.class)
3838
public class EmbeddedPostgreSQLBootstrapConfiguration {
3939

40-
private static final String POSTGRESQL_NETWORK_ALIAS = "postgresql.testcontainer.docker";
41-
4240
@Bean
4341
@ConditionalOnToxiProxyEnabled(module = "postgresql")
4442
ToxiproxyClientProxy postgresqlContainerProxy(ToxiproxyClient toxiproxyClient,
@@ -68,7 +66,7 @@ public PostgreSQLContainer postgresql(ConfigurableEnvironment environment,
6866
.withPassword(properties.getPassword())
6967
.withDatabaseName(properties.getDatabase())
7068
.withInitScript(properties.initScriptPath)
71-
.withNetworkAliases(POSTGRESQL_NETWORK_ALIAS);
69+
.withNetworkAliases(properties.getNetworkAlias());
7270

7371
network.ifPresent(postgresql::withNetwork);
7472

@@ -96,7 +94,7 @@ private void registerPostgresqlEnvironment(PostgreSQLContainer postgresql,
9694
map.put("embedded.postgresql.schema", properties.getDatabase());
9795
map.put("embedded.postgresql.user", properties.getUser());
9896
map.put("embedded.postgresql.password", properties.getPassword());
99-
map.put("embedded.postgresql.networkAlias", POSTGRESQL_NETWORK_ALIAS);
97+
map.put("embedded.postgresql.networkAlias", properties.getNetworkAlias());
10098
map.put("embedded.postgresql.internalPort", PostgreSQLContainer.POSTGRESQL_PORT);
10199

102100
String jdbcURL = "jdbc:postgresql://{}:{}/{}";

embedded-postgresql/src/main/java/com/playtika/testcontainer/postgresql/PostgreSQLProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
@ConfigurationProperties("embedded.postgresql")
1111
public class PostgreSQLProperties extends CommonContainerProperties {
1212
static final String BEAN_NAME_EMBEDDED_POSTGRESQL = "embeddedPostgreSql";
13+
static final String DEFAULT_NETWORK_ALIAS = "postgresql.testcontainer.docker";
1314

1415
String user = "postgresql";
1516
String password = "letmein";
1617
String database = "test_db";
18+
String networkAlias = DEFAULT_NETWORK_ALIAS;
1719
String startupLogCheckRegex;
1820
/**
1921
* The SQL file path to execute after the container starts to initialize the database.

embedded-postgresql/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"type": "java.lang.String",
1818
"defaultValue": "null",
1919
"description": "The SQL file path to execute after the container starts to initialize the database."
20+
},
21+
{
22+
"name": "embedded.postgresql.network-alias",
23+
"type": "java.lang.String",
24+
"defaultValue": "postgresql.testcontainer.docker"
2025
}
2126
],
2227
"hints": [

0 commit comments

Comments
 (0)