diff --git a/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java b/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java index eb9169fd1ac..48c588d0e60 100644 --- a/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java +++ b/core/src/test/java/org/testcontainers/junit/wait/strategy/HttpWaitStrategyTest.java @@ -4,6 +4,7 @@ import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import org.rnorth.ducttape.RetryCountExceededException; +import org.testcontainers.TestImages; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; import org.testcontainers.images.builder.ImageFromDockerfile; @@ -66,10 +67,15 @@ void testWaitUntilReadyWithSuccessWithCustomHeaders() { @Test void testWaitUntilReadyWithTlsAndAllowUnsecure() { try ( - GenericContainer container = startContainerWithCommand( - createHttpsShellCommand("200 OK", GOOD_RESPONSE_BODY, 8080), - createHttpWaitStrategy(ready).usingTls().allowInsecure() - ) + GenericContainer container = new GenericContainer<>(createHttpsImage()) + .withExposedPorts(8080) + .withCommand("sh", "-c", createHttpsShellCommand("200 OK", GOOD_RESPONSE_BODY, 8080)) + .waitingFor( + createHttpWaitStrategy(ready) + .usingTls() + .allowInsecure() + .withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS)) + ) ) { waitUntilReadyAndSucceed(container); } @@ -287,10 +293,29 @@ private String createShellCommand(String header, String responseBody, int port) ); } + /** + * Image with {@code ncat} and a pre-generated self-signed certificate. Installing packages and generating keys + * happens at image build time, so neither a package mirror download nor RSA key generation counts against the + * short {@link #WAIT_TIMEOUT_MILLIS} startup timeout. + */ + private ImageFromDockerfile createHttpsImage() { + return new ImageFromDockerfile() + .withDockerfileFromBuilder(builder -> { + builder + .from(TestImages.ALPINE_IMAGE.asCanonicalNameString()) + .run( + "apk add --no-cache nmap-ncat openssl && " + + "openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -subj /CN=localhost " + + "-keyout /key.pem -out /cert.pem" + ) + .build(); + }); + } + private String createHttpsShellCommand(String header, String responseBody, int port) { int length = responseBody.getBytes().length; return ( - "apk add nmap-ncat; while true; do { echo -e \"HTTP/1.1 " + + "while true; do { echo -e \"HTTP/1.1 " + header + NEWLINE + "Content-Type: text/html" + @@ -303,7 +328,7 @@ private String createHttpsShellCommand(String header, String responseBody, int p responseBody + "\";} | ncat -lp " + port + - " --ssl; done" + " --ssl --ssl-cert /cert.pem --ssl-key /key.pem; done" ); } }