diff --git a/Jenkinsfile b/Jenkinsfile index dcd3fdd2..64831691 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -28,7 +28,7 @@ node { stage('Run IT test') { withMaven(maven: 'Maven-3.9', mavenSettingsConfig: mavenSetting) { - sh "mvn -Parq-remote verify" + sh "mvn -Parq-remote verify -Djboss.managementAddress=${hostIp(c)} -Darquillian.useMappedPorts=false" } } @@ -59,3 +59,8 @@ node { } } } + +def hostIp(container) { + def ipAddress = sh(returnStdout: true, script: "docker inspect -f {{.NetworkSettings.IPAddress}} ${container.id}").trim() + return ipAddress +} diff --git a/src/test/java/x1/arquillian/ArquillianTestContainers.java b/src/test/java/x1/arquillian/ArquillianTestContainers.java index 1f58385d..30ed159c 100644 --- a/src/test/java/x1/arquillian/ArquillianTestContainers.java +++ b/src/test/java/x1/arquillian/ArquillianTestContainers.java @@ -1,5 +1,6 @@ package x1.arquillian; +import java.util.Collections; import java.util.List; import org.jboss.arquillian.container.spi.ContainerRegistry; @@ -10,7 +11,9 @@ * with @ContainerDefinition */ public interface ArquillianTestContainers { - List> instances(); + default List> instances() { + return Collections.emptyList(); + } default void configureAfterStart(ContainerRegistry registry) { }; diff --git a/src/test/java/x1/arquillian/ContainerDefinition.java b/src/test/java/x1/arquillian/ContainerDefinition.java index 97166974..f076f410 100644 --- a/src/test/java/x1/arquillian/ContainerDefinition.java +++ b/src/test/java/x1/arquillian/ContainerDefinition.java @@ -10,5 +10,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE }) public @interface ContainerDefinition { - } diff --git a/src/test/java/x1/arquillian/Containers.java b/src/test/java/x1/arquillian/Containers.java index dc45bbeb..cedcffa1 100644 --- a/src/test/java/x1/arquillian/Containers.java +++ b/src/test/java/x1/arquillian/Containers.java @@ -40,15 +40,17 @@ public List> instances() { @Override public void configureAfterStart(ContainerRegistry registry) { - var arquillianContainer = registry.getContainers().iterator().next(); - var containerConfiguration = arquillianContainer.getContainerConfiguration(); - containerConfiguration.property("managementPort", Integer.toString(wildfly.getMappedPort(9990))); - - // if we would run the test as client, we would need to access the servlet from the host - // same in Windows we can not access the container network directly - var protocolConfiguration = arquillianContainer.getProtocolConfiguration(new ProtocolDescription("Servlet 5.0")); - protocolConfiguration.property("port", Integer.toString(wildfly.getMappedPort(8080))); - protocolConfiguration.property("host", System.getProperty("DOCKER_HOST", wildfly.getHost())); + if (Boolean.valueOf(System.getProperty("arquillian.useMappedPorts", "true"))) { + var arquillianContainer = registry.getContainers().iterator().next(); + var containerConfiguration = arquillianContainer.getContainerConfiguration(); + containerConfiguration.property("managementPort", Integer.toString(wildfly.getMappedPort(9990))); + + // if we would run the test as client, we would need to access the servlet from the host + // same in Windows we can not access the container network directly + var protocolConfiguration = arquillianContainer.getProtocolConfiguration(new ProtocolDescription("Servlet 5.0")); + protocolConfiguration.property("port", Integer.toString(wildfly.getMappedPort(8080))); + protocolConfiguration.property("host", System.getProperty("DOCKER_HOST", wildfly.getHost())); + } } @Override @@ -67,4 +69,7 @@ public boolean simpleLog(GenericContainer container) { return false; } + public static boolean isRemoteArquillian() { + return System.getProperty("arquillian.launch").equals("remote"); + } } diff --git a/src/test/java/x1/arquillian/TestContainersExtension.java b/src/test/java/x1/arquillian/TestContainersExtension.java index 1e5e0e70..dc925a08 100644 --- a/src/test/java/x1/arquillian/TestContainersExtension.java +++ b/src/test/java/x1/arquillian/TestContainersExtension.java @@ -1,5 +1,7 @@ package x1.arquillian; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -12,47 +14,54 @@ import org.reflections.Reflections; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; public class TestContainersExtension implements LoadableExtension { private static final Logger LOGGER = LoggerFactory.getLogger(TestContainersExtension.class); private static final String PACKAGE_NAME = "x1.arquillian"; - public static boolean isRemoteArquillian() { - return System.getProperty("arquillian.launch").equals("remote"); - } - @Override public void register(ExtensionBuilder builder) { - if (isRemoteArquillian()) { - findArquillianTestContainers().ifPresent(containerDefinition -> { - LoadContainerConfiguration.containerDefinition = containerDefinition; - builder.observer(LoadContainerConfiguration.class); - }); - } + findArquillianTestContainers().ifPresent(containerDefinition -> { + LoadContainerConfiguration.containerDefinition = containerDefinition; + builder.observer(LoadContainerConfiguration.class); + }); } public static final class LoadContainerConfiguration { private static ArquillianTestContainers containerDefinition; public void registerInstance(@Observes ContainerRegistry registry, ServiceLoader serviceLoader) { - containerDefinition.instances().forEach(container -> { - container.start(); - if (containerDefinition.followLog(container)) { - var logConsumer = containerDefinition.simpleLog(container) ? new SimpleLogConsumer() - : new Slf4jLogConsumer(LOGGER).withSeparateOutputStreams(); - container.followOutput(logConsumer); - } - }); + containerDefinition.instances().forEach(this::startContainer); LOGGER.info("Started {}", getImageNames()); containerDefinition.configureAfterStart(registry); } + private void startContainer(GenericContainer container) { + container.start(); + if (containerDefinition.followLog(container)) { + var logConsumer = containerDefinition.simpleLog(container) ? new SimpleLogConsumer() + : new Slf4jLogConsumer(LOGGER).withSeparateOutputStreams(); + container.followOutput(logConsumer); + } + } + public void stopInstance(@Observes AfterStop event) { - containerDefinition.instances().forEach(container -> container.stop()); + reverse(containerDefinition.instances()).forEach(this::stopContainer); LOGGER.info("Stopped {}", getImageNames()); } + private void stopContainer(GenericContainer container) { + container.stop(); + } + + private List> reverse(List> containers) { + var reverse = new ArrayList<>(containers); + Collections.reverse(containers); + return reverse; + } + private List getImageNames() { return containerDefinition.instances().stream().map(instance -> instance.getDockerImageName()) .collect(Collectors.toList()); @@ -64,8 +73,7 @@ private Optional findArquillianTestContainers() { if (classes.isEmpty()) { return Optional.empty(); } else if (classes.size() > 1) { - throw new IllegalArgumentException( - "Found more than one ContainerDefinition under " + PACKAGE_NAME + ": " + classes); + throw new IllegalStateException("Found more than one ContainerDefinition under " + PACKAGE_NAME + ": " + classes); } try { LOGGER.debug("Found ContainerDefinition in {}", classes); diff --git a/src/test/java/x1/stomp/test/AbstractIT.java b/src/test/java/x1/stomp/test/AbstractIT.java index cfd88838..f59bb8c8 100644 --- a/src/test/java/x1/stomp/test/AbstractIT.java +++ b/src/test/java/x1/stomp/test/AbstractIT.java @@ -17,7 +17,7 @@ import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.extension.ExtendWith; -import x1.arquillian.TestContainersExtension; +import x1.arquillian.Containers; import x1.stomp.boundary.JacksonConfig; import x1.stomp.version.VersionData; @@ -35,7 +35,7 @@ public static Archive createTestArchive() { var libraries = Maven.resolver().loadPomFromFile("pom.xml") .resolve("org.assertj:assertj-core", "org.hamcrest:hamcrest-core").withTransitivity().asFile(); - if (TestContainersExtension.isRemoteArquillian()) { + if (Containers.isRemoteArquillian()) { return ShrinkWrap.create(WebArchive.class, VersionData.APP_NAME_MAJOR_MINOR + ".war") .addPackages(true, "x1.stomp").addAsResource("remote-persistence.xml", "META-INF/persistence.xml") .addAsResource("microprofile-config.properties", "META-INF/microprofile-config.properties")