mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 06:00:47 -05:00
fix(start): mount a relative --persist path as a directory and validate --port and --pull (#50)
* fix(start): mount a relative --persist path as a directory and validate --port and --pull --persist ./data is resolved to an absolute path and created before docker run, so state lands in that directory instead of a Docker named volume; --port outside 1-65535 and an unknown --pull policy are rejected with exit 2 (restart checks them before stopping anything). start creates the persist directory itself (preparePersistDir). restart now does that before it stops anything, so an unwritable directory fails the restart with the instance still running, instead of after the container was already removed. * fix(start): leave a remote daemon's persist directory alone and accept pull policies in any locale The persist directory is created only for a local daemon, an invalid path is reported instead of thrown while building the message, and pull policies are lowercased with Locale.ROOT. The permission test is skipped when permissions do not bind the user. * 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. * test(restart): let the recorded start answer restart's pre-stop checks The recording start used by the call-path tests was never parsed, so the new --pull validation rejected it. It now delegates the checks to the start restart really built. * fix(start): pass a remote daemon's persist path on unchanged A remote daemon's bind source is no longer resolved by the CLI machine's path rules, which turned /srv/data into a drive path on a Windows CLI. Only a local daemon has relative paths resolved. * fix(start): refuse a relative --persist path for a remote daemon A remote daemon cannot resolve a path relative to the CLI's working directory, and docker reads a bare name as a named volume, so the state would land somewhere a later restart does not carry over. * refactor(start): keep bindSource's javadoc on the method
This commit is contained in:
1 parent
f6cec794d7
commit
3ca4108498
4 files changed
+302
-3
No files matched your search
@@ -10,6 +10,8 @@ import io.floci.cli.config.ProfileStore;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerException;
|
||||
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;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
@@ -25,6 +27,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
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.
|
||||
@@ -156,6 +159,37 @@ class RestartCommandTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A persist directory that cannot be created fails the restart before the stop, so the running
|
||||
* instance is left alone instead of being removed and never started again.
|
||||
*/
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void anUnwritablePersistDirFailsBeforeTheStop() throws Exception {
|
||||
Path readOnly = Files.createDirectories(tempDir.resolve("read-only"));
|
||||
assertTrue(readOnly.toFile().setWritable(false));
|
||||
writeProfile("ro", "container: floci-ro-none\npersistDir: " + readOnly.resolve("state") + "\n");
|
||||
RestartCommand restart = parse(ProductProfile.AWS, "--profile", "ro");
|
||||
|
||||
PrintStream out = System.out;
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
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);
|
||||
System.setErr(err);
|
||||
readOnly.toFile().setWritable(true);
|
||||
}
|
||||
assertTrue(errBuf.toString().contains("Could not create the persist directory"), errBuf.toString());
|
||||
assertFalse(outBuf.toString().contains("Stopping"), "nothing was stopped: " + outBuf);
|
||||
}
|
||||
|
||||
/**
|
||||
* One snapshot: restart applies the profile the parse read, not a second read of the file, so
|
||||
* an edit or delete in between cannot mix two versions into one restart.
|
||||
@@ -214,8 +248,20 @@ class RestartCommandTest {
|
||||
RestartCommand restart = new RestartCommand(ProductProfile.AWS, docker) {
|
||||
@Override
|
||||
public StartCommand buildStartCommand() throws DockerException {
|
||||
super.buildStartCommand(); // the real one first, so its docker failure still surfaces
|
||||
// The real one first, so its docker failure still surfaces; it also answers the
|
||||
// checks restart runs before the stop, as only it carries the resolved settings.
|
||||
StartCommand real = super.buildStartCommand();
|
||||
return new StartCommand(ProductProfile.AWS) {
|
||||
@Override
|
||||
public String validationError() {
|
||||
return real.validationError();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String preparePersistDir() {
|
||||
return real.preparePersistDir();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
ran.add("start");
|
||||
|
||||
@@ -3,8 +3,13 @@ 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 org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -13,6 +18,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.*;
|
||||
|
||||
@@ -191,6 +197,152 @@ class StartCommandArgsTest {
|
||||
.dockerRunArgs(SOCKET).contains("FLOCI_BASE_URL=http://floci.internal:14566"));
|
||||
}
|
||||
|
||||
/** BL-034: Docker reads "./data:/app/data" as a named volume, so the path goes in absolute. */
|
||||
@Test
|
||||
void aRelativePersistDirIsMountedAsAnAbsoluteBindPath() {
|
||||
String expected = Path.of("data").toAbsolutePath().normalize() + ":/app/data";
|
||||
|
||||
List<String> args = parse("start", "--persist", "./data").dockerRunArgs(SOCKET);
|
||||
|
||||
assertTrue(args.contains(expected), args.toString());
|
||||
assertFalse(args.contains("./data:/app/data"), args.toString());
|
||||
}
|
||||
|
||||
/** BL-035: an out-of-range port or an unknown pull policy is refused before docker runs. */
|
||||
@Test
|
||||
void portAndPullPolicyAreValidated() {
|
||||
assertNull(parse("start").validationError());
|
||||
assertNull(parse("start", "--pull", "ALWAYS").validationError());
|
||||
assertTrue(parse("start", "--port", "0").validationError().contains("--port must be between 1 and 65535"));
|
||||
assertTrue(parse("start", "--port", "70000").validationError().contains("--port must be between 1 and 65535"));
|
||||
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("Invalid persist directory"), error);
|
||||
assertTrue(error.contains("Pass a --persist path"), error);
|
||||
}
|
||||
|
||||
/**
|
||||
* A remote daemon's path is its own: an absolute one is passed on as given, never resolved by
|
||||
* this machine's rules (a Windows CLI would turn /srv/floci-data into a drive path).
|
||||
*/
|
||||
@Test
|
||||
void aRemoteDaemonGetsThePersistPathExactlyAsGiven() {
|
||||
StartCommand start = parse("start", "--persist", "/srv/floci-data/../state");
|
||||
|
||||
assertNull(start.preparePersistDir(REMOTE));
|
||||
assertEquals("/srv/floci-data/../state", start.bindSource(REMOTE));
|
||||
assertTrue(start.dockerRunArgs(SOCKET, REMOTE).contains("/srv/floci-data/../state:/app/data"));
|
||||
}
|
||||
|
||||
/** Only a local daemon has relative paths resolved against this machine's working directory. */
|
||||
@Test
|
||||
void onlyALocalDaemonHasItsPersistPathResolved() {
|
||||
StartCommand start = parse("start", "--persist", "./data");
|
||||
String resolved = Path.of("data").toAbsolutePath().normalize().toString();
|
||||
|
||||
assertEquals(resolved, start.bindSource(LOCAL));
|
||||
assertTrue(start.dockerRunArgs(SOCKET, LOCAL).contains(resolved + ":/app/data"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A remote daemon has no working directory of ours to resolve against, and docker reads a bare
|
||||
* name as a named volume: a relative path is refused before anything is started or stopped.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"data", "./data", "../state", "C:data"})
|
||||
void aRemoteDaemonRefusesARelativePersistPath(String relative) {
|
||||
String error = parse("start", "--persist", relative).preparePersistDir(REMOTE);
|
||||
|
||||
assertNotNull(error);
|
||||
assertTrue(error.contains("must be an absolute path on the daemon's machine"), error);
|
||||
assertTrue(error.contains("'" + relative + "'"), error);
|
||||
}
|
||||
|
||||
/** Absolute for the daemon's system, whichever it is; this machine's path rules do not decide. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/srv/floci-data", "C:\\floci\\data", "c:/floci/data", "\\\\nas\\share\\floci"})
|
||||
void aRemoteDaemonAcceptsAnAbsolutePathOfEitherSystem(String absolute) {
|
||||
StartCommand start = parse("start", "--persist", absolute);
|
||||
|
||||
assertNull(start.preparePersistDir(REMOTE));
|
||||
assertEquals(absolute, start.bindSource(REMOTE));
|
||||
}
|
||||
|
||||
/**
|
||||
* What preparePersistDir accepts, dockerRunArgs can build. Restart runs the check before the
|
||||
* stop, so a path this machine cannot even parse must not throw afterwards for a remote daemon.
|
||||
*/
|
||||
@Test
|
||||
void aPathThisMachineCannotParseStillBuildsForARemoteDaemon() {
|
||||
StartCommand start = parse("start", "--persist", "/srv/data\u0000dir");
|
||||
|
||||
assertNull(start.preparePersistDir(REMOTE));
|
||||
assertDoesNotThrow(() -> start.dockerRunArgs(SOCKET, REMOTE));
|
||||
assertNotNull(start.preparePersistDir(LOCAL));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
|
||||
Reference in new issue
Block a user