dasniko / testcontainers-keycloak

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

Configurable command and cli args #85

Closed danielFesenmeyer closed 1 year ago

danielFesenmeyer commented 1 year ago

Description

It would be nice if the command to run could be configured: Currently "start-dev" is hard-coded, which is great for most testing scenarios. But for some scenarios it would be better to have a more production-like setup, where it makes sense to use the "start" command.

Furthermore, it would be nice, if arbitrary CLI args could be configured for the command.

Motivation

After updating testcontainers-keycloak to the latest version (from 2.1.2 to 2.3.0), most of our selenium tests (running against our keycloak extensions) failed. It turned out that they were failing because the account page loaded seemingly forever... Then I checked what changed in testcontainers-keycloak and found that the command changed from "start" to "start-dev", which amongst other things, disables the cache.

With the following workaround, I explicitly enabled the cache and the selenium tests ran fine again:

public class KeycloakXContainer extends KeycloakContainer {

    public KeycloakXContainer(final String dockerImageName) {
        super(dockerImageName);
    }

    @Override
    protected void configure() {
        super.configure();

        customConfigure();
    }

    private void customConfigure() {
        var commandParts = getCommandParts();
       ...

        /*
         * Have to enable theme caching (which is disabled in dev by default): otherwise pages load too slow, leading to
         * many UI test failures.
         */
        var updatedCommandParts = new ArrayList<>(Arrays.asList(commandParts));
        updatedCommandParts.add("--spi-theme-cache-themes=true");
        updatedCommandParts.add("--spi-theme-cache-templates=true");
        updatedCommandParts.add("--spi-theme-static-max-age=2592000");

        setCommandParts(updatedCommandParts.toArray(new String[0]));
    }

}

Details

If you think this extension could also be useful for others, I would be happy to implement a PR.

danielFesenmeyer commented 1 year ago

Actually, when thinking more about it, instead of CLI args, I could also use the corresponding env vars (like KC_SPI_THEME_CACHE_THEMES=true). Then I wouldn't have to extend KeycloakContainer - but we have already extended it for another reason, so it was pragmatic to implement it this way.

dasniko commented 1 year ago

Hi @danielFesenmeyer thanks for filing this.

After I recognized that people are using the config cli options with this testcontainer in a way that they misconfigure the whole Keycloak setup, I intentionally disabled it. So, as you already found, using the environment variables is the way to set custom config options.

Not setting the start-dev mode, will lead to more confusion and support requests than I'm willing to handle.

Regarding the caching (I did not have any issues with it yet), I'm open for an enhancement that the caching can be enabled and disabled. I'll guess, enabled caching can be default, as during the test executions, only a very few use cases have the requirement to change cached theme settings. Therefore, a method like withDisabledCaching() (or similar) should be available.