dasniko / testcontainers-keycloak

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

Image name substitution not working #78

Closed m4st3rm4tz3 closed 2 years ago

m4st3rm4tz3 commented 2 years ago

Describe the bug

I tried to use the the testcontainers-keycloak extension with an automatic image name substitution, but it turns out this doesn't work. I want to add a prefix to the image name from the property hub.image.name.prefix which is defined in a testcontainers.properties file inside my test resource folder, but the prefix is not added.

Version

2.2.2

Expected behavior

The prefix from the property hub.image.name.prefix should be added to the image name.

Actual behavior

The prefix from the property hub.image.name.prefix is not added to the image name.

How to Reproduce?

No response

Relevant log output

No response

Anything else?

In addition to the keycloak container I have a MongoDbContainer and a GenericContainer from the testcontainers library in my integration tests. With those the image name substitution works fine.

dasniko commented 2 years ago

I can't reproduce your described behavior.

How do you use/instantiate the KeycloakContainer? Do you just use the default constructor without a custom image name (new KeycloakContainer())? In this case, the default image quay.io/keycloak/keycloak:<version> is used, which already contains a repo prefix, so no other prefix is added (like also mentioned in the Testcontainers docs).

If you want to use an image from another repository and use the repository from the testcontainers.properties file given at the property hub.image.name.prefix, you should instantiate the container class e.g. with new KeycloakContainer("keycloak/keycloak"). Testcontainers then successfully tries to use the image from the repository specified in the properties file.

m4st3rm4tz3 commented 2 years ago

Okay, sorry I didn't elaborate more. Here is a more detailed description on what I am doing:

  1. I set up the containers in my test class like this:
    
    @Container
    private static final MongoDBContainer MONGO_DB_CONTAINER = new MongoDBContainer("mongo:5.0")
        .withCopyFileToContainer(MountableFile.forClasspathResource("data/persons.json"), "data/persons.json")
        .withExposedPorts(27017);

@Container private static final KeycloakContainer KEYCLOAK_CONTAINER = new KeycloakContainer("quay.io/keycloak/keycloak:18.0.0") .withRealmImportFile("data/realm-export.json") .withExposedPorts(8080);


2. In my test resources folder i have the `testcontainers.properties` file containing this line:

hub.image.name.prefix=my.company/



3. I tagged the testcontainers images locally with the following commands, so that they are found:
`docker tag testcontainers/ryuk:0.3.3 my.company/testcontainers/ryuk:0.3.3`
`docker tag mongo:5.0 my.company/mongo:5.0`

Since I didn't tag the keycloak image I am expecting my tests to fail, because `my.company/quay.io/keycloak/keycloak:18.0.0` should not be found. But the tests actually pass and if I take a look with the command `docker containers ls` during the execution i see three containers: `my.company/testcontainers/ryuk:0.3.3`, `my.company/mongo:5.0` and `quay.io/keycloak/keycloak:18.0.0`.

I have this repo containing a spring boot project, which was meant as a small proof of concept of setting up everything we need. You can reproduce the behaviour by just running the gradle task `test` (and tagging the images locally with the my.company prefix beforehand). [reactive-spring-graphql-keycloak-demo](https://github.com/m4st3rm4tz3/reactive-spring-graphql-keycloak-demo)
dasniko commented 2 years ago

Everything is working as expected. From the Testcontainers.org documentation:

Testcontainers will not apply the prefix to:

  • non-Hub image names (e.g. where another registry is set)
  • Docker Hub image names where the hub registry is explicitly part of the name (i.e. anything with a docker.io or registry.hub.docker.com host part)

As you are specifiying „quay.io/keycloak/keycloak“, this name already contains a registry part (quay.io), so it won‘t be substituted, as mentioned in the quote above. If you‘d use only „keycloak/keycloak“ as your image name, the registry prefix from your properties file will be used (like it is in your mongo container).

m4st3rm4tz3 commented 2 years ago

Oh, I somehow totally overlooked this part of the documentation. Then eveything is really working as expected and I will use another way to make it work on our pipeline.

Thanks for the great testcontainers plugin. It really makes it easier to write integration tests for keylcoak! 👍🏻