Skip to content

Commit d968891

Browse files
author
admitrov
committed
chore: migrate embedded-artifactory to use embedded-postgresql module
- Replace direct PostgreSQL container with embedded-postgresql dependency - Remove PostgreSQLProperties class in favor of shared module - Add system.yaml configuration for database connection - Increase default wait timeout from 120 to 300 seconds - Update README with PostgreSQL configuration options
1 parent e242801 commit d968891

7 files changed

Lines changed: 68 additions & 60 deletions

File tree

embedded-artifactory/README.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323
* `embedded.artifactory.password` `(default is 'password')`
2424
* `embedded.toxiproxy.proxies.artifactory.enabled` Enables both creation of the container with ToxiProxy TCP proxy and a proxy to the `embedded-artifactory` container.
2525

26+
==== PostgreSQL backing store
27+
28+
Artifactory uses PostgreSQL as its backing database, provided by the `embedded-postgresql` module.
29+
Configure the database via `embedded.postgresql.*` properties:
30+
31+
* `embedded.postgresql.user` `(default is 'postgresql')`
32+
* `embedded.postgresql.password` `(default is 'letmein')`
33+
* `embedded.postgresql.database` `(default is 'test_db')`
34+
* `embedded.postgresql.dockerImage` `(default is 'postgres:18-alpine')`
35+
36+
Example override in `bootstrap.properties`:
37+
[source,properties]
38+
----
39+
embedded.postgresql.user=artifactory
40+
embedded.postgresql.password=artifactory
41+
embedded.postgresql.database=artifactory
42+
----
43+
2644
==== Produces
2745

2846
* `embedded.artifactory.host`

embedded-artifactory/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
<artifactId>embedded-toxiproxy</artifactId>
2525
</dependency>
2626
<dependency>
27-
<groupId>org.testcontainers</groupId>
28-
<artifactId>postgresql</artifactId>
27+
<groupId>com.playtika.testcontainers</groupId>
28+
<artifactId>embedded-postgresql</artifactId>
2929
</dependency>
3030

3131
<dependency>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ArtifactoryProperties extends CommonContainerProperties {
2020
int generalPort = 8082;
2121

2222
public ArtifactoryProperties() {
23-
setWaitTimeoutInSeconds(120);
23+
setWaitTimeoutInSeconds(300);
2424
}
2525

2626
@Override

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

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration;
44
import com.playtika.testcontainer.common.utils.ContainerUtils;
5+
import com.playtika.testcontainer.postgresql.EmbeddedPostgreSQLBootstrapConfiguration;
6+
import com.playtika.testcontainer.postgresql.PostgreSQLProperties;
57
import com.playtika.testcontainer.toxiproxy.ToxiproxyClientProxy;
68
import com.playtika.testcontainer.toxiproxy.ToxiproxyHelper;
79
import com.playtika.testcontainer.toxiproxy.condition.ConditionalOnToxiProxyEnabled;
810
import eu.rekawek.toxiproxy.ToxiproxyClient;
911
import lombok.extern.slf4j.Slf4j;
12+
import org.jspecify.annotations.NonNull;
1013
import org.springframework.beans.factory.annotation.Qualifier;
1114
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
1215
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
@@ -19,28 +22,37 @@
1922
import org.springframework.core.env.MapPropertySource;
2023
import org.testcontainers.containers.GenericContainer;
2124
import org.testcontainers.containers.Network;
22-
import org.testcontainers.containers.PostgreSQLContainer;
23-
import org.testcontainers.containers.ToxiproxyContainer;
2425
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
2526
import org.testcontainers.containers.wait.strategy.WaitStrategy;
27+
import org.testcontainers.images.builder.Transferable;
28+
import org.testcontainers.postgresql.PostgreSQLContainer;
2629
import org.testcontainers.toxiproxy.ToxiproxyContainer;
2730

31+
import java.nio.charset.StandardCharsets;
2832
import java.util.LinkedHashMap;
29-
import java.util.Optional;
3033

3134
import static com.playtika.testcontainer.artifactory.ArtifactoryProperties.ARTIFACTORY_BEAN_NAME;
3235
import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart;
36+
import static org.testcontainers.postgresql.PostgreSQLContainer.POSTGRESQL_PORT;
3337

3438
@Slf4j
3539
@Configuration
3640
@ConditionalOnExpression("${embedded.containers.enabled:true}")
37-
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
41+
@AutoConfigureAfter({DockerPresenceBootstrapConfiguration.class, EmbeddedPostgreSQLBootstrapConfiguration.class})
3842
@ConditionalOnProperty(name = "embedded.artifactory.enabled", matchIfMissing = true)
39-
@EnableConfigurationProperties({ArtifactoryProperties.class, PostgreSQLProperties.class})
43+
@EnableConfigurationProperties(ArtifactoryProperties.class)
4044
public class EmbeddedArtifactoryBootstrapConfiguration {
4145

4246
private static final String ARTIFACTORY_NETWORK_ALIAS = "artifactory.testcontainer.docker";
4347

48+
@Bean
49+
@ConditionalOnMissingBean(Network.class)
50+
Network artifactoryNetwork() {
51+
Network network = Network.newNetwork();
52+
log.info("Created docker Network with id={}", network.getId());
53+
return network;
54+
}
55+
4456
@Bean
4557
@ConditionalOnMissingBean(name = "artifactoryWaitStrategy")
4658
public WaitStrategy artifactoryWaitStrategy(ArtifactoryProperties properties) {
@@ -72,43 +84,46 @@ ToxiproxyClientProxy artifactoryContainerProxy(ToxiproxyClient toxiproxyClient,
7284
@Bean(name = ARTIFACTORY_BEAN_NAME, destroyMethod = "stop")
7385
public GenericContainer<?> artifactory(ConfigurableEnvironment environment,
7486
ArtifactoryProperties properties,
87+
PostgreSQLContainer postgreSQLContainer,
7588
PostgreSQLProperties postgresqlProperties,
7689
WaitStrategy artifactoryWaitStrategy,
77-
Optional<Network> network) {
90+
Network network) {
7891

79-
PostgreSQLContainer postgresql =
80-
new PostgreSQLContainer<>(ContainerUtils.getDockerImageName(postgresqlProperties))
81-
.withNetwork(Network.SHARED)
82-
.withUsername(postgresqlProperties.getUser())
83-
.withPassword(postgresqlProperties.getPassword())
84-
.withDatabaseName(postgresqlProperties.getDatabase())
85-
.withInitScript(postgresqlProperties.initScriptPath)
86-
.withNetworkAliases(properties.getNetworkAlias(), ARTIFACTORY_NETWORK_ALIAS)
87-
.withNetworkAliases(ARTIFACTORY_NETWORK_ALIAS);
88-
89-
network.ifPresent(postgresql::withNetwork);
90-
configureCommonsAndStart(postgresql, postgresqlProperties, log);
92+
String systemYaml = getSystemYaml(postgresqlProperties,postgreSQLContainer);
9193

9294
GenericContainer<?> container =
9395
new GenericContainer<>(ContainerUtils.getDockerImageName(properties))
9496
.withExposedPorts(properties.getRestApiPort(), properties.getGeneralPort())
95-
.withNetwork(Network.SHARED)
97+
.withNetwork(network)
9698
.withNetworkAliases(properties.getNetworkAlias(), ARTIFACTORY_NETWORK_ALIAS)
97-
.withEnv("username", postgresqlProperties.user)
98-
.withEnv("password", postgresqlProperties.password)
99-
.withEnv("url", "jdbc:postgresql://localhost:5432/" + postgresqlProperties.database)
100-
.withEnv("type", "postgresql")
101-
.withEnv("driver", "org.postgresql.Driver")
99+
.withCopyToContainer(
100+
Transferable.of(systemYaml.getBytes(StandardCharsets.UTF_8), 0666),
101+
"/opt/jfrog/artifactory/var/etc/system.yaml")
102102
.waitingFor(artifactoryWaitStrategy);
103103

104-
network.ifPresent(container::withNetwork);
105104
configureCommonsAndStart(container, properties, log);
106105

107106
registerEnvironment(container, environment, properties);
108107

109108
return container;
110109
}
111110

111+
private static @NonNull String getSystemYaml(PostgreSQLProperties postgresqlProperties,
112+
PostgreSQLContainer postgreSQLContainer) {
113+
String jdbcUrl = "jdbc:postgresql://%s:%d/%s"
114+
.formatted(postgreSQLContainer.getNetwork(), POSTGRESQL_PORT, postgresqlProperties.getDatabase());
115+
116+
return """
117+
shared:
118+
database:
119+
type: "postgresql"
120+
driver: "org.postgresql.Driver"
121+
url: "%s"
122+
username: "%s"
123+
password: "%s"
124+
""".formatted(jdbcUrl, postgreSQLContainer.getUsername(), postgreSQLContainer.getPassword());
125+
}
126+
112127
private void registerEnvironment(GenericContainer<?> artifactory,
113128
ConfigurableEnvironment environment,
114129
ArtifactoryProperties properties) {

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import org.springframework.core.env.MapPropertySource;
1919
import org.springframework.util.StringUtils;
2020
import org.testcontainers.containers.Network;
21-
import org.testcontainers.containers.PostgreSQLContainer;
2221
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
2322
import org.testcontainers.containers.wait.strategy.WaitStrategy;
23+
import org.testcontainers.postgresql.PostgreSQLContainer;
2424
import org.testcontainers.toxiproxy.ToxiproxyContainer;
2525

2626
import java.util.LinkedHashMap;
@@ -63,7 +63,7 @@ public PostgreSQLContainer postgresql(ConfigurableEnvironment environment,
6363
Optional<Network> network) {
6464

6565
PostgreSQLContainer postgresql =
66-
new PostgreSQLContainer<>(ContainerUtils.getDockerImageName(properties))
66+
new PostgreSQLContainer(ContainerUtils.getDockerImageName(properties))
6767
.withUsername(properties.getUser())
6868
.withPassword(properties.getPassword())
6969
.withDatabaseName(properties.getDatabase())

testcontainers-spring-boot-parent/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
<artifactId>embedded-toxiproxy</artifactId>
8181
<version>${project.version}</version>
8282
</dependency>
83+
<dependency>
84+
<groupId>com.playtika.testcontainers</groupId>
85+
<artifactId>embedded-postgresql</artifactId>
86+
<version>${project.version}</version>
87+
</dependency>
8388
<dependency>
8489
<groupId>com.playtika.testcontainers</groupId>
8590
<artifactId>embedded-aerospike</artifactId>

0 commit comments

Comments
 (0)