mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(profile): apply --profile to every command, not just config show (#23)
`--profile <name>` was declared on the shared GlobalOptions mixin but only
ConfigShowCommand ever read it. Every other command, `start` included, fell
through to the built-in defaults, so a profile's container, image, port,
persistDir and services were silently dropped.
The worst consequence was silent data loss: the `-v <dir>:/app/data` mount and
`FLOCI_STORAGE_MODE=persistent` were gated on the `--persist` flag alone, so a
profile with `persistDir` set produced neither. The image declares
`VOLUME /app/data`, so every start attached a fresh anonymous volume and the
server stayed in-memory.
A profile is now applied by a single picocli IDefaultValueProvider registered in
FlociCli.buildCommandLine(). Picocli consults it only for options the user did
not pass, and only after that command's arguments are processed, which gives the
documented precedence without per-command plumbing:
command-line flag > --profile <name> > FLOCI_* env var > product default
Behaviour changes:
- An unknown, unreadable or invalid profile is now an error (exit 2) rather than
a silent fall back to the defaults. `--help` still works with a bad profile.
- `restart --profile <name>` re-applies the profile's persistDir, image, port
and services. It builds its StartCommand programmatically, so it carries the
profile across by hand.
- `config show` merges instead of replacing, so an explicit --endpoint /
--container / -o is no longer discarded when --profile resolves. It now also
reports the profile's start-only fields.
- `config profile create` seeds the file from the tree it was run under;
previously every tree wrote AWS values.
- Profile names are validated, closing a path traversal
(`config profile delete ../../foo` resolved the raw name).
- Reads accept the `.yml` spelling that `config profile list` has always
listed, and `list` backfills a missing `name:` from the file name.
- The readiness poll after `start` keeps the endpoint's host when swapping in
the bound port, instead of always rewriting it to localhost.
Adds 64 tests, including the first coverage of ProfileStore.
Closes #22
This commit is contained in:
1 parent
588edc235a
commit
52c08ecec5
19 files changed
+1193
-62
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.config.ConfigProfileCommand;
|
||||
import io.floci.cli.commands.config.ConfigShowCommand;
|
||||
import io.floci.cli.config.Profile;
|
||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** 'config show' must merge with the profile rather than replace the command line with it. */
|
||||
class ConfigCommandsTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private ProfileStore store() {
|
||||
return new ProfileStore(tempDir);
|
||||
}
|
||||
|
||||
private void writeProfile(String name, String yaml) throws IOException {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve(name + ".yaml"), yaml);
|
||||
}
|
||||
|
||||
private String runShow(ProductProfile product, String... args) {
|
||||
CommandLine cmd = new CommandLine(new ConfigShowCommand(product, store()))
|
||||
.setCaseInsensitiveEnumValuesAllowed(true)
|
||||
.setDefaultValueProvider(new ProfileDefaultValueProvider(store()));
|
||||
PrintStream original = System.out;
|
||||
ByteArrayOutputStream captured = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(captured));
|
||||
try {
|
||||
assertEquals(0, cmd.execute(args));
|
||||
} finally {
|
||||
System.setOut(original);
|
||||
}
|
||||
return captured.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
void showsTheProfilesValues() throws Exception {
|
||||
writeProfile("probe", """
|
||||
container: floci-probe
|
||||
endpoint: http://localhost:4566
|
||||
image: floci/floci:enforced
|
||||
port: 4599
|
||||
persistDir: /tmp/floci-persist-test
|
||||
""");
|
||||
|
||||
String out = runShow(ProductProfile.AWS, "--profile", "probe", "-o", "json");
|
||||
assertTrue(out.contains("\"container\" : \"floci-probe\""), out);
|
||||
assertTrue(out.contains("\"image\" : \"floci/floci:enforced\""), out);
|
||||
assertTrue(out.contains("\"persistDir\" : \"/tmp/floci-persist-test\""), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitFlagIsNoLongerDiscardedByTheProfile() throws Exception {
|
||||
writeProfile("probe", """
|
||||
container: floci-probe
|
||||
endpoint: http://localhost:4566
|
||||
""");
|
||||
|
||||
String out = runShow(ProductProfile.AWS, "--profile", "probe",
|
||||
"--endpoint", "http://explicit:1234", "-o", "json");
|
||||
|
||||
assertTrue(out.contains("\"endpoint\" : \"http://explicit:1234\""), out);
|
||||
assertTrue(out.contains("\"container\" : \"floci-probe\""), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAProfileItReportsTheProductDefaults() {
|
||||
String out = runShow(ProductProfile.GCP, "-o", "json");
|
||||
assertTrue(out.contains("\"profile\" : \"default\""), out);
|
||||
assertTrue(out.contains("\"container\" : \"floci-gcp\""), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWritesTheDefaultsOfTheTreeItWasRunUnder() throws Exception {
|
||||
assertEquals(0, new CommandLine(new ConfigProfileCommand(ProductProfile.GCP, store()))
|
||||
.execute("create", "gcp-profile"));
|
||||
|
||||
Profile created = store().get("gcp-profile").orElseThrow();
|
||||
assertEquals("floci-gcp", created.container);
|
||||
assertEquals("http://localhost:4588", created.endpoint);
|
||||
assertEquals("floci/floci-gcp:latest", created.image);
|
||||
assertEquals(4588, created.port);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.config.ProfileNotFoundException;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* A profile that does not resolve must stop the command rather than silently fall back to the
|
||||
* built-in defaults — silently falling back is what made issue #22 lose data unnoticed.
|
||||
*/
|
||||
class MissingProfileTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private CommandLine cli() {
|
||||
return FlociCli.buildCommandLine(new ProfileStore(tempDir));
|
||||
}
|
||||
|
||||
private void writeProfile(String name, String yaml) throws IOException {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve(name + ".yaml"), yaml);
|
||||
}
|
||||
|
||||
private static String captureStderr(Supplier<Integer> action, int[] exitCode) {
|
||||
PrintStream original = System.err;
|
||||
ByteArrayOutputStream captured = new ByteArrayOutputStream();
|
||||
System.setErr(new PrintStream(captured));
|
||||
try {
|
||||
exitCode[0] = action.get();
|
||||
} finally {
|
||||
System.setErr(original);
|
||||
}
|
||||
return captured.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownProfileExitsTwoWithAMessageThatNamesTheNextStep() {
|
||||
int[] exit = new int[1];
|
||||
String stderr = captureStderr(() -> cli().execute("start", "--profile", "typo"), exit);
|
||||
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(stderr.contains("Profile 'typo' not found"), stderr);
|
||||
assertTrue(stderr.contains(tempDir.toString()), stderr);
|
||||
assertTrue(stderr.contains("floci config profile list"), stderr);
|
||||
// The message stands alone; picocli's usage dump would bury it.
|
||||
assertFalse(stderr.contains("Usage:"), stderr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsEvenWhenEveryProfileBackedOptionWasPassedExplicitly() {
|
||||
int[] exit = new int[1];
|
||||
captureStderr(() -> cli().execute("start", "--profile", "typo",
|
||||
"--endpoint", "http://x:1", "--container", "c", "--image", "i",
|
||||
"--port", "1", "--persist", "/p", "--services", "s", "-o", "json"), exit);
|
||||
|
||||
assertEquals(2, exit[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void helpStillWorksWithABadProfile() {
|
||||
int[] exit = new int[1];
|
||||
captureStderr(() -> cli().execute("start", "--profile", "typo", "--help"), exit);
|
||||
|
||||
assertEquals(0, exit[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void traversalInTheProfileNameIsRejected() {
|
||||
int[] exit = new int[1];
|
||||
String stderr = captureStderr(() -> cli().execute("start", "--profile", "../../escape"), exit);
|
||||
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(stderr.contains("Invalid profile name"), stderr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unusableProfileValuesNameTheFileRatherThanAnOptionTheUserNeverTyped() throws Exception {
|
||||
writeProfile("bad-output", "output: bogus\n");
|
||||
writeProfile("bad-port", "port: 99999\n");
|
||||
|
||||
int[] exit = new int[1];
|
||||
String outputErr = captureStderr(() -> cli().execute("start", "--profile", "bad-output"), exit);
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(outputErr.contains("bad-output.yaml"), outputErr);
|
||||
assertTrue(outputErr.contains("output must be text, json or yaml"), outputErr);
|
||||
|
||||
String portErr = captureStderr(() -> cli().execute("start", "--profile", "bad-port"), exit);
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(portErr.contains("bad-port.yaml"), portErr);
|
||||
assertTrue(portErr.contains("port must be between 1 and 65535"), portErr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsingRaisesTheDedicatedException() {
|
||||
assertThrows(ProfileNotFoundException.class,
|
||||
() -> cli().parseArgs("start", "--profile", "typo"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.config.Profile;
|
||||
import io.floci.cli.config.ProfileDefaults;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Pins which CLI option each Profile field feeds, and which options it must leave alone. */
|
||||
class ProfileDefaultsTest {
|
||||
|
||||
private static Profile fullProfile() {
|
||||
Profile p = new Profile();
|
||||
p.name = "probe";
|
||||
p.endpoint = "http://localhost:4566";
|
||||
p.container = "floci-probe";
|
||||
p.image = "floci/floci:enforced";
|
||||
p.port = 4599;
|
||||
p.persistDir = "/tmp/floci-persist-test";
|
||||
p.services = "s3,lambda";
|
||||
p.output = "json";
|
||||
return p;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"--endpoint, http://localhost:4566",
|
||||
"--container, floci-probe",
|
||||
"--image, floci/floci:enforced",
|
||||
"--port, 4599",
|
||||
"--persist, /tmp/floci-persist-test",
|
||||
"--services, 's3,lambda'",
|
||||
"--output, json",
|
||||
})
|
||||
void mapsEveryProfileField(String option, String expected) {
|
||||
assertEquals(expected, ProfileDefaults.valueFor(fullProfile(), option), option);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"--service", // wait/logs/env filter — NOT the profile's 'services'
|
||||
"--profile-name", // the OCI CLI profile written by 'floci oci setup'
|
||||
"--profile",
|
||||
"--pull",
|
||||
"--timeout",
|
||||
"--file",
|
||||
})
|
||||
void leavesUnrelatedOptionsAlone(String option) {
|
||||
assertNull(ProfileDefaults.valueFor(fullProfile(), option), option);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsetFieldsAreSilentSoPicocliKeepsItsOwnDefault() {
|
||||
Profile empty = new Profile();
|
||||
for (String option : new String[]{"--endpoint", "--container", "--image", "--port", "--persist", "--services", "--output"}) {
|
||||
assertNull(ProfileDefaults.valueFor(empty, option), option);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullInputsAreSilent() {
|
||||
assertNull(ProfileDefaults.valueFor(null, "--endpoint"));
|
||||
assertNull(ProfileDefaults.valueFor(fullProfile(), null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.Model.CommandSpec;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Pins issue #22: --profile must reach every command, and the command line must still win.
|
||||
* Precedence is CLI flag > profile > environment variable > product default.
|
||||
*/
|
||||
class ProfilePrecedenceTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private static final String FULL_PROFILE = """
|
||||
name: probe
|
||||
endpoint: http://localhost:4566
|
||||
container: floci-probe
|
||||
image: floci/floci:enforced
|
||||
port: 4599
|
||||
persistDir: /tmp/floci-persist-test
|
||||
services: s3,lambda
|
||||
output: json
|
||||
""";
|
||||
|
||||
private void writeProfile(String name, String yaml) throws IOException {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve(name + ".yaml"), yaml);
|
||||
}
|
||||
|
||||
private CommandSpec leafSpec(String... args) {
|
||||
ParseResult r = FlociCli.buildCommandLine(new ProfileStore(tempDir)).parseArgs(args);
|
||||
while (r.hasSubcommand()) r = r.subcommand();
|
||||
return r.commandSpec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void profileSuppliesEveryStartOption() throws Exception {
|
||||
writeProfile("probe", FULL_PROFILE);
|
||||
CommandSpec spec = leafSpec("start", "--profile", "probe");
|
||||
|
||||
assertEquals("http://localhost:4566", spec.findOption("--endpoint").getValue());
|
||||
assertEquals("floci-probe", spec.findOption("--container").getValue());
|
||||
assertEquals("floci/floci:enforced", spec.findOption("--image").getValue());
|
||||
assertEquals(Integer.valueOf(4599), spec.findOption("--port").getValue());
|
||||
assertEquals("/tmp/floci-persist-test", spec.findOption("--persist").getValue());
|
||||
assertEquals("s3,lambda", spec.findOption("--services").getValue());
|
||||
assertEquals("json", spec.findOption("--output").getValue().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void commandLineFlagsBeatTheProfile() throws Exception {
|
||||
writeProfile("probe", FULL_PROFILE);
|
||||
CommandSpec spec = leafSpec("start", "--profile", "probe",
|
||||
"--container", "explicit", "--image", "explicit:1", "--port", "1234",
|
||||
"--persist", "/explicit", "--services", "sqs", "--endpoint", "http://explicit:1", "-o", "yaml");
|
||||
|
||||
assertEquals("http://explicit:1", spec.findOption("--endpoint").getValue());
|
||||
assertEquals("explicit", spec.findOption("--container").getValue());
|
||||
assertEquals("explicit:1", spec.findOption("--image").getValue());
|
||||
assertEquals(Integer.valueOf(1234), spec.findOption("--port").getValue());
|
||||
assertEquals("/explicit", spec.findOption("--persist").getValue());
|
||||
assertEquals("sqs", spec.findOption("--services").getValue());
|
||||
assertEquals("yaml", spec.findOption("--output").getValue().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldsTheProfileOmitsKeepTheProductDefault() throws Exception {
|
||||
writeProfile("partial", "container: floci-partial\n");
|
||||
CommandSpec spec = leafSpec("start", "--profile", "partial");
|
||||
|
||||
assertEquals("floci-partial", spec.findOption("--container").getValue());
|
||||
assertEquals("floci/floci:latest", spec.findOption("--image").getValue());
|
||||
assertEquals(Integer.valueOf(4566), spec.findOption("--port").getValue());
|
||||
assertNull(spec.findOption("--persist").getValue());
|
||||
assertEquals("text", spec.findOption("--output").getValue().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyProductTreeAppliesTheProfileAndKeepsItsOwnDefaults() throws Exception {
|
||||
writeProfile("partial", "container: floci-partial\n");
|
||||
String[][] trees = {
|
||||
{"aws", "4566", "floci/floci:latest"},
|
||||
{"gcp", "4588", "floci/floci-gcp:latest"},
|
||||
{"az", "4577", "floci/floci-az:latest"},
|
||||
{"oci", "4599", "floci/floci-oci:latest"},
|
||||
};
|
||||
for (String[] tree : trees) {
|
||||
CommandSpec spec = leafSpec(tree[0], "start", "--profile", "partial");
|
||||
assertEquals("floci-partial", spec.findOption("--container").getValue(), tree[0]);
|
||||
assertEquals(Integer.valueOf(tree[1]), spec.findOption("--port").getValue(), tree[0]);
|
||||
assertEquals(tree[2], spec.findOption("--image").getValue(), tree[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void profileAppliesToCommandsOtherThanStart() throws Exception {
|
||||
writeProfile("probe", FULL_PROFILE);
|
||||
// 'env' is one of the commands that does not pre-initialize its GlobalOptions mixin.
|
||||
for (String command : new String[]{"status", "logs", "wait", "doctor", "env", "services"}) {
|
||||
CommandSpec spec = leafSpec(command, "--profile", "probe");
|
||||
assertEquals("floci-probe", spec.findOption("--container").getValue(), command);
|
||||
assertEquals("http://localhost:4566", spec.findOption("--endpoint").getValue(), command);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void singularServiceFilterIsNotTheProfilesServicesList() throws Exception {
|
||||
writeProfile("probe", FULL_PROFILE);
|
||||
assertNull(leafSpec("wait", "--profile", "probe").findOption("--service").getValue());
|
||||
assertNull(leafSpec("logs", "--profile", "probe").findOption("--service").getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAProfileNothingChanges() {
|
||||
CommandSpec spec = leafSpec("start");
|
||||
assertEquals("floci", spec.findOption("--container").getValue());
|
||||
assertEquals("floci/floci:latest", spec.findOption("--image").getValue());
|
||||
assertEquals(Integer.valueOf(4566), spec.findOption("--port").getValue());
|
||||
assertNull(spec.findOption("--persist").getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void commandsWithoutGlobalOptionsAreUntouched() {
|
||||
// 'update' has no GlobalOptions mixin at all, so the provider must bail out rather
|
||||
// than dereference a missing --profile option.
|
||||
assertEquals(Boolean.TRUE,
|
||||
leafSpec("update", "--check").findOption("--check").getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void picocliInterpolatesProfileValues() throws Exception {
|
||||
// Documented consequence of routing values through picocli's default-value machinery.
|
||||
writeProfile("interp", "persistDir: ${env:HOME}/floci-data\n");
|
||||
assertEquals(System.getenv("HOME") + "/floci-data",
|
||||
leafSpec("start", "--profile", "interp").findOption("--persist").getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.config.Profile;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
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.ValueSource;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** First coverage of the profile persistence layer, including the path-traversal guard. */
|
||||
class ProfileStoreTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private ProfileStore store() {
|
||||
return new ProfileStore(tempDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTripsEveryField() throws Exception {
|
||||
Profile saved = new Profile();
|
||||
saved.name = "probe";
|
||||
saved.endpoint = "http://localhost:4566";
|
||||
saved.container = "floci-probe";
|
||||
saved.image = "floci/floci:enforced";
|
||||
saved.port = 4599;
|
||||
saved.persistDir = "/tmp/floci-persist-test";
|
||||
saved.services = "s3,lambda";
|
||||
saved.output = "json";
|
||||
store().save(saved);
|
||||
|
||||
Profile loaded = store().get("probe").orElseThrow();
|
||||
assertEquals("probe", loaded.name);
|
||||
assertEquals("http://localhost:4566", loaded.endpoint);
|
||||
assertEquals("floci-probe", loaded.container);
|
||||
assertEquals("floci/floci:enforced", loaded.image);
|
||||
assertEquals(4599, loaded.port);
|
||||
assertEquals("/tmp/floci-persist-test", loaded.persistDir);
|
||||
assertEquals("s3,lambda", loaded.services);
|
||||
assertEquals("json", loaded.output);
|
||||
}
|
||||
|
||||
@Test
|
||||
void newProfileTakesItsProductsDefaults() throws Exception {
|
||||
store().save(new Profile(ProductProfile.GCP, "g"));
|
||||
|
||||
Profile loaded = store().get("g").orElseThrow();
|
||||
assertEquals("http://localhost:4588", loaded.endpoint);
|
||||
assertEquals("floci-gcp", loaded.container);
|
||||
assertEquals("floci/floci-gcp:latest", loaded.image);
|
||||
assertEquals(4588, loaded.port);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingProfileIsEmptyAndDeleteReportsIt() throws Exception {
|
||||
assertTrue(store().get("absent").isEmpty());
|
||||
assertFalse(store().delete("absent"));
|
||||
|
||||
store().save(new Profile(ProductProfile.AWS, "here"));
|
||||
assertTrue(store().delete("here"));
|
||||
assertFalse(store().delete("here"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsTheYmlSpellingThatListHasAlwaysAccepted() throws Exception {
|
||||
Files.writeString(tempDir.resolve("staging.yml"), "container: floci-staging\n");
|
||||
|
||||
assertEquals("floci-staging", store().get("staging").orElseThrow().container);
|
||||
assertTrue(store().delete("staging"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBackfillsTheNameFromTheFileName() throws Exception {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve("handwritten.yaml"), "container: floci-hand\n");
|
||||
|
||||
List<Profile> profiles = store().list();
|
||||
assertEquals(1, profiles.size());
|
||||
assertEquals("handwritten", profiles.get(0).name);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"ok", "ok-1", "ok_1", "ok.1", "OK"})
|
||||
void acceptsOrdinaryNames(String name) {
|
||||
assertEquals(name, ProfileStore.validateName(name));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", " ", ".", "..", "../x", "../../etc/passwd", "a/b", "a\\b", "/abs", "a:b"})
|
||||
void rejectsNamesThatCouldEscapeTheProfilesDirectory(String name) {
|
||||
assertThrows(IllegalArgumentException.class, () -> ProfileStore.validateName(name), name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsNullName() {
|
||||
assertThrows(IllegalArgumentException.class, () -> ProfileStore.validateName(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void traversalNeverEscapesTheProfilesDirectory() {
|
||||
assertThrows(IllegalArgumentException.class, () -> store().profileFile("../../escaped"));
|
||||
assertThrows(IllegalArgumentException.class, () -> store().delete("../../escaped"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.RestartCommand;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Restart builds its StartCommand programmatically, so picocli never applies the profile to it.
|
||||
* Without the hand-off in buildStartCommand(), 'restart --profile x' would drop the persistence
|
||||
* directory that 'start --profile x' honours.
|
||||
*/
|
||||
class RestartCommandTest {
|
||||
|
||||
private static final List<String> SOCKET = List.of("-v", "/sock:/sock");
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private RestartCommand parse(ProductProfile product, String... args) {
|
||||
RestartCommand restart = new RestartCommand(product, new ProfileStore(tempDir));
|
||||
new CommandLine(restart).parseArgs(args);
|
||||
return restart;
|
||||
}
|
||||
|
||||
private void writeProfile(String name, String yaml) throws IOException {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve(name + ".yaml"), yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
void carriesTheProfileIntoTheStartItRuns() throws Exception {
|
||||
writeProfile("probe", """
|
||||
image: floci/floci:enforced
|
||||
port: 4599
|
||||
persistDir: /tmp/floci-persist-test
|
||||
services: s3,lambda
|
||||
""");
|
||||
|
||||
List<String> args = parse(ProductProfile.AWS, "--profile", "probe")
|
||||
.buildStartCommand().dockerRunArgs(SOCKET);
|
||||
|
||||
assertEquals(List.of(
|
||||
"-d", "--name", "floci",
|
||||
"-p", "4599:4566",
|
||||
"-v", "/sock:/sock",
|
||||
"-v", "/tmp/floci-persist-test:/app/data",
|
||||
"-e", "FLOCI_STORAGE_MODE=persistent",
|
||||
"-e", "FLOCI_SERVICES=s3,lambda",
|
||||
"floci/floci:enforced"), args);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAProfileItStillUsesTheProductDefaults() {
|
||||
List<String> args = parse(ProductProfile.GCP).buildStartCommand().dockerRunArgs(SOCKET);
|
||||
|
||||
assertEquals(List.of("-d", "--name", "floci-gcp", "-p", "4588:4588",
|
||||
"-v", "/sock:/sock", "floci/floci-gcp:latest"), args);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldsTheProfileOmitsKeepTheProductDefault() throws Exception {
|
||||
writeProfile("partial", "persistDir: /tmp/only-persist\n");
|
||||
|
||||
List<String> args = parse(ProductProfile.OCI, "--profile", "partial")
|
||||
.buildStartCommand().dockerRunArgs(SOCKET);
|
||||
|
||||
assertEquals(List.of("-d", "--name", "floci-oci", "-p", "4599:4599",
|
||||
"-v", "/sock:/sock",
|
||||
"-v", "/tmp/only-persist:/app/data",
|
||||
"-e", "FLOCI_OCI_STORAGE_MODE=persistent",
|
||||
"floci/floci-oci:latest"), args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.commands.StartCommand;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* The issue #22 regression: a profile's persistDir must produce the bind mount and the
|
||||
* persistent storage mode, or every start silently drops state into a fresh anonymous volume.
|
||||
* Built without touching Docker — the host socket arguments are passed in.
|
||||
*/
|
||||
class StartCommandArgsTest {
|
||||
|
||||
private static final List<String> SOCKET = List.of("-v", "/sock:/sock");
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private StartCommand parse(String... args) {
|
||||
ParseResult r = FlociCli.buildCommandLine(new ProfileStore(tempDir)).parseArgs(args);
|
||||
while (r.hasSubcommand()) r = r.subcommand();
|
||||
return (StartCommand) r.commandSpec().userObject();
|
||||
}
|
||||
|
||||
private void writeProfile(String name, String yaml) throws IOException {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve(name + ".yaml"), yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
void profilePersistDirProducesTheBindMountAndPersistentStorageMode() throws Exception {
|
||||
writeProfile("probe", """
|
||||
container: floci-probe
|
||||
image: floci/floci:enforced
|
||||
port: 4599
|
||||
persistDir: /tmp/floci-persist-test
|
||||
services: s3,lambda
|
||||
""");
|
||||
|
||||
assertEquals(List.of(
|
||||
"-d", "--name", "floci-probe",
|
||||
"-p", "4599:4566",
|
||||
"-v", "/sock:/sock",
|
||||
"-v", "/tmp/floci-persist-test:/app/data",
|
||||
"-e", "FLOCI_STORAGE_MODE=persistent",
|
||||
"-e", "FLOCI_SERVICES=s3,lambda",
|
||||
"floci/floci:enforced"),
|
||||
parse("start", "--profile", "probe").dockerRunArgs(SOCKET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void perProductEnvPrefixesComeFromTheProductNotTheProfile() throws Exception {
|
||||
writeProfile("probe", """
|
||||
persistDir: /tmp/floci-persist-test
|
||||
services: storage
|
||||
""");
|
||||
|
||||
assertEquals(List.of(
|
||||
"-d", "--name", "floci-gcp",
|
||||
"-p", "4588:4588",
|
||||
"-v", "/sock:/sock",
|
||||
"-v", "/tmp/floci-persist-test:/app/data",
|
||||
"-e", "FLOCI_GCP_STORAGE_MODE=persistent",
|
||||
"-e", "FLOCI_GCP_SERVICES=storage",
|
||||
"floci/floci-gcp:latest"),
|
||||
parse("gcp", "start", "--profile", "probe").dockerRunArgs(SOCKET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noPersistDirMeansNoMountAndNoStorageMode() {
|
||||
List<String> args = parse("start").dockerRunArgs(SOCKET);
|
||||
|
||||
assertEquals(List.of("-d", "--name", "floci", "-p", "4566:4566",
|
||||
"-v", "/sock:/sock", "floci/floci:latest"), args);
|
||||
assertFalse(args.contains("/app/data"));
|
||||
assertFalse(args.stream().anyMatch(a -> a.contains("STORAGE_MODE")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitPersistFlagStillWinsOverTheProfile() throws Exception {
|
||||
writeProfile("probe", "persistDir: /from-profile\n");
|
||||
|
||||
assertTrue(parse("start", "--profile", "probe", "--persist", "/from-flag")
|
||||
.dockerRunArgs(SOCKET).contains("/from-flag:/app/data"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readinessPollKeepsTheHostFromTheEndpointWhenSwappingThePort() {
|
||||
assertEquals("http://floci.internal:4599", StartCommand.withPort("http://floci.internal:4566", 4599));
|
||||
assertEquals("http://localhost:4599", StartCommand.withPort("http://localhost:4566", 4599));
|
||||
assertEquals("http://localhost:4599", StartCommand.withPort("not a url", 4599));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user