dasniko / testcontainers-keycloak

A Testcontainer implementation for Keycloak IAM & SSO.
Apache License 2.0
328 stars 51 forks source link

How to include local dependency #56

Closed simse07 closed 2 years ago

simse07 commented 2 years ago

Hi, thank you for the great project.

How can I include my local dependency together with the extension I want to test?

Example:

I want to test my extension, which has a dependency to my core package e.g. some exception or util classes. When I start the testcontainer test in the extension/src/test while passing withExtensionClassesFrom("target/classes") I get an error org.jboss.modules.ModuleNotFoundException: core.exception.

Maybe we can define multiple paths for withExtensionClassesFrom?

dasniko commented 2 years ago

Hi @simse07, thanks for coming up with this, sounds like a good idea.

I don't know if it's a good idea to include several different classes folders, but I will think about. It may work in your usecase, but others might some classpath errors in some cases!?

What I could think of something like withExtensionLibsFrom(...), this would empower to include 3rd party libs together with an extension.

WDYT?

simse07 commented 2 years ago

Thanks for the fast reply. withExtensionLibsFrom(...) sounds like a good extension to me.

simse07 commented 2 years ago

I was thinking about this and probably I have expressed myself in a misleading way.

The extension lib should be added as module to the Keycloak. So I would rather name it like addCustomModule(...) where you have to specify the name & package of the lib, path to the jar and to the module.xml:

addCustomModule("core", "my.package.core", "../core/target/core.jar", "../core/module.xml")

This will be deployed to /opt/jboss/keycloak/modules/my/package/core/main/core.jar and /opt/jboss/modules/my/package/core/main/module.xml.

What do you think?

dasniko commented 2 years ago

Ah, seems that you reference to the v1.x version of this project and thus to Wildfly-based Keycloak. As the Wildfly distribution is now deprecated and the Quarkus-based distribution is the default, I also deprecated the v1.x version of this project. V2.x uses the Quarkus-based Keycloak. And there, you don't have modules. Unfortunately (? no, not really), I won't add new features to the v1.x branch, only 2.x is now supported.

However, I will add some feature like I mentioned for libs to one of the next releases.

simse07 commented 2 years ago

I was hoping that you would not say that, but totally understandable.

For others who would like to add a module to v1.x:

public class ModuleDto {
    String moduleName; // core
    String modulePackage; // my.package.core, same as the name in the module.xml
    String moduleJarPath; // ../core/target/core.jar
    String modulePath; // ../core/module.xml
}

/* ----------------------------- */

public class KeycloakContainerModule extends KeycloakContainer {
    private ModuleDto moduleDto;

    public KeycloakContainerModule withModuleDeployment(ModuleDto moduleDto) {
        this.moduleDto = moduleDto;
        return (KeycloakContainerModule) self();
    }

    @Override
    protected void configure() {
        super.configure();
        if (moduleDto != null) {
            createKeycloakModuleDeployment(moduleDto);
        }
    }

    public void createKeycloakModuleDeployment(ModuleDto moduleDto) {
        String deploymentLocation = "/opt/jboss/keycloak/modules/" + moduleDto.modulePackage.replace(".", "/") + "/main/";
        withCopyFileToContainer(MountableFile.forHostPath(Paths.get(moduleDto.moduleJarPath)), deploymentLocation + moduleDto.moduleName + ".jar");
        withCopyFileToContainer(MountableFile.forHostPath(Paths.get(moduleDto.modulePath)), deploymentLocation + "module.xml");
    }
}
dasniko commented 2 years ago

Nice, thanks. 🙂 If you don't mind, I will convert this issue to a discussion thread, so it is saved for others. I'll create another issue to add the withExtensionLibsFrom() as discussed above.