Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

test: share one profile across four Bedrock proxy test classes (#5850)

Priamo Germosendea7e53f8f81
diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java
index 67f5670014..9c1bebd434 100644
--- a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java
@@ -1,8 +1,8 @@
package io.github.hectorvent.floci.services.bedrockruntime;
+import io.github.hectorvent.floci.testing.BedrockProxyBackendProfile;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
-import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
@@ -17,17 +17,26 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
-import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.PORT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+/**
+ * Verifies that a caller who disconnects closes the upstream request, whether the upstream has
+ * sent nothing yet, part of a response, or one streamed token.
+ *
+ * <p>The backend URL and passthrough come from {@link BedrockProxyBackendProfile}, which this class
+ * shares with the other proxy classes that talk to a stand-in. The {@code fixture} model id is not
+ * in that profile's mapping, so it reaches the stand-in through passthrough. The profile keeps the
+ * default 60-second request timeout, far longer than these tests wait, so only the disconnect can
+ * close an upstream that stalls before its headers.
+ */
@QuarkusTest
-@TestProfile(BedrockProxyCancellationIntegrationTest.Profile.class)
+@TestProfile(BedrockProxyBackendProfile.class)
class BedrockProxyCancellationIntegrationTest {
- private static final int PORT = 18939;
@TestHTTPResource
URI base;
@@ -169,13 +178,4 @@ class BedrockProxyCancellationIntegrationTest {
}
}
}
-
- public static final class Profile implements QuarkusTestProfile {
- @Override
- public Map<String, String> getConfigOverrides() {
- return Map.of("floci.services.bedrock-runtime.backend", "proxy",
- "floci.services.bedrock-runtime.proxy.url", "http://127.0.0.1:" + PORT + "/v1",
- "floci.services.bedrock-runtime.proxy.passthrough", "true");
- }
- }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyIntegrationTest.java
index fa50995b6a..76c65e9502 100644
--- a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyIntegrationTest.java
@@ -6,8 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpServer;
+import io.github.hectorvent.floci.testing.BedrockProxyBackendProfile;
import io.quarkus.test.junit.QuarkusTest;
-import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
@@ -20,9 +20,10 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
-import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
+import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.MAPPED_MODEL_ID;
+import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.PORT;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
@@ -37,15 +38,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Verifies the Bedrock Runtime {@code proxy} backend translates Converse requests to
* OpenAI Chat Completions and translates the response back to the Bedrock shape,
* against a real (mock) OpenAI-compatible backend.
+ *
+ * <p>The backend, its URL, the API key and the model mapping come from
+ * {@link BedrockProxyBackendProfile}, which this class shares with the other proxy classes that
+ * talk to a stand-in. That profile also turns passthrough on, which no test here reaches, since
+ * every call names the mapped model id. The case with no mapping, no default model and no
+ * passthrough is in {@code BedrockProxyTimeoutIntegrationTest}, where passthrough stays off.
*/
@QuarkusTest
-@TestProfile(BedrockProxyIntegrationTest.ProxyBackendProfile.class)
+@TestProfile(BedrockProxyBackendProfile.class)
class BedrockProxyIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request";
- private static final int PORT = 18930;
- private static final String MAPPED_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0";
@Inject
ObjectMapper objectMapper;
@@ -1322,32 +1327,4 @@ class BedrockProxyIntegrationTest {
.statusCode(400)
.body("__type", equalTo("ValidationException"));
}
-
- @Test
- void converse_noMappingNoDefaultNoPassthrough_returns400() {
- given()
- .contentType("application/json")
- .header("Authorization", AUTH_HEADER)
- .body("""
- {"messages": [{"role": "user", "content": [{"text": "hi"}]}]}
- """)
- .when()
- .post("/model/unmapped.model-v1:0/converse")
- .then()
- .statusCode(400)
- .body("__type", equalTo("ValidationException"));
- }
-
- public static final class ProxyBackendProfile implements QuarkusTestProfile {
- @Override
- public Map<String, String> getConfigOverrides() {
- return Map.of(
- "floci.services.bedrock-runtime.backend", "proxy",
- "floci.services.bedrock-runtime.proxy.url", "http://127.0.0.1:" + PORT + "/v1",
- "floci.services.bedrock-runtime.proxy.api-key", "test-key",
- "floci.services.bedrock-runtime.proxy.model-mapping",
- MAPPED_MODEL_ID + "=claude-3-haiku"
- );
- }
- }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java
index 7ec83fe5b5..20cab6932d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.bedrockruntime;
import com.sun.net.httpserver.HttpServer;
import io.github.hectorvent.floci.services.bedrockruntime.backend.ProxyBackend;
+import io.github.hectorvent.floci.testing.BedrockProxyBackendProfile;
import io.github.hectorvent.floci.testutil.LogCapture;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
@@ -25,6 +26,7 @@ import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
+import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.PORT;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@@ -32,8 +34,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+/**
+ * Verifies that an upstream error reaches the caller as ModelErrorException with a bounded
+ * description, while the operational log keeps the status and leaves out the upstream body.
+ *
+ * <p>The backend URL and passthrough come from {@link BedrockProxyBackendProfile}, which this class
+ * shares with the other proxy classes that talk to a stand-in. The {@code fixture} model id is not
+ * in that profile's mapping, so it reaches the stand-in through passthrough.
+ */
@QuarkusTest
-@TestProfile(BedrockProxyCancellationIntegrationTest.Profile.class)
+@TestProfile(BedrockProxyBackendProfile.class)
class BedrockProxyLoggingIntegrationTest {
@TestHTTPResource
@@ -43,7 +53,7 @@ class BedrockProxyLoggingIntegrationTest {
@ValueSource(strings = {"converse", "converse-stream"})
void upstreamErrorIsLoggedWithoutResponseBody(String operation) throws IOException {
String marker = "synthetic-private-upstream-content";
- HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 18939), 0);
+ HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", PORT), 0);
backend.createContext("/v1/chat/completions", exchange -> {
exchange.getRequestBody().readAllBytes();
byte[] body = ("{\"error\":\"" + marker + "\"}").getBytes(StandardCharsets.UTF_8);
@@ -80,7 +90,7 @@ class BedrockProxyLoggingIntegrationTest {
@Test
void streamingErrorBodyReadIsBoundedWithoutWaitingForEof() throws Exception {
CountDownLatch release = new CountDownLatch(1);
- HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 18939), 0);
+ HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", PORT), 0);
backend.createContext("/v1/chat/completions", exchange -> {
exchange.getRequestBody().readAllBytes();
exchange.sendResponseHeaders(503, 32768);
diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyPassthroughIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyPassthroughIntegrationTest.java
index 65df8e3fb4..9927b83f61 100644
--- a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyPassthroughIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyPassthroughIntegrationTest.java
@@ -3,8 +3,8 @@ package io.github.hectorvent.floci.services.bedrockruntime;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpServer;
+import io.github.hectorvent.floci.testing.BedrockProxyBackendProfile;
import io.quarkus.test.junit.QuarkusTest;
-import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
@@ -14,9 +14,9 @@ import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
-import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
+import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.PORT;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -24,16 +24,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Verifies that with {@code proxy.passthrough=true} the raw Bedrock model id is forwarded
* as-is to the OpenAI-compatible backend, bypassing the mapping/default-model requirement.
- * Kept in a separate class/profile since {@code @TestProfile} config overrides require a
- * fresh Quarkus application context, one per distinct override set.
+ *
+ * <p>Passthrough and the backend URL come from {@link BedrockProxyBackendProfile}, which this class
+ * shares with the other proxy classes that talk to a stand-in. The raw model id is not in that
+ * profile's mapping, so it reaches the backend through passthrough alone.
*/
@QuarkusTest
-@TestProfile(BedrockProxyPassthroughIntegrationTest.PassthroughProfile.class)
+@TestProfile(BedrockProxyBackendProfile.class)
class BedrockProxyPassthroughIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request";
- private static final int PORT = 18931;
private static final String RAW_MODEL_ID = "meta.llama3-8b-instruct-v1:0";
@Inject
@@ -87,15 +88,4 @@ class BedrockProxyPassthroughIntegrationTest {
JsonNode sentRequest = objectMapper.readTree(receivedBody.get());
assertEquals(RAW_MODEL_ID, sentRequest.path("model").asText());
}
-
- public static final class PassthroughProfile implements QuarkusTestProfile {
- @Override
- public Map<String, String> getConfigOverrides() {
- return Map.of(
- "floci.services.bedrock-runtime.backend", "proxy",
- "floci.services.bedrock-runtime.proxy.url", "http://127.0.0.1:" + PORT + "/v1",
- "floci.services.bedrock-runtime.proxy.passthrough", "true"
- );
- }
- }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java
index 2d624ee79a..588d5d87a1 100644
--- a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java
@@ -6,6 +6,7 @@ import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -21,6 +22,13 @@ import static org.hamcrest.Matchers.equalTo;
* Verifies that a slow backend response is surfaced as ModelTimeoutException (408),
* not the generic ModelErrorException (424) - the AWS SDKs' default retry strategy
* treats 408 as retryable and 424 as terminal.
+ *
+ * <p>Its profile leaves passthrough off and the default model unset, so this class also checks that
+ * a model id with no mapping is refused before any backend call. It stays apart from
+ * {@code BedrockProxyBackendProfile}, which the other proxy classes share: that profile turns
+ * passthrough on, and a one-second timeout there would also close the upstreams that
+ * {@code BedrockProxyCancellationIntegrationTest} stalls before their headers, so those tests
+ * would pass without a disconnect closing them.
*/
@QuarkusTest
@TestProfile(BedrockProxyTimeoutIntegrationTest.SlowProxyBackendProfile.class)
@@ -74,6 +82,21 @@ class BedrockProxyTimeoutIntegrationTest {
.body("__type", equalTo("ModelTimeoutException"));
}
+ @Test
+ void converse_noMappingNoDefaultNoPassthrough_returns400() {
+ given()
+ .contentType("application/json")
+ .header("Authorization", AUTH_HEADER)
+ .body("""
+ {"messages": [{"role": "user", "content": [{"text": "hi"}]}]}
+ """)
+ .when()
+ .post("/model/unmapped.model-v1:0/converse")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+ }
+
public static final class SlowProxyBackendProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
diff --git a/src/test/java/io/github/hectorvent/floci/testing/BedrockProxyBackendProfile.java b/src/test/java/io/github/hectorvent/floci/testing/BedrockProxyBackendProfile.java
new file mode 100644
index 0000000000..b94d84133b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/testing/BedrockProxyBackendProfile.java
@@ -0,0 +1,49 @@
+package io.github.hectorvent.floci.testing;
+
+import io.quarkus.test.junit.QuarkusTestProfile;
+
+import java.util.Map;
+
+/**
+ * Runs the Bedrock Runtime {@code proxy} backend against a stand-in OpenAI-compatible server on
+ * port {@value #PORT}, for the classes that test the proxy. Quarkus builds the application once
+ * per distinct profile class, so they share this one rather than each declaring their own. Each
+ * class starts its own stand-in on that port and stops it when its test ends.
+ *
+ * <p>The overrides, all under {@code floci.services.bedrock-runtime}, and who relies on them:
+ * <ul>
+ * <li>{@code backend=proxy}, with {@code proxy.url} on port {@value #PORT}, sends Converse to the
+ * stand-in. Every class here relies on it.</li>
+ * <li>{@code proxy.api-key=test-key} is sent as a bearer token.
+ * {@code BedrockProxyIntegrationTest} relies on it.</li>
+ * <li>{@code proxy.model-mapping} maps {@value #MAPPED_MODEL_ID} to {@code claude-3-haiku}.
+ * {@code BedrockProxyIntegrationTest} relies on it.</li>
+ * <li>{@code proxy.passthrough=true} forwards a model id with no mapping as it is.
+ * {@code BedrockProxyPassthroughIntegrationTest}, {@code BedrockProxyCancellationIntegrationTest}
+ * and {@code BedrockProxyLoggingIntegrationTest} rely on it.</li>
+ * </ul>
+ *
+ * <p>Each override is inert for the classes that do not rely on it. Only the stand-in in
+ * {@code BedrockProxyIntegrationTest} reads the {@code Authorization} header. The other classes
+ * call model ids the mapping does not name, and every call in {@code BedrockProxyIntegrationTest}
+ * names the mapped id, which the mapping resolves before passthrough is consulted.
+ *
+ * <p>{@code BedrockProxyTimeoutIntegrationTest} stays apart. Its one-second request timeout would
+ * also close an upstream that stalls before its headers, so the cancellation tests would pass
+ * without the disconnect handling they check. It also holds the test that needs passthrough off.
+ */
+public class BedrockProxyBackendProfile implements QuarkusTestProfile {
+
+ public static final int PORT = 18930;
+ public static final String MAPPED_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0";
+
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return Map.of(
+ "floci.services.bedrock-runtime.backend", "proxy",
+ "floci.services.bedrock-runtime.proxy.url", "http://127.0.0.1:" + PORT + "/v1",
+ "floci.services.bedrock-runtime.proxy.api-key", "test-key",
+ "floci.services.bedrock-runtime.proxy.model-mapping", MAPPED_MODEL_ID + "=claude-3-haiku",
+ "floci.services.bedrock-runtime.proxy.passthrough", "true");
+ }
+}