|
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; |
|
22 | 25 | import org.testcontainers.containers.PostgreSQLContainer; |
23 | 26 | import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
24 | 27 | import org.testcontainers.containers.wait.strategy.WaitStrategy; |
| 28 | +import org.testcontainers.images.builder.Transferable; |
| 29 | +import org.testcontainers.postgresql.PostgreSQLContainer; |
25 | 30 | import org.testcontainers.toxiproxy.ToxiproxyContainer; |
26 | 31 |
|
| 32 | +import java.nio.charset.StandardCharsets; |
27 | 33 | import java.util.LinkedHashMap; |
28 | | -import java.util.Optional; |
29 | 34 |
|
30 | 35 | import static com.playtika.testcontainer.artifactory.ArtifactoryProperties.ARTIFACTORY_BEAN_NAME; |
31 | 36 | import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart; |
| 37 | +import static org.testcontainers.postgresql.PostgreSQLContainer.POSTGRESQL_PORT; |
32 | 38 |
|
33 | 39 | @Slf4j |
34 | 40 | @Configuration |
35 | 41 | @ConditionalOnExpression("${embedded.containers.enabled:true}") |
36 | | -@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class) |
| 42 | +@AutoConfigureAfter({DockerPresenceBootstrapConfiguration.class, EmbeddedPostgreSQLBootstrapConfiguration.class}) |
37 | 43 | @ConditionalOnProperty(name = "embedded.artifactory.enabled", matchIfMissing = true) |
38 | | -@EnableConfigurationProperties({ArtifactoryProperties.class, PostgreSQLProperties.class}) |
| 44 | +@EnableConfigurationProperties(ArtifactoryProperties.class) |
39 | 45 | public class EmbeddedArtifactoryBootstrapConfiguration { |
40 | 46 |
|
41 | 47 | private static final String ARTIFACTORY_NETWORK_ALIAS = "artifactory.testcontainer.docker"; |
42 | 48 |
|
| 49 | + @Bean |
| 50 | + @ConditionalOnMissingBean(Network.class) |
| 51 | + Network artifactoryNetwork() { |
| 52 | + Network network = Network.newNetwork(); |
| 53 | + log.info("Created docker Network with id={}", network.getId()); |
| 54 | + return network; |
| 55 | + } |
| 56 | + |
43 | 57 | @Bean |
44 | 58 | @ConditionalOnMissingBean(name = "artifactoryWaitStrategy") |
45 | 59 | public WaitStrategy artifactoryWaitStrategy(ArtifactoryProperties properties) { |
@@ -71,43 +85,46 @@ ToxiproxyClientProxy artifactoryContainerProxy(ToxiproxyClient toxiproxyClient, |
71 | 85 | @Bean(name = ARTIFACTORY_BEAN_NAME, destroyMethod = "stop") |
72 | 86 | public GenericContainer<?> artifactory(ConfigurableEnvironment environment, |
73 | 87 | ArtifactoryProperties properties, |
| 88 | + PostgreSQLContainer postgreSQLContainer, |
74 | 89 | PostgreSQLProperties postgresqlProperties, |
75 | 90 | WaitStrategy artifactoryWaitStrategy, |
76 | | - Optional<Network> network) { |
| 91 | + Network network) { |
77 | 92 |
|
78 | | - PostgreSQLContainer postgresql = |
79 | | - new PostgreSQLContainer<>(ContainerUtils.getDockerImageName(postgresqlProperties)) |
80 | | - .withNetwork(Network.SHARED) |
81 | | - .withUsername(postgresqlProperties.getUser()) |
82 | | - .withPassword(postgresqlProperties.getPassword()) |
83 | | - .withDatabaseName(postgresqlProperties.getDatabase()) |
84 | | - .withInitScript(postgresqlProperties.initScriptPath) |
85 | | - .withNetworkAliases(properties.getNetworkAlias(), ARTIFACTORY_NETWORK_ALIAS) |
86 | | - .withNetworkAliases(ARTIFACTORY_NETWORK_ALIAS); |
87 | | - |
88 | | - network.ifPresent(postgresql::withNetwork); |
89 | | - configureCommonsAndStart(postgresql, postgresqlProperties, log); |
| 93 | + String systemYaml = getSystemYaml(postgresqlProperties,postgreSQLContainer); |
90 | 94 |
|
91 | 95 | GenericContainer<?> container = |
92 | 96 | new GenericContainer<>(ContainerUtils.getDockerImageName(properties)) |
93 | 97 | .withExposedPorts(properties.getRestApiPort(), properties.getGeneralPort()) |
94 | | - .withNetwork(Network.SHARED) |
| 98 | + .withNetwork(network) |
95 | 99 | .withNetworkAliases(properties.getNetworkAlias(), ARTIFACTORY_NETWORK_ALIAS) |
96 | | - .withEnv("username", postgresqlProperties.user) |
97 | | - .withEnv("password", postgresqlProperties.password) |
98 | | - .withEnv("url", "jdbc:postgresql://localhost:5432/" + postgresqlProperties.database) |
99 | | - .withEnv("type", "postgresql") |
100 | | - .withEnv("driver", "org.postgresql.Driver") |
| 100 | + .withCopyToContainer( |
| 101 | + Transferable.of(systemYaml.getBytes(StandardCharsets.UTF_8), 0666), |
| 102 | + "/opt/jfrog/artifactory/var/etc/system.yaml") |
101 | 103 | .waitingFor(artifactoryWaitStrategy); |
102 | 104 |
|
103 | | - network.ifPresent(container::withNetwork); |
104 | 105 | configureCommonsAndStart(container, properties, log); |
105 | 106 |
|
106 | 107 | registerEnvironment(container, environment, properties); |
107 | 108 |
|
108 | 109 | return container; |
109 | 110 | } |
110 | 111 |
|
| 112 | + private static @NonNull String getSystemYaml(PostgreSQLProperties postgresqlProperties, |
| 113 | + PostgreSQLContainer postgreSQLContainer) { |
| 114 | + String jdbcUrl = "jdbc:postgresql://%s:%d/%s" |
| 115 | + .formatted(postgreSQLContainer.getNetwork(), POSTGRESQL_PORT, postgresqlProperties.getDatabase()); |
| 116 | + |
| 117 | + return """ |
| 118 | + shared: |
| 119 | + database: |
| 120 | + type: "postgresql" |
| 121 | + driver: "org.postgresql.Driver" |
| 122 | + url: "%s" |
| 123 | + username: "%s" |
| 124 | + password: "%s" |
| 125 | + """.formatted(jdbcUrl, postgreSQLContainer.getUsername(), postgreSQLContainer.getPassword()); |
| 126 | + } |
| 127 | + |
111 | 128 | private void registerEnvironment(GenericContainer<?> artifactory, |
112 | 129 | ConfigurableEnvironment environment, |
113 | 130 | ArtifactoryProperties properties) { |
|
0 commit comments