feat(reset): stop when a selected container publishes no port for the emulator

A selected container that is running without a matching published port has nothing to be found by, and the configured endpoint may be another instance.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-10 21:51:42 -05:00
1 parent c95d2ffb16
commit 11de360c11
3 files changed
+49 -5

No files matched your search

+13 -4
View File
@@ -9,6 +9,7 @@ import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
import java.net.URI;
import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
@@ -147,9 +148,17 @@ 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) {
if (ports == null || ports.isBlank()) return fallback;
return publishedEndpoint(ports, fallback).orElse(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.
*/
public Optional<String> publishedEndpoint(String ports, String configured) {
if (ports == null || ports.isBlank()) return Optional.empty();
try {
int containerPort = URI.create(fallback).getPort();
int containerPort = URI.create(configured).getPort();
if (containerPort == -1) containerPort = product.defaultPort();
for (String mapping : ports.trim().split("\\s+")) {
int arrow = mapping.indexOf("->");
@@ -158,11 +167,11 @@ public class GlobalOptions {
String rest = mapping.substring(arrow + 2);
String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest;
if (String.valueOf(containerPort).equals(cPort)) {
return "http://localhost:" + hostPort;
return Optional.of("http://localhost:" + hostPort);
}
}
} catch (Exception ignored) {}
return fallback;
return Optional.empty();
}
private static boolean isStdoutTty() {
@@ -109,7 +109,13 @@ 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);
}
return new Target(global.endpointFromPorts(info.get().ports(), global.endpoint), null);
Optional<String> published = global.publishedEndpoint(info.get().ports(), global.endpoint);
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
+ "' publishes no port for the emulator, so nothing was reset." + how);
}
return new Target(published.orElse(global.endpoint), null);
}
@Override
@@ -193,6 +193,35 @@ class ResetCommandTest {
assertTrue(target.error().contains("is not running (state: exited)"), target.error());
}
/**
* Running, but with no published port for the emulator (or only an unrelated one): there is
* nothing to find it by, and the configured endpoint may be the default instance.
*/
@ParameterizedTest
@ValueSource(strings = {"", "18080->8080/tcp"})
void aChosenContainerWithoutAMatchingPortResetsNothing(String ports) {
ResetCommand.Target target = targetOf(dockerWith("floci-team-b", "running", ports), "--container", "floci-team-b");
assertNull(target.endpoint());
assertTrue(target.error().contains("publishes no port for the emulator, so nothing was reset."), target.error());
}
@Test
void aChosenContainerIsFoundThroughItsPortMapping() {
ResetCommand.Target target = targetOf(dockerWith("floci-team-b", "running", "14566->4566/tcp"),
"--container", "floci-team-b");
assertEquals("http://localhost:14566", target.endpoint());
}
/** The default container without a published port (host networking) is still the default instance. */
@Test
void theDefaultContainerWithoutAPublishedPortUsesItsConfiguredEndpoint() {
ResetCommand.Target target = targetOf(dockerWith("floci", "running", ""));
assertEquals("http://localhost:4566", target.endpoint());
}
@Test
void aChosenContainerDockerCannotLookUpResetsNothing() {
DockerClient broken = new DockerClient() {