Skip to content
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

Provide access to id of container network in integration tests #21287

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1336,3 +1336,10 @@ a utility class.
`QuarkusTestResourceLifecycleManager` implementations can also implement `ContextAware` to get access to these properties,
which allows you to setup the resource before Quarkus starts (e.g. configure a KeyCloak instance, add data to a database etc).


[NOTE]
====
For `@QuarkusIntegrationTest` tests that result in launcher the application as a container, `io.quarkus.test.common.DevServicesContext` also provides access to the id of the container network on which the application container was launched (via the `containerNetworkId` method).
This can be used by `QuarkusTestResourceLifecycleManager` that need to launch additional containers that the application will communicate with.
====

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.acme;

import io.quarkus.test.common.DevServicesContext;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class DummyResource implements QuarkusTestResourceLifecycleManager, DevServicesContext.ContextAware {

public static final AtomicReference<Optional<String>> CONTAINER_NETWORK_ID = new AtomicReference<>(null);

@Override
public Map<String, String> start() {
return Collections.emptyMap();
}

@Override
public void stop() {

}


@Override
public void setIntegrationTestContext(DevServicesContext context) {
CONTAINER_NETWORK_ID.set(context.containerNetworkId());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package org.acme;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusIntegrationTest
@QuarkusTestResource(DummyResource.class)
public class FruitsEndpointIT extends FruitsEndpointTest {


@Test
public void containerNetworkIdSet() {
Optional<String> optional = DummyResource.CONTAINER_NETWORK_ID.get();
assertNotNull(optional);
assertTrue(optional.isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.test.common;

import java.util.Map;
import java.util.Optional;

/**
* Interface that can be used to get properties from DevServices for {@link QuarkusTest} and {@link QuarkusIntegrationTest}
Expand All @@ -18,6 +19,12 @@ public interface DevServicesContext {
*/
Map<String, String> devServicesProperties();

/**
* If the application is going to be launched in a container, this method returns the id of container network
* it will be launched on.
*/
Optional<String> containerNetworkId();

/**
* Interface that can be implemented to allow automatic injection of the context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.CancellationException;
Expand Down Expand Up @@ -47,11 +48,13 @@ public TestResourceManager(Class<?> testClass) {

public TestResourceManager(Class<?> testClass, Class<?> profileClass, List<TestResourceClassEntry> additionalTestResources,
boolean disableGlobalTestResources) {
this(testClass, profileClass, additionalTestResources, disableGlobalTestResources, Collections.emptyMap());
this(testClass, profileClass, additionalTestResources, disableGlobalTestResources, Collections.emptyMap(),
Optional.empty());
}

public TestResourceManager(Class<?> testClass, Class<?> profileClass, List<TestResourceClassEntry> additionalTestResources,
boolean disableGlobalTestResources, Map<String, String> devServicesProperties) {
boolean disableGlobalTestResources, Map<String, String> devServicesProperties,
Optional<String> containerNetworkId) {
this.parallelTestResourceEntries = new ArrayList<>();
this.sequentialTestResourceEntries = new ArrayList<>();

Expand All @@ -73,6 +76,11 @@ public TestResourceManager(Class<?> testClass, Class<?> profileClass, List<TestR
public Map<String, String> devServicesProperties() {
return devServicesProperties;
}

@Override
public Optional<String> containerNetworkId() {
return containerNetworkId;
}
};
for (var i : allTestResourceEntries) {
if (i.getTestResource() instanceof DevServicesContext.ContextAware) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.function.Function;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class QuarkusIntegrationTestExtension
private static boolean hasPerTestResources;

private static Map<String, String> devServicesProps;
private static String containerNetworkId;

@Override
public void afterEach(ExtensionContext context) throws Exception {
Expand Down Expand Up @@ -124,7 +126,8 @@ private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactP

ArtifactLauncher.InitContext.DevServicesLaunchResult devServicesLaunchResult = handleDevServices(context,
isDockerLaunch);
QuarkusIntegrationTestExtension.devServicesProps = devServicesLaunchResult.properties();
devServicesProps = devServicesLaunchResult.properties();
containerNetworkId = devServicesLaunchResult.networkId();
quarkusTestProfile = profile;
currentJUnitTestClass = context.getRequiredTestClass();
TestResourceManager testResourceManager = null;
Expand All @@ -139,7 +142,7 @@ private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactP
context.getRequiredTestClass().getClassLoader()),
testProfileAndProperties.testProfile != null
&& testProfileAndProperties.testProfile.disableGlobalTestResources(),
devServicesProps);
devServicesProps, containerNetworkId == null ? Optional.empty() : Optional.of(containerNetworkId));
testResourceManager.init();
hasPerTestResources = testResourceManager.hasPerTestResources();

Expand Down Expand Up @@ -251,7 +254,8 @@ private void injectTestContext(Object testInstance) {
private DevServicesContext createTestContext() {
Map<String, String> devServicesPropsCopy = devServicesProps.isEmpty() ? Collections.emptyMap()
: Collections.unmodifiableMap(devServicesProps);
return new DefaultQuarkusIntegrationTestContext(devServicesPropsCopy);
return new DefaultQuarkusIntegrationTestContext(devServicesPropsCopy,
containerNetworkId == null ? Optional.empty() : Optional.of(containerNetworkId));
}

private void throwBootFailureException() {
Expand Down Expand Up @@ -301,15 +305,23 @@ public ArtifactLauncher.InitContext.DevServicesLaunchResult devServicesLaunchRes

private static class DefaultQuarkusIntegrationTestContext implements DevServicesContext {

private final Map<String, String> map;
private final Map<String, String> devServicesProperties;
private final Optional<String> containerNetworkId;

private DefaultQuarkusIntegrationTestContext(Map<String, String> map) {
this.map = map;
private DefaultQuarkusIntegrationTestContext(Map<String, String> devServicesProperties,
Optional<String> containerNetworkId) {
this.devServicesProperties = devServicesProperties;
this.containerNetworkId = containerNetworkId;
}

@Override
public Map<String, String> devServicesProperties() {
return map;
return devServicesProperties;
}

@Override
public Optional<String> containerNetworkId() {
return containerNetworkId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingDeque;

import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -142,12 +143,12 @@ private int doJavaStart(ExtensionContext context, Class<? extends QuarkusTestPro

//must be done after the TCCL has been set
testResourceManager = (Closeable) startupAction.getClassLoader().loadClass(TestResourceManager.class.getName())
.getConstructor(Class.class, Class.class, List.class, boolean.class, Map.class)
.getConstructor(Class.class, Class.class, List.class, boolean.class, Map.class, Optional.class)
.newInstance(context.getRequiredTestClass(),
profile != null ? profile : null,
getAdditionalTestResources(profileInstance, startupAction.getClassLoader()),
profileInstance != null && profileInstance.disableGlobalTestResources(),
startupAction.getDevServicesProperties());
startupAction.getDevServicesProperties(), Optional.empty());
testResourceManager.getClass().getMethod("init").invoke(testResourceManager);
Map<String, String> properties = (Map<String, String>) testResourceManager.getClass().getMethod("start")
.invoke(testResourceManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ public Thread newThread(Runnable r) {

//must be done after the TCCL has been set
testResourceManager = (Closeable) startupAction.getClassLoader().loadClass(TestResourceManager.class.getName())
.getConstructor(Class.class, Class.class, List.class, boolean.class, Map.class)
.getConstructor(Class.class, Class.class, List.class, boolean.class, Map.class, Optional.class)
.newInstance(requiredTestClass,
profile != null ? profile : null,
getAdditionalTestResources(profileInstance, startupAction.getClassLoader()),
profileInstance != null && profileInstance.disableGlobalTestResources(),
startupAction.getDevServicesProperties());
startupAction.getDevServicesProperties(), Optional.empty());
testResourceManager.getClass().getMethod("init").invoke(testResourceManager);
Map<String, String> properties = (Map<String, String>) testResourceManager.getClass().getMethod("start")
.invoke(testResourceManager);
Expand Down