mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
A failed docker run with more than one line of output lost its 'floci doctor' hint, and a profile or Compose file that does not parse was reported with the parser's source-location line instead of what to do. Causes are now shown as one line, and docker's own output always gets its next step. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
211 lines
7.9 KiB
Java
211 lines
7.9 KiB
Java
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]);
|
|
}
|
|
|
|
/** BL-014: help renders defaults through the provider, so a good profile's values show. */
|
|
@Test
|
|
void helpShowsTheProfilesDefaults() throws Exception {
|
|
writeProfile("probe", "port: 4599\nimage: floci/floci:enforced\n");
|
|
|
|
PrintStream original = System.out;
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
System.setOut(new PrintStream(out));
|
|
int exit;
|
|
try {
|
|
exit = cli().execute("start", "--profile", "probe", "--help");
|
|
} finally {
|
|
System.setOut(original);
|
|
}
|
|
|
|
assertEquals(0, exit);
|
|
assertTrue(out.toString().contains("(default: 4599)"), out.toString());
|
|
assertTrue(out.toString().contains("enforced"), out.toString()); // help wraps the image value
|
|
}
|
|
|
|
/** BL-013: a misspelled key is still ignored, but no longer silently. */
|
|
@Test
|
|
void aMisspelledKeyIsReportedOnceAndTheProfileStillApplies() throws Exception {
|
|
writeProfile("typo-key", "container: floci-typo\npersist_dir: /data\n");
|
|
|
|
int[] exit = new int[1];
|
|
String err = captureStderr(() -> {
|
|
cli().parseArgs("start", "--profile", "typo-key");
|
|
return 0;
|
|
}, exit);
|
|
|
|
assertEquals(0, exit[0]);
|
|
assertTrue(err.contains("Profile 'typo-key' sets 'persist_dir'"), err);
|
|
assertTrue(err.contains("persistDir"), err);
|
|
assertEquals(1, err.split("Warning").length - 1, err);
|
|
}
|
|
|
|
/** BL-017: -V takes the same bail-out as --help, so a bad profile cannot break it. */
|
|
@Test
|
|
void versionStillWorksWithABadProfile() {
|
|
int[] exit = new int[1];
|
|
captureStderr(() -> cli().execute("start", "--profile", "typo", "-V"), exit);
|
|
|
|
assertEquals(0, exit[0]);
|
|
}
|
|
|
|
/** BL-017: config show used to warn and exit 0 for a missing profile; it exits 2 like every command. */
|
|
@Test
|
|
void configShowWithAMissingProfileExitsTwo() {
|
|
int[] exit = new int[1];
|
|
String err = captureStderr(() -> cli().execute("config", "show", "--profile", "missing"), exit);
|
|
|
|
assertEquals(2, exit[0]);
|
|
assertTrue(err.contains("Profile 'missing' not found"), err);
|
|
}
|
|
|
|
@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);
|
|
}
|
|
|
|
/** BL-018: one cause, one exit code, whichever command validates the name. */
|
|
@Test
|
|
void anInvalidProfileNameExitsTwoFromConfigProfileAsFromProfile() {
|
|
int[] exit = new int[1];
|
|
String fromProfile = captureStderr(() -> cli().execute("start", "--profile", "../x"), exit);
|
|
assertEquals(2, exit[0], fromProfile);
|
|
|
|
for (String action : new String[]{"create", "show", "delete"}) {
|
|
String err = captureStderr(() -> cli().execute("config", "profile", action, "../x"), exit);
|
|
assertEquals(2, exit[0], action + ": " + err);
|
|
assertTrue(err.contains("Invalid profile name '../x'"), err);
|
|
}
|
|
}
|
|
|
|
/** BL-010: the message must name the file to open, which for a .yml profile is the .yml. */
|
|
@Test
|
|
void anInvalidYmlProfileNamesTheYmlFile() throws Exception {
|
|
Files.createDirectories(tempDir);
|
|
Files.writeString(tempDir.resolve("bad-port.yml"), "port: 99999\n");
|
|
|
|
int[] exit = new int[1];
|
|
String err = captureStderr(() -> cli().execute("start", "--profile", "bad-port"), exit);
|
|
|
|
assertEquals(2, exit[0]);
|
|
assertTrue(err.contains("bad-port.yml"), err);
|
|
assertFalse(err.contains("bad-port.yaml"), err);
|
|
}
|
|
|
|
@Test
|
|
void parsingRaisesTheDedicatedException() {
|
|
assertThrows(ProfileNotFoundException.class,
|
|
() -> cli().parseArgs("start", "--profile", "typo"));
|
|
}
|
|
|
|
/** BL-096: --profile on a file that does not parse says so in two lines: the cause, then what to do. */
|
|
@Test
|
|
void aProfileThatDoesNotParseIsReportedInTwoLines() throws IOException {
|
|
writeProfile("broken", "port: [unterminated\n");
|
|
int[] exit = new int[1];
|
|
|
|
String stderr = captureStderr(() -> cli().execute("status", "--profile", "broken"), exit);
|
|
|
|
assertEquals(2, exit[0]);
|
|
assertEquals(2, stderr.strip().lines().count(), stderr);
|
|
assertTrue(stderr.contains("Profile 'broken' is invalid: "), stderr);
|
|
assertTrue(stderr.strip().endsWith("and try again."), stderr);
|
|
assertFalse(stderr.contains("[Source"), stderr);
|
|
}
|
|
}
|