diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index af8764e..d551405 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -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 { /** {@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."; } } diff --git a/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java b/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java index 6299841..350d0df 100644 --- a/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java +++ b/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java @@ -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"))); } /**