From 946d20452c88da83caa11ff4aeefce5462d55d42 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 10 Oct 2026 22:03:03 -0500 Subject: [PATCH] feat(reset): find a selected instance by the emulator's port, not the configured host port A profile for an instance on its own host port names that port in its endpoint, while the container still listens on the emulator's; reset matched the wrong one and refused such instances. Signed-off-by: Hector Ventura --- src/main/java/io/floci/cli/GlobalOptions.java | 37 ++++++++++--------- .../io/floci/cli/commands/ResetCommand.java | 4 +- .../io/floci/cli/unit/ResetCommandTest.java | 31 ++++++++++++++++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/floci/cli/GlobalOptions.java b/src/main/java/io/floci/cli/GlobalOptions.java index 7f213fa..a2f895b 100644 --- a/src/main/java/io/floci/cli/GlobalOptions.java +++ b/src/main/java/io/floci/cli/GlobalOptions.java @@ -148,29 +148,32 @@ public class GlobalOptions { // Matches the container-side port from the configured endpoint to find the actual host port. public String endpointFromPorts(String ports, String fallback) { - return publishedEndpoint(ports, fallback).orElse(fallback); + if (ports == null || ports.isBlank()) return fallback; + try { + int containerPort = URI.create(fallback).getPort(); + if (containerPort == -1) containerPort = product.defaultPort(); + return publishedEndpoint(ports, containerPort).orElse(fallback); + } catch (Exception ignored) {} + return fallback; } /** - * The endpoint a container with these published {@code ports} is reached on, or empty when it - * publishes no port for the one {@code configured} names. For callers that must not fall back. + * Where a container with these published {@code ports} ("14566->4566/tcp ...") is reached for + * its {@code containerPort}, or empty when that port is not published. For callers that must + * not fall back to a configured endpoint. */ - public Optional publishedEndpoint(String ports, String configured) { + public static Optional publishedEndpoint(String ports, int containerPort) { if (ports == null || ports.isBlank()) return Optional.empty(); - try { - int containerPort = URI.create(configured).getPort(); - if (containerPort == -1) containerPort = product.defaultPort(); - for (String mapping : ports.trim().split("\\s+")) { - int arrow = mapping.indexOf("->"); - if (arrow < 0) continue; - String hostPort = mapping.substring(0, arrow); - String rest = mapping.substring(arrow + 2); - String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; - if (String.valueOf(containerPort).equals(cPort)) { - return Optional.of("http://localhost:" + hostPort); - } + for (String mapping : ports.trim().split("\\s+")) { + int arrow = mapping.indexOf("->"); + if (arrow < 0) continue; + String hostPort = mapping.substring(0, arrow); + String rest = mapping.substring(arrow + 2); + String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; + if (String.valueOf(containerPort).equals(cPort)) { + return Optional.of("http://localhost:" + hostPort); } - } catch (Exception ignored) {} + } return Optional.empty(); } diff --git a/src/main/java/io/floci/cli/commands/ResetCommand.java b/src/main/java/io/floci/cli/commands/ResetCommand.java index aa2f877..5e4de19 100644 --- a/src/main/java/io/floci/cli/commands/ResetCommand.java +++ b/src/main/java/io/floci/cli/commands/ResetCommand.java @@ -109,7 +109,9 @@ public class ResetCommand implements Callable { return new Target(null, "Container '" + global.container + "' is not running (state: " + info.get().state() + "), so nothing was reset." + how); } - Optional published = global.publishedEndpoint(info.get().ports(), global.endpoint); + // By the emulator's own port inside the container, which never changes; the configured + // endpoint names the host port, and that differs on every instance but the default one. + Optional published = GlobalOptions.publishedEndpoint(info.get().ports(), profile.defaultPort()); if (chosen && published.isEmpty()) { // No port to find it by: the configured endpoint may well be another instance. return new Target(null, "Container '" + global.container diff --git a/src/test/java/io/floci/cli/unit/ResetCommandTest.java b/src/test/java/io/floci/cli/unit/ResetCommandTest.java index 804ee30..38288e1 100644 --- a/src/test/java/io/floci/cli/unit/ResetCommandTest.java +++ b/src/test/java/io/floci/cli/unit/ResetCommandTest.java @@ -4,6 +4,7 @@ import com.sun.net.httpserver.HttpServer; import io.floci.cli.FlociCli; import io.floci.cli.ProductProfile; import io.floci.cli.commands.ResetCommand; +import io.floci.cli.config.ProfileDefaultValueProvider; import io.floci.cli.config.ProfileStore; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; @@ -20,6 +21,7 @@ import java.io.OutputStream; import java.io.PrintStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -214,6 +216,35 @@ class ResetCommandTest { assertEquals("http://localhost:14566", target.endpoint()); } + /** + * An instance on its own host port: the profile's endpoint names that host port (14566), the + * container still listens on the emulator's (4566). It is found by the latter. + */ + @Test + void anInstanceOnACustomHostPortIsFoundByTheEmulatorsPort() throws Exception { + Files.createDirectories(tempDir); + Files.writeString(tempDir.resolve("team-b.yaml"), + "container: floci-team-b\nendpoint: http://localhost:14566\nport: 14566\n"); + ResetCommand reset = new ResetCommand(ProductProfile.AWS, + dockerWith("floci-team-b", "running", "14566->4566/tcp"), NOBODY); + new CommandLine(reset) + .setDefaultValueProvider(new ProfileDefaultValueProvider(new ProfileStore(tempDir))) + .parseArgs("--profile", "team-b"); + + ResetCommand.Target target = reset.target(); + + assertNull(target.error(), target.error()); + assertEquals("http://localhost:14566", target.endpoint()); + } + + @Test + void theOtherProductsAreFoundByTheirOwnPort() { + ResetCommand gcp = new ResetCommand(ProductProfile.GCP, dockerWith("floci-gcp-b", "running", "14588->4588/tcp"), NOBODY); + new CommandLine(gcp).parseArgs("--container", "floci-gcp-b"); + + assertEquals("http://localhost:14588", gcp.target().endpoint()); + } + /** The default container without a published port (host networking) is still the default instance. */ @Test void theDefaultContainerWithoutAPublishedPortUsesItsConfiguredEndpoint() {