mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test(ec2): bound IMDS fixture response completion (#6800)
This commit is contained in:
1 parent
485ec4e39f
commit
153eaa95c9
2 files changed
+180
-3
No files matched your search
@@ -0,0 +1,152 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpTimeoutException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||
|
||||
class Ec2MetadataResponseTest {
|
||||
|
||||
@Test
|
||||
void completeUserDataBodyIsReadBeforeReturning() throws Exception {
|
||||
try (BodyServer server = new BodyServer(false, false); HttpClient client = HttpClient.newHttpClient()) {
|
||||
HttpResponse<byte[]> response = Ec2MetadataTestFixture.sendResponse(client, server.request(),
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
assertEquals(200, response.statusCode());
|
||||
assertEquals(16384, response.body().length);
|
||||
server.completed.get(3, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stalledBodyTimesOutAndCancelsTheExchange() throws Exception {
|
||||
try (BodyServer server = new BodyServer(true, false); HttpClient client = HttpClient.newHttpClient()) {
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(8), () ->
|
||||
assertThrows(HttpTimeoutException.class, () -> Ec2MetadataTestFixture.sendResponse(
|
||||
client, server.request(), HttpResponse.BodyHandlers.ofString())));
|
||||
server.headersSent.get(3, TimeUnit.SECONDS);
|
||||
server.completed.get(3, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void interruptedWaitCancelsTheExchange() throws Exception {
|
||||
try (BodyServer server = new BodyServer(true, false); HttpClient client = HttpClient.newHttpClient()) {
|
||||
CompletableFuture<Throwable> result = new CompletableFuture<>();
|
||||
Thread sender = Thread.ofPlatform().start(() -> {
|
||||
try {
|
||||
Ec2MetadataTestFixture.sendResponse(client, server.request(Duration.ofSeconds(30)),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
result.complete(new AssertionError("Interrupted response unexpectedly completed"));
|
||||
} catch (Exception failure) {
|
||||
result.complete(failure);
|
||||
}
|
||||
});
|
||||
try {
|
||||
server.headersSent.get(3, TimeUnit.SECONDS);
|
||||
sender.interrupt();
|
||||
assertInstanceOf(InterruptedException.class, result.get(3, TimeUnit.SECONDS));
|
||||
server.completed.get(3, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
sender.interrupt();
|
||||
sender.join(3000);
|
||||
assertFalse(sender.isAlive(), "Response sender must stop");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void truncatedBodyRetainsTheTransportFailure() throws Exception {
|
||||
try (BodyServer server = new BodyServer(true, true); HttpClient client = HttpClient.newHttpClient()) {
|
||||
IOException failure = assertThrows(IOException.class, () -> Ec2MetadataTestFixture.sendResponse(
|
||||
client, server.request(), HttpResponse.BodyHandlers.ofString()));
|
||||
assertFalse(failure instanceof HttpTimeoutException);
|
||||
server.completed.get(3, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class BodyServer implements AutoCloseable {
|
||||
private final ServerSocket listener;
|
||||
private final AtomicReference<Socket> accepted = new AtomicReference<>();
|
||||
private final CompletableFuture<Void> headersSent = new CompletableFuture<>();
|
||||
private final CompletableFuture<Void> completed = new CompletableFuture<>();
|
||||
private final Thread thread;
|
||||
|
||||
BodyServer(boolean partial, boolean closeEarly) throws IOException {
|
||||
listener = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"));
|
||||
listener.setSoTimeout(5000);
|
||||
thread = Thread.ofPlatform().start(() -> serve(partial, closeEarly));
|
||||
}
|
||||
|
||||
HttpRequest request() {
|
||||
return request(Duration.ofSeconds(2));
|
||||
}
|
||||
|
||||
HttpRequest request(Duration timeout) {
|
||||
return HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + listener.getLocalPort() + "/latest/user-data"))
|
||||
.timeout(timeout).GET().build();
|
||||
}
|
||||
|
||||
private void serve(boolean partial, boolean closeEarly) {
|
||||
try (Socket socket = listener.accept()) {
|
||||
accepted.set(socket);
|
||||
socket.setSoTimeout(10000);
|
||||
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.US_ASCII));
|
||||
String line;
|
||||
do {
|
||||
line = input.readLine();
|
||||
} while (line != null && !line.isEmpty());
|
||||
OutputStream output = socket.getOutputStream();
|
||||
output.write("HTTP/1.1 200 OK\r\nContent-Length: 16384\r\nConnection: close\r\n\r\n"
|
||||
.getBytes(StandardCharsets.US_ASCII));
|
||||
output.write(new byte[partial ? 1 : 16384]);
|
||||
output.flush();
|
||||
headersSent.complete(null);
|
||||
if (partial && !closeEarly) {
|
||||
try {
|
||||
assertEquals(-1, input.read(), "Cancellation must close the client exchange");
|
||||
} catch (SocketException expected) {
|
||||
// Cancellation may reset the connection instead of sending EOF.
|
||||
}
|
||||
}
|
||||
completed.complete(null);
|
||||
} catch (Throwable failure) {
|
||||
headersSent.completeExceptionally(failure);
|
||||
completed.completeExceptionally(failure);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
listener.close();
|
||||
Socket socket = accepted.get();
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
}
|
||||
thread.join(3000);
|
||||
assertFalse(thread.isAlive(), "Response server must stop");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,13 @@ import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpTimeoutException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -96,7 +100,7 @@ final class Ec2MetadataTestFixture implements AutoCloseable {
|
||||
}
|
||||
|
||||
HttpResponse<String> putToken(String ttl) throws Exception {
|
||||
return client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
return sendResponse(client, HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.timeout(Duration.ofSeconds(5)).header(TTL_HEADER, ttl).PUT(HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
@@ -112,7 +116,7 @@ final class Ec2MetadataTestFixture implements AutoCloseable {
|
||||
if (token != null) {
|
||||
request.header(TOKEN_HEADER, token);
|
||||
}
|
||||
return client.send(request.build(), HttpResponse.BodyHandlers.ofString());
|
||||
return sendResponse(client, request.build(), HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
<T> HttpResponse<T> get(String path, String token, HttpResponse.BodyHandler<T> bodyHandler) throws Exception {
|
||||
@@ -121,6 +125,27 @@ final class Ec2MetadataTestFixture implements AutoCloseable {
|
||||
if (token != null) {
|
||||
request.header(TOKEN_HEADER, token);
|
||||
}
|
||||
return client.send(request.GET().build(), bodyHandler);
|
||||
return sendResponse(client, request.GET().build(), bodyHandler);
|
||||
}
|
||||
|
||||
static <T> HttpResponse<T> sendResponse(HttpClient client, HttpRequest request,
|
||||
HttpResponse.BodyHandler<T> bodyHandler) throws Exception {
|
||||
Duration timeout = request.timeout().orElseThrow();
|
||||
CompletableFuture<HttpResponse<T>> response = client.sendAsync(request, bodyHandler);
|
||||
try {
|
||||
return response.get(timeout.toNanos(), TimeUnit.NANOSECONDS);
|
||||
} catch (TimeoutException expired) {
|
||||
HttpTimeoutException failure = new HttpTimeoutException("IMDS response body did not complete within " + timeout);
|
||||
failure.initCause(expired);
|
||||
throw failure;
|
||||
} catch (ExecutionException failure) {
|
||||
if (failure.getCause() instanceof Exception cause) {
|
||||
throw cause;
|
||||
}
|
||||
throw failure;
|
||||
} finally {
|
||||
response.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user