mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
Merge remote-tracking branch 'origin/main' into feat/cli-ergonomics
config validate keeps the structure-based checks from main and prints its marks through Glyphs. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
16 files changed
+1575
-90
No files matched your search
@@ -0,0 +1,400 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.MappingIterator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.RestartCommand;
|
||||
import io.floci.cli.commands.StartCommand;
|
||||
import io.floci.cli.commands.StatusCommand;
|
||||
import io.floci.cli.commands.StopCommand;
|
||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerClient.DockerHost;
|
||||
import io.floci.cli.docker.DockerClient.Kind;
|
||||
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 picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* BL-056 and BL-057: start, stop and restart print one structured document with -o json/yaml and
|
||||
* nothing else on stdout, and status says through its exit code whether the instance is up.
|
||||
*/
|
||||
class ActionOutputTest {
|
||||
|
||||
// A remote daemon, so start does not probe the local port its own stub server listens on.
|
||||
private static final DockerHost REMOTE = new DockerHost(Kind.TCP, null, "tcp://build-host:2375");
|
||||
// One document and nothing after it: a second value on stdout fails the parse.
|
||||
private static final ObjectMapper JSON = new ObjectMapper().enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
private static final ObjectMapper YAML = new ObjectMapper(new YAMLFactory());
|
||||
|
||||
/** The single YAML document on {@code stdout}; fails when there are several, or none. */
|
||||
private static JsonNode oneYamlDocument(String stdout) throws Exception {
|
||||
List<JsonNode> documents = new ArrayList<>();
|
||||
try (MappingIterator<JsonNode> all = YAML.readerFor(JsonNode.class).readValues(stdout)) {
|
||||
all.forEachRemaining(documents::add);
|
||||
}
|
||||
assertEquals(1, documents.size(), "exactly one YAML document on stdout: " + stdout);
|
||||
assertTrue(documents.get(0).isObject(), stdout);
|
||||
return documents.get(0);
|
||||
}
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private HttpServer emulator;
|
||||
private int port;
|
||||
|
||||
@BeforeEach
|
||||
void startEmulatorStub() throws Exception {
|
||||
emulator = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
emulator.createContext("/", exchange -> {
|
||||
byte[] body = "{\"version\":\"1.6.0\",\"edition\":\"community\",\"services\":[\"s3\"]}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(body);
|
||||
}
|
||||
exchange.close();
|
||||
});
|
||||
emulator.start();
|
||||
port = emulator.getAddress().getPort();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopEmulatorStub() {
|
||||
emulator.stop(0);
|
||||
}
|
||||
|
||||
/** A docker that holds one container (or none) and records what was asked of it. */
|
||||
private static class FakeDocker extends DockerClient {
|
||||
final List<String> calls = new ArrayList<>();
|
||||
String state; // null: no such container
|
||||
String ports = "";
|
||||
|
||||
FakeDocker(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DockerHost daemonHost() {
|
||||
return REMOTE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ContainerInfo> inspectContainer(String name) {
|
||||
return state == null ? Optional.empty()
|
||||
: Optional.of(new ContainerInfo("abc123", name, "floci/floci:latest", state, ports));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<RunSettings> inspectRunSettings(String name, int containerPort) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pull(String image, String policy) {
|
||||
calls.add("pull:stdout");
|
||||
System.out.println("latest: Pulling from floci/floci");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pull(String image, String policy, PrintStream progress) {
|
||||
calls.add("pull:" + (progress == System.err ? "stderr" : "other"));
|
||||
progress.println("latest: Pulling from floci/floci");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String startContainer(List<String> args) {
|
||||
calls.add("run");
|
||||
state = "running";
|
||||
return "0123456789abcdef0123";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopContainer(String name, int timeout) {
|
||||
calls.add("stop");
|
||||
state = "exited";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeContainer(String name) {
|
||||
calls.add("rm");
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
|
||||
private record Run(int exit, String out, String err) {}
|
||||
|
||||
private Run execute(Callable<Integer> command, String... args) {
|
||||
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 = new CommandLine(command)
|
||||
.setDefaultValueProvider(new ProfileDefaultValueProvider(new ProfileStore(tempDir)))
|
||||
.execute(args);
|
||||
return new Run(exit, outBuf.toString(), errBuf.toString());
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
}
|
||||
|
||||
private String[] startArgs(String... more) {
|
||||
List<String> args = new ArrayList<>(List.of("--port", String.valueOf(port), "--container", "floci-actions"));
|
||||
args.addAll(List.of(more));
|
||||
return args.toArray(String[]::new);
|
||||
}
|
||||
|
||||
// ---- start -----------------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void startPrintsOneJsonDocumentAndKeepsProgressOffStdout() throws Exception {
|
||||
FakeDocker docker = new FakeDocker(null);
|
||||
|
||||
Run r = execute(new StartCommand(ProductProfile.AWS, docker), startArgs("-o", "json", "--pull", "always"));
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode json = JSON.readTree(r.out()); // fails if anything but the document is on stdout
|
||||
assertEquals("floci-actions", json.path("container").asText());
|
||||
assertEquals("0123456789abcdef0123", json.path("id").asText());
|
||||
assertEquals("floci/floci:latest", json.path("image").asText());
|
||||
assertEquals("http://localhost:" + port, json.path("endpoint").asText());
|
||||
assertEquals("running", json.path("state").asText());
|
||||
assertTrue(json.path("ready").asBoolean());
|
||||
assertEquals(List.of("pull:stderr", "run"), docker.calls);
|
||||
assertTrue(r.err().contains("Pulling from"), "docker's pull progress goes to stderr: " + r.err());
|
||||
assertFalse(r.out().contains("Starting"), r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDetachedStartReportsNoReadiness() throws Exception {
|
||||
Run r = execute(new StartCommand(ProductProfile.AWS, new FakeDocker(null)), startArgs("-o", "json", "--detach"));
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode json = JSON.readTree(r.out());
|
||||
assertEquals("running", json.path("state").asText());
|
||||
assertFalse(json.has("ready"), "readiness was not waited for: " + r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startInYamlIsOneDocumentToo() throws Exception {
|
||||
Run r = execute(new StartCommand(ProductProfile.AWS, new FakeDocker(null)), startArgs("-o", "yaml", "--detach"));
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode yaml = oneYamlDocument(r.out());
|
||||
assertEquals("floci-actions", yaml.path("container").asText());
|
||||
assertEquals("running", yaml.path("state").asText());
|
||||
assertEquals(5, yaml.size(), "container, id, image, endpoint, state: " + r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anythingExtraOnStdoutFailsTheseTests() {
|
||||
assertThrows(Exception.class, () -> JSON.readTree("{\"a\":1}\n{\"b\":2}\n"));
|
||||
assertThrows(Exception.class, () -> JSON.readTree("{\"a\":1}\nStarting...\n"));
|
||||
assertThrows(AssertionError.class, () -> oneYamlDocument("a: 1\n---\nb: 2\n"));
|
||||
}
|
||||
|
||||
/** A container that died while start waited for it is reported as it is, not as running. */
|
||||
@Test
|
||||
void aContainerThatExitedDuringTheWaitIsNotReportedRunning() throws Exception {
|
||||
emulator.stop(0); // nothing answers the readiness poll
|
||||
FakeDocker docker = new FakeDocker(null) {
|
||||
@Override
|
||||
public String startContainer(List<String> args) {
|
||||
String id = super.startContainer(args);
|
||||
state = "exited"; // it crashed right after docker run returned
|
||||
return id;
|
||||
}
|
||||
};
|
||||
StartCommand start = new StartCommand(ProductProfile.AWS, docker) {
|
||||
@Override
|
||||
protected String readinessTimeout() {
|
||||
return "300ms";
|
||||
}
|
||||
};
|
||||
|
||||
Run r = execute(start, startArgs("-o", "json"));
|
||||
|
||||
assertEquals(1, r.exit());
|
||||
JsonNode json = JSON.readTree(r.out());
|
||||
assertEquals("exited", json.path("state").asText());
|
||||
assertFalse(json.path("ready").asBoolean());
|
||||
}
|
||||
|
||||
/** Text output is what it was: the progress lines, and docker's pull output on stdout. */
|
||||
@Test
|
||||
void startInTextStillNarrates() {
|
||||
FakeDocker docker = new FakeDocker(null);
|
||||
|
||||
Run r = execute(new StartCommand(ProductProfile.AWS, docker), startArgs("--detach", "--pull", "always"));
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
assertTrue(r.out().contains("Container started"), r.out());
|
||||
assertTrue(r.out().contains("Detached."), r.out());
|
||||
assertEquals(List.of("pull:stdout", "run"), docker.calls);
|
||||
assertFalse(r.out().contains("\"container\""), r.out());
|
||||
}
|
||||
|
||||
// ---- stop ------------------------------------------------------------------------------------
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false, exited, stop", "true, removed, stop;rm"})
|
||||
void stopReportsWhatItDid(boolean remove, String state, String calls) throws Exception {
|
||||
FakeDocker docker = new FakeDocker("running");
|
||||
List<String> args = new ArrayList<>(List.of("-o", "json", "--container", "floci-actions"));
|
||||
if (remove) args.add("--remove");
|
||||
|
||||
Run r = execute(new StopCommand(ProductProfile.AWS, docker), args.toArray(String[]::new));
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode json = JSON.readTree(r.out());
|
||||
assertEquals("floci-actions", json.path("container").asText());
|
||||
assertEquals(state, json.path("state").asText());
|
||||
assertTrue(json.path("stopped").asBoolean());
|
||||
assertEquals(remove, json.path("removed").asBoolean());
|
||||
assertEquals(List.of(calls.split(";")), docker.calls);
|
||||
}
|
||||
|
||||
/** A container run with docker's --rm is gone once it stops, whatever --remove said. */
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false", "true"})
|
||||
void aContainerDockerRemovesItselfIsReportedRemoved(boolean remove) throws Exception {
|
||||
FakeDocker docker = new FakeDocker("running") {
|
||||
@Override
|
||||
public void stopContainer(String name, int timeout) {
|
||||
calls.add("stop");
|
||||
state = null; // --rm: docker removed it
|
||||
}
|
||||
};
|
||||
List<String> args = new ArrayList<>(List.of("-o", "json"));
|
||||
if (remove) args.add("--remove");
|
||||
|
||||
Run r = execute(new StopCommand(ProductProfile.AWS, docker), args.toArray(String[]::new));
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode json = JSON.readTree(r.out());
|
||||
assertEquals("removed", json.path("state").asText());
|
||||
assertTrue(json.path("removed").asBoolean());
|
||||
assertEquals(List.of("stop"), docker.calls, "nothing left to remove");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppingAContainerThatIsNotRunningSaysSo() throws Exception {
|
||||
Run r = execute(new StopCommand(ProductProfile.AWS, new FakeDocker("exited")), "-o", "json");
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode json = JSON.readTree(r.out());
|
||||
assertEquals("exited", json.path("state").asText());
|
||||
assertFalse(json.path("stopped").asBoolean());
|
||||
assertFalse(json.path("removed").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopInTextStillNarrates() {
|
||||
Run r = execute(new StopCommand(ProductProfile.AWS, new FakeDocker("running")), "--remove");
|
||||
|
||||
assertTrue(r.out().contains("Stopped."), r.out());
|
||||
assertTrue(r.out().contains("Container removed."), r.out());
|
||||
}
|
||||
|
||||
// ---- restart ---------------------------------------------------------------------------------
|
||||
|
||||
/** A restart is a stop and a start, but its structured output is the one document of the start. */
|
||||
@Test
|
||||
void restartPrintsOneDocument() throws Exception {
|
||||
FakeDocker docker = new FakeDocker("running");
|
||||
docker.ports = port + "->4566/tcp";
|
||||
RestartCommand restart = new RestartCommand(ProductProfile.AWS, docker) {
|
||||
@Override
|
||||
public StartCommand buildStartCommand() {
|
||||
StartCommand start = new StartCommand(ProductProfile.AWS, docker);
|
||||
new CommandLine(start).parseArgs(startArgs("-o", "json"));
|
||||
return start;
|
||||
}
|
||||
};
|
||||
|
||||
Run r = execute(restart, "-o", "json", "--container", "floci-actions");
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
JsonNode json = JSON.readTree(r.out());
|
||||
assertEquals("running", json.path("state").asText());
|
||||
assertTrue(json.path("ready").asBoolean());
|
||||
assertEquals(List.of("stop", "rm", "pull:stderr", "run"), docker.calls);
|
||||
}
|
||||
|
||||
// ---- status ----------------------------------------------------------------------------------
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"running, true, 0",
|
||||
"running, false, 1",
|
||||
"exited, false, 1",
|
||||
"exited, true, 1", // something else answers there; this container is down
|
||||
"not found, true, 0", // a remote endpoint, or a Compose service under another name
|
||||
"not found, false, 1",
|
||||
"'error: Cannot connect to the Docker daemon', true, 0",
|
||||
"'error: Cannot connect to the Docker daemon', false, 1"})
|
||||
void statusExitCodeSaysWhetherTheInstanceIsUp(String state, boolean reachable, int exit) {
|
||||
assertEquals(exit, StatusCommand.exitCode(state, reachable));
|
||||
}
|
||||
|
||||
@Test
|
||||
void statusOfARunningInstanceExitsZero() throws Exception {
|
||||
FakeDocker docker = new FakeDocker("running");
|
||||
docker.ports = port + "->4566/tcp";
|
||||
|
||||
Run json = execute(new StatusCommand(ProductProfile.AWS, docker), "-o", "json");
|
||||
Run text = execute(new StatusCommand(ProductProfile.AWS, docker));
|
||||
|
||||
assertEquals(0, json.exit(), json.err());
|
||||
assertTrue(JSON.readTree(json.out()).path("reachable").asBoolean());
|
||||
assertEquals(0, text.exit());
|
||||
assertTrue(text.out().contains("Reachable: yes"), text.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void statusOfAnInstanceThatIsDownExitsOneInEveryFormat() throws Exception {
|
||||
int closed;
|
||||
try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
|
||||
closed = socket.getLocalPort();
|
||||
}
|
||||
FakeDocker docker = new FakeDocker(null);
|
||||
String endpoint = "http://127.0.0.1:" + closed;
|
||||
|
||||
Run json = execute(new StatusCommand(ProductProfile.AWS, docker), "-o", "json", "--endpoint", endpoint);
|
||||
Run text = execute(new StatusCommand(ProductProfile.AWS, docker), "--endpoint", endpoint);
|
||||
|
||||
assertEquals(1, json.exit());
|
||||
assertFalse(JSON.readTree(json.out()).path("reachable").asBoolean());
|
||||
assertEquals("not found", JSON.readTree(json.out()).path("state").asText());
|
||||
assertEquals(1, text.exit());
|
||||
assertTrue(text.out().contains("floci start"), text.out());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
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 org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** BL-054: completion offers the shells it can generate a script for, and only those. */
|
||||
class CompletionCommandTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private record Run(int exit, String out, String err) {}
|
||||
|
||||
private Run run(String... args) {
|
||||
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(args);
|
||||
return new Run(exit, outBuf.toString(), errBuf.toString());
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHelpListsOnlyTheShellsThatWork() {
|
||||
CommandLine completion = FlociCli.buildCommandLine(new ProfileStore(tempDir)).getSubcommands().get("completion");
|
||||
|
||||
assertEquals("bash|zsh", completion.getCommandSpec().positionalParameters().get(0).paramLabel());
|
||||
String usage = completion.getUsageMessage(CommandLine.Help.Ansi.OFF);
|
||||
assertFalse(usage.contains("fish"), usage);
|
||||
assertFalse(usage.contains("powershell"), usage);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"bash", "zsh", "BASH"})
|
||||
void aSupportedShellGetsAScript(String shell) {
|
||||
Run r = run("completion", shell);
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
assertTrue(r.out().contains("floci"), "a completion script for the floci command");
|
||||
assertEquals("", r.err());
|
||||
}
|
||||
|
||||
/** A shell there is no script for is a usage error, and nothing is written to stdout to be sourced. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"fish", "powershell", "nushell"})
|
||||
void anUnsupportedShellIsAUsageError(String shell) {
|
||||
Run r = run("completion", shell);
|
||||
|
||||
assertEquals(2, r.exit());
|
||||
assertEquals("", r.out());
|
||||
assertTrue(r.err().contains("Shell '" + shell + "' is not supported."), r.err());
|
||||
assertTrue(r.err().contains("Use bash or zsh."), r.err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.config.ComposeCheck;
|
||||
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.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** BL-055: config validate reads the Compose file's structure and checks the emulator's own service. */
|
||||
class ConfigValidateTest {
|
||||
|
||||
private static final ObjectMapper YAML = new ObjectMapper(new YAMLFactory());
|
||||
private static final String NEEDED_FOR = "Lambda, EC2";
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private ComposeCheck.Report aws(String compose) throws Exception {
|
||||
return ComposeCheck.analyze(YAML.readTree(compose), ProductProfile.AWS, true, NEEDED_FOR, tempDir);
|
||||
}
|
||||
|
||||
private static String service(String ports, String extra) {
|
||||
return """
|
||||
services:
|
||||
floci:
|
||||
image: floci/floci:latest
|
||||
ports:
|
||||
- %s
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
%s""".formatted(ports, extra);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCompleteServiceHasNothingToReport() throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", ""));
|
||||
|
||||
assertEquals("floci", r.service());
|
||||
assertEquals(List.of(), r.errors());
|
||||
assertEquals(List.of(), r.warnings());
|
||||
assertTrue(r.valid());
|
||||
}
|
||||
|
||||
/** Any host port works: only the container side has to be the emulator's port. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"4566:4566\"", "\"5566:4566\"", "\"127.0.0.1:5566:4566\"", "\"5566:4566/tcp\"",
|
||||
"4566", "\"4566\"", "\"14510-14600:4510-4600\"", "{target: 4566, published: 5566}",
|
||||
"{target: 4566, published: \"5566\", protocol: tcp}"})
|
||||
void everyWayOfMappingTheEmulatorPortIsAccepted(String ports) throws Exception {
|
||||
assertEquals(List.of(), aws(service(ports, "")).errors(), ports);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"4566:8080\"", "\"8080:8080\"", "\"45660:45660\"", "8080", "{target: 8080, published: 4566}",
|
||||
"\"${HOST_PORT}:${CONTAINER_PORT}\""})
|
||||
void aMappingThatDoesNotReachTheEmulatorPortIsAnError(String ports) throws Exception {
|
||||
List<String> errors = aws(service(ports, "")).errors();
|
||||
|
||||
assertEquals(1, errors.size(), errors.toString());
|
||||
assertTrue(errors.get(0).startsWith("No port mapping for 4566 found"), errors.get(0));
|
||||
}
|
||||
|
||||
/** The emulator's endpoint is HTTP: a UDP mapping of the same number does not reach it. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"4566:4566/udp\"", "{target: 4566, published: 5566, protocol: udp}", "\"5566:4566/sctp\""})
|
||||
void aMappingThatIsNotTcpIsAnError(String ports) throws Exception {
|
||||
List<String> errors = aws(service(ports, "")).errors();
|
||||
|
||||
assertEquals(1, errors.size(), errors.toString());
|
||||
assertTrue(errors.get(0).startsWith("No port mapping for 4566 found"), errors.get(0));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"4566:4566/TCP\"", "{target: 4566, published: 5566, protocol: tcp}", "{target: 4566}"})
|
||||
void tcpIsAcceptedHoweverItIsSpelled(String ports) throws Exception {
|
||||
assertEquals(List.of(), aws(service(ports, "")).errors(), ports);
|
||||
}
|
||||
|
||||
/** What matters is where the socket lands in the container; the host side may be any socket. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/run/user/1000/podman/podman.sock:/var/run/docker.sock", "/var/run/docker.sock:/var/run/docker.sock:ro",
|
||||
"{type: bind, source: /var/run/docker.sock, target: /var/run/docker.sock}"})
|
||||
void aSocketMountedAtTheExpectedPathIsAccepted(String volume) throws Exception {
|
||||
assertEquals(List.of(), aws(service("\"4566:4566\"", "").replace("/var/run/docker.sock:/var/run/docker.sock", volume)).errors());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"./notdocker.sock:/tmp/notdocker.sock", "/var/run/docker.sock:/tmp/docker.sock",
|
||||
"{type: bind, source: /var/run/docker.sock, target: /run/docker.sock}"})
|
||||
void aSocketMountedSomewhereElseDoesNotCount(String volume) throws Exception {
|
||||
List<String> errors = aws(service("\"4566:4566\"", "").replace("/var/run/docker.sock:/var/run/docker.sock", volume)).errors();
|
||||
|
||||
assertEquals(1, errors.size(), errors.toString());
|
||||
assertTrue(errors.get(0).startsWith("Missing /var/run/docker.sock volume mount"), errors.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostNetworkingNeedsNoPortMapping() throws Exception {
|
||||
ComposeCheck.Report r = aws("""
|
||||
services:
|
||||
floci:
|
||||
image: floci/floci:latest
|
||||
network_mode: host
|
||||
volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
|
||||
""");
|
||||
|
||||
assertEquals(List.of(), r.errors());
|
||||
}
|
||||
|
||||
/** Another service's port or socket mount must not satisfy the emulator's checks. */
|
||||
@Test
|
||||
void onlyTheEmulatorsOwnServiceCounts() throws Exception {
|
||||
ComposeCheck.Report r = aws("""
|
||||
services:
|
||||
proxy:
|
||||
image: nginx
|
||||
ports: ["4566:4566"]
|
||||
volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
|
||||
floci:
|
||||
image: ghcr.io/floci-io/floci:1.6.0
|
||||
""");
|
||||
|
||||
assertEquals("floci", r.service());
|
||||
assertEquals(2, r.errors().size(), r.errors().toString());
|
||||
assertTrue(r.errors().get(0).contains("docker.sock"), r.errors().toString());
|
||||
assertTrue(r.errors().get(1).contains("No port mapping for 4566"), r.errors().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSocketIsOnlyAWarningWhereTheProductDoesNotNeedIt() throws Exception {
|
||||
JsonNode compose = YAML.readTree("""
|
||||
services:
|
||||
gcp:
|
||||
image: floci/floci-gcp:latest
|
||||
ports: ["4588:4588"]
|
||||
""");
|
||||
|
||||
ComposeCheck.Report r = ComposeCheck.analyze(compose, ProductProfile.GCP, false, "Cloud Run", tempDir);
|
||||
|
||||
assertTrue(r.valid());
|
||||
assertEquals(1, r.warnings().size());
|
||||
assertTrue(r.warnings().get(0).contains("docker.sock"), r.warnings().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theLongVolumeFormIsRead() throws Exception {
|
||||
ComposeCheck.Report r = aws("""
|
||||
services:
|
||||
floci:
|
||||
image: floci/floci
|
||||
ports: ["4566:4566"]
|
||||
volumes:
|
||||
- type: bind
|
||||
source: /var/run/docker.sock
|
||||
target: /var/run/docker.sock
|
||||
""");
|
||||
|
||||
assertEquals(List.of(), r.errors());
|
||||
}
|
||||
|
||||
// ---- storage mode and the data volume ----------------------------------------------------
|
||||
|
||||
@Test
|
||||
void aDataVolumeWithoutPersistentStorageIsFlagged() throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", " - ./data:/app/data\n"));
|
||||
|
||||
assertTrue(r.valid());
|
||||
assertEquals(1, r.warnings().size(), r.warnings().toString());
|
||||
assertTrue(r.warnings().get(0).contains("FLOCI_STORAGE_MODE is not 'persistent'"), r.warnings().get(0));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"""
|
||||
environment:
|
||||
FLOCI_STORAGE_MODE: persistent
|
||||
""", """
|
||||
environment:
|
||||
- FLOCI_STORAGE_MODE=persistent
|
||||
""", """
|
||||
environment:
|
||||
FLOCI_STORAGE_MODE: ${MODE:-memory}
|
||||
"""})
|
||||
void aDataVolumeWithPersistentStorageIsFine(String environment) throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", " - floci-data:/app/data\n" + environment));
|
||||
|
||||
assertEquals(List.of(), r.warnings(), environment);
|
||||
}
|
||||
|
||||
/** An env_file may set the storage mode; it is not read, so nothing is claimed about it. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {" env_file: .env\n", " env_file:\n - .env\n - .env.local\n"})
|
||||
void anEnvFileMakesTheStorageModeUnknown(String envFile) throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", " - ./state:/app/data\n" + envFile));
|
||||
|
||||
assertEquals(List.of(), r.warnings(), envFile);
|
||||
}
|
||||
|
||||
/** A mode the file itself spells out is still judged, env_file or not. */
|
||||
@Test
|
||||
void anExplicitStorageModeIsJudgedEvenWithAnEnvFile() throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"",
|
||||
" - ./state:/app/data\n env_file: .env\n environment: {FLOCI_STORAGE_MODE: memory}\n"));
|
||||
|
||||
assertEquals(1, r.warnings().size(), r.warnings().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistentStorageWithoutADataVolumeIsFlagged() throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", " environment:\n FLOCI_STORAGE_MODE: persistent\n"));
|
||||
|
||||
assertEquals(1, r.warnings().size(), r.warnings().toString());
|
||||
assertTrue(r.warnings().get(0).contains("nothing is mounted at /app/data"), r.warnings().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theStorageVariableIsTheProductsOwn() throws Exception {
|
||||
JsonNode compose = YAML.readTree("""
|
||||
services:
|
||||
oci:
|
||||
image: floci/floci-oci:latest
|
||||
ports: ["4599:4599"]
|
||||
volumes: ["./state:/app/data", "/var/run/docker.sock:/var/run/docker.sock"]
|
||||
environment:
|
||||
FLOCI_STORAGE_MODE: persistent
|
||||
""");
|
||||
|
||||
ComposeCheck.Report r = ComposeCheck.analyze(compose, ProductProfile.OCI, true, "Functions", tempDir);
|
||||
|
||||
assertTrue(r.warnings().get(0).contains("FLOCI_OCI_STORAGE_MODE is not 'persistent'"), r.warnings().toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {" - ./data:/app/data:ro\n", " - floci-data:/app/data/:ro,z\n",
|
||||
" - {type: bind, source: ./data, target: /app/data, read_only: true}\n"})
|
||||
void aReadOnlyDataVolumeIsAnError(String volume) throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", volume + " environment: {FLOCI_STORAGE_MODE: persistent}\n"));
|
||||
|
||||
assertEquals(1, r.errors().size(), r.errors().toString());
|
||||
assertTrue(r.errors().get(0).contains("mounted read-only"), r.errors().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDataVolumeBoundToAFileIsAnError() throws Exception {
|
||||
Files.writeString(tempDir.resolve("data"), "not a directory");
|
||||
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"",
|
||||
" - ./data:/app/data\n environment: {FLOCI_STORAGE_MODE: persistent}\n"));
|
||||
|
||||
assertEquals(1, r.errors().size(), r.errors().toString());
|
||||
assertTrue(r.errors().get(0).contains("which is a file, not a directory"), r.errors().get(0));
|
||||
}
|
||||
|
||||
/** In the long form the type says the source is a host path, even when it is a bare name. */
|
||||
@Test
|
||||
void aLongFormBindToAFileIsAnErrorEvenWithABareName() throws Exception {
|
||||
Files.writeString(tempDir.resolve("data"), "not a directory");
|
||||
|
||||
ComposeCheck.Report bind = aws(service("\"4566:4566\"",
|
||||
" - {type: bind, source: data, target: /app/data}\n environment: {FLOCI_STORAGE_MODE: persistent}\n"));
|
||||
ComposeCheck.Report named = aws(service("\"4566:4566\"",
|
||||
" - {type: volume, source: data, target: /app/data}\n environment: {FLOCI_STORAGE_MODE: persistent}\n"));
|
||||
ComposeCheck.Report shortNamed = aws(service("\"4566:4566\"",
|
||||
" - data:/app/data\n environment: {FLOCI_STORAGE_MODE: persistent}\n"));
|
||||
|
||||
assertEquals(1, bind.errors().size(), bind.errors().toString());
|
||||
assertTrue(bind.errors().get(0).contains("which is a file, not a directory"), bind.errors().get(0));
|
||||
assertEquals(List.of(), named.errors(), "a named volume called 'data' is not the file");
|
||||
assertEquals(List.of(), shortNamed.errors(), "nor is the short form's bare name");
|
||||
}
|
||||
|
||||
/** A directory, a path that does not exist yet and a named volume say nothing against the mount. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"./state", "./not-there-yet", "floci-data", "${DATA_DIR}"})
|
||||
void otherDataSourcesAreNotJudged(String source) throws Exception {
|
||||
Files.createDirectories(tempDir.resolve("state"));
|
||||
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"",
|
||||
" - " + source + ":/app/data\n environment: {FLOCI_STORAGE_MODE: persistent}\n"));
|
||||
|
||||
assertEquals(List.of(), r.errors(), source);
|
||||
}
|
||||
|
||||
// ---- image ---------------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void anImageOlderThanTheSupportedMinimumIsFlagged() throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", "").replace("floci/floci:latest", "floci/floci:1.4.2"));
|
||||
|
||||
assertTrue(r.valid());
|
||||
assertEquals(1, r.warnings().size(), r.warnings().toString());
|
||||
assertTrue(r.warnings().get(0).contains("Image tag 1.4.2 is older than 1.5.0"), r.warnings().get(0));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"floci/floci", "floci/floci:latest", "floci/floci:1.5.0", "floci/floci:2.0.1",
|
||||
"ghcr.io/floci-io/floci:1.6.0", "docker.io/floci/floci:1.6.0", "floci/floci:1.6.0@sha256:abc", "floci/floci:nightly"})
|
||||
void supportedOrUnversionedImagesAreNotFlagged(String image) throws Exception {
|
||||
ComposeCheck.Report r = aws(service("\"4566:4566\"", "").replace("floci/floci:latest", image));
|
||||
|
||||
assertEquals("floci", r.service(), image);
|
||||
assertEquals(List.of(), r.warnings(), image);
|
||||
}
|
||||
|
||||
@Test
|
||||
void repositoriesAndTagsAreSplitAtTheRightColon() {
|
||||
assertEquals("registry.local:5000/floci/floci", ComposeCheck.repositoryOf("registry.local:5000/floci/floci:1.6.0"));
|
||||
assertEquals("1.6.0", ComposeCheck.tagOf("registry.local:5000/floci/floci:1.6.0"));
|
||||
assertEquals("registry.local:5000/floci/floci", ComposeCheck.repositoryOf("registry.local:5000/floci/floci"));
|
||||
assertEquals("", ComposeCheck.tagOf("registry.local:5000/floci/floci"));
|
||||
}
|
||||
|
||||
/** One service and no recognisable image (built locally, mirrored): it is still the one to check. */
|
||||
@Test
|
||||
void aSingleServiceIsCheckedEvenWithoutARecognisedImage() throws Exception {
|
||||
ComposeCheck.Report r = aws("""
|
||||
services:
|
||||
emulator:
|
||||
build: .
|
||||
""");
|
||||
|
||||
assertEquals("emulator", r.service());
|
||||
assertEquals(2, r.errors().size(), r.errors().toString());
|
||||
assertTrue(r.warnings().get(0).contains("image reference not detected"), r.warnings().toString());
|
||||
}
|
||||
|
||||
/** Several services and none recognisable: checking one at random would only mislead. */
|
||||
@Test
|
||||
void severalServicesWithoutARecognisedImageAreNotGuessedAt() throws Exception {
|
||||
ComposeCheck.Report r = aws("""
|
||||
services:
|
||||
web: {image: nginx}
|
||||
db: {image: postgres}
|
||||
""");
|
||||
|
||||
assertNull(r.service());
|
||||
assertEquals(List.of(), r.errors());
|
||||
assertEquals(1, r.warnings().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFileWithoutServicesIsAnError() throws Exception {
|
||||
ComposeCheck.Report r = aws("version: '3'\n");
|
||||
|
||||
assertFalse(r.valid());
|
||||
assertTrue(r.errors().get(0).startsWith("No services found"), r.errors().toString());
|
||||
}
|
||||
|
||||
// ---- the command ---------------------------------------------------------------------------
|
||||
|
||||
private record Run(int exit, String out, String err) {}
|
||||
|
||||
private Run run(String... args) {
|
||||
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.resolve("profiles"))).execute(args);
|
||||
return new Run(exit, outBuf.toString(), errBuf.toString());
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonOutputCarriesTheFindingsAndTheExitCode() throws Exception {
|
||||
Path file = Files.writeString(tempDir.resolve("compose.yml"), """
|
||||
services:
|
||||
floci:
|
||||
image: floci/floci:1.4.0
|
||||
ports: ["4566:8080"]
|
||||
volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
|
||||
""");
|
||||
|
||||
Run r = run("config", "validate", "-f", file.toString(), "-o", "json");
|
||||
|
||||
assertEquals(1, r.exit());
|
||||
JsonNode json = new ObjectMapper().readTree(r.out());
|
||||
assertEquals(file.toString(), json.path("file").asText());
|
||||
assertEquals("floci", json.path("service").asText());
|
||||
assertFalse(json.path("valid").asBoolean());
|
||||
assertEquals(1, json.path("errors").size());
|
||||
assertEquals(1, json.path("warnings").size());
|
||||
assertFalse(r.out().contains("Validating"), "no text banner in structured output");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aValidFileExitsZeroInEveryTree() throws Exception {
|
||||
Path file = Files.writeString(tempDir.resolve("compose.yml"), """
|
||||
services:
|
||||
gcp:
|
||||
image: floci/floci-gcp:latest
|
||||
ports: ["14588:4588"]
|
||||
volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
|
||||
""");
|
||||
|
||||
Run text = run("gcp", "config", "validate", "-f", file.toString());
|
||||
Run yaml = run("gcp", "config", "validate", "-f", file.toString(), "-o", "yaml");
|
||||
|
||||
assertEquals(0, text.exit(), text.out() + text.err());
|
||||
assertTrue(text.out().contains("Compose file looks good"), text.out());
|
||||
assertEquals(0, yaml.exit());
|
||||
assertTrue(yaml.out().contains("valid: true"), yaml.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFileThatIsNotYamlIsReportedWithANextStep() throws Exception {
|
||||
Path file = Files.writeString(tempDir.resolve("compose.yml"), "services: [unterminated\n");
|
||||
|
||||
Run r = run("config", "validate", "-f", file.toString());
|
||||
|
||||
assertEquals(1, r.exit());
|
||||
assertTrue(r.err().contains("as YAML"), r.err());
|
||||
assertTrue(r.err().contains("pass another with --file"), r.err());
|
||||
}
|
||||
|
||||
/** A script reading the report gets one for a file that cannot be parsed too. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"services: [unterminated\n", "- just\n- a list\n", "plain text\n"})
|
||||
void aFileThatCannotBeReadStillGetsAStructuredReport(String content) throws Exception {
|
||||
Path file = Files.writeString(tempDir.resolve("compose.yml"), content);
|
||||
|
||||
Run r = run("config", "validate", "-f", file.toString(), "-o", "json");
|
||||
|
||||
assertEquals(1, r.exit());
|
||||
JsonNode json = new ObjectMapper().readTree(r.out());
|
||||
assertEquals(file.toString(), json.path("file").asText());
|
||||
assertTrue(json.path("service").isNull(), r.out());
|
||||
assertFalse(json.path("valid").asBoolean());
|
||||
assertEquals(1, json.path("errors").size());
|
||||
assertTrue(json.path("errors").get(0).asText().startsWith("Could not read "), r.out());
|
||||
assertEquals(0, json.path("warnings").size());
|
||||
assertEquals("", r.err());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerException;
|
||||
@@ -271,4 +273,74 @@ class DockerClientProcessTest {
|
||||
assertTrue(errBuf.toString().contains("+ docker rm"), errBuf.toString());
|
||||
assertTrue(errBuf.toString().contains("floci-x"), errBuf.toString());
|
||||
}
|
||||
|
||||
private DockerClient dockerPrinting(String inspectJson) throws Exception {
|
||||
Path json = Files.createTempFile(tempDir, "inspect", ".json");
|
||||
Files.writeString(json, inspectJson);
|
||||
Path fakeDocker = Files.createTempFile(tempDir, "docker", "");
|
||||
Files.writeString(fakeDocker, "#!/bin/sh\ncat '" + json + "'\n");
|
||||
assertTrue(fakeDocker.toFile().setExecutable(true));
|
||||
return new DockerClient(fakeDocker.toString());
|
||||
}
|
||||
|
||||
/** BL-046: container details come from the inspect document, not from a delimiter-split template. */
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void containerDetailsAreReadFromTheInspectDocument() throws Exception {
|
||||
DockerClient docker = dockerPrinting("""
|
||||
{"Id":"0123abcd","Name":"/floci-team-b","Config":{"Image":"floci/floci:1.6.0"},
|
||||
"State":{"Status":"running"},
|
||||
"NetworkSettings":{"Ports":{"4566/tcp":[{"HostIp":"0.0.0.0","HostPort":"14566"},{"HostIp":"::","HostPort":"14566"}],
|
||||
"8080/tcp":null}}}
|
||||
""");
|
||||
|
||||
DockerClient.ContainerInfo info = docker.inspectContainer("floci-team-b").orElseThrow();
|
||||
|
||||
assertEquals("0123abcd", info.id());
|
||||
assertEquals("floci-team-b", info.name());
|
||||
assertEquals("floci/floci:1.6.0", info.image());
|
||||
assertEquals("running", info.state());
|
||||
assertEquals("14566->4566/tcp", info.ports());
|
||||
}
|
||||
|
||||
/** A value with the old delimiter in it cannot shift the fields any more. */
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void aPipeInAValueDoesNotShiftTheFields() throws Exception {
|
||||
DockerClient docker = dockerPrinting("""
|
||||
{"Id":"0123abcd","Name":"/floci","Config":{"Image":"registry.local/team|floci:latest"},
|
||||
"State":{"Status":"exited"},"NetworkSettings":{"Ports":{}}}
|
||||
""");
|
||||
|
||||
DockerClient.ContainerInfo info = docker.inspectContainer("floci").orElseThrow();
|
||||
|
||||
assertEquals("registry.local/team|floci:latest", info.image());
|
||||
assertEquals("exited", info.state());
|
||||
assertEquals("", info.ports());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishedPortsListEveryBindingAndSkipWhatIsNotPublished() throws Exception {
|
||||
JsonNode ports = new ObjectMapper().readTree("""
|
||||
{"4566/tcp":[{"HostIp":"","HostPort":"14566"}],"8080/tcp":null,"9000/tcp":[],
|
||||
"4510/tcp":[{"HostIp":"","HostPort":"24510"}]}
|
||||
""");
|
||||
|
||||
assertEquals("14566->4566/tcp 24510->4510/tcp", DockerClient.publishedPorts(ports));
|
||||
assertEquals("", DockerClient.publishedPorts(new ObjectMapper().readTree("{}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void aMissingContainerIsEmptyAndGarbageIsReportedWithANextStep() throws Exception {
|
||||
Path missing = Files.createTempFile(tempDir, "docker", "");
|
||||
Files.writeString(missing, "#!/bin/sh\necho 'Error response from daemon: No such container: floci' >&2\nexit 1\n");
|
||||
assertTrue(missing.toFile().setExecutable(true));
|
||||
|
||||
assertTrue(new DockerClient(missing.toString()).inspectContainer("floci").isEmpty());
|
||||
|
||||
DockerException e = assertThrows(DockerException.class, () -> dockerPrinting("not json").inspectContainer("floci"));
|
||||
assertTrue(e.getMessage().contains("Could not read the details of container 'floci'"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("docker info"), e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,11 @@ class StartPreflightTest {
|
||||
calls.add("pull");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pull(String image, String policy, PrintStream progress) {
|
||||
calls.add("pull");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String startContainer(List<String> args) {
|
||||
runArgs.addAll(args);
|
||||
|
||||
Reference in new issue
Block a user