|
2 | 2 |
|
3 | 3 | import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration; |
4 | 4 | import com.playtika.testcontainer.common.utils.ContainerUtils; |
| 5 | +import com.playtika.testcontainer.postgresql.EmbeddedPostgreSQLBootstrapConfiguration; |
| 6 | +import com.playtika.testcontainer.postgresql.PostgreSQLProperties; |
5 | 7 | import com.playtika.testcontainer.toxiproxy.ToxiproxyClientProxy; |
6 | 8 | import com.playtika.testcontainer.toxiproxy.ToxiproxyHelper; |
7 | 9 | import com.playtika.testcontainer.toxiproxy.condition.ConditionalOnToxiProxyEnabled; |
8 | 10 | import eu.rekawek.toxiproxy.ToxiproxyClient; |
9 | 11 | import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.jspecify.annotations.NonNull; |
10 | 13 | import org.springframework.beans.factory.annotation.Qualifier; |
11 | 14 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
12 | 15 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
|
19 | 22 | import org.springframework.core.env.MapPropertySource; |
20 | 23 | import org.testcontainers.containers.GenericContainer; |
21 | 24 | import org.testcontainers.containers.Network; |
22 | | -import org.testcontainers.containers.PostgreSQLContainer; |
23 | | -import org.testcontainers.containers.ToxiproxyContainer; |
24 | 25 | import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
25 | 26 | import org.testcontainers.containers.wait.strategy.WaitStrategy; |
| 27 | +import org.testcontainers.images.builder.Transferable; |
| 28 | +import org.testcontainers.postgresql.PostgreSQLContainer; |
26 | 29 | import org.testcontainers.toxiproxy.ToxiproxyContainer; |
27 | 30 |
|
| 31 | +import java.nio.charset.StandardCharsets; |
28 | 32 | import java.util.LinkedHashMap; |
29 | | -import java.util.Optional; |
30 | 33 |
|
31 | 34 | import static com.playtika.testcontainer.artifactory.ArtifactoryProperties.ARTIFACTORY_BEAN_NAME; |
32 | 35 | import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart; |
| 36 | +import static org.testcontainers.postgresql.PostgreSQLContainer.POSTGRESQL_PORT; |
33 | 37 |
|
34 | 38 | @Slf4j |
35 | 39 | @Configuration |
36 | 40 | @ConditionalOnExpression("${embedded.containers.enabled:true}") |
37 | | -@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class) |
| 41 | +@AutoConfigureAfter({DockerPresenceBootstrapConfiguration.class, EmbeddedPostgreSQLBootstrapConfiguration.class}) |
38 | 42 | @ConditionalOnProperty(name = "embedded.artifactory.enabled", matchIfMissing = true) |
39 | | -@EnableConfigurationProperties({ArtifactoryProperties.class, PostgreSQLProperties.class}) |
| 43 | +@EnableConfigurationProperties(ArtifactoryProperties.class) |
40 | 44 | public class EmbeddedArtifactoryBootstrapConfiguration { |
41 | 45 |
|
42 | 46 | private static final String ARTIFACTORY_NETWORK_ALIAS = "artifactory.testcontainer.docker"; |
43 | 47 |
|
| 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 | + |
44 | 56 | @Bean |
45 | 57 | @ConditionalOnMissingBean(name = "artifactoryWaitStrategy") |
46 | 58 | public WaitStrategy artifactoryWaitStrategy(ArtifactoryProperties properties) { |
@@ -72,43 +84,46 @@ ToxiproxyClientProxy artifactoryContainerProxy(ToxiproxyClient toxiproxyClient, |
72 | 84 | @Bean(name = ARTIFACTORY_BEAN_NAME, destroyMethod = "stop") |
73 | 85 | public GenericContainer<?> artifactory(ConfigurableEnvironment environment, |
74 | 86 | ArtifactoryProperties properties, |
| 87 | + PostgreSQLContainer postgreSQLContainer, |
75 | 88 | PostgreSQLProperties postgresqlProperties, |
76 | 89 | WaitStrategy artifactoryWaitStrategy, |
77 | | - Optional<Network> network) { |
| 90 | + Network network) { |
78 | 91 |
|
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); |
91 | 93 |
|
92 | 94 | GenericContainer<?> container = |
93 | 95 | new GenericContainer<>(ContainerUtils.getDockerImageName(properties)) |
94 | 96 | .withExposedPorts(properties.getRestApiPort(), properties.getGeneralPort()) |
95 | | - .withNetwork(Network.SHARED) |
| 97 | + .withNetwork(network) |
96 | 98 | .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") |
102 | 102 | .waitingFor(artifactoryWaitStrategy); |
103 | 103 |
|
104 | | - network.ifPresent(container::withNetwork); |
105 | 104 | configureCommonsAndStart(container, properties, log); |
106 | 105 |
|
107 | 106 | registerEnvironment(container, environment, properties); |
108 | 107 |
|
109 | 108 | return container; |
110 | 109 | } |
111 | 110 |
|
| 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 | + |
112 | 127 | private void registerEnvironment(GenericContainer<?> artifactory, |
113 | 128 | ConfigurableEnvironment environment, |
114 | 129 | ArtifactoryProperties properties) { |
|
0 commit comments