Skip to content
Merged
Show file tree
Hide file tree
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 @@ -84,7 +84,9 @@ protected GenericContainer<?> startContainerWithCommand(
* @param shellCommand the shell command to execute
*/
protected void waitUntilReadyAndSucceed(String shellCommand) {
waitUntilReadyAndSucceed(startContainerWithCommand(shellCommand));
try (GenericContainer<?> container = startContainerWithCommand(shellCommand)) {
waitUntilReadyAndSucceed(container);
}
}

/**
Expand All @@ -94,7 +96,9 @@ protected void waitUntilReadyAndSucceed(String shellCommand) {
* @param shellCommand the shell command to execute
*/
protected void waitUntilReadyAndTimeout(String shellCommand) {
waitUntilReadyAndTimeout(startContainerWithCommand(shellCommand));
try (GenericContainer<?> container = startContainerWithCommand(shellCommand)) {
waitUntilReadyAndTimeout(container);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;

/**
* Tests for {@link HttpWaitStrategy}.
Expand Down Expand Up @@ -45,16 +45,19 @@ public void testWaitUntilReadyWithSuccess() {
public void testWaitUntilReadyWithSuccessWithCustomHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("baz", "boo");
GenericContainer container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).withHeader("foo", "bar").withHeaders(headers)
);
waitUntilReadyAndSucceed(container);
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).withHeader("foo", "bar").withHeaders(headers)
)
) {
waitUntilReadyAndSucceed(container);

String logs = container.getLogs();
String logs = container.getLogs();

assertThat(logs, containsString("foo: bar"));
assertThat(logs, containsString("baz: boo"));
assertThat(logs, containsString("foo: bar"));
assertThat(logs, containsString("baz: boo"));
}
}

/**
Expand All @@ -63,12 +66,14 @@ public void testWaitUntilReadyWithSuccessWithCustomHeaders() {
*/
@Test
public void testWaitUntilReadyWithTlsAndAllowUnsecure() {
waitUntilReadyAndSucceed(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createHttpsShellCommand("200 OK", GOOD_RESPONSE_BODY, 8080),
createHttpWaitStrategy(ready).usingTls().allowInsecure()
)
);
) {
waitUntilReadyAndSucceed(container);
}
}

/**
Expand All @@ -77,12 +82,14 @@ public void testWaitUntilReadyWithTlsAndAllowUnsecure() {
*/
@Test
public void testWaitUntilReadyWithUnauthorizedWithLambda() {
waitUntilReadyAndSucceed(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 200 && it < 300 || it == 401)
)
);
) {
waitUntilReadyAndSucceed(container);
}
}

/**
Expand All @@ -91,12 +98,14 @@ public void testWaitUntilReadyWithUnauthorizedWithLambda() {
*/
@Test
public void testWaitUntilReadyWithManyStatusCodes() {
waitUntilReadyAndSucceed(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCode(300).forStatusCode(401).forStatusCode(500)
)
);
) {
waitUntilReadyAndSucceed(container);
}
}

/**
Expand All @@ -106,15 +115,17 @@ public void testWaitUntilReadyWithManyStatusCodes() {
*/
@Test
public void testWaitUntilReadyWithManyStatusCodesAndLambda() {
waitUntilReadyAndSucceed(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready)
.forStatusCode(300)
.forStatusCode(500)
.forStatusCodeMatching(it -> it == 401)
)
);
) {
waitUntilReadyAndSucceed(container);
}
}

/**
Expand All @@ -124,12 +135,14 @@ public void testWaitUntilReadyWithManyStatusCodesAndLambda() {
*/
@Test
public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() {
waitUntilReadyAndTimeout(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCode(300).forStatusCodeMatching(it -> it == 500)
)
);
) {
waitUntilReadyAndTimeout(container);
}
}

/**
Expand All @@ -141,12 +154,14 @@ public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() {
*/
@Test
public void testWaitUntilReadyWithTimeoutAndWithLambdaShouldNotMatchOk() {
waitUntilReadyAndTimeout(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY),
createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 300)
)
);
) {
waitUntilReadyAndTimeout(container);
}
}

/**
Expand All @@ -172,26 +187,30 @@ public void testWaitUntilReadyWithTimeoutAndBadResponseBody() {
*/
@Test
public void testWaitUntilReadyWithSpecificPort() {
waitUntilReadyAndSucceed(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("200 OK", GOOD_RESPONSE_BODY, 9090),
createHttpWaitStrategy(ready).forPort(9090),
7070,
8080,
9090
)
);
) {
waitUntilReadyAndSucceed(container);
}
}

@Test
public void testWaitUntilReadyWithTimoutCausedByReadTimeout() {
waitUntilReadyAndTimeout(
startContainerWithCommand(
try (
GenericContainer<?> container = startContainerWithCommand(
createShellCommand("0 Connection Refused", GOOD_RESPONSE_BODY, 9090),
createHttpWaitStrategy(ready).forPort(9090).withReadTimeout(Duration.ofMillis(1)),
9090
)
);
) {
waitUntilReadyAndTimeout(container);
}
}

/**
Expand Down