feat(ui): publish a console contract so any web console can be the sidecar (#3641)

Floci could run exactly one web console, the image it ships. This publishes the contract
between Floci and a console so any image can take that slot, and an operator gets there by
setting one image name rather than by configuring seven fields.

Three things make a console a Floci console: it listens on the port in PORT, it serves
GET /api/health, and it talks to Floci at AWS_ENDPOINT_URL. Everything else is detail the
contract spells out, including the AWS baseline every launched container already receives,
so a console built on any AWS SDK is configured by that SDK's ordinary endpoint and
credential discovery.

Floci learns a console's shape from three sources, in order. The image's own
io.floci.console.* labels come first, so a console describes itself and needs no operator
input at all. A built-in profile covers the consoles Floci already knows. The contract
defaults (/api/health, port 4500, status: ok) cover the rest. Configuration overrides all
three, for a console that describes itself neither way.

That ladder is why the new config keys ship commented out in application.yml. They exist for
the console that fits none of the three sources, and setting one silently disables the
discovery that would otherwise just work. Every accessor is Optional or OptionalInt, so
nothing is required and the test configuration needs no entry.

A malformed label is ignored rather than fatal: a console that declares port="banana" starts
on the default instead of failing the emulator, and the reason is logged.

Docs: docs/ui/console-contract.md is the contract itself, written for console authors, and
docs/ui/index.md is the operator's page. Both are in the mkdocs nav.

Tests: 608 lines. ConsoleProfileResolverTest covers the resolution ladder including the
precedence between its three sources; ContainerLifecycleManagerImageLabelsTest covers reading
labels off an image; FlociUiManagerTest grows to cover the manager against the contract.
Neutralising the label arm fails 5 of the 13 resolver tests, so the ladder is genuinely
exercised rather than the tests only asserting defaults.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-14 21:53:59 -05:00
1 parent 8532755cf4
commit a5ebda01fb
15 files changed
+1760 -86

No files matched your search

@@ -0,0 +1,98 @@
package io.github.hectorvent.floci.core.common.docker;
import java.util.Map;
import java.util.Optional;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectImageCmd;
import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerConfig;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* An image's labels are advisory metadata: the web console reads them to learn the console's shape.
* A failure to read them must therefore never break the start they were being read for.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class ContainerLifecycleManagerImageLabelsTest {
@Mock
DockerClient dockerClient;
@Mock
ImageCacheService imageCacheService;
@Mock
ContainerDetector containerDetector;
@Mock
PortAllocator portAllocator;
@Mock
EmulatorConfig config;
private ContainerLifecycleManager manager() {
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector,
portAllocator, config);
}
private void imageInspectReturns(Map<String, String> labels) {
ContainerConfig containerConfig = new ContainerConfig().withLabels(labels);
InspectImageResponse response = mock(InspectImageResponse.class);
when(response.getConfig()).thenReturn(containerConfig);
InspectImageCmd cmd = mock(InspectImageCmd.class);
when(cmd.exec()).thenReturn(response);
when(dockerClient.inspectImageCmd("acme/console:1")).thenReturn(cmd);
}
@Test
void labelsAreReadFromTheImageThePullResolvedTo() {
when(imageCacheService.ensureImageExists("acme/console")).thenReturn("acme/console:1");
imageInspectReturns(Map.of("io.floci.console.contract", "1"));
assertEquals(Optional.of(Map.of("io.floci.console.contract", "1")),
manager().imageLabels("acme/console"));
}
@Test
void anImageWithNoLabelsReadsAsEmptyRatherThanAsUnreadable() {
// "This image declares nothing" and "I could not ask" are different answers: only the
// second should send the caller looking for another source.
when(imageCacheService.ensureImageExists("acme/console")).thenReturn("acme/console:1");
imageInspectReturns(null);
assertEquals(Optional.of(Map.of()), manager().imageLabels("acme/console"));
}
@Test
void anUnavailableImageReportsUnreadableLabelsRatherThanThrowing() {
when(imageCacheService.ensureImageExists("acme/console"))
.thenThrow(new NotFoundException("no such image"));
assertTrue(manager().imageLabels("acme/console").isEmpty(),
"a failed pull must leave the caller to its own defaults, not fail the start");
}
@Test
void anUnreachableRuntimeReportsUnreadableLabelsRatherThanThrowing() {
when(imageCacheService.ensureImageExists("acme/console")).thenReturn("acme/console:1");
when(dockerClient.inspectImageCmd("acme/console:1"))
.thenThrow(new RuntimeException("Cannot connect to the Docker daemon"));
assertTrue(manager().imageLabels("acme/console").isEmpty());
}
}
@@ -0,0 +1,206 @@
package io.github.hectorvent.floci.services.floci.ui;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import io.github.hectorvent.floci.config.EmulatorConfig;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class ConsoleProfileResolverTest {
private final EmulatorConfig.UiServiceConfig ui = mock(EmulatorConfig.UiServiceConfig.class);
/** Nothing configured: the state an operator who only set an image is in. */
private void nothingConfigured() {
when(ui.internalPort()).thenReturn(OptionalInt.empty());
when(ui.endpointEnv()).thenReturn(Optional.empty());
when(ui.statusPath()).thenReturn(Optional.empty());
when(ui.statusReadyField()).thenReturn(Optional.empty());
when(ui.statusReadyValue()).thenReturn(Optional.empty());
when(ui.statusUnavailableValue()).thenReturn(Optional.empty());
}
private static Map<String, String> contractLabels(String... pairs) {
Map<String, String> labels = new HashMap<>();
labels.put(ConsoleProfileResolver.LABEL_CONTRACT, ConsoleProfileResolver.CONTRACT_VERSION);
for (int i = 0; i < pairs.length; i += 2) {
labels.put(ConsoleProfileResolver.LABEL_PREFIX + pairs[i], pairs[i + 1]);
}
return labels;
}
// --- layer 4: the contract itself ---
@Test
void aConsoleThatImplementsTheContractNeedsNoConfigurationAtAll() {
nothingConfigured();
ConsoleProfile profile = ConsoleProfileResolver.resolve("acme/console:1", Optional.empty(), ui);
assertEquals(4500, profile.internalPort());
assertEquals("/api/health", profile.healthPath());
assertEquals("status", profile.healthReadyField());
assertEquals("ok", profile.healthReadyValue());
assertEquals("unavailable", profile.healthUnavailableValue());
assertFalse(profile.hasEndpointAlias(),
"AWS_ENDPOINT_URL is in the baseline, so a contract console needs no alias");
}
// --- layer 3: a console Floci recognises by name ---
@Test
void flociUiKeepsItsOwnHealthShapeHoweverTheImageIsSpelled() {
for (String image : new String[]{
"floci-ui",
"floci/floci-ui",
"floci/floci-ui:latest",
"docker.io/floci/floci-ui:2.0.1",
"ghcr.io/floci-io/floci-ui@sha256:0123456789abcdef",
"registry.example.com:5000/mirror/floci/floci-ui:latest"}) {
ConsoleProfile profile = ConsoleProfileResolver.builtIn(image);
assertEquals("/api/clouds/aws/status", profile.healthPath(), image);
assertEquals("runtime", profile.healthReadyField(), image);
assertEquals("reachable", profile.healthReadyValue(), image);
}
}
@Test
void anImageThatMerelyLooksLikeFlociUiGetsTheContractDefaults() {
for (String image : new String[]{"floci/floci-uix:1", "acme/floci-ui-next", "floci/console"}) {
assertEquals("/api/health", ConsoleProfileResolver.builtIn(image).healthPath(), image);
}
}
// --- layer 2: the image describes itself ---
@Test
void labelsDescribeAConsoleFlociHasNeverHeardOf() {
nothingConfigured();
Map<String, String> labels = contractLabels(
"port", "8080",
"health-path", "healthz",
"health-ready-field", "state",
"health-ready-value", "up",
"health-unavailable-value", "down",
"endpoint-env", "CONSOLE_API_URL",
"name", "StackPort");
ConsoleProfile profile = ConsoleProfileResolver.resolve("acme/console:1", Optional.of(labels), ui);
assertEquals(8080, profile.internalPort());
assertEquals("/healthz", profile.healthPath(), "a label without a leading slash still forms a URL");
assertEquals("state", profile.healthReadyField());
assertEquals("up", profile.healthReadyValue());
assertEquals("down", profile.healthUnavailableValue());
assertEquals("CONSOLE_API_URL", profile.endpointEnv());
assertEquals("StackPort", profile.displayName());
}
@Test
void labelsForAContractVersionFlociDoesNotImplementAreIgnored() {
// Reading a future contract's labels under this one's meaning is worse than ignoring them:
// the console would be probed at a path that means something else.
Map<String, String> labels = new HashMap<>(contractLabels("health-path", "/v2/health"));
labels.put(ConsoleProfileResolver.LABEL_CONTRACT, "2");
ConsoleProfile profile = ConsoleProfileResolver.fromLabels(labels, ConsoleProfileResolver.contractV1());
assertEquals("/api/health", profile.healthPath());
}
@Test
void labelsWithoutTheOptInAreIgnored() {
Map<String, String> labels = Map.of(
ConsoleProfileResolver.LABEL_PREFIX + "health-path", "/v2/health");
assertEquals("/api/health",
ConsoleProfileResolver.fromLabels(labels, ConsoleProfileResolver.contractV1()).healthPath());
}
@Test
void anUnusablePortLabelIsIgnoredRatherThanBreakingTheStart() {
// A typo in one label should cost the label, not the console.
for (String port : new String[]{"eighty-eighty", "0", "70000", "8080 "}) {
ConsoleProfile profile = ConsoleProfileResolver.fromLabels(
contractLabels("port", port), ConsoleProfileResolver.contractV1());
assertEquals("8080 ".equals(port) ? 8080 : 4500, profile.internalPort(), port);
}
}
@Test
void aDeclaredButEmptyReadyFieldLabelMeansAnyTwoHundredIsReady() {
// The one label where "declared as empty" differs from "not declared": it is how a console
// with a plain liveness endpoint says it has no field worth reading.
ConsoleProfile profile = ConsoleProfileResolver.fromLabels(
contractLabels("health-ready-field", ""), ConsoleProfileResolver.contractV1());
assertFalse(profile.hasReadyField());
}
@Test
void labelsRefineTheBuiltInProfileTheyDoNotReplaceIt() {
// A floci-ui image that starts declaring only its health path keeps the rest of its profile.
ConsoleProfile profile = ConsoleProfileResolver.fromLabels(
contractLabels("health-path", "/api/health"),
ConsoleProfileResolver.builtIn("floci/floci-ui:latest"));
assertEquals("/api/health", profile.healthPath());
assertEquals("runtime", profile.healthReadyField());
}
// --- layer 1: explicit configuration ---
@Test
void configurationBeatsLabelsWhichBeatTheBuiltInProfile() {
nothingConfigured();
when(ui.statusPath()).thenReturn(Optional.of("/operator/health"));
Map<String, String> labels = contractLabels(
"health-path", "/label/health",
"health-ready-field", "state");
ConsoleProfile profile = ConsoleProfileResolver.resolve("floci/floci-ui:latest", Optional.of(labels), ui);
assertEquals("/operator/health", profile.healthPath(), "configuration wins");
assertEquals("state", profile.healthReadyField(), "an unconfigured field falls through to the label");
}
@Test
void configuringTheReadyFieldToNoneTurnsItOff() {
nothingConfigured();
when(ui.statusReadyField()).thenReturn(Optional.of("none"));
assertFalse(ConsoleProfileResolver.withOverrides(ui, ConsoleProfileResolver.contractV1())
.hasReadyField());
}
@Test
void anEmptyConfiguredValueIsTreatedAsUnsetRatherThanAsAnEmptyPath() {
// An environment variable set to nothing reaches SmallRye as an absent property; treating a
// blank the same way keeps the two spellings from meaning different things.
nothingConfigured();
when(ui.statusPath()).thenReturn(Optional.of(" "));
assertEquals("/api/clouds/aws/status",
ConsoleProfileResolver.resolve("floci/floci-ui:latest", Optional.empty(), ui).healthPath());
}
@Test
void unreadableLabelsFallThroughToTheProfileRatherThanFailing() {
nothingConfigured();
ConsoleProfile profile = ConsoleProfileResolver.resolve("floci/floci-ui:latest", Optional.empty(), ui);
assertEquals("/api/clouds/aws/status", profile.healthPath());
assertTrue(profile.hasReadyField());
}
}
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpServer;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.iam.model.SessionCreds;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
@@ -12,7 +13,9 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.C
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Container;
@@ -22,9 +25,11 @@ import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.concurrent.TimeUnit;
import static org.awaitility.Awaitility.await;
@@ -33,6 +38,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -51,22 +58,37 @@ class FlociUiManagerTest {
private final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
private final EmulatorConfig.UiServiceConfig ui = mock(EmulatorConfig.UiServiceConfig.class);
/** Wires config.services().ui() with the defaults every UI test starts from. */
/** The profile floci-ui resolves to, which most of these tests exercise. */
private static final ConsoleProfile FLOCI_UI = ConsoleProfileResolver.builtIn("floci/floci-ui:latest");
/** Wires config.services().ui() with the defaults every console test starts from. */
private void withUiConfig() {
when(config.services()).thenReturn(services);
when(services.ui()).thenReturn(ui);
when(ui.endpoint()).thenReturn(Optional.empty());
when(ui.insecureSkipTlsVerify()).thenReturn(false);
when(ui.internalPort()).thenReturn(OptionalInt.empty());
when(ui.endpointEnv()).thenReturn(Optional.empty());
when(ui.extraEnv()).thenReturn(Optional.empty());
when(ui.statusPath()).thenReturn(Optional.empty());
when(ui.statusReadyField()).thenReturn(Optional.empty());
when(ui.statusReadyValue()).thenReturn(Optional.empty());
when(ui.statusUnavailableValue()).thenReturn(Optional.empty());
}
private FlociUiManager newManager() {
return newManager(mock(ContainerBuilder.class));
}
private FlociUiManager newManager(ContainerBuilder containerBuilder) {
return new FlociUiManager(
mock(ContainerBuilder.class),
containerBuilder,
lifecycleManager,
logStreamer,
containerDetector,
mock(CurrentContainerNetworkResolver.class),
dockerHostResolver,
new LaunchedContainerAwsEnv(mock(ContainerReachableEndpoint.class)),
config,
regionResolver,
new ObjectMapper());
@@ -172,24 +194,27 @@ class FlociUiManagerTest {
void probeUsesSidecarContainerIpWhenContainerized() {
// In a container the published host port is not reachable via localhost; the
// probe must target the sidecar's container IP on the shared Docker network.
withUiConfig();
EndpointInfo endpoint = new EndpointInfo("10.88.0.20", 4500);
assertEquals("http://10.88.0.20:4500/api/clouds/aws/status",
newManager().resolveProbeUrl(endpoint, 4500));
newManager().resolveProbeUrl(FLOCI_UI, endpoint, 4500));
}
@Test
void probeUsesLocalhostHostPortNatively() {
withUiConfig();
EndpointInfo endpoint = new EndpointInfo("localhost", 4500);
assertEquals("http://localhost:4500/api/clouds/aws/status",
newManager().resolveProbeUrl(endpoint, 4500));
newManager().resolveProbeUrl(FLOCI_UI, endpoint, 4500));
}
@Test
void probeFallsBackToLocalhostWhenEndpointMissing() {
withUiConfig();
assertEquals("http://localhost:4500/api/clouds/aws/status",
newManager().resolveProbeUrl(null, 4500));
newManager().resolveProbeUrl(FLOCI_UI, null, 4500));
}
@Test
@@ -324,8 +349,8 @@ class FlociUiManagerTest {
@Test
void endpointDriftDetectedWhenAdoptedContainerPointsAtAnotherAddress() {
// The sidecar outlives Floci and is re-adopted after a restart, but its
// FLOCI_ENDPOINT was baked in at create time against the previous container IP.
List<String> staleEnv = List.of("PORT=4500", "FLOCI_ENDPOINT=http://10.88.4.98:4566");
// AWS_ENDPOINT_URL was baked in at create time against the previous container IP.
List<String> staleEnv = List.of("PORT=4500", "AWS_ENDPOINT_URL=http://10.88.4.98:4566");
assertTrue(FlociUiManager.endpointDrifted(staleEnv, "http://10.88.3.252:4566"),
"an adopted sidecar pointing at a previous Floci IP must be treated as drifted");
@@ -333,7 +358,7 @@ class FlociUiManagerTest {
@Test
void noEndpointDriftWhenAdoptedContainerAlreadyMatches() {
List<String> env = List.of("PORT=4500", "FLOCI_ENDPOINT=http://10.88.3.252:4566");
List<String> env = List.of("PORT=4500", "AWS_ENDPOINT_URL=http://10.88.3.252:4566");
assertFalse(FlociUiManager.endpointDrifted(env, "http://10.88.3.252:4566"),
"a sidecar already pointing at the current endpoint must be adopted as-is");
@@ -341,7 +366,7 @@ class FlociUiManagerTest {
@Test
void endpointDriftDetectedWhenAdoptedContainerHasNoEndpointAtAll() {
// Missing is not "fine" — it is unknown, and adopting it would strand the UI.
// Missing is not "fine": it is unknown, and adopting it would strand the console.
assertTrue(FlociUiManager.endpointDrifted(List.of("PORT=4500"), "http://10.88.3.252:4566"));
assertTrue(FlociUiManager.endpointDrifted(null, "http://10.88.3.252:4566"));
}
@@ -349,7 +374,7 @@ class FlociUiManagerTest {
@Test
void endpointDriftDetectedAcrossSchemeChange() {
// TLS toggled between runs: same host, different scheme, still unreachable.
List<String> env = List.of("FLOCI_ENDPOINT=http://10.88.3.252:4566");
List<String> env = List.of("AWS_ENDPOINT_URL=http://10.88.3.252:4566");
assertTrue(FlociUiManager.endpointDrifted(env, "https://10.88.3.252:4566"));
}
@@ -363,14 +388,14 @@ class FlociUiManagerTest {
@Test
void readableEnvironmentStillReplacesOnDrift() {
Optional<List<String>> env = Optional.of(List.of("FLOCI_ENDPOINT=http://10.88.4.98:4566"));
Optional<List<String>> env = Optional.of(List.of("AWS_ENDPOINT_URL=http://10.88.4.98:4566"));
assertTrue(FlociUiManager.shouldReplace(env, "http://10.88.3.252:4566"));
}
@Test
void readableMatchingEnvironmentIsAdopted() {
Optional<List<String>> env = Optional.of(List.of("FLOCI_ENDPOINT=http://10.88.3.252:4566"));
Optional<List<String>> env = Optional.of(List.of("AWS_ENDPOINT_URL=http://10.88.3.252:4566"));
assertFalse(FlociUiManager.shouldReplace(env, "http://10.88.3.252:4566"));
}
@@ -447,7 +472,8 @@ class FlociUiManagerTest {
when(dockerHostResolver.resolve()).thenReturn("host.docker.internal");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
assertTrue(newManager().injectedEnv().contains("NODE_TLS_REJECT_UNAUTHORIZED=0"));
assertTrue(newManager().injectedEnv(ConsoleProfileResolver.contractV1())
.contains("NODE_TLS_REJECT_UNAUTHORIZED=0"));
}
@Test
@@ -460,7 +486,7 @@ class FlociUiManagerTest {
when(dockerHostResolver.resolve()).thenReturn("host.docker.internal");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
List<String> env = newManager().injectedEnv();
List<String> env = newManager().injectedEnv(ConsoleProfileResolver.contractV1());
assertFalse(env.stream().anyMatch(v -> v.startsWith("NODE_TLS_REJECT_UNAUTHORIZED")),
"TLS verification must stay on unless explicitly opted out, was: " + env);
}
@@ -486,7 +512,9 @@ class FlociUiManagerTest {
EndpointInfo endpoint = new EndpointInfo("localhost", 4500);
when(lifecycleManager.adopt("abc123", List.of(4500)))
.thenReturn(new ContainerLifecycleManager.ContainerInfo("abc123", Map.of(4500, endpoint)));
when(lifecycleManager.presenceOf("abc123")).thenReturn(ContainerPresence.ABSENT);
// Running when it is first adopted, gone by the time status() checks on it.
when(lifecycleManager.presenceOf("abc123"))
.thenReturn(ContainerPresence.RUNNING, ContainerPresence.ABSENT);
FlociUiManager manager = new FlociUiManager(
mock(ContainerBuilder.class),
@@ -495,6 +523,7 @@ class FlociUiManagerTest {
containerDetector,
mock(CurrentContainerNetworkResolver.class),
dockerHostResolver,
new LaunchedContainerAwsEnv(mock(ContainerReachableEndpoint.class)),
config,
regionResolver,
new ObjectMapper());
@@ -504,11 +533,25 @@ class FlociUiManagerTest {
.untilAsserted(() -> verify(lifecycleManager, times(1)).adopt("abc123", List.of(4500)));
// The sidecar is gone (ABSENT) and unreachable (nothing listens on localhost:4500), so
// status() must trigger a second async restart.
// status() must trigger a second async restart. That restart no longer re-adopts the dead
// container, so the kick is observed by the start running a second time at all.
manager.status();
await().atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> verify(lifecycleManager, times(2)).adopt("abc123", List.of(4500)));
.untilAsserted(() -> verify(lifecycleManager, times(2)).findByName("floci-ui"));
}
@Test
void aStoppedSidecarIsRecreatedRatherThanAdoptedForever() {
// Adopting an exited container is a loop: the probe fails, the container reads as gone,
// the re-arm comes back here, and the same dead container is adopted again on every poll
// with nothing ever starting it.
assertTrue(FlociUiManager.mustRecreate(ContainerPresence.STOPPED));
assertTrue(FlociUiManager.mustRecreate(ContainerPresence.ABSENT));
assertFalse(FlociUiManager.mustRecreate(ContainerPresence.RUNNING),
"a running sidecar must still be adopted, not churned");
assertFalse(FlociUiManager.mustRecreate(ContainerPresence.UNKNOWN),
"acting on an unknown is how a transient runtime hiccup destroys a healthy sidecar");
}
// --- a start that fails before the try block must not vanish silently ---
@@ -534,6 +577,7 @@ class FlociUiManagerTest {
containerDetector,
mock(CurrentContainerNetworkResolver.class),
dockerHostResolver,
new LaunchedContainerAwsEnv(mock(ContainerReachableEndpoint.class)),
config,
regionResolver,
new ObjectMapper());
@@ -587,6 +631,7 @@ class FlociUiManagerTest {
containerDetector,
mock(CurrentContainerNetworkResolver.class),
dockerHostResolver,
new LaunchedContainerAwsEnv(mock(ContainerReachableEndpoint.class)),
config,
regionResolver,
new ObjectMapper());
@@ -599,31 +644,313 @@ class FlociUiManagerTest {
+ manager.status().error());
}
@Test
void healthPathFollowsTheProfileSoAThirdPartyConsoleCanBeProbed() {
// A console other than floci-ui answers the contract's /api/health, which floci-ui's
// own /api/clouds/aws/status route would miss entirely.
withUiConfig();
assertEquals("http://10.88.0.20:8080/api/health",
newManager().resolveProbeUrl(ConsoleProfileResolver.contractV1(),
new EndpointInfo("10.88.0.20", 8080), 8080));
}
@Test
void statusPathWithoutALeadingSlashStillFormsAUrl() {
withUiConfig();
when(ui.statusPath()).thenReturn(Optional.of("api/health"));
ConsoleProfile profile = ConsoleProfileResolver.withOverrides(ui, ConsoleProfileResolver.contractV1());
assertEquals("http://localhost:8080/api/health",
newManager().resolveProbeUrl(profile, null, 8080));
}
@Test
void everyConsoleGetsTheStandardAwsEnvironmentAndItsListenPort() {
// The contract's side of the bargain: an SDK-based console is configured entirely by its
// ordinary endpoint and credential discovery, with no Floci-specific code.
withUiConfig();
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(config.hostname()).thenReturn(Optional.of("floci"));
when(config.effectiveBaseUrl()).thenReturn("http://floci:4566");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
List<String> env = newManager().injectedEnv(ConsoleProfileResolver.contractV1());
assertTrue(env.contains("AWS_ENDPOINT_URL=http://floci:4566"), env.toString());
assertTrue(env.contains("FLOCI_ENDPOINT=http://floci:4566"), env.toString());
assertTrue(env.contains("FLOCI_HOSTNAME=floci"), env.toString());
assertTrue(env.contains("AWS_REGION=us-east-1"), env.toString());
assertTrue(env.contains("AWS_DEFAULT_REGION=us-east-1"), env.toString());
assertTrue(env.contains("FLOCI_CLOUD=aws"), env.toString());
assertTrue(env.contains("PORT=4500"), env.toString());
}
@Test
void theConsoleIsNeverHandedFlocisOwnAwsCredentials() {
// The baseline forwards Floci's ambient credentials when none are supplied, which is
// acceptable for a workload the user wrote and not for a third-party console image. So
// placeholders must be passed explicitly rather than left to that fallback.
withUiConfig();
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(config.hostname()).thenReturn(Optional.of("floci"));
when(config.effectiveBaseUrl()).thenReturn("http://floci:4566");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);
when(awsEnv.sdkBaselineEnv(anyString(), any(), anyString(), any()))
.thenReturn(List.of("AWS_ACCESS_KEY_ID=test"));
FlociUiManager manager = new FlociUiManager(
mock(ContainerBuilder.class), lifecycleManager, logStreamer, containerDetector,
mock(CurrentContainerNetworkResolver.class), dockerHostResolver,
awsEnv, config, regionResolver, new ObjectMapper());
List<String> env = manager.injectedEnv(ConsoleProfileResolver.contractV1());
assertTrue(env.contains("AWS_ACCESS_KEY_ID=test"), env.toString());
verify(awsEnv).sdkBaselineEnv("us-east-1", Optional.empty(), "http://floci:4566",
Optional.of(new SessionCreds("test", "test", "test")));
}
@Test
void aConsoleReadingItsOwnEndpointVariableGetsTheEndpointUnderThatNameToo() {
withUiConfig();
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(config.hostname()).thenReturn(Optional.of("floci"));
when(config.effectiveBaseUrl()).thenReturn("http://floci:4566");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
ConsoleProfile profile = new ConsoleProfile(8080, "CONSOLE_API_URL", "/api/health",
"status", "ok", "unavailable", "the console");
List<String> env = newManager().injectedEnv(profile);
assertTrue(env.contains("CONSOLE_API_URL=http://floci:4566"), env.toString());
assertTrue(env.contains("AWS_ENDPOINT_URL=http://floci:4566"), env.toString());
assertTrue(env.contains("PORT=8080"), env.toString());
}
@Test
void skippingTlsVerificationIsAnnouncedInBothTheContractAndTheNodeForm() {
withUiConfig();
when(ui.insecureSkipTlsVerify()).thenReturn(true);
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(config.hostname()).thenReturn(Optional.of("floci"));
when(config.effectiveBaseUrl()).thenReturn("http://floci:4566");
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
List<String> env = newManager().injectedEnv(ConsoleProfileResolver.contractV1());
assertTrue(env.contains("FLOCI_TLS_SKIP_VERIFY=1"), env.toString());
assertTrue(env.contains("NODE_TLS_REJECT_UNAUTHORIZED=0"), env.toString());
}
@Test
void extraEnvIsAppliedAndReplacesAnInjectedDefaultRatherThanDuplicatingIt() {
withUiConfig();
when(ui.extraEnv()).thenReturn(Optional.of(List.of(
"STACKPORT_ALLOW_WRITES=false",
"AWS_ACCESS_KEY_ID=custom")));
when(containerDetector.isRunningInContainer()).thenReturn(false);
when(dockerHostResolver.resolve()).thenReturn("host.docker.internal");
when(config.tls()).thenReturn(tls);
when(tls.enabled()).thenReturn(false);
when(config.port()).thenReturn(4566);
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
List<String> env = newManager().injectedEnv(ConsoleProfileResolver.contractV1());
assertTrue(env.contains("STACKPORT_ALLOW_WRITES=false"), env.toString());
assertTrue(env.contains("AWS_ACCESS_KEY_ID=custom"), env.toString());
assertEquals(1, env.stream().filter(entry -> entry.startsWith("AWS_ACCESS_KEY_ID=")).count(),
"an overridden variable must appear once, not twice: " + env);
}
@Test
void extraEnvCannotOverrideThePortThePortBindingAndProbeAreBuiltFrom() {
// PORT is structural: withPortBinding() and the readiness probe both read
// profile.internalPort(). Honouring an override here would leave the console listening on
// 8080 while Floci published and polled 4500, so the sidecar never becomes reachable.
LinkedHashMap<String, String> env = new LinkedHashMap<>();
env.put(FlociUiManager.PORT_ENV, "4500");
FlociUiManager.applyExtraEnv(env, Optional.of(List.of("PORT=8080")));
assertEquals("4500", env.get(FlociUiManager.PORT_ENV),
"extra-env must not move the port the binding and probe were built from");
}
@Test
void extraEnvCannotOverrideTheEndpointAdoptionComparesAgainst() {
// AWS_ENDPOINT_URL is what endpointDrifted() compares to spot a sidecar left addressing a
// previous Floci. An override here reads as permanent drift, so the container would be
// recreated on every check.
LinkedHashMap<String, String> env = new LinkedHashMap<>();
env.put(FlociUiManager.CANONICAL_ENDPOINT_ENV, "http://floci:4566");
FlociUiManager.applyExtraEnv(env, Optional.of(List.of("AWS_ENDPOINT_URL=http://elsewhere:9999")));
assertEquals("http://floci:4566", env.get(FlociUiManager.CANONICAL_ENDPOINT_ENV),
"extra-env must not move the endpoint adoption compares against");
}
@Test
void reservingTheStructuralKeysDoesNotBlockTheEntriesBesideThem() {
// The guard is two keys, not a general refusal: everything else still applies.
LinkedHashMap<String, String> env = new LinkedHashMap<>();
env.put(FlociUiManager.PORT_ENV, "4500");
FlociUiManager.applyExtraEnv(env, Optional.of(List.of(
"PORT=8080",
"MY_CONSOLE_FLAG=true")));
assertEquals("4500", env.get(FlociUiManager.PORT_ENV));
assertEquals("true", env.get("MY_CONSOLE_FLAG"));
}
@Test
void extraEnvKeepsEveryEqualsSignAfterTheFirstInTheValue() {
LinkedHashMap<String, String> env = new LinkedHashMap<>();
FlociUiManager.applyExtraEnv(env, Optional.of(List.of("TOKEN=a=b==c")));
assertEquals("a=b==c", env.get("TOKEN"));
}
@Test
void malformedExtraEnvEntryIsDroppedRatherThanFailingTheStart() {
LinkedHashMap<String, String> env = new LinkedHashMap<>();
env.put("PORT", "4500");
FlociUiManager.applyExtraEnv(env, Optional.of(java.util.Arrays.asList(
"NOT_A_PAIR", "=orphan", " ", null, "GOOD=yes")));
assertEquals(Map.of("PORT", "4500", "GOOD", "yes"), env);
}
@Test
void readinessFollowsTheConfiguredFieldAndValue() throws Exception {
HttpServer server = startStatusServer("/api/health", """
{"status":"ok","endpoint_url":"http://floci:4566"}
""");
try {
FlociUiManager manager = adoptSidecar(
server.getAddress().getPort(), "/api/health", "status", "ok");
assertTrue(manager.status().ready());
} finally {
server.stop(0);
}
}
@Test
void aReadyFieldOfNoneTreatsAnyTwoHundredAsReady() throws Exception {
// All a plain liveness endpoint can report. Reading a field that is not in the
// response would otherwise leave the console reported as never ready. The opt-out is
// the literal "none": an empty value arrives as an absent property and would silently
// mean "use the default field".
HttpServer server = startStatusServer("/healthz", "OK");
try {
FlociUiManager manager = adoptSidecar(
server.getAddress().getPort(), "/healthz", "none", "reachable");
assertTrue(manager.status().ready());
} finally {
server.stop(0);
}
}
@Test
void aConfiguredReadyFieldThatDoesNotMatchIsNotReady() throws Exception {
HttpServer server = startStatusServer("/api/health", """
{"status":"degraded"}
""");
try {
FlociUiManager manager = adoptSidecar(
server.getAddress().getPort(), "/api/health", "status", "ok");
assertFalse(manager.status().ready());
} finally {
server.stop(0);
}
}
@Test
void driftIsDetectedAgainstTheCanonicalEndpointVariable() {
List<String> env = List.of("AWS_ENDPOINT_URL=http://10.88.3.251:4566", "PORT=8080");
assertTrue(FlociUiManager.endpointDrifted(env, "http://10.88.3.252:4566"));
assertFalse(FlociUiManager.endpointDrifted(env, "http://10.88.3.251:4566"),
"a sidecar already pointing at this Floci must be adopted, not recreated");
}
@Test
void aSidecarFromAFlociThatOnlySetFlociEndpointIsRecreatedOnce() {
// Before the console contract only FLOCI_ENDPOINT was injected. Such a container cannot be
// checked for drift, so it is recreated on the first start after the upgrade and adopted
// normally from then on.
List<String> env = List.of("PORT=4500", "FLOCI_ENDPOINT=http://10.88.3.252:4566");
assertTrue(FlociUiManager.endpointDrifted(env, "http://10.88.3.252:4566"),
"a pre-contract sidecar must be recreated so it gains the canonical variable");
}
/** Adopts a console that resolves to the built-in floci-ui profile. */
private FlociUiManager adoptSidecar(int port) {
return adoptSidecar(port, "floci/floci-ui:latest",
Optional.empty(), Optional.empty(), Optional.empty());
}
/** Adopts a console whose health shape is pinned by explicit configuration. */
private FlociUiManager adoptSidecar(int port, String statusPath, String readyField, String readyValue) {
return adoptSidecar(port, "acme/console:1", Optional.of(statusPath),
Optional.of(readyField), Optional.of(readyValue));
}
private FlociUiManager adoptSidecar(int port, String image, Optional<String> statusPath,
Optional<String> readyField, Optional<String> readyValue) {
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.UiServiceConfig ui = mock(EmulatorConfig.UiServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.ui()).thenReturn(ui);
when(ui.enabled()).thenReturn(true);
when(ui.image()).thenReturn(image);
when(ui.containerName()).thenReturn("floci-ui");
when(ui.port()).thenReturn(port);
when(ui.internalPort()).thenReturn(OptionalInt.empty());
when(ui.endpointEnv()).thenReturn(Optional.empty());
when(ui.extraEnv()).thenReturn(Optional.empty());
when(ui.statusPath()).thenReturn(statusPath);
when(ui.statusReadyField()).thenReturn(readyField);
when(ui.statusReadyValue()).thenReturn(readyValue);
when(ui.statusUnavailableValue()).thenReturn(Optional.empty());
when(ui.endpoint()).thenReturn(Optional.of("http://custom:4566"));
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
when(containerBuilder.resolveImage(image)).thenReturn(image);
Container existing = mock(Container.class);
when(existing.getId()).thenReturn("ui-container");
when(lifecycleManager.findByName("floci-ui")).thenReturn(Optional.of(existing));
when(lifecycleManager.containerEnv("ui-container"))
.thenReturn(Optional.of(List.of("AWS_ENDPOINT_URL=http://custom:4566")));
when(lifecycleManager.adopt("ui-container", List.of(4500))).thenReturn(
new ContainerInfo("ui-container", Map.of(4500, new EndpointInfo("127.0.0.1", port))));
FlociUiManager manager = newManager();
FlociUiManager manager = newManager(containerBuilder);
manager.ensureStarted();
return manager;
}
private static HttpServer startRuntimeStatusServer(String response) throws IOException {
return startStatusServer("/api/clouds/aws/status", response);
}
private static HttpServer startStatusServer(String path, String response) throws IOException {
byte[] body = response.getBytes(StandardCharsets.UTF_8);
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/api/clouds/aws/status", exchange -> {
server.createContext(path, exchange -> {
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, body.length);
try (var output = exchange.getResponseBody()) {