fix: refining how the junit Keycloak is launched (#46182)

closes: #46160

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2026-02-11 10:44:43 -05:00 committed by GitHub
parent 783ea304af
commit 27fb8fae5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 9 deletions

View file

@ -184,7 +184,7 @@ public class Picocli {
// to reuse the previous result either means we need to duplicate the logic in the execute method
// or refactor the above logic so that it happens in the command logic
// We could also reduce the memory footprint of the ParseResult, but that looks a little hackish
int exitCode = cmd.execute(argArray);
int exitCode = execute(cmd, argArray);
exit(exitCode);
} catch (ParameterException parEx) {
@ -194,6 +194,10 @@ public class Picocli {
}
}
protected int execute(CommandLine cmd, String[] argArray) {
return cmd.execute(argArray);
}
public Optional<AbstractCommand> getParsedCommand() {
return parsedCommand;
}

View file

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.keycloak.common.Version;
import org.keycloak.common.crypto.FipsMode;
@ -35,6 +36,7 @@ import org.keycloak.platform.Platform;
import org.keycloak.quarkus.runtime.Environment;
import org.keycloak.quarkus.runtime.KeycloakMain;
import org.keycloak.quarkus.runtime.cli.Picocli;
import org.keycloak.quarkus.runtime.cli.command.AbstractAutoBuildCommand;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.IgnoredArtifacts;
@ -55,6 +57,7 @@ import io.quarkus.maven.dependency.Dependency;
import io.quarkus.maven.dependency.DependencyBuilder;
import io.quarkus.runtime.configuration.QuarkusConfigFactory;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import picocli.CommandLine;
import static java.util.Optional.ofNullable;
@ -206,7 +209,9 @@ public class Keycloak {
curated = builder.build().bootstrap();
AugmentAction action = curated.createAugmentor();
Environment.setHomeDir(homeDir);
initSys(args.toArray(String[]::new));
if (!initSys(args.toArray(String[]::new))) {
return this;
}
System.setProperty(Environment.KC_TEST_REBUILD, "true");
StartupAction startupAction = action.createInitialRuntimeApplication();
System.getProperties().remove(Environment.KC_TEST_REBUILD);
@ -215,7 +220,7 @@ public class Keycloak {
return this;
} catch (Exception cause) {
throw new RuntimeException("Fail to start the server", cause);
throw new RuntimeException("Failed to start the server", cause);
}
}
@ -326,7 +331,8 @@ public class Keycloak {
* Uses a dummy {@link Picocli} to process the args and set system
* variables needed to run augmentation
*/
public static void initSys(String... args) {
public static boolean initSys(String... args) {
AtomicBoolean result = new AtomicBoolean();
Picocli picocli = new Picocli() {
@Override
@ -339,12 +345,21 @@ public class Keycloak {
throw new AssertionError();
}
@Override
protected int execute(CommandLine cmd, String[] argArray) {
if (this.getParsedCommand().filter(ac -> ac instanceof AbstractAutoBuildCommand).isPresent()) {
return super.execute(cmd, argArray);
}
return 0;
}
@Override
public void exit(int exitCode) {
// do nothing
result.set(exitCode == AbstractAutoBuildCommand.REBUILT_EXIT_CODE);
}
};
picocli.parseAndRun(List.of(args));
System.setProperty(Environment.KC_CONFIG_BUILT, "true");
return result.get();
}
}

View file

@ -110,7 +110,9 @@ public class CLITestExtension extends QuarkusMainTestExtension {
result = dist.run(List.of(launch.value()));
}
} else {
Keycloak.initSys(launch == null ? new String[] {} : launch.value());
if (!Keycloak.initSys(launch == null ? new String[] {} : launch.value())) {
return;
}
configureProfile(context);
super.beforeEach(context);
}

View file

@ -49,7 +49,7 @@ public class BasicTimerProviderFactory implements TimerProviderFactory {
@Override
public void init(Config.Scope config) {
transactionTimeout = config.getInt(TRANSACTION_TIMEOUT, 0);
timer = new Timer();
timer = new Timer(true);
}
@Override
@ -59,8 +59,10 @@ public class BasicTimerProviderFactory implements TimerProviderFactory {
@Override
public void close() {
timer.cancel();
timer = null;
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override