diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 537f842..af8764e 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -15,6 +15,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.TreeMap; import java.util.concurrent.Callable; import java.util.regex.Pattern; @@ -120,7 +121,7 @@ public class StartCommand implements Callable { return "--port must be between 1 and 65535, but was " + port + ".\nPick a free port, for example " + (profile.defaultPort() + 10000) + "."; } - if (pull == null || !PULL_POLICIES.contains(pull.toLowerCase())) { + if (pull == null || !PULL_POLICIES.contains(pull.toLowerCase(Locale.ROOT))) { // Anything else used to fall through to a pull, so a typo silently meant "always". return "Invalid --pull '" + pull + "'.\nUse always, missing or never."; } @@ -143,14 +144,28 @@ public class StartCommand implements Callable { * the CLI rather than docker: a bind source docker creates is owned by root on Linux, and the * emulator then cannot write to it. Restart calls this before it stops anything, so a * directory that cannot be created leaves the running instance alone. + * + *

An existing directory is accepted as it is, with no write check: the emulator runs as its + * own user (uid 1001) and the AWS image re-owns its data directory at startup, so whether the + * CLI's user can write there says nothing about whether the emulator can. The emulator reports + * a directory it cannot write to itself. */ public String preparePersistDir() { + return preparePersistDir(DockerClient.dockerHost()); + } + + /** {@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; + // 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())); return null; } catch (Exception e) { - return "Could not create the persist directory " + persistPath() + ": " + e.getMessage() + // 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() + "\nPass a --persist directory you can write to."; } } diff --git a/src/test/java/io/floci/cli/unit/RestartCommandTest.java b/src/test/java/io/floci/cli/unit/RestartCommandTest.java index 1081f2a..68bed09 100644 --- a/src/test/java/io/floci/cli/unit/RestartCommandTest.java +++ b/src/test/java/io/floci/cli/unit/RestartCommandTest.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeFalse; /** * Restart builds its StartCommand programmatically, so picocli never applies the profile to it. @@ -152,9 +153,11 @@ class RestartCommandTest { PrintStream err = System.err; ByteArrayOutputStream outBuf = new ByteArrayOutputStream(); ByteArrayOutputStream errBuf = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outBuf)); - System.setErr(new PrintStream(errBuf)); try { + // Root writes through the permission bits; the restart would then reach Docker. + assumeFalse(Files.isWritable(readOnly), "permissions do not bind this user (root)"); + System.setOut(new PrintStream(outBuf)); + System.setErr(new PrintStream(errBuf)); assertEquals(1, restart.call()); } finally { System.setOut(out); diff --git a/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java b/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java index c037cf8..6299841 100644 --- a/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java +++ b/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java @@ -3,7 +3,10 @@ package io.floci.cli.unit; import io.floci.cli.FlociCli; import io.floci.cli.commands.StartCommand; import io.floci.cli.config.ProfileStore; +import io.floci.cli.docker.DockerClient; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.io.TempDir; import picocli.CommandLine.ParseResult; @@ -13,6 +16,7 @@ import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import java.util.Locale; import static org.junit.jupiter.api.Assertions.*; @@ -212,6 +216,71 @@ class StartCommandArgsTest { assertTrue(parse("start", "--pull", "alwayz").validationError().contains("Invalid --pull 'alwayz'")); } + /** Pull policies are plain ASCII words: a Turkish default locale lowercases "MISSING" with a dotless i. */ + @Test + void pullPolicyDoesNotDependOnTheDefaultLocale() { + Locale before = Locale.getDefault(); + Locale.setDefault(Locale.forLanguageTag("tr-TR")); + try { + assertNull(parse("start", "--pull", "MISSING").validationError()); + } finally { + Locale.setDefault(before); + } + } + + private static final DockerClient.DockerHost LOCAL = + new DockerClient.DockerHost(DockerClient.Kind.UNIX, "/var/run/docker.sock", null); + private static final DockerClient.DockerHost REMOTE = + new DockerClient.DockerHost(DockerClient.Kind.TCP, null, "tcp://build-host:2375"); + + @Test + void aLocalDaemonGetsItsPersistDirCreated() { + Path state = tempDir.resolve("made").resolve("state"); + + assertNull(parse("start", "--persist", state.toString()).preparePersistDir(LOCAL)); + + assertTrue(Files.isDirectory(state)); + } + + /** A remote daemon reads the bind source on its own machine: nothing is created or refused here. */ + @Test + void aRemoteDaemonsPersistDirIsNotCreatedLocally() throws Exception { + Path blocker = Files.writeString(Files.createDirectories(tempDir).resolve("a-file"), "x"); + Path state = blocker.resolve("state"); // cannot exist locally: its parent is a file + + assertNull(parse("start", "--persist", state.toString()).preparePersistDir(REMOTE)); + assertFalse(Files.exists(state)); + assertNotNull(parse("start", "--persist", state.toString()).preparePersistDir(LOCAL)); + } + + /** A path that is not valid on this platform gets the same hint, not an exception from building the message. */ + @Test + void anInvalidPersistPathIsReportedNotThrown() { + String invalid = "data\u0000dir"; // NUL is rejected by every platform's Path + + 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); + } + + /** + * An existing directory this user cannot write to is left to the emulator, which runs as its + * own user and reports an unwritable data directory itself. + */ + @Test + @DisabledOnOs(OS.WINDOWS) + void anExistingDirectoryIsAcceptedWithoutAWriteCheck() throws Exception { + Path readOnly = Files.createDirectories(tempDir.resolve("read-only")); + assertTrue(readOnly.toFile().setWritable(false)); + try { + assertNull(parse("start", "--persist", readOnly.toString()).preparePersistDir(LOCAL)); + } finally { + readOnly.toFile().setWritable(true); + } + } + @Test void readinessPollKeepsTheHostFromTheEndpointWhenSwappingThePort() { assertEquals("http://floci.internal:4599", StartCommand.withPort("http://floci.internal:4566", 4599));