-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathEmbeddedPostgreSQLBootstrapConfiguration.java
More file actions
108 lines (91 loc) · 5.46 KB
/
EmbeddedPostgreSQLBootstrapConfiguration.java
File metadata and controls
108 lines (91 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.playtika.testcontainer.postgresql;
import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration;
import com.playtika.testcontainer.common.utils.ContainerUtils;
import com.playtika.testcontainer.toxiproxy.ToxiproxyClientProxy;
import com.playtika.testcontainer.toxiproxy.ToxiproxyHelper;
import com.playtika.testcontainer.toxiproxy.condition.ConditionalOnToxiProxyEnabled;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.util.StringUtils;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.postgresql.PostgreSQLContainer;
import org.testcontainers.toxiproxy.ToxiproxyContainer;
import java.util.LinkedHashMap;
import java.util.Optional;
import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart;
import static com.playtika.testcontainer.postgresql.PostgreSQLProperties.BEAN_NAME_EMBEDDED_POSTGRESQL;
@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.postgresql.enabled", matchIfMissing = true)
@EnableConfigurationProperties(PostgreSQLProperties.class)
public class EmbeddedPostgreSQLBootstrapConfiguration {
@Bean
@ConditionalOnToxiProxyEnabled(module = "postgresql")
ToxiproxyClientProxy postgresqlContainerProxy(ToxiproxyClient toxiproxyClient,
ToxiproxyContainer toxiproxyContainer,
@Qualifier(BEAN_NAME_EMBEDDED_POSTGRESQL) PostgreSQLContainer postgresql,
ConfigurableEnvironment environment) {
ToxiproxyClientProxy proxy = ToxiproxyHelper.createProxy(
toxiproxyClient,
toxiproxyContainer,
postgresql,
PostgreSQLContainer.POSTGRESQL_PORT,
"postgresql");
ToxiproxyHelper.registerProxyEnvironment(proxy, "embedded.postgresql", "embeddedPostgresqlToxiproxyInfo", environment);
return proxy;
}
@Bean(name = BEAN_NAME_EMBEDDED_POSTGRESQL, destroyMethod = "stop")
public PostgreSQLContainer postgresql(ConfigurableEnvironment environment,
PostgreSQLProperties properties,
Optional<Network> network) {
PostgreSQLContainer postgresql =
new PostgreSQLContainer(ContainerUtils.getDockerImageName(properties))
.withUsername(properties.getUser())
.withPassword(properties.getPassword())
.withDatabaseName(properties.getDatabase())
.withInitScript(properties.initScriptPath)
.withNetworkAliases(properties.getNetworkAlias());
network.ifPresent(postgresql::withNetwork);
String startupLogCheckRegex = properties.getStartupLogCheckRegex();
if (StringUtils.hasLength(startupLogCheckRegex)) {
WaitStrategy waitStrategy = new LogMessageWaitStrategy()
.withRegEx(startupLogCheckRegex);
postgresql.waitingFor(waitStrategy);
}
postgresql = (PostgreSQLContainer) configureCommonsAndStart(postgresql, properties, log);
registerPostgresqlEnvironment(postgresql, environment, properties);
return postgresql;
}
private void registerPostgresqlEnvironment(PostgreSQLContainer postgresql,
ConfigurableEnvironment environment,
PostgreSQLProperties properties) {
Integer mappedPort = postgresql.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT);
String host = postgresql.getHost();
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("embedded.postgresql.port", mappedPort);
map.put("embedded.postgresql.host", host);
map.put("embedded.postgresql.schema", properties.getDatabase());
map.put("embedded.postgresql.user", properties.getUser());
map.put("embedded.postgresql.password", properties.getPassword());
map.put("embedded.postgresql.networkAlias", properties.getNetworkAlias());
map.put("embedded.postgresql.internalPort", PostgreSQLContainer.POSTGRESQL_PORT);
String jdbcURL = "jdbc:postgresql://{}:{}/{}";
log.info("Started postgresql server. Connection details: {}, " +
"JDBC connection url: " + jdbcURL, map, host, mappedPort, properties.getDatabase());
MapPropertySource propertySource = new MapPropertySource("embeddedPostgreInfo", map);
environment.getPropertySources().addFirst(propertySource);
}
}