Skip to content

[Enhancement]: Expose compatibleSubstituteFor() through API #7305

Description

@oleg-nenashev

Module

Core

Proposal

I would like to allow unversioned images like wiremock/wiremock:test if and only if compatibleSubstituteFor is explicitly set by the users. By default, IMHO the validation should fail, because we cannot compare against the minimum required version.

At the moment it is not possible, because there is no getter method for compatibleSubstituteFor

Sample code

    public WireMockContainer(DockerImageName dockerImage) {
        super(dockerImage);
        dockerImage.assertCompatibleWith(new DockerImageName(OFFICIAL_IMAGE_NAME));

        // Verify the minimum version for the official image
        final ComparableVersion version = new ComparableVersion(dockerImage.getVersionPart());
        if (!version.isSemanticVersion()) { // Accept only images when compatibility is declared explicitly
            // TODO: We cannot extract compatibleSubstituteFor from Testcontainers API
        } else {
            boolean isLessThanBaseVersion = version.isLessThan(WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
            if (OFFICIAL_IMAGE_NAME.equals(dockerImage.getUnversionedPart()) && isLessThanBaseVersion) {
                throw new IllegalArgumentException("For the official image, the WireMock version must be >= " + WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
            }
        }

      // ...

Sample Test

    @Test
    @Disabled("Requires https://github.com/testcontainers/testcontainers-java/issues/7305")
    public void shouldFailForUnversionedImage() {
        IllegalStateException ex = Assertions.assertThrows(IllegalStateException.class, () -> {
            WireMockContainer container = new WireMockContainer(
                    new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "test"));
        });
        assertThat(ex.getMessage())
                .as("Wrong exception message")
                .contains("Failed to verify that image")
                .contains("is a compatible substitute for '" + WireMockContainer.OFFICIAL_IMAGE_NAME + "'");
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions