fix(start): check the persist path for a remote daemon before restart stops anything

A remote daemon skips only the local directory creation. The path itself is resolved for every daemon, so an unusable one is reported before the stop instead of thrown while building the run arguments afterwards.
This commit is contained in:
Hector Ventura committed 2026-10-10 17:05:20 -05:00
1 parent 0535e9cc67
commit 36b03a8cb7
2 files changed
+38 -5

No files matched your search

@@ -12,6 +12,7 @@ import picocli.CommandLine.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@@ -157,15 +158,24 @@ public class StartCommand implements Callable<Integer> {
/** {@link #preparePersistDir()} for a daemon reached through {@code daemon}; the test seam. */
public String preparePersistDir(DockerClient.DockerHost daemon) {
if (persistDir == null || persistDir.isBlank()) return null;
Path path;
try {
// Resolved for every daemon: dockerRunArgs() resolves it again after restart has
// stopped the container, and must not be the first to find the path unusable.
path = Path.of(persistPath());
} catch (InvalidPathException e) {
// Names persistDir as given: resolving it is what failed.
return "Invalid persist directory '" + persistDir + "': " + e.getReason()
+ ".\nPass a --persist path that is valid on this system.";
}
// A remote daemon reads the bind source on its own machine, where this CLI can neither
// create nor check it.
if (daemon.kind() == DockerClient.Kind.TCP) return null;
try {
Files.createDirectories(Path.of(persistPath()));
Files.createDirectories(path);
return null;
} catch (Exception e) {
// Names persistDir as given: persistPath() is what threw for a path that is not valid here.
return "Could not create the persist directory " + persistDir + ": " + e.getMessage()
return "Could not create the persist directory " + path + ": " + e.getMessage()
+ "\nPass a --persist directory you can write to.";
}
}
@@ -261,8 +261,31 @@ class StartCommandArgsTest {
String error = parse("start", "--persist", invalid).preparePersistDir(LOCAL);
assertNotNull(error);
assertTrue(error.startsWith("Could not create the persist directory"), error);
assertTrue(error.contains("Pass a --persist directory"), error);
assertTrue(error.startsWith("Invalid persist directory"), error);
assertTrue(error.contains("Pass a --persist path"), error);
}
/**
* A remote daemon skips the local directory, not the path check: restart runs this before the
* stop, and dockerRunArgs() would otherwise be the first to throw, after the container is gone.
*/
@Test
void anInvalidPersistPathIsReportedForARemoteDaemonToo() {
StartCommand start = parse("start", "--persist", "data\u0000dir");
String error = start.preparePersistDir(REMOTE);
assertNotNull(error);
assertTrue(error.startsWith("Invalid persist directory"), error);
}
/** What preparePersistDir accepts, dockerRunArgs can build: nothing is left to fail after a stop. */
@Test
void aPathARemoteDaemonAcceptsBuildsItsRunArguments() {
StartCommand start = parse("start", "--persist", "/srv/floci-data");
assertNull(start.preparePersistDir(REMOTE));
assertTrue(start.dockerRunArgs(SOCKET).stream().anyMatch(a -> a.endsWith(":/app/data")));
}
/**