diff --git a/compatibility-tests/sdk-test-awscli/test/floci-ui.bats b/compatibility-tests/sdk-test-awscli/test/floci-ui.bats new file mode 100644 index 0000000000..4ee6f1df6b --- /dev/null +++ b/compatibility-tests/sdk-test-awscli/test/floci-ui.bats @@ -0,0 +1,16 @@ +#!/usr/bin/env bats +# Floci UI launcher endpoints. These suites run against the native image, where a JSON body +# Jackson needs reflection metadata for answers 500 if that metadata is missing (#5251). + +setup() { + load 'test_helper/common-setup' +} + +@test "Floci UI: the status endpoint answers with its JSON body before the UI starts" { + run curl -sS -w '\n%{http_code}' "$FLOCI_ENDPOINT/_floci/ui/status" + assert_success + [ "$(printf '%s\n' "$output" | tail -n 1)" = "200" ] + body="$(printf '%s\n' "$output" | head -n -1)" + [ "$(printf '%s' "$body" | jq -r '.ready | type')" = "boolean" ] + [ "$(printf '%s' "$body" | jq -r 'has("url") and has("error")')" = "true" ] +} diff --git a/src/main/java/io/floci/aws/lifecycle/UiController.java b/src/main/java/io/floci/aws/lifecycle/UiController.java index 7f537fcaec..43d2b0a5a7 100644 --- a/src/main/java/io/floci/aws/lifecycle/UiController.java +++ b/src/main/java/io/floci/aws/lifecycle/UiController.java @@ -3,7 +3,6 @@ package io.floci.aws.lifecycle; import io.floci.aws.services.floci.ui.FlociUiManager; import io.floci.aws.services.floci.ui.FlociUiManager.UiStatus; import io.floci.aws.services.floci.ui.UiPages; -import io.quarkus.runtime.annotations.RegisterForReflection; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.HeaderParam; @@ -12,6 +11,9 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import java.util.LinkedHashMap; +import java.util.Map; + /** * Browser-facing endpoints that launch and redirect to the floci-ui sidecar. * @@ -42,21 +44,31 @@ public class UiController { return Response.ok(uiPages.landingHtml()).build(); } - @RegisterForReflection - public record StatusResponse(boolean ready, String url, String error) {} - + /** + * The sidecar's state. The body is a map, as the other {@code /_floci} endpoints build theirs: a record needs + * reflection metadata for its components that the native image did not have, and Jackson found no properties on + * it there, so the endpoint answered 500 and the wait view never redirected. + */ @GET @Path("/status") @Produces(MediaType.APPLICATION_JSON) public Response status(@HeaderParam("Host") String host) { UiStatus s = uiManager.status(); if (s.error() != null) { - return Response.ok(new StatusResponse(false, null, s.error())).build(); + return Response.ok(statusBody(false, null, s.error())).build(); } if (s.ready() && s.hostPort() > 0) { - return Response.ok(new StatusResponse(true, redirectUrl(host, s.hostPort()), null)).build(); + return Response.ok(statusBody(true, redirectUrl(host, s.hostPort()), null)).build(); } - return Response.ok(new StatusResponse(false, null, null)).build(); + return Response.ok(statusBody(false, null, null)).build(); + } + + private static Map statusBody(boolean ready, String url, String error) { + Map body = new LinkedHashMap<>(); + body.put("ready", ready); + body.put("url", url); + body.put("error", error); + return body; } /** Builds the browser-reachable UI URL from the request Host and the published port. */ diff --git a/src/test/java/io/floci/aws/lifecycle/UiControllerTest.java b/src/test/java/io/floci/aws/lifecycle/UiControllerTest.java index 8efbddba3c..c184bf22bf 100644 --- a/src/test/java/io/floci/aws/lifecycle/UiControllerTest.java +++ b/src/test/java/io/floci/aws/lifecycle/UiControllerTest.java @@ -1,6 +1,5 @@ package io.floci.aws.lifecycle; -import io.floci.aws.lifecycle.UiController.StatusResponse; import io.floci.aws.services.floci.ui.FlociUiManager; import io.floci.aws.services.floci.ui.FlociUiManager.UiStatus; import io.floci.aws.services.floci.ui.UiPages; @@ -11,6 +10,9 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.ValueSource; +import java.util.HashMap; +import java.util.Map; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -40,7 +42,7 @@ class UiControllerTest { void readyStatusReplacesOnlyTheHostPort(String host, String url) { when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null)); - assertEquals(new StatusResponse(true, url, null), status(host)); + assertEquals(body(true, url, null), status(host)); } @ParameterizedTest @@ -52,7 +54,7 @@ class UiControllerTest { void malformedHostsKeepTheExistingBehavior(String host, String url) { when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null)); - assertEquals(new StatusResponse(true, url, null), status(host)); + assertEquals(body(true, url, null), status(host)); } @ParameterizedTest @@ -61,7 +63,7 @@ class UiControllerTest { void missingHostDefaultsToLocalhost(String host) { when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null)); - assertEquals(new StatusResponse(true, "http://localhost:4500/", null), status(host)); + assertEquals(body(true, "http://localhost:4500/", null), status(host)); } @ParameterizedTest @@ -69,17 +71,26 @@ class UiControllerTest { void statusWithoutAReadyPublishedPortHasNoUrl(boolean ready, int port) { when(uiManager.status()).thenReturn(new UiStatus(true, ready, port, null)); - assertEquals(new StatusResponse(false, null, null), status("[::1]:4566")); + assertEquals(body(false, null, null), status("[::1]:4566")); } @Test void statusErrorTakesPrecedenceOverReadiness() { when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, "sidecar failed")); - assertEquals(new StatusResponse(false, null, "sidecar failed"), status("[::1]:4566")); + assertEquals(body(false, null, "sidecar failed"), status("[::1]:4566")); } - private StatusResponse status(String host) { - return (StatusResponse) controller.status(host).getEntity(); + private Map status(String host) { + return (Map) controller.status(host).getEntity(); + } + + /** The {@code {ready, url, error}} body the wait view reads, every key present even when its value is null. */ + private static Map body(boolean ready, String url, String error) { + Map body = new HashMap<>(); + body.put("ready", ready); + body.put("url", url); + body.put("error", error); + return body; } } diff --git a/src/test/java/io/floci/aws/services/floci/ui/UiLandingIntegrationTest.java b/src/test/java/io/floci/aws/services/floci/ui/UiLandingIntegrationTest.java index 81a8248de4..fb7ee4f870 100644 --- a/src/test/java/io/floci/aws/services/floci/ui/UiLandingIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/floci/ui/UiLandingIntegrationTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; /** @@ -59,6 +60,9 @@ class UiLandingIntegrationTest { .get("/_floci/ui/status") .then() .statusCode(200) - .body("ready", is(false)); + .contentType(containsString("application/json")) + .body("ready", is(false)) + .body("$", hasKey("url")) + .body("$", hasKey("error")); } }