Skip to content

Commit

Permalink
Jenkins in Docker needs to use unmapped ports
Browse files Browse the repository at this point in the history
  • Loading branch information
kifj committed Mar 22, 2024
1 parent b765a60 commit 9e270e8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 35 deletions.
7 changes: 6 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -59,3 +59,8 @@ node {
}
}
}

def hostIp(container) {
def ipAddress = sh(returnStdout: true, script: "docker inspect -f {{.NetworkSettings.IPAddress}} ${container.id}").trim()
return ipAddress
}
5 changes: 4 additions & 1 deletion src/test/java/x1/arquillian/ArquillianTestContainers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package x1.arquillian;

import java.util.Collections;
import java.util.List;

import org.jboss.arquillian.container.spi.ContainerRegistry;
Expand All @@ -10,7 +11,9 @@
* with @ContainerDefinition
*/
public interface ArquillianTestContainers {
List<GenericContainer<?>> instances();
default List<GenericContainer<?>> instances() {
return Collections.emptyList();
}

default void configureAfterStart(ContainerRegistry registry) {
};
Expand Down
1 change: 0 additions & 1 deletion src/test/java/x1/arquillian/ContainerDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface ContainerDefinition {

}
23 changes: 14 additions & 9 deletions src/test/java/x1/arquillian/Containers.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ public List<GenericContainer<?>> 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
Expand All @@ -67,4 +69,7 @@ public boolean simpleLog(GenericContainer<?> container) {
return false;
}

public static boolean isRemoteArquillian() {
return System.getProperty("arquillian.launch").equals("remote");
}
}
50 changes: 29 additions & 21 deletions src/test/java/x1/arquillian/TestContainersExtension.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<GenericContainer<?>> reverse(List<GenericContainer<?>> containers) {
var reverse = new ArrayList<>(containers);
Collections.reverse(containers);
return reverse;
}

private List<String> getImageNames() {
return containerDefinition.instances().stream().map(instance -> instance.getDockerImageName())
.collect(Collectors.toList());
Expand All @@ -64,8 +73,7 @@ private Optional<ArquillianTestContainers> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/x1/stomp/test/AbstractIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")
Expand Down

0 comments on commit 9e270e8

Please sign in to comment.