fix(reset): reset the server a profile or FLOCI_ENDPOINT names, not a local container of the default name

With no container selected, an endpoint that came from a profile or the environment was replaced by the port of a container running locally under the default name, so reset --yes could wipe the wrong instance. Such an endpoint is now used as given, like a typed one.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-11 09:47:20 -05:00
1 parent 97eaa5e820
commit f0c3dfa99b
3 files changed
+143 -4

No files matched your search

+3 -1
View File
@@ -724,7 +724,9 @@ It calls the emulator's `POST <prefix>/state/reset`. The AWS, GCP and OCI emulat
server without it (currently floci-az) the command says reset is not available and exits 1. Without
`--yes` and with no terminal to ask on, it exits 2.
Reset never guesses which server to wipe. An `--endpoint` you type is used as given. A container you
Reset never guesses which server to wipe. An `--endpoint` you type is used as given, and so is an
endpoint that a profile or `FLOCI_ENDPOINT` supplies when no container is selected: a container
that happens to run locally under the default name does not take its place. A container you
select (`--container`, a profile, or `FLOCI_CONTAINER`) has to be running: if it is missing or
stopped, nothing is reset and the command exits 1 instead of falling back to the default instance.
@@ -83,13 +83,19 @@ public class ResetCommand implements Callable<Integer> {
/**
* Where this reset acts. Reset deletes data, so unlike the read-only commands it never
* guesses: an {@code --endpoint} the user typed is used as given, and a container the user
* chose must be found running. Falling back to the configured endpoint, as
* {@link GlobalOptions#resolvedEndpoint} does, is right only for the default container: there
* the configured endpoint names the same default instance.
* chose must be found running.
*
* <p>An endpoint that a profile or {@code <PREFIX>_ENDPOINT} supplies names a server just as
* a typed one does. With no container chosen it is used as given too: looking up the default
* container instead, as {@link GlobalOptions#resolvedEndpoint} does, would reset whatever
* runs locally under the default name rather than the server the user pointed at. Only when
* nothing at all was selected is the default container looked up, falling back to the
* default endpoint, which names the same default instance.
*/
public Target target() {
if (global.typed("--endpoint")) return new Target(global.endpoint, null);
boolean chosen = global.typed("--container") || !profile.defaultContainer().equals(global.container);
if (!chosen && namesAServer(global.endpoint, profile)) return new Target(global.endpoint, null);
String how = "\nRun '" + profile.commandPrefix() + " status" + global.instanceSelector()
+ "' to check it, or pass --endpoint <url> to name the server to reset.";
Optional<DockerClient.ContainerInfo> info;
@@ -121,6 +127,21 @@ public class ResetCommand implements Callable<Integer> {
return new Target(published.orElse(global.endpoint), null);
}
/**
* Whether {@code endpoint} is something other than the product's default, that is, whether a
* profile or the environment pointed this command at a particular server. A trailing slash
* does not make it a different one.
*/
public static boolean namesAServer(String endpoint, ProductProfile product) {
return endpoint != null && !withoutTrailingSlashes(endpoint).equals(product.defaultEndpoint());
}
private static String withoutTrailingSlashes(String url) {
String trimmed = url.strip();
while (trimmed.endsWith("/")) trimmed = trimmed.substring(0, trimmed.length() - 1);
return trimmed;
}
@Override
public Integer call() {
Printer printer = global.printer();
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;
@@ -20,6 +21,7 @@ import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -167,6 +169,120 @@ class ResetCommandTest {
assertNull(target.error());
}
private ResetCommand.Target targetWithProfile(DockerClient docker, String yaml, String... args) throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve("p.yaml"), yaml);
ResetCommand reset = new ResetCommand(ProductProfile.AWS, docker, NOBODY);
List<String> all = new ArrayList<>(List.of("--profile", "p"));
all.addAll(List.of(args));
new CommandLine(reset)
.setDefaultValueProvider(new ProfileDefaultValueProvider(new ProfileStore(tempDir)))
.parseArgs(all.toArray(String[]::new));
return reset.target();
}
/**
* BL-084: an endpoint from a profile names a server just as a typed one does. A container that
* happens to run locally under the default name must not take its place.
*/
@Test
void anEndpointFromAProfileIsNotReplacedByTheLocalDefaultContainer() throws Exception {
DockerClient local = dockerWith("floci", "running", "4566->4566/tcp");
ResetCommand.Target target = targetWithProfile(local, "endpoint: http://remote-host:4566\n");
assertNull(target.error(), target.error());
assertEquals("http://remote-host:4566", target.endpoint());
}
/** The same server is reset whether or not something runs locally under the default name. */
@Test
void anEndpointFromAProfileResolvesTheSameWithOrWithoutALocalContainer() throws Exception {
String yaml = "endpoint: http://localhost:14566\n";
ResetCommand.Target running = targetWithProfile(dockerWith("floci", "running", "4566->4566/tcp"), yaml);
ResetCommand.Target absent = targetWithProfile(dockerWith("something-else", "running", ""), yaml);
ResetCommand.Target stopped = targetWithProfile(dockerWith("floci", "exited", ""), yaml);
assertEquals("http://localhost:14566", running.endpoint());
assertEquals(running, absent);
assertEquals(running, stopped);
}
/** A profile that names a container is found through the container, endpoint or not. */
@Test
void aProfileContainerStillDecidesOverItsEndpoint() throws Exception {
ResetCommand.Target target = targetWithProfile(dockerWith("floci-b", "running", "24566->4566/tcp"),
"container: floci-b\nendpoint: http://localhost:14566\n");
assertEquals("http://localhost:24566", target.endpoint());
}
/** A typed flag wins over the profile's endpoint, as everywhere else. */
@Test
void typedFlagsWinOverAProfilesEndpoint() throws Exception {
String yaml = "endpoint: http://remote-host:4566\n";
ResetCommand.Target typedEndpoint = targetWithProfile(dockerWith("floci", "running", "4566->4566/tcp"), yaml,
"--endpoint", "http://other-host:4566");
ResetCommand.Target typedContainer = targetWithProfile(dockerWith("floci-b", "running", "24566->4566/tcp"), yaml,
"--container", "floci-b");
assertEquals("http://other-host:4566", typedEndpoint.endpoint());
assertEquals("http://localhost:24566", typedContainer.endpoint());
}
/** The environment's endpoint counts the same way; what matters is that it is not the default. */
@ParameterizedTest
@CsvSource({
"http://remote-host:4566, true",
"http://localhost:14566, true",
"https://localhost:4566, true",
"http://localhost:4566, false",
"http://localhost:4566/, false",
"' http://localhost:4566// ', false"})
void anEndpointOtherThanTheDefaultNamesAServer(String endpoint, boolean names) {
assertEquals(names, ResetCommand.namesAServer(endpoint, ProductProfile.AWS));
}
@Test
void eachProductHasItsOwnDefaultEndpoint() {
assertFalse(ResetCommand.namesAServer("http://localhost:4588", ProductProfile.GCP));
assertTrue(ResetCommand.namesAServer("http://localhost:4566", ProductProfile.GCP));
assertFalse(ResetCommand.namesAServer(null, ProductProfile.OCI));
}
/** End to end with --yes: the request goes to the profile's server, not to the local container's port. */
@Test
void yesWithAProfileEndpointPostsToThatEndpoint() throws Exception {
Files.createDirectories(tempDir);
Files.writeString(tempDir.resolve("p.yaml"), "endpoint: http://127.0.0.1:" + server.getAddress().getPort() + "\n");
// A local default container on another port: resetting it would hit nothing and fail.
int elsewhere;
try (ServerSocket socket = new ServerSocket(0)) {
elsewhere = socket.getLocalPort();
}
ResetCommand reset = new ResetCommand(ProductProfile.AWS,
dockerWith("floci", "running", elsewhere + "->4566/tcp"), NOBODY);
PrintStream out = System.out;
PrintStream err = System.err;
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
System.setOut(new PrintStream(new ByteArrayOutputStream()));
System.setErr(new PrintStream(errBuf));
int exit;
try {
exit = new CommandLine(reset)
.setDefaultValueProvider(new ProfileDefaultValueProvider(new ProfileStore(tempDir)))
.execute("--yes", "--profile", "p");
} finally {
System.setOut(out);
System.setErr(err);
}
assertEquals(0, exit, errBuf.toString());
assertEquals(List.of("POST /_floci/state/reset"), requests);
}
@Test
void theDefaultContainerIsFoundThroughItsPortMapping() {
ResetCommand.Target target = targetOf(dockerWith("floci", "running", "14566->4566/tcp"));