mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
* fix(http): keep snapshot names in one path segment and report odd server responses clearly Snapshot names are URL-encoded and names that would change the path are refused; every trailing slash is stripped from the endpoint; a non-JSON body or a redirect gets a message that says so instead of a parser error; requests use HTTP/1.1. snapshot delete reports a missing snapshot API like save and load, save warns that --message is not sent yet, and export/import no longer cite a server version that does not exist. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * fix(http): tell a missing snapshot API from its status, not from the error text The error message carries the request path, so a snapshot named nightly404 turned a real server error into 'not available'. FlociException now carries the HTTP status and the snapshot commands ask that. Signed-off-by: Hector Ventura <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
107 lines
3.4 KiB
Java
107 lines
3.4 KiB
Java
package io.floci.cli.unit;
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
import io.floci.cli.FlociCli;
|
|
import io.floci.cli.config.ProfileStore;
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
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.CsvSource;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.PrintStream;
|
|
import java.net.InetSocketAddress;
|
|
import java.nio.file.Path;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
/** BL-048: the snapshot commands against a server that has no snapshot API (404). */
|
|
class SnapshotCommandsTest {
|
|
|
|
@TempDir
|
|
Path tempDir;
|
|
|
|
private HttpServer server;
|
|
private volatile int status = 404;
|
|
|
|
@BeforeEach
|
|
void start() throws Exception {
|
|
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
server.createContext("/", exchange -> {
|
|
exchange.sendResponseHeaders(status, -1);
|
|
exchange.close();
|
|
});
|
|
server.start();
|
|
}
|
|
|
|
@AfterEach
|
|
void stop() {
|
|
server.stop(0);
|
|
}
|
|
|
|
private String[] run(String... args) {
|
|
String[] all = new String[args.length + 4];
|
|
System.arraycopy(args, 0, all, 0, args.length);
|
|
all[args.length] = "--endpoint";
|
|
all[args.length + 1] = "http://127.0.0.1:" + server.getAddress().getPort();
|
|
all[args.length + 2] = "--container";
|
|
all[args.length + 3] = "floci-snapshot-test-none";
|
|
PrintStream out = System.out;
|
|
PrintStream err = System.err;
|
|
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
|
System.setOut(new PrintStream(outBuf));
|
|
System.setErr(new PrintStream(errBuf));
|
|
try {
|
|
int exit = FlociCli.buildCommandLine(new ProfileStore(tempDir)).execute(all);
|
|
return new String[]{String.valueOf(exit), outBuf.toString(), errBuf.toString()};
|
|
} finally {
|
|
System.setOut(out);
|
|
System.setErr(err);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void deleteSaysTheApiIsMissingLikeSaveAndLoad() {
|
|
String[] r = run("snapshot", "delete", "nightly");
|
|
|
|
assertEquals("1", r[0]);
|
|
assertTrue(r[2].contains("Snapshot API not available"), r[2]);
|
|
}
|
|
|
|
@Test
|
|
void aMessageIsNotDroppedSilently() {
|
|
String[] r = run("snapshot", "save", "nightly", "-m", "before upgrade");
|
|
|
|
assertTrue(r[2].contains("--message is not sent"), r[2]);
|
|
}
|
|
|
|
/**
|
|
* Only the status says the API is missing. A name that happens to contain "404" or "501" must
|
|
* not turn a real server error into "not available".
|
|
*/
|
|
@ParameterizedTest
|
|
@CsvSource({"delete, nightly404", "save, build-501", "load, 404"})
|
|
void aStatusLookingNameDoesNotHideARealError(String command, String name) {
|
|
status = 500;
|
|
|
|
String[] r = run("snapshot", command, name);
|
|
|
|
assertEquals("1", r[0]);
|
|
assertFalse(r[2].contains("Snapshot API not available"), r[2]);
|
|
assertTrue(r[2].contains("HTTP 500"), r[2]);
|
|
}
|
|
|
|
@ParameterizedTest
|
|
@CsvSource({"404", "501"})
|
|
void bothMissingEndpointStatusesReadAsNotAvailable(int missing) {
|
|
status = missing;
|
|
|
|
String[] r = run("snapshot", "delete", "nightly");
|
|
|
|
assertTrue(r[2].contains("Snapshot API not available"), r[2]);
|
|
}
|
|
}
|