-
Notifications
You must be signed in to change notification settings - Fork 244
Implement Consul test container #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
888d564
implement simple consul server
roskh 640fba6
implement specifying consul config file
roskh 1810041
add readme
roskh 304970f
add consul tests
roskh c5bdae4
cleanup code
roskh b91c0da
update README
roskh 0723a7c
Merge branch 'develop' into feature/consul-container
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| === embedded-consul | ||
|
|
||
| ==== Maven dependency | ||
|
|
||
| .pom.xml | ||
| [source,xml] | ||
| ---- | ||
| <dependency> | ||
| <groupId>com.playtika.testcontainers</groupId> | ||
| <artifactId>embedded-consul</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| ---- | ||
|
|
||
| ==== Consumer (via `bootstrap.properties`) | ||
|
|
||
| * `embedded.redis.enabled` `(true|false, default is 'true')` | ||
| * `embedded.redis.configurationFile` `(path for the consul configuration file to be mounted, relative to the resources folder, default is 'null')` | ||
|
|
||
| Example spring configuration: | ||
|
|
||
| .application.yml | ||
| [source,yaml] | ||
| ---- | ||
| embedded: | ||
| containers: | ||
| enabled: true | ||
| consul: | ||
| enabled: true | ||
| # file to be mounted in docker as '/consul/config/test-acl.hcl' | ||
| # path relative from the resources directory (usually 'src/test/resources') | ||
| configurationFile: consul/test-acl.hcl | ||
| ---- | ||
|
|
||
| ==== Produces | ||
|
|
||
| * `embedded.consul.host` | ||
| * `embedded.consul.port` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <artifactId>testcontainers-spring-boot-parent</artifactId> | ||
| <groupId>com.playtika.testcontainers</groupId> | ||
| <version>2.0.11-SNAPSHOT</version> | ||
| <relativePath>../testcontainers-spring-boot-parent</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>embedded-consul</artifactId> | ||
|
|
||
| <properties> | ||
| <consul-api-version>1.4.5</consul-api-version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.playtika.testcontainers</groupId> | ||
| <artifactId>testcontainers-common</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.ecwid.consul</groupId> | ||
| <artifactId>consul-api</artifactId> | ||
| <version>${consul-api-version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
16 changes: 16 additions & 0 deletions
16
embedded-consul/src/main/java/com/playtika/test/consul/ConsulProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import com.playtika.test.common.properties.CommonContainerProperties; | ||
| import lombok.Data; | ||
| import lombok.EqualsAndHashCode; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @Data | ||
| @EqualsAndHashCode(callSuper = true) | ||
| @ConfigurationProperties("embedded.consul") | ||
| public class ConsulProperties extends CommonContainerProperties { | ||
| public static final String BEAN_NAME_EMBEDDED_CONSUL = "embeddedConsul"; | ||
| private String dockerImage = "consul:1.9"; | ||
| private int port = 8500; | ||
| private String configurationFile = null; | ||
| } |
66 changes: 66 additions & 0 deletions
66
...d-consul/src/main/java/com/playtika/test/consul/EmbeddedConsulBootstrapConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import com.playtika.test.common.spring.DockerPresenceBootstrapConfiguration; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| 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.testcontainers.containers.BindMode; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
|
|
||
| import static com.playtika.test.common.utils.ContainerUtils.configureCommonsAndStart; | ||
| import static com.playtika.test.consul.ConsulProperties.BEAN_NAME_EMBEDDED_CONSUL; | ||
|
|
||
| @Slf4j | ||
| @Configuration | ||
| @ConditionalOnExpression("${embedded.containers.enabled:true}") | ||
| @AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class) | ||
| @EnableConfigurationProperties(ConsulProperties.class) | ||
| @ConditionalOnProperty(name = "embedded.consul.enabled", matchIfMissing = true) | ||
| public class EmbeddedConsulBootstrapConfiguration { | ||
|
|
||
| @Bean(name = BEAN_NAME_EMBEDDED_CONSUL, destroyMethod = "stop") | ||
| public GenericContainer consulContainer(ConfigurableEnvironment environment, ConsulProperties properties) { | ||
| log.info("Starting consul server. Docker image {}", properties.getDockerImage()); | ||
|
|
||
| GenericContainer consul = new GenericContainer<>(properties.getDockerImage()) | ||
| .withExposedPorts(properties.getPort()) | ||
| .waitingFor( | ||
| Wait.forHttp("/v1/status/leader") | ||
| .forStatusCode(200) | ||
| ).withStartupTimeout(properties.getTimeoutDuration()); | ||
|
|
||
| if (properties.getConfigurationFile() != null) { | ||
| consul = consul.withClasspathResourceMapping( | ||
| properties.getConfigurationFile(), "/consul/config/test.hcl", | ||
| BindMode.READ_ONLY); | ||
| } | ||
|
|
||
| consul = configureCommonsAndStart(consul, properties, log); | ||
| registerConsulEnvironment(consul, environment, properties); | ||
| return consul; | ||
| } | ||
|
|
||
| private void registerConsulEnvironment(GenericContainer consul, ConfigurableEnvironment environment, | ||
| ConsulProperties properties) { | ||
| Integer mappedPort = consul.getMappedPort(properties.getPort()); | ||
| String host = consul.getContainerIpAddress(); | ||
|
|
||
| LinkedHashMap<String, Object> map = new LinkedHashMap<>(); | ||
| map.put("embedded.consul.port", mappedPort); | ||
| map.put("embedded.consul.host", host); | ||
|
|
||
| log.info("Started consul. Connection Details: {}", map); | ||
|
|
||
| MapPropertySource propertySource = new MapPropertySource("embeddedConsulInfo", map); | ||
| environment.getPropertySources().addFirst(propertySource); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| org.springframework.cloud.bootstrap.BootstrapConfiguration=\ | ||
| com.playtika.test.consul.EmbeddedConsulBootstrapConfiguration |
18 changes: 18 additions & 0 deletions
18
.../src/test/java/com/playtika/test/consul/EmbeddedConsulBootstrapConfigurationBaseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import com.ecwid.consul.v1.ConsulClient; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
| import org.testcontainers.containers.GenericContainer; | ||
|
|
||
| public class EmbeddedConsulBootstrapConfigurationBaseTest { | ||
| @Autowired | ||
| protected ConfigurableEnvironment environment; | ||
|
|
||
| @Autowired | ||
| protected GenericContainer consulContainer; | ||
|
|
||
| protected ConsulClient buildClient() { | ||
| return new ConsulClient(consulContainer.getHost(), consulContainer.getFirstMappedPort()); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...rc/test/java/com/playtika/test/consul/EmbeddedConsulBootstrapConfigurationConfigTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import com.ecwid.consul.v1.ConsulClient; | ||
| import com.ecwid.consul.v1.OperationException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| @SpringBootTest( | ||
| properties = { | ||
| "embedded.consul.enabled=true", | ||
| "embedded.consul.configurationFile=consul.hcl" | ||
| }, | ||
| classes = TestConfiguration.class | ||
| ) | ||
| public class EmbeddedConsulBootstrapConfigurationConfigTest extends EmbeddedConsulBootstrapConfigurationBaseTest { | ||
|
|
||
| @Test | ||
| public void propertiesAvailable() { | ||
| assertThat(environment.getProperty("embedded.consul.enabled")) | ||
| .isEqualTo("true"); | ||
| assertThat(environment.getProperty("embedded.consul.host")) | ||
| .isEqualTo(consulContainer.getHost()); | ||
| assertThat(environment.getProperty("embedded.consul.port")) | ||
| .isEqualTo(consulContainer.getFirstMappedPort().toString()); | ||
| assertThat(environment.getProperty("embedded.consul.configurationFile")) | ||
| .isEqualTo("consul.hcl"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldUpdateKeyForbidden() { | ||
| ConsulClient client = buildClient(); | ||
|
|
||
| // with the loaded config consul should require use of an access token, | ||
| // which is not provided so 403 should be returned by consul | ||
| OperationException ex = assertThrows(OperationException.class, () -> { | ||
| client.setKVValue("key", "val"); | ||
| }); | ||
| assertThat(ex.getStatusCode()).isEqualTo(403); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...nsul/src/test/java/com/playtika/test/consul/EmbeddedConsulBootstrapConfigurationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import com.ecwid.consul.v1.ConsulClient; | ||
| import com.ecwid.consul.v1.Response; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @SpringBootTest( | ||
| properties = "embedded.consul.enabled=true", | ||
| classes = TestConfiguration.class | ||
| ) | ||
| public class EmbeddedConsulBootstrapConfigurationTest extends EmbeddedConsulBootstrapConfigurationBaseTest { | ||
|
|
||
| @Test | ||
| public void propertiesAvailable() { | ||
| assertThat(environment.getProperty("embedded.consul.enabled")) | ||
| .isEqualTo("true"); | ||
| assertThat(environment.getProperty("embedded.consul.configurationFile")) | ||
| .isEqualTo(null); | ||
| assertThat(environment.getProperty("embedded.consul.host")) | ||
| .isEqualTo(consulContainer.getHost()); | ||
| assertThat(environment.getProperty("embedded.consul.port")) | ||
| .isEqualTo(consulContainer.getFirstMappedPort().toString()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldUpdateKey() { | ||
| ConsulClient client = buildClient(); | ||
|
|
||
| Response<Boolean> booleanResponse = client.setKVValue("key", "val"); | ||
| assertThat(booleanResponse.getValue()).isEqualTo(true); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
embedded-consul/src/test/java/com/playtika/test/consul/EmbeddedConsulDisabledTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.BeanFactoryUtils; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.testcontainers.containers.GenericContainer; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @SpringBootTest( | ||
| properties = "embedded.consul.enabled=false", | ||
| classes = TestConfiguration.class | ||
| ) | ||
| public class EmbeddedConsulDisabledTest { | ||
| @Autowired | ||
| ConfigurableListableBeanFactory beanFactory; | ||
|
|
||
| @Test | ||
| public void contextLoads() { | ||
| String[] containers = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, GenericContainer.class); | ||
| assertThat(containers).isEmpty(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
embedded-consul/src/test/java/com/playtika/test/consul/TestConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.playtika.test.consul; | ||
|
|
||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @EnableAutoConfiguration | ||
| @Configuration | ||
| class TestConfiguration { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| acl { | ||
| enabled = true | ||
| default_policy = "deny" | ||
| down_policy = "extend-cache" | ||
| tokens = { | ||
| master = "78b7ad52-1f0b-e100-7b02-000001122333" # master consul access_token, use as bearer token in HTTP requests | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, renamed.