task: refine DistributionTest.keepAlive (#27612)

closes: #26981

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2024-03-11 06:38:55 -04:00 committed by GitHub
parent a6ec682d97
commit ccc4cd2ab4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 16 deletions

View file

@ -97,12 +97,12 @@ public class CLITestExtension extends QuarkusMainTestExtension {
infinispanContainer = configureExternalInfinispan(context);
if (distConfig != null) {
onKeepServerAlive(context.getRequiredTestMethod().getAnnotation(KeepServerAlive.class));
if (dist == null) {
dist = createDistribution(distConfig, getStoreConfig(context), getDatabaseConfig(context));
}
onKeepServerAlive(context.getRequiredTestMethod().getAnnotation(KeepServerAlive.class), true);
copyTestProvider(context.getRequiredTestClass().getAnnotation(TestProvider.class));
copyTestProvider(context.getRequiredTestMethod().getAnnotation(TestProvider.class));
onBeforeStartDistribution(context.getRequiredTestClass().getAnnotation(BeforeStartDistribution.class));
@ -163,10 +163,10 @@ public class CLITestExtension extends QuarkusMainTestExtension {
}
}
private void onKeepServerAlive(KeepServerAlive annotation) {
private void onKeepServerAlive(KeepServerAlive annotation, boolean setting) {
if(annotation != null && dist != null) {
try {
dist.setManualStop(true);
dist.setManualStop(setting);
} catch (Exception cause) {
throw new RuntimeException("Error when invoking " + annotation, cause);
}
@ -177,12 +177,11 @@ public class CLITestExtension extends QuarkusMainTestExtension {
public void afterEach(ExtensionContext context) throws Exception {
DistributionTest distConfig = getDistributionConfig(context);
if (distConfig != null) {
if (distConfig.keepAlive()) {
if (dist != null) {
onKeepServerAlive(context.getRequiredTestMethod().getAnnotation(KeepServerAlive.class), false);
dist.stop();
}
if (DistributionTest.ReInstall.BEFORE_TEST.equals(distConfig.reInstall())) {
if (distConfig != null && DistributionTest.ReInstall.BEFORE_TEST.equals(distConfig.reInstall())) {
dist = null;
}
}

View file

@ -29,6 +29,9 @@ import org.junit.jupiter.api.extension.ExtendWith;
public @interface DistributionTest {
boolean debug() default false;
/**
* If the distribution should be left running after the launch.
*/
boolean keepAlive() default false;
boolean enableTls() default false;

View file

@ -17,13 +17,10 @@
package org.keycloak.it.junit5.extension;
import org.keycloak.it.utils.KeycloakDistribution;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Consumer;
/**
* {@link KeepServerAlive} is used in a distributiontest to keep the server alive on test / method level.

View file

@ -52,7 +52,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
this.envVars.put(name, value);
}
private GenericContainer getKeycloakContainer() {
private GenericContainer<?> getKeycloakContainer() {
File distributionFile = new File("../../dist/" + File.separator + "target" + File.separator + "keycloak-" + Version.VERSION + ".tar.gz");
if (!distributionFile.exists()) {
@ -77,7 +77,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
image = new RemoteDockerImage(DockerImageName.parse("quay.io/keycloak/keycloak"));
}
return new GenericContainer(image)
return new GenericContainer<>(image)
.withEnv(envVars)
.withExposedPorts(8080)
.withStartupAttempts(1)
@ -113,6 +113,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
LOGGER.warn("Failed to start Keycloak container", cause);
} finally {
if (!manualStop) {
stop();
envVars.clear();
}
}
@ -181,7 +182,6 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
private void cleanupContainer() {
if (containerId != null) {
try {
final String finalContainerId = containerId;
Runnable reaper = new Runnable() {
@Override
public void run() {
@ -253,7 +253,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
}
if (type.isInstance(this)) {
return (D) this;
return type.cast(this);
}
throw new IllegalArgumentException("Not a " + type + " type");