Extract JUnit5 support in the distributoin testsuite to a separate module

Closes #19552
This commit is contained in:
Pedro Igor 2023-04-06 20:53:23 -03:00 committed by Václav Muzikář
parent 37af5fbffe
commit 83676bf927
36 changed files with 413 additions and 201 deletions

View file

@ -1464,6 +1464,11 @@
<artifactId>keycloak-quarkus-server-app</artifactId> <artifactId>keycloak-quarkus-server-app</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-junit5</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-ui</artifactId> <artifactId>keycloak-admin-ui</artifactId>

View file

@ -41,7 +41,37 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-quarkus-server</artifactId> <artifactId>keycloak-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.approvaltests</groupId>
<artifactId>approvaltests</artifactId>
<version>${approvaltests.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bc-fips</artifactId>
<version>${bouncycastle.fips.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-fips</artifactId>
<version>${bouncycastle.pkixfips.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bctls-fips</artifactId>
<version>${bouncycastle.tlsfips.version}</version>
<scope>test</scope>
</dependency> </dependency>
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order --> <!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
@ -58,58 +88,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-quarkus-dist</artifactId>
<type>zip</type>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>com.approvaltests</groupId>
<artifactId>approvaltests</artifactId>
<version>${approvaltests.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cockroachdb</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http-shared</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -1,12 +0,0 @@
package org.keycloak.it.junit5.extension.approvalTests;
import org.approvaltests.namer.NamedEnvironment;
import org.approvaltests.namer.NamerFactory;
public class KcNamerFactory extends NamerFactory {
public static NamedEnvironment asWindowsOsSpecificTest()
{
return asMachineSpecificTest(new WindowsOrUnixOsEnvironmentLabeller());
}
}

View file

@ -1,24 +0,0 @@
package org.keycloak.it.junit5.extension.approvalTests;
import org.lambda.functions.Function0;
import java.util.Locale;
public class WindowsOrUnixOsEnvironmentLabeller implements Function0<String> {
private static final String WINDOWS_NAME = "windows";
private static final String UNIX_NAME = "unix";
@Override
public String call()
{
String osName = System.getProperty("os.name");
if(osName.toLowerCase(Locale.ROOT).contains(WINDOWS_NAME)) {
return WINDOWS_NAME;
}
//unix suffices, as basically all other OSses use sh files
return UNIX_NAME;
}
}

View file

@ -1,69 +0,0 @@
/*
* Copyright 2022 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.it.utils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.resolution.ArtifactRequest;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
import io.quarkus.bootstrap.utils.BuildToolHelper;
public final class Maven {
public static Path resolveArtifact(String groupId, String artifactId) {
try {
Path projectDir = BuildToolHelper.getProjectDir(Paths.get(Maven.class.getResource(".").toURI()));
BootstrapMavenContext ctx = new BootstrapMavenContext(
BootstrapMavenContext.config().setPreferPomsFromWorkspace(true).setWorkspaceModuleParentHierarchy(true)
.setCurrentProject(projectDir.toString()));
LocalProject project = ctx.getCurrentProject();
RepositorySystem repositorySystem = ctx.getRepositorySystem();
List<RemoteRepository> remoteRepositories = ctx.getRemoteRepositories();
ArtifactDescriptorResult descrResult = repositorySystem.readArtifactDescriptor(
ctx.getRepositorySystemSession(),
new ArtifactDescriptorRequest()
.setArtifact(new DefaultArtifact(project.getGroupId(), project.getArtifactId(), "pom", project.getVersion()))
.setRepositories(remoteRepositories));
for (org.eclipse.aether.graph.Dependency dependency : descrResult.getManagedDependencies()) {
Artifact artifact = dependency.getArtifact();
if (artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId)) {
return repositorySystem.resolveArtifact(
ctx.getRepositorySystemSession(),
new ArtifactRequest().setArtifact(artifact)
.setRepositories(remoteRepositories))
.getArtifact().getFile().toPath();
}
}
} catch (Exception cause) {
throw new RuntimeException("Failed to resolve artifact", cause);
}
return null;
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.it.approvaltests;
import org.approvaltests.namer.NamedEnvironment;
import org.approvaltests.namer.NamerFactory;
public class KcNamerFactory extends NamerFactory {
public static NamedEnvironment asWindowsOsSpecificTest()
{
return asMachineSpecificTest(new WindowsOrUnixOsEnvironmentLabeller());
}
}

View file

@ -0,0 +1,41 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.it.approvaltests;
import org.lambda.functions.Function0;
import java.util.Locale;
public class WindowsOrUnixOsEnvironmentLabeller implements Function0<String> {
private static final String WINDOWS_NAME = "windows";
private static final String UNIX_NAME = "unix";
@Override
public String call()
{
String osName = System.getProperty("os.name");
if(osName.toLowerCase(Locale.ROOT).contains(WINDOWS_NAME)) {
return WINDOWS_NAME;
}
//unix suffices, as basically all other OSses use sh files
return UNIX_NAME;
}
}

View file

@ -21,8 +21,12 @@ import static org.junit.Assert.assertEquals;
import static org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.OPTIMIZED_BUILD_OPTION_LONG; import static org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.OPTIMIZED_BUILD_OPTION_LONG;
import java.util.List; import java.util.List;
import org.approvaltests.Approvals;
import org.approvaltests.namer.NamedEnvironment;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.condition.OS;
import org.keycloak.it.approvaltests.KcNamerFactory;
import org.keycloak.it.junit5.extension.CLIResult; import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest; import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.junit5.extension.RawDistOnly; import org.keycloak.it.junit5.extension.RawDistOnly;
@ -44,42 +48,42 @@ public class HelpCommandDistTest {
@Launch({}) @Launch({})
void testDefaultToHelp(LaunchResult result) { void testDefaultToHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ "--help" }) @Launch({ "--help" })
void testHelp(LaunchResult result) { void testHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ "-h" }) @Launch({ "-h" })
void testHelpShort(LaunchResult result) { void testHelpShort(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Start.NAME, "--help", OPTIMIZED_BUILD_OPTION_LONG}) @Launch({ Start.NAME, "--help", OPTIMIZED_BUILD_OPTION_LONG})
void testStartOptimizedHelp(LaunchResult result) { void testStartOptimizedHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Start.NAME, "--help" }) @Launch({ Start.NAME, "--help" })
void testStartHelp(LaunchResult result) { void testStartHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Start.NAME, "--optimized", "--help-all" }) @Launch({ Start.NAME, "--optimized", "--help-all" })
void testStartOptimizedHelpAll(LaunchResult result) { void testStartOptimizedHelpAll(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
cliResult.assertNoMessage("--storage "); cliResult.assertNoMessage("--storage ");
} }
@ -87,21 +91,21 @@ public class HelpCommandDistTest {
@Launch({ StartDev.NAME, "--help" }) @Launch({ StartDev.NAME, "--help" })
void testStartDevHelp(LaunchResult result) { void testStartDevHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ StartDev.NAME, "--help-all" }) @Launch({ StartDev.NAME, "--help-all" })
void testStartDevHelpAll(LaunchResult result) { void testStartDevHelpAll(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Start.NAME, "--help-all" }) @Launch({ Start.NAME, "--help-all" })
void testStartHelpAll(LaunchResult result) { void testStartHelpAll(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
cliResult.assertMessage("--storage"); cliResult.assertMessage("--storage");
} }
@ -109,35 +113,35 @@ public class HelpCommandDistTest {
@Launch({ Build.NAME, "--help" }) @Launch({ Build.NAME, "--help" })
void testBuildHelp(LaunchResult result) { void testBuildHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Export.NAME, "--help" }) @Launch({ Export.NAME, "--help" })
void testExportHelp(LaunchResult result) { void testExportHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Export.NAME, "--help-all" }) @Launch({ Export.NAME, "--help-all" })
void testExportHelpAll(LaunchResult result) { void testExportHelpAll(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Import.NAME, "--help" }) @Launch({ Import.NAME, "--help" })
void testImportHelp(LaunchResult result) { void testImportHelp(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@Launch({ Import.NAME, "--help-all" }) @Launch({ Import.NAME, "--help-all" })
void testImportHelpAll(LaunchResult result) { void testImportHelpAll(LaunchResult result) {
CLIResult cliResult = (CLIResult) result; CLIResult cliResult = (CLIResult) result;
cliResult.assertHelp(); assertHelp(cliResult);
} }
@Test @Test
@ -159,4 +163,12 @@ public class HelpCommandDistTest {
private void assertSingleJvmStarted(CLIResult run) { private void assertSingleJvmStarted(CLIResult run) {
assertEquals(1, run.getOutputStream().stream().filter(s -> s.contains("Listening for transport dt_socket")).count()); assertEquals(1, run.getOutputStream().stream().filter(s -> s.contains("Listening for transport dt_socket")).count());
} }
private void assertHelp(CLIResult result) {
try (NamedEnvironment env = KcNamerFactory.asWindowsOsSpecificTest()) {
Approvals.verify(result.getOutput());
} catch (Exception cause) {
throw new RuntimeException("Failed to assert help", cause);
}
}
} }

View file

@ -0,0 +1,95 @@
<?xml version="1.0"?>
<!--
~ Copyright 2021 Red Hat, Inc. and/or its affiliates
~ and other contributors as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>keycloak-quarkus-test-parent</artifactId>
<groupId>org.keycloak</groupId>
<version>999.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>Keycloak Quarkus Server JUnit 5 Internal Test Framework</name>
<artifactId>keycloak-junit5</artifactId>
<packaging>jar</packaging>
<properties>
<kc.quarkus.tests.dist>raw</kc.quarkus.tests.dist>
<approvaltests.version>14.0.0</approvaltests.version>
<build-helper-maven-plugin.version>3.3.0</build-helper-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-quarkus-server</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-quarkus-dist</artifactId>
<type>zip</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cockroachdb</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http-shared</artifactId>
</dependency>
</dependencies>
</project>

View file

@ -30,10 +30,7 @@ import java.util.regex.Pattern;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.approvaltests.Approvals;
import io.quarkus.test.junit.main.LaunchResult; import io.quarkus.test.junit.main.LaunchResult;
import org.approvaltests.namer.NamedEnvironment;
import org.keycloak.it.junit5.extension.approvalTests.KcNamerFactory;
public interface CLIResult extends LaunchResult { public interface CLIResult extends LaunchResult {
@ -82,14 +79,6 @@ public interface CLIResult extends LaunchResult {
() -> "The Error Output:\n " + getErrorOutput() + "\ndoesn't contains " + msg); () -> "The Error Output:\n " + getErrorOutput() + "\ndoesn't contains " + msg);
} }
default void assertHelp() {
try (NamedEnvironment env = KcNamerFactory.asWindowsOsSpecificTest()) {
Approvals.verify(getOutput());
} catch (Exception cause) {
throw new RuntimeException("Failed to assert help", cause);
}
}
default void assertMessage(String message) { default void assertMessage(String message) {
assertThat(getOutput(), containsString(message)); assertThat(getOutput(), containsString(message));
} }

View file

@ -7,10 +7,14 @@ import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame; import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.ToStringConsumer; import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.RemoteDockerImage;
import org.testcontainers.images.builder.ImageFromDockerfile; import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.LazyFuture;
import org.testcontainers.utility.ResourceReaper; import org.testcontainers.utility.ResourceReaper;
import java.io.File; import java.io.File;
import java.lang.reflect.Field;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -27,9 +31,6 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
private String stdout = ""; private String stdout = "";
private String stderr = ""; private String stderr = "";
private ToStringConsumer backupConsumer = new ToStringConsumer(); private ToStringConsumer backupConsumer = new ToStringConsumer();
private File distributionFile = new File("../../dist/target/keycloak-" + Version.VERSION + ".tar.gz");
private File dockerFile = new File("../../container/Dockerfile");
private File dockerScriptFile = new File("../../container/ubi-null.sh"); private File dockerScriptFile = new File("../../container/ubi-null.sh");
private GenericContainer<?> keycloakContainer = null; private GenericContainer<?> keycloakContainer = null;
@ -43,16 +44,31 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
} }
private GenericContainer getKeycloakContainer() { private GenericContainer getKeycloakContainer() {
File distributionFile = new File("../../dist/" + File.separator + "target" + File.separator + "keycloak-" + Version.VERSION + ".tar.gz");
if (!distributionFile.exists()) {
distributionFile = Maven.resolveArtifact("org.keycloak", "keycloak-quarkus-dist").toFile();
}
if (!distributionFile.exists()) { if (!distributionFile.exists()) {
throw new RuntimeException("Distribution archive " + distributionFile.getAbsolutePath() +" doesn't exist"); throw new RuntimeException("Distribution archive " + distributionFile.getAbsolutePath() +" doesn't exist");
} }
return new GenericContainer(
new ImageFromDockerfile("keycloak-under-test", false) File dockerFile = new File("../../container/Dockerfile");
LazyFuture<String> image;
if (dockerFile.exists()) {
image = new ImageFromDockerfile("keycloak-under-test", false)
.withFileFromFile("keycloak.tar.gz", distributionFile) .withFileFromFile("keycloak.tar.gz", distributionFile)
.withFileFromFile("ubi-null.sh", dockerScriptFile) .withFileFromFile("ubi-null.sh", dockerScriptFile)
.withFileFromFile("Dockerfile", dockerFile) .withFileFromFile("Dockerfile", dockerFile)
.withBuildArg("KEYCLOAK_DIST", "keycloak.tar.gz") .withBuildArg("KEYCLOAK_DIST", "keycloak.tar.gz");
) toString();
} else {
image = new RemoteDockerImage(DockerImageName.parse("quay.io/keycloak/keycloak"));
}
return new GenericContainer(image)
.withExposedPorts(8080) .withExposedPorts(8080)
.withStartupAttempts(1) .withStartupAttempts(1)
.withStartupTimeout(Duration.ofSeconds(120)) .withStartupTimeout(Duration.ofSeconds(120))
@ -78,9 +94,6 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
containerId = keycloakContainer.getContainerId(); containerId = keycloakContainer.getContainerId();
waitForStableOutput(); waitForStableOutput();
// TODO: this is based on a lot of assumptions
io.restassured.RestAssured.port = keycloakContainer.getMappedPort(8080);
} catch (Exception cause) { } catch (Exception cause) {
this.exitCode = -1; this.exitCode = -1;
this.stdout = backupConsumer.toUtf8String(); this.stdout = backupConsumer.toUtf8String();
@ -90,9 +103,23 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
LOGGER.warn("Failed to start Keycloak container", cause); LOGGER.warn("Failed to start Keycloak container", cause);
} }
trySetRestAssuredPort();
return CLIResult.create(getOutputStream(), getErrorStream(), getExitCode()); return CLIResult.create(getOutputStream(), getErrorStream(), getExitCode());
} }
private void trySetRestAssuredPort() {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class<?> restAssured = classLoader.loadClass("io.restassured.RestAssured");
Field port = restAssured.getDeclaredField("port");
port.set(null, keycloakContainer.getMappedPort(8080));
} catch (Exception ignore) {
// keeping the workaround to set the container port to restassured
// TODO: better way to expose the port to tests
}
}
// After the web server is responding we are still producing some logs that got checked in the tests // After the web server is responding we are still producing some logs that got checked in the tests
private void waitForStableOutput() { private void waitForStableOutput() {
int retry = 10; int retry = 10;

View file

@ -0,0 +1,133 @@
/*
* Copyright 2022 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.it.utils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
import io.quarkus.bootstrap.utils.BuildToolHelper;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
public final class Maven {
public static Path resolveArtifact(String groupId, String artifactId) {
try {
Path classPathDir = Paths.get(Thread.currentThread().getContextClassLoader().getResource(".").toURI());
Path projectDir = BuildToolHelper.getProjectDir(classPathDir);
BootstrapMavenContext ctx = new BootstrapMavenContext(
BootstrapMavenContext.config().setPreferPomsFromWorkspace(true).setWorkspaceModuleParentHierarchy(true)
.setCurrentProject(projectDir.toString()));
LocalProject project = ctx.getCurrentProject();
RepositorySystem repositorySystem = ctx.getRepositorySystem();
List<RemoteRepository> remoteRepositories = ctx.getRemoteRepositories();
ArtifactDescriptorResult projectDescriptor = repositorySystem.readArtifactDescriptor(
ctx.getRepositorySystemSession(),
new ArtifactDescriptorRequest()
.setArtifact(new DefaultArtifact(project.getGroupId(), project.getArtifactId(), "pom", project.getVersion()))
.setRepositories(remoteRepositories));
Artifact artifact = resolveArtifact(groupId, artifactId, projectDescriptor.getDependencies());
if (artifact == null) {
resolveArtifact(groupId, artifactId, projectDescriptor.getManagedDependencies());
}
if (artifact == null) {
artifact = resolveArtifactRecursively(ctx, projectDescriptor, groupId, artifactId);
}
if (artifact == null) {
throw new RuntimeException("Failed to resolve artifact [" + groupId + ":" + artifactId + "] from project [" + projectDescriptor.getArtifact() + "] dependency graph");
}
return repositorySystem.resolveArtifact(
ctx.getRepositorySystemSession(),
new ArtifactRequest().setArtifact(artifact)
.setRepositories(remoteRepositories))
.getArtifact().getFile().toPath();
} catch (Exception cause) {
throw new RuntimeException("Failed to resolve artifact: " + groupId + ":" + artifactId, cause);
}
}
private static Artifact resolveArtifact(String groupId, String artifactId, List<Dependency> dependencies) {
for (Dependency dependency : dependencies) {
Artifact artifact = dependency.getArtifact();
if (artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId)) {
return artifact;
}
}
return null;
}
private static Artifact resolveArtifactRecursively(BootstrapMavenContext ctx, ArtifactDescriptorResult artifactDescriptor, String groupId, String artifactId) throws BootstrapMavenException, DependencyResolutionException {
CollectRequest collectRequest = MavenArtifactResolver.newCollectRequest(artifactDescriptor.getArtifact(), artifactDescriptor.getDependencies(),
List.of(),
List.of(),
ctx.getRemoteRepositories());
List<ArtifactResult> artifactResults = ctx.getRepositorySystem().resolveDependencies(ctx.getRepositorySystemSession(),
new DependencyRequest()
.setFilter(new DependencyFilter() {
@Override
public boolean accept(DependencyNode node, List<DependencyNode> parents) {
Dependency dependency = node.getDependency();
if (dependency == null) {
return false;
}
Artifact artifact = dependency.getArtifact();
return artifact.getGroupId().equals(groupId)
&& artifact.getArtifactId().equals(artifactId);
}
})
.setCollectRequest(collectRequest))
.getArtifactResults();
if (artifactResults.isEmpty()) {
return null;
}
if (artifactResults.size() > 1) {
throw new RuntimeException("Unexpected number of resolved artifacts: " + artifactResults);
}
return artifactResults.get(0).getArtifact();
}
}

View file

@ -375,11 +375,15 @@ public final class RawKeycloakDistribution implements KeycloakDistribution {
distRootPath.toFile().mkdirs(); distRootPath.toFile().mkdirs();
File distFile = new File("../../dist/" + File.separator + "target" + File.separator + "keycloak-" + Version.VERSION + ".zip"); File distFile = new File("../../dist/" + File.separator + "target" + File.separator + "keycloak-" + Version.VERSION + ".zip");
if (!distFile.exists()) { String distDirName;
throw new RuntimeException("Distribution archive " + distFile.getAbsolutePath() +" doesn't exist");
if (distFile.exists()) {
distDirName = distFile.getName();
} else {
distFile = Maven.resolveArtifact("org.keycloak", "keycloak-quarkus-dist").toFile();
distDirName = distFile.getName().replace("-quarkus-dist", "");
} }
distRootPath.toFile().mkdirs(); distRootPath.toFile().mkdirs();
String distDirName = distFile.getName();
Path dPath = distRootPath.resolve(distDirName.substring(0, distDirName.lastIndexOf('.'))); Path dPath = distRootPath.resolve(distDirName.substring(0, distDirName.lastIndexOf('.')));
if (!inited || (reCreate || !dPath.toFile().exists())) { if (!inited || (reCreate || !dPath.toFile().exists())) {

View file

@ -32,6 +32,10 @@
<artifactId>keycloak-quarkus-test-parent</artifactId> <artifactId>keycloak-quarkus-test-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules>
<module>junit5</module>
</modules>
<profiles> <profiles>
<profile> <profile>
<id>noIntegrations</id> <id>noIntegrations</id>

View file

@ -180,7 +180,7 @@
<!-- Embedded Distribution --> <!-- Embedded Distribution -->
<dependency> <dependency>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-quarkus-integration-tests</artifactId> <artifactId>keycloak-junit5</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -1147,7 +1147,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-quarkus-integration-tests</artifactId> <artifactId>keycloak-junit5</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<exclusions> <exclusions>