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 <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-10 22:03:03 -05:00
1 parent 11de360c11
commit 946d20452c
3 files changed
+54 -18

No files matched your search

+20 -17
View File
@@ -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<String> publishedEndpoint(String ports, String configured) {
public static Optional<String> 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();
}
@@ -109,7 +109,9 @@ public class ResetCommand implements Callable<Integer> {
return new Target(null, "Container '" + global.container + "' is not running (state: "
+ info.get().state() + "), so nothing was reset." + how);
}
Optional<String> 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<String> 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
@@ -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() {