This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 315af9d Migrates consul component to the new test-infra (#4671) 315af9d is described below commit 315af9d043f76f61e80d8eb73fe1b2bbbefd3f9d Author: Otavio Rodolfo Piske <orpi...@users.noreply.github.com> AuthorDate: Tue Nov 24 16:28:48 2020 +0100 Migrates consul component to the new test-infra (#4671) --- components/camel-consul/pom.xml | 39 ++++++--- .../camel/component/consul/ConsulHealthTest.java | 30 ++++++- .../camel/component/consul/ConsulRegistryTest.java | 18 ++--- .../camel/component/consul/ConsulTestSupport.java | 40 +++------- .../cloud/ConsulDefaultServiceCallRouteTest.java | 2 +- .../cloud/ConsulRibbonServiceCallRouteTest.java | 2 +- .../ConsulServiceCallWithRegistrationTest.java | 11 +-- .../consul/cloud/ConsulServiceDiscoveryTest.java | 2 +- .../cloud/ConsulServiceRegistrationTestBase.java | 2 +- .../consul/cloud/ConsulServiceRegistryTest.java | 3 +- .../cloud/SpringConsulServiceCallRouteTest.java | 24 +++--- .../ConsulClusteredRoutePolicyFactoryTest.java | 25 +++--- .../cluster/ConsulClusteredRoutePolicyTest.java | 25 +++--- .../component/consul/cluster/ConsulMasterTest.java | 25 +++--- .../SpringConsulDefaultServiceCallRouteTest.xml | 2 +- .../SpringConsulExpressionServiceCallRouteTest.xml | 2 +- .../SpringConsulRibbonServiceCallRouteTest.xml | 2 +- test-infra/camel-test-infra-consul/pom.xml | 66 +++++++++++++++ .../src/main/resources/META-INF/MANIFEST.MF | 0 .../test/infra/consul/common/ConsulProperties.java | 28 +++++++ .../services/ConsulLocalContainerService.java | 93 ++++++++++++++++++++++ .../infra/consul/services/ConsulRemoteService.java | 59 ++++++++++++++ .../test/infra/consul/services/ConsulService.java | 44 ++++++++++ .../consul/services/ConsulServiceFactory.java | 43 ++++++++++ test-infra/pom.xml | 1 + 25 files changed, 459 insertions(+), 129 deletions(-) diff --git a/components/camel-consul/pom.xml b/components/camel-consul/pom.xml index c6e7737..77d085b 100644 --- a/components/camel-consul/pom.xml +++ b/components/camel-consul/pom.xml @@ -62,16 +62,6 @@ <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-testcontainers-spring-junit5</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-testcontainers-junit5</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> <artifactId>camel-mock</artifactId> <scope>test</scope> </dependency> @@ -146,6 +136,35 @@ <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring-junit5</artifactId> + <scope>test</scope> + </dependency> + + <!-- test infra --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-common</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-consul</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + </dependencies> <profiles> diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java index 68183f4..9a69707 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java @@ -22,24 +22,46 @@ import java.util.Random; import java.util.UUID; import com.orbitz.consul.AgentClient; +import com.orbitz.consul.Consul; import com.orbitz.consul.model.agent.ImmutableRegistration; import com.orbitz.consul.model.agent.Registration; import com.orbitz.consul.model.health.ServiceHealth; +import org.apache.camel.BindToRegistry; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulHealthActions; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; +import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; -public class ConsulHealthTest extends ConsulTestSupport { +public class ConsulHealthTest extends CamelTestSupport { + /* + NOTE: this one is not registered as extension because it requires a different lifecycle. It + needs to be started much earlier than usual, so in this test we take care of handling it. + */ + private ConsulService consulService = ConsulServiceFactory.createService(); + private AgentClient client; private List<Registration> registrations; private String service; - // ************************************************************************* - // Setup / tear down - // ************************************************************************* + public ConsulHealthTest() { + consulService.initialize(); + } + + @BindToRegistry("consul") + public ConsulComponent getConsulComponent() { + ConsulComponent component = new ConsulComponent(); + component.getConfiguration().setUrl(consulService.getConsulUrl()); + return component; + } + + protected Consul getConsul() { + return Consul.builder().withUrl(consulService.getConsulUrl()).build(); + } @Override public void doPreSetup() throws Exception { diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java index fd835ca..fddf25c 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java @@ -24,10 +24,11 @@ import java.util.Set; import com.orbitz.consul.Consul; import org.apache.camel.NoSuchBeanException; -import org.junit.jupiter.api.AfterAll; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.testcontainers.containers.GenericContainer; +import org.junit.jupiter.api.extension.RegisterExtension; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -38,10 +39,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; * Unit test for Camel Registry implementation for Consul */ public class ConsulRegistryTest implements Serializable { + @RegisterExtension + public static ConsulService consulService = ConsulServiceFactory.createService(); private static final long serialVersionUID = -3482971969351609265L; private static ConsulRegistry registry; - private static GenericContainer container; public class ConsulTestClass implements Serializable { private static final long serialVersionUID = -4815945688487114891L; @@ -53,15 +55,7 @@ public class ConsulRegistryTest implements Serializable { @BeforeAll public static void setUp() { - container = ConsulTestSupport.consulContainer(); - container.start(); - - registry = new ConsulRegistry(container.getContainerIpAddress(), container.getMappedPort(Consul.DEFAULT_HTTP_PORT)); - } - - @AfterAll - public static void tearDown() { - container.stop(); + registry = new ConsulRegistry(consulService.host(), consulService.port()); } @Test diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java index 67cd02a..3de233b 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java @@ -21,30 +21,27 @@ import java.util.List; import java.util.UUID; import com.orbitz.consul.Consul; -import com.orbitz.consul.KeyValueClient; import org.apache.camel.BindToRegistry; -import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport; -import org.apache.camel.test.testcontainers.junit5.Wait; -import org.testcontainers.containers.GenericContainer; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class ConsulTestSupport extends CamelTestSupport { + @RegisterExtension + public static ConsulService service = ConsulServiceFactory.createService(); -public class ConsulTestSupport extends ContainerAwareTestSupport { - public static final String CONTAINER_IMAGE = "consul:1.8.3"; - public static final String CONTAINER_NAME = "consul"; public static final String KV_PREFIX = "/camel"; @BindToRegistry("consul") public ConsulComponent getConsulComponent() { ConsulComponent component = new ConsulComponent(); - component.getConfiguration().setUrl(consulUrl()); + component.getConfiguration().setUrl(service.getConsulUrl()); return component; } protected Consul getConsul() { - return Consul.builder().withUrl(consulUrl()).build(); - } - - protected KeyValueClient getKeyValueClient() { - return getConsul().keyValueClient(); + return Consul.builder().withUrl(service.getConsulUrl()).build(); } protected String generateRandomString() { @@ -65,21 +62,4 @@ public class ConsulTestSupport extends ContainerAwareTestSupport { protected String generateKey() { return KV_PREFIX + "/" + getCurrentTestName() + "/" + generateRandomString(); } - - protected String consulUrl() { - return String.format("http://%s:%d", getContainerHost(CONTAINER_NAME), - getContainerPort(CONTAINER_NAME, Consul.DEFAULT_HTTP_PORT)); - } - - @Override - protected GenericContainer<?> createContainer() { - return consulContainer(); - } - - public static GenericContainer consulContainer() { - return new GenericContainer(CONTAINER_IMAGE).withNetworkAliases(CONTAINER_NAME) - .withExposedPorts(Consul.DEFAULT_HTTP_PORT) - .waitingFor(Wait.forLogMessageContaining("Synced node info", 1)) - .withCommand("agent", "-dev", "-server", "-bootstrap", "-client", "0.0.0.0", "-log-level", "trace"); - } } diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java index 1699ad6..189623e 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java @@ -91,7 +91,7 @@ public class ConsulDefaultServiceCallRouteTest extends ConsulTestSupport { @Override public void configure() throws Exception { from("direct:start").serviceCall().name(SERVICE_NAME).component("http").defaultLoadBalancer() - .consulServiceDiscovery().url(consulUrl()).endParent() + .consulServiceDiscovery().url(service.getConsulUrl()).endParent() .to("log:org.apache.camel.component.consul.cloud?level=INFO&showAll=true&multiline=true") .to("mock:result"); diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java index 029117e..f9a1878 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java @@ -91,7 +91,7 @@ public class ConsulRibbonServiceCallRouteTest extends ConsulTestSupport { @Override public void configure() throws Exception { from("direct:start").serviceCall().name(SERVICE_NAME).component("http").consulServiceDiscovery() - .url(consulUrl()).endParent() + .url(service.getConsulUrl()).endParent() .to("log:org.apache.camel.component.consul.processor.service?level=INFO&showAll=true&multiline=true") .to("mock:result"); diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java index 8b077d4..f8ebe3f 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java @@ -48,7 +48,7 @@ public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { ConsulServiceRegistry registry = new ConsulServiceRegistry(); registry.setId(context.getUuidGenerator().generateUuid()); registry.setCamelContext(context()); - registry.setUrl(consulUrl()); + registry.setUrl(service.getConsulUrl()); registry.setServiceHost(SERVICE_HOST); registry.setOverrideServiceHost(true); @@ -74,7 +74,7 @@ public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { from("direct:start") .serviceCall() .name(serviceName).component("undertow").defaultLoadBalancer() - .consulServiceDiscovery().url(consulUrl()).end() + .consulServiceDiscovery().url(service.getConsulUrl()).end() .end() .log("${body}"); @@ -98,12 +98,12 @@ public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { context.addRoutes(new RouteBuilder() { @Override public void configure() { - // context path is had coded so it should fail as it not exposed + // context path is hard coded so it should fail as it not exposed // by jetty from("direct:start") .serviceCall() .name(serviceName + "/bad/path").component("http") - .defaultLoadBalancer().consulServiceDiscovery().url(consulUrl()).end() + .defaultLoadBalancer().consulServiceDiscovery().url(service.getConsulUrl()).end() .end() .log("${body}"); @@ -115,6 +115,7 @@ public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { context.start(); - assertThrows(CamelExecutionException.class, () -> template.requestBody("direct:start", "ping", String.class)); + assertThrows(CamelExecutionException.class, + () -> template.requestBody("direct:start", "ping", String.class)); } } diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java index 7dddf28..74e999c 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java @@ -81,7 +81,7 @@ public class ConsulServiceDiscoveryTest extends ConsulTestSupport { @Test public void testServiceDiscovery() throws Exception { ConsulConfiguration configuration = new ConsulConfiguration(); - configuration.setUrl(consulUrl()); + configuration.setUrl(service.getConsulUrl()); ServiceDiscovery discovery = new ConsulServiceDiscovery(configuration); diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java index b1cc5d6..672a082 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java @@ -51,7 +51,7 @@ public abstract class ConsulServiceRegistrationTestBase extends ConsulTestSuppor ConsulServiceRegistry registry = new ConsulServiceRegistry(); registry.setId(context.getUuidGenerator().generateUuid()); registry.setCamelContext(context()); - registry.setUrl(consulUrl()); + registry.setUrl(service.getConsulUrl()); registry.setServiceHost(SERVICE_HOST); registry.setOverrideServiceHost(true); diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java index 2bf707e..d1a0c8f 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java @@ -37,8 +37,9 @@ public class ConsulServiceRegistryTest extends ConsulTestSupport { @Test public void testSimpleServiceRegistration() { ConsulServiceRegistry registry = new ConsulServiceRegistry(); + registry.setCamelContext(context()); - registry.setUrl(consulUrl()); + registry.setUrl(service.getConsulUrl()); registry.setServiceHost("service-host"); registry.setOverrideServiceHost(true); registry.start(); diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java index cf56942..33272a5 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java @@ -27,16 +27,20 @@ import com.orbitz.consul.model.agent.Registration; import org.apache.camel.Navigate; import org.apache.camel.Processor; import org.apache.camel.Route; -import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; import org.apache.camel.processor.ChoiceProcessor; import org.apache.camel.processor.FilterProcessor; -import org.apache.camel.test.testcontainers.spring.junit5.ContainerAwareSpringTestSupport; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; +import org.apache.camel.test.spring.junit5.CamelSpringTestSupport; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.testcontainers.containers.GenericContainer; +import org.junit.jupiter.api.extension.RegisterExtension; + +public abstract class SpringConsulServiceCallRouteTest extends CamelSpringTestSupport { + @RegisterExtension + public static ConsulService service = ConsulServiceFactory.createService(); -public abstract class SpringConsulServiceCallRouteTest extends ContainerAwareSpringTestSupport { private AgentClient client; private List<Registration> registrations; @@ -48,7 +52,7 @@ public abstract class SpringConsulServiceCallRouteTest extends ContainerAwareSpr public void doPreSetup() throws Exception { super.doPreSetup(); - this.client = Consul.builder().withUrl(consulUrl()).build().agentClient(); + this.client = Consul.builder().withUrl(service.getConsulUrl()).build().agentClient(); this.registrations = Arrays.asList( ImmutableRegistration.builder().id("service-1-1").name("http-service-1").address("127.0.0.1").port(9011) @@ -124,14 +128,4 @@ public abstract class SpringConsulServiceCallRouteTest extends ContainerAwareSpr return processors; } - - @Override - protected GenericContainer<?> createContainer() { - return ConsulTestSupport.consulContainer(); - } - - protected String consulUrl() { - return String.format("http://%s:%d", getContainerHost(ConsulTestSupport.CONTAINER_NAME), - getContainerPort(ConsulTestSupport.CONTAINER_NAME, Consul.DEFAULT_HTTP_PORT)); - } } diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryTest.java index 4865275..676e461 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryTest.java @@ -26,24 +26,20 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.IntStream; -import com.orbitz.consul.Consul; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.cluster.ClusteredRoutePolicyFactory; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -@Testcontainers public class ConsulClusteredRoutePolicyFactoryTest { - - @Container - public static GenericContainer container = ConsulTestSupport.consulContainer(); + @RegisterExtension + public static ConsulService service = ConsulServiceFactory.createService(); private static final Logger LOGGER = LoggerFactory.getLogger(ConsulClusteredRoutePolicyFactoryTest.class); private static final List<String> CLIENTS = IntStream.range(0, 3).mapToObj(Integer::toString).collect(Collectors.toList()); @@ -77,17 +73,16 @@ public class ConsulClusteredRoutePolicyFactoryTest { int events = ThreadLocalRandom.current().nextInt(2, 6); CountDownLatch contextLatch = new CountDownLatch(events); - ConsulClusterService service = new ConsulClusterService(); - service.setId("node-" + id); - service.setUrl(String.format("http://%s:%d", container.getContainerIpAddress(), - container.getMappedPort(Consul.DEFAULT_HTTP_PORT))); + ConsulClusterService consulClusterService = new ConsulClusterService(); + consulClusterService.setId("node-" + id); + consulClusterService.setUrl(service.getConsulUrl()); - LOGGER.info("Consul URL {}", service.getUrl()); + LOGGER.info("Consul URL {}", consulClusterService.getUrl()); DefaultCamelContext context = new DefaultCamelContext(); context.disableJMX(); context.setName("context-" + id); - context.addService(service); + context.addService(consulClusterService); context.addRoutePolicyFactory(ClusteredRoutePolicyFactory.forNamespace("my-ns")); context.addRoutes(new RouteBuilder() { @Override diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyTest.java index 0977bf8..185240a 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyTest.java @@ -26,24 +26,20 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.IntStream; -import com.orbitz.consul.Consul; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.cluster.ClusteredRoutePolicy; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -@Testcontainers public class ConsulClusteredRoutePolicyTest { - - @Container - public static GenericContainer container = ConsulTestSupport.consulContainer(); + @RegisterExtension + public static ConsulService service = ConsulServiceFactory.createService(); private static final Logger LOGGER = LoggerFactory.getLogger(ConsulClusteredRoutePolicyTest.class); private static final List<String> CLIENTS = IntStream.range(0, 3).mapToObj(Integer::toString).collect(Collectors.toList()); @@ -77,17 +73,16 @@ public class ConsulClusteredRoutePolicyTest { int events = ThreadLocalRandom.current().nextInt(2, 6); CountDownLatch contextLatch = new CountDownLatch(events); - ConsulClusterService service = new ConsulClusterService(); - service.setId("node-" + id); - service.setUrl(String.format("http://%s:%d", container.getContainerIpAddress(), - container.getMappedPort(Consul.DEFAULT_HTTP_PORT))); + ConsulClusterService consulClusterService = new ConsulClusterService(); + consulClusterService.setId("node-" + id); + consulClusterService.setUrl(service.getConsulUrl()); - LOGGER.info("Consul URL {}", service.getUrl()); + LOGGER.info("Consul URL {}", consulClusterService.getUrl()); DefaultCamelContext context = new DefaultCamelContext(); context.disableJMX(); context.setName("context-" + id); - context.addService(service); + context.addService(consulClusterService); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulMasterTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulMasterTest.java index 377c1e6..7a45201 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulMasterTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulMasterTest.java @@ -26,23 +26,19 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.IntStream; -import com.orbitz.consul.Consul; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.test.infra.consul.services.ConsulService; +import org.apache.camel.test.infra.consul.services.ConsulServiceFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -@Testcontainers public class ConsulMasterTest { - - @Container - public static GenericContainer container = ConsulTestSupport.consulContainer(); + @RegisterExtension + public static ConsulService service = ConsulServiceFactory.createService(); private static final Logger LOGGER = LoggerFactory.getLogger(ConsulMasterTest.class); private static final List<String> CLIENTS = IntStream.range(0, 3).mapToObj(Integer::toString).collect(Collectors.toList()); @@ -76,17 +72,16 @@ public class ConsulMasterTest { int events = ThreadLocalRandom.current().nextInt(2, 6); CountDownLatch contextLatch = new CountDownLatch(events); - ConsulClusterService service = new ConsulClusterService(); - service.setId("node-" + id); - service.setUrl(String.format("http://%s:%d", container.getContainerIpAddress(), - container.getMappedPort(Consul.DEFAULT_HTTP_PORT))); + ConsulClusterService consulClusterService = new ConsulClusterService(); + consulClusterService.setId("node-" + id); + consulClusterService.setUrl(service.getConsulUrl()); - LOGGER.info("Consul URL {}", service.getUrl()); + LOGGER.info("Consul URL {}", consulClusterService.getUrl()); DefaultCamelContext context = new DefaultCamelContext(); context.disableJMX(); context.setName("context-" + id); - context.addService(service); + context.addService(consulClusterService); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml index 8d3cfa7..b4f2639 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml @@ -33,7 +33,7 @@ <defaultServiceCallConfiguration id="default" component="http"> <!-- service discovery --> - <consulServiceDiscovery url="http://{{container:host:consul}}:{{container:port:8500@consul}}"/> + <consulServiceDiscovery url="http://{{consul.host}}:{{consul.port}}"/> <!-- service filter --> <blacklistServiceFilter> diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml index 67420ba..3f3d8d6 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulExpressionServiceCallRouteTest.xml @@ -33,7 +33,7 @@ <defaultServiceCallConfiguration id="default" component="http"> <!-- service discovery --> - <consulServiceDiscovery url="http://{{container:host:consul}}:{{container:port:8500@consul}}"/> + <consulServiceDiscovery url="http://{{consul.host}}:{{consul.port}}"/> <!-- service filter --> <blacklistServiceFilter> diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml index 0bdcec2..59cb058 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml @@ -33,7 +33,7 @@ <defaultServiceCallConfiguration id="default" component="http"> <!-- service discovery --> - <consulServiceDiscovery url="http://{{container:host:consul}}:{{container:port:8500@consul}}"/> + <consulServiceDiscovery url="http://{{consul.host}}:{{consul.port}}"/> <!-- service filter --> <blacklistServiceFilter> diff --git a/test-infra/camel-test-infra-consul/pom.xml b/test-infra/camel-test-infra-consul/pom.xml new file mode 100644 index 0000000..4cd63f0 --- /dev/null +++ b/test-infra/camel-test-infra-consul/pom.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<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"> + <parent> + <artifactId>camel-test-infra-parent</artifactId> + <groupId>org.apache.camel</groupId> + <relativePath>../camel-test-infra-parent/pom.xml</relativePath> + <version>3.7.0-SNAPSHOT</version> + </parent> + + <modelVersion>4.0.0</modelVersion> + + <artifactId>camel-test-infra-consul</artifactId> + <name>Camel :: Test Infra :: Consul</name> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-common</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + </dependency> + + <dependency> + <groupId>com.orbitz.consul</groupId> + <artifactId>consul-client</artifactId> + <version>${consul-client-version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + </plugin> + </plugins> + </build> + + +</project> \ No newline at end of file diff --git a/test-infra/camel-test-infra-consul/src/main/resources/META-INF/MANIFEST.MF b/test-infra/camel-test-infra-consul/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e69de29 diff --git a/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/common/ConsulProperties.java b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/common/ConsulProperties.java new file mode 100644 index 0000000..c5bbf80 --- /dev/null +++ b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/common/ConsulProperties.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.test.infra.consul.common; + +public final class ConsulProperties { + public static final String CONSUL_URL = "consul.url"; + public static final String CONSUL_HOST = "consul.host"; + public static final String CONSUL_PORT = "consul.port"; + + private ConsulProperties() { + + } +} diff --git a/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulLocalContainerService.java b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulLocalContainerService.java new file mode 100644 index 0000000..e4d7a2a --- /dev/null +++ b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulLocalContainerService.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.consul.services; + +import com.orbitz.consul.Consul; +import org.apache.camel.test.infra.common.services.ContainerService; +import org.apache.camel.test.infra.consul.common.ConsulProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; + +public class ConsulLocalContainerService implements ConsulService, ContainerService<GenericContainer> { + public static final String CONTAINER_IMAGE = "consul:1.8.3"; + public static final String CONTAINER_NAME = "consul"; + + private static final Logger LOG = LoggerFactory.getLogger(ConsulLocalContainerService.class); + + private GenericContainer container; + + public ConsulLocalContainerService() { + String containerName = System.getProperty("consul.container", CONTAINER_IMAGE); + initContainer(containerName); + } + + public ConsulLocalContainerService(String containerName) { + initContainer(containerName); + } + + protected void initContainer(String containerName) { + container = new GenericContainer(containerName) + .withNetworkAliases(CONTAINER_NAME) + .withExposedPorts(Consul.DEFAULT_HTTP_PORT) + .waitingFor(Wait.forLogMessage(".*Synced node info.*", 1)) + .withCommand("agent", "-dev", "-server", "-bootstrap", "-client", "0.0.0.0", "-log-level", "trace"); + } + + @Override + public void registerProperties() { + System.setProperty(ConsulProperties.CONSUL_URL, getConsulUrl()); + System.setProperty(ConsulProperties.CONSUL_HOST, host()); + System.setProperty(ConsulProperties.CONSUL_PORT, String.valueOf(port())); + } + + @Override + public void initialize() { + LOG.info("Trying to start the Consul container"); + container.start(); + + registerProperties(); + LOG.info("Consul instance running at {}", getConsulUrl()); + } + + @Override + public void shutdown() { + LOG.info("Stopping the Consul container"); + container.stop(); + } + + @Override + public GenericContainer getContainer() { + return container; + } + + @Override + public String getConsulUrl() { + return String.format("http://%s:%d", container.getHost(), container.getMappedPort(Consul.DEFAULT_HTTP_PORT)); + } + + @Override + public String host() { + return container.getHost(); + } + + @Override + public int port() { + return container.getMappedPort(Consul.DEFAULT_HTTP_PORT); + } +} diff --git a/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulRemoteService.java b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulRemoteService.java new file mode 100644 index 0000000..0a36970 --- /dev/null +++ b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulRemoteService.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.consul.services; + +import com.orbitz.consul.Consul; +import org.apache.camel.test.infra.consul.common.ConsulProperties; + +public class ConsulRemoteService implements ConsulService { + + @Override + public void registerProperties() { + // NO-OP + } + + @Override + public void initialize() { + registerProperties(); + } + + @Override + public void shutdown() { + // NO-OP + } + + @Override + public String getConsulUrl() { + return System.getProperty(ConsulProperties.CONSUL_URL); + } + + @Override + public String host() { + return System.getProperty(ConsulProperties.CONSUL_HOST); + } + + @Override + public int port() { + String strPort = System.getProperty(ConsulProperties.CONSUL_PORT); + + if (strPort == null) { + return Consul.DEFAULT_HTTP_PORT; + } + + return Integer.parseInt(strPort); + } +} diff --git a/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulService.java b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulService.java new file mode 100644 index 0000000..3d50bbf --- /dev/null +++ b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulService.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.consul.services; + +import org.apache.camel.test.infra.common.services.TestService; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * Test infra service for Consul + */ +public interface ConsulService extends BeforeAllCallback, AfterAllCallback, TestService { + + String getConsulUrl(); + + String host(); + + int port(); + + @Override + default void beforeAll(ExtensionContext extensionContext) throws Exception { + initialize(); + } + + @Override + default void afterAll(ExtensionContext extensionContext) throws Exception { + shutdown(); + } +} diff --git a/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulServiceFactory.java b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulServiceFactory.java new file mode 100644 index 0000000..6dfd191 --- /dev/null +++ b/test-infra/camel-test-infra-consul/src/test/java/org/apache/camel/test/infra/consul/services/ConsulServiceFactory.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.consul.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class ConsulServiceFactory { + private static final Logger LOG = LoggerFactory.getLogger(ConsulServiceFactory.class); + + private ConsulServiceFactory() { + + } + + public static ConsulService createService() { + String instanceType = System.getProperty("consul.instance.type"); + + if (instanceType == null || instanceType.equals("local-consul-container")) { + return new ConsulLocalContainerService(); + } + + if (instanceType.equals("remote")) { + return new ConsulRemoteService(); + } + + LOG.error("Consul instance must be one of 'local-consul-container' or 'remote"); + throw new UnsupportedOperationException("Invalid Consul instance type"); + } +} diff --git a/test-infra/pom.xml b/test-infra/pom.xml index 04256ea..383e789 100644 --- a/test-infra/pom.xml +++ b/test-infra/pom.xml @@ -53,5 +53,6 @@ <module>camel-test-infra-hdfs</module> <module>camel-test-infra-jdbc</module> <module>camel-test-infra-arangodb</module> + <module>camel-test-infra-consul</module> </modules> </project>