Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customization of test containers image name #266

Merged
merged 1 commit into from
Feb 28, 2024

Conversation

andythsu
Copy link
Member

@andythsu andythsu commented Feb 27, 2024

Problem: Some tests download trino docker images from public dockerhub. For example:


However, this will fail when running behind a proxy. Our in-house images have different names from the names on dockerhub. Therefore, we need a way to override trinodb/trino to a completely different name when necessary. This will eventually expand to other public images as they are added to the codebase.

Copy link

cla-bot bot commented Feb 27, 2024

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: Andy Su (Apps).
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email [email protected]
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

@andythsu andythsu force-pushed the image_name_substitutor branch from 3cd79ca to aa99a08 Compare February 27, 2024 21:54
Copy link

cla-bot bot commented Feb 27, 2024

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: Andy Su (Apps).
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email [email protected]
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

@andythsu andythsu force-pushed the image_name_substitutor branch from aa99a08 to b4a5de7 Compare February 27, 2024 21:56
@cla-bot cla-bot bot added the cla-signed label Feb 27, 2024
@andythsu andythsu force-pushed the image_name_substitutor branch 2 times, most recently from ba9e9cd to 3fb6096 Compare February 27, 2024 22:10
@Override
public DockerImageName apply(DockerImageName dockerImageName)
{
if (dockerImageName.asCanonicalNameString().equals("trinodb/trino:latest") && System.getenv(TESTCONTAINERS_TRINO_IMAGE_SUBSTITUTE) != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the 1st condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first condition makes sure we are overriding a specific image. If we don't specify it will just substitute all images

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you want to change only Trino image names. I suppose we should tweak the condition to exclude the version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
return "custom image name substitutor";
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testcontainers.properties isn't required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was going to use env variables to configure this. https://java.testcontainers.org/features/configuration/
It looks like we can just choose one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use env variables, why this class is required? I'm looking the following sentence:

Setting environment variables TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/
https://java.testcontainers.org/features/image_name_substitution/#automatically-modifying-docker-hub-image-names

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may not work because it needs image name to be predictable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it needs image name to be predictable

I don't understand this. Can you elaborate on that?

Copy link
Member Author

@andythsu andythsu Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use env variables, why this class is required? I'm looking the following sentence:

Setting environment variables TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/
https://java.testcontainers.org/features/image_name_substitution/#automatically-modifying-docker-hub-image-names

The link you are looking at is specifically adding prefix to image name. It has an assumption:

Consider this if:

Your private registry has copies of images from Docker Hub where the names are predictable, and just adding a prefix is enough. For example, registry.mycompany.com/mirror/mysql:8.0.36 can be derived from the original Docker Hub image name (mysql:8.0.36) with a consistent prefix string: registry.mycompany.com/mirror/

Also, it looks like even though we can change the registry, the names of the image have to match. In our case, we have different image names from the dockerhub.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your explanation.

we have different image names from the dockerhub.

This is the important fact I didn't know. Please leave the code comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

I can't find it.

@andythsu andythsu force-pushed the image_name_substitutor branch from 3fb6096 to 46a14fe Compare February 27, 2024 22:20
@ebyhr ebyhr added the no-release-notes This pull request does not require release notes entry label Feb 27, 2024
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.ImageNameSubstitutor;

public class CustomImageNameSubstitutor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class name is misleading. Please rename to CustomTrinoImageNameSubstitutor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the doc I suppose we can only have one ImageNameSubstitutor, then we use TESTCONTAINERS_IMAGE_SUBSTITUTOR=com.mycompany.testcontainers.ExampleImageNameSubstitutor to tell testcontainer to use this class. Therefore, I think this class should have a generic name as this is the only substitutor that testcontainer will look at.

Copy link
Member

@ebyhr ebyhr Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still disagree with the current name. Even CustomImageNameSubstitutor doesn't explain whether TESTCONTAINERS_IMAGE_SUBSTITUTOR supports single substitutor or several substitutors either.

I don't understand your concern to be honest. There's only one class extending ImageNameSubstitutor in this repository. Who will try setting several substitutors?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the class CustomImageNameSubstitutor is used for other images, CustomImageNameSubstitutor seems right but since this is only applied to trino image, I think it's better off with what ebyhr saids. We can change the name to a more general one if there are other images to substitute in future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still disagree with the current name. Even CustomImageNameSubstitutor doesn't explain whether TESTCONTAINERS_IMAGE_SUBSTITUTOR supports single substitutor or several substitutors either.

I don't understand your concern to be honest. There's only one class extending ImageNameSubstitutor in this repository. Who will try setting several substitutors?

The concern is we can only have one ImageSubstitutor. Naming it to CustomTrinoImageNameSubstitutor is misleading because it might lead to thinking it's only for substituting Trino image. However, this class should be flexible to substitute other images as well. I agree with @Chaho12's suggestion. We can change the name to be generic later as we need.

@andythsu andythsu force-pushed the image_name_substitutor branch from 46a14fe to 4f404b6 Compare February 28, 2024 14:26
@andythsu
Copy link
Member Author

andythsu commented Feb 28, 2024

We probably want to add how to use this class to the documentation. @mosabua could you suggest a good place for adding this doc?

@ebyhr
Copy link
Member

ebyhr commented Feb 28, 2024

We probably want to add how to use this class to the documentation

Most people won't use this feature. Please leave javadoc in the class.

@Override
public DockerImageName apply(DockerImageName dockerImageName)
{
String trinoImage = "trinodb/trino";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to a constant.

@Override
protected String getDescription()
{
return "custom image name substitutor";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "custom image name substitutor";
return "custom Trino image name substitutor";

@andythsu andythsu force-pushed the image_name_substitutor branch 2 times, most recently from 249f71d to 28fb73f Compare February 28, 2024 20:14
@wendigo
Copy link
Contributor

wendigo commented Feb 28, 2024

0:16:09.097 [main] INFO org.testcontainers.utility.ImageNameSubstitutor -- Found configured ImageNameSubstitutor: custom Trino image name substitutor
20:16:09.098 [main] INFO org.testcontainers.utility.ImageNameSubstitutor -- Image name substitution will be performed by: Chained substitutor of 'DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')' and then 'custom Trino image name substitutor'

{
String trinoImageSubstitute = System.getenv(TESTCONTAINERS_TRINO_IMAGE_SUBSTITUTE);
if (dockerImageName.getUnversionedPart().equals(TRINO_IMAGE) && trinoImageSubstitute != null) {
String imageSubstitute = System.getenv(TESTCONTAINERS_TRINO_IMAGE_SUBSTITUTE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant variable. You have it above

Copy link
Contributor

@wendigo wendigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change requested

@andythsu andythsu force-pushed the image_name_substitutor branch from 28fb73f to 8455dd6 Compare February 28, 2024 20:56
@wendigo wendigo merged commit 5fff194 into trinodb:main Feb 28, 2024
2 checks passed
@github-actions github-actions bot added this to the 7 milestone Feb 28, 2024
@andythsu andythsu changed the title allow customization of test containers image name Allow customization of test containers image name Feb 29, 2024
@andythsu andythsu deleted the image_name_substitutor branch September 18, 2024 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed no-release-notes This pull request does not require release notes entry
Development

Successfully merging this pull request may close these issues.

4 participants