Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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" +
Expand All @@ -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"
);
}
}
Loading