|
| 1 | +package com.playtika.testcontainer.temporal; |
| 2 | + |
| 3 | +import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 6 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
| 7 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 8 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 12 | +import org.springframework.core.env.MapPropertySource; |
| 13 | +import org.testcontainers.containers.GenericContainer; |
| 14 | +import org.testcontainers.containers.Network; |
| 15 | +import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy; |
| 16 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
| 17 | + |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart; |
| 23 | +import static com.playtika.testcontainer.temporal.TemporalProperties.BEAN_NAME_EMBEDDED_TEMPORAL; |
| 24 | + |
| 25 | +@Slf4j |
| 26 | +@Configuration |
| 27 | +@ConditionalOnExpression("${embedded.containers.enabled:true}") |
| 28 | +@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class) |
| 29 | +@ConditionalOnProperty( |
| 30 | + name = "embedded.temporal.enabled", |
| 31 | + havingValue = "true", |
| 32 | + matchIfMissing = true) |
| 33 | +@EnableConfigurationProperties(TemporalProperties.class) |
| 34 | +public class EmbeddedTemporalBootstrapConfiguration { |
| 35 | + |
| 36 | + private static final String TEMPORAL_NETWORK_ALIAS = "temporal.testcontainer.docker"; |
| 37 | + |
| 38 | + @Bean(value = BEAN_NAME_EMBEDDED_TEMPORAL, destroyMethod = "stop") |
| 39 | + public GenericContainer<?> temporal(ConfigurableEnvironment environment, |
| 40 | + TemporalProperties properties, |
| 41 | + Optional<Network> network) { |
| 42 | + |
| 43 | + GenericContainer<?> container = |
| 44 | + new GenericContainer<>(new ImageFromDockerfile(properties.getDefaultDockerImage(), false) |
| 45 | + .withDockerfile(Paths.get("src/main/resources/Dockerfile")) |
| 46 | + .withBuildArg("TEMPORAL_CLI_VERSION", properties.getCliVersion())) |
| 47 | + .withExposedPorts(properties.getPort()) |
| 48 | + .withNetworkAliases(TEMPORAL_NETWORK_ALIAS) |
| 49 | + .waitingFor(new HostPortWaitStrategy()); |
| 50 | + |
| 51 | + network.ifPresent(container::withNetwork); |
| 52 | + |
| 53 | + configureCommonsAndStart(container, properties, log); |
| 54 | + |
| 55 | + registerTemporalEnvironment(container, environment, properties); |
| 56 | + |
| 57 | + return container; |
| 58 | + } |
| 59 | + |
| 60 | + private void registerTemporalEnvironment(GenericContainer<?> container, |
| 61 | + ConfigurableEnvironment environment, |
| 62 | + TemporalProperties properties) { |
| 63 | + |
| 64 | + String host = container.getHost(); |
| 65 | + Integer port = container.getMappedPort(properties.getPort()); |
| 66 | + |
| 67 | + LinkedHashMap<String, Object> map = new LinkedHashMap<>(); |
| 68 | + map.put("embedded.temporal.host", host); |
| 69 | + map.put("embedded.temporal.port", port); |
| 70 | + map.put("embedded.temporal.networkAlias", TEMPORAL_NETWORK_ALIAS); |
| 71 | + map.put("embedded.temporal.internalPort", properties.getPort()); |
| 72 | + |
| 73 | + log.info("Started Temporal service. Connection details: {}, URI: http://{}:{}", map, host, port); |
| 74 | + |
| 75 | + MapPropertySource propertySource = new MapPropertySource("embeddedTemporalInfo", map); |
| 76 | + environment.getPropertySources().addFirst(propertySource); |
| 77 | + } |
| 78 | +} |
0 commit comments