diff --git a/src/main/java/io/floci/cli/commands/DoctorCommand.java b/src/main/java/io/floci/cli/commands/DoctorCommand.java index c8aa501..be5e435 100644 --- a/src/main/java/io/floci/cli/commands/DoctorCommand.java +++ b/src/main/java/io/floci/cli/commands/DoctorCommand.java @@ -68,11 +68,11 @@ public class DoctorCommand implements Callable { new DockerDaemonCheck(docker), new DockerSocketCheck(), new DockerVersionCheck(docker), - new PortAvailableCheck(docker), - new ImagePresentCheck(profile.image(), docker), - new ImageVersionCheck(profile.image(), docker), - new ContainerRunningCheck(docker), - new EndpointReachableCheck(profile.controlPrefix()) + new PortAvailableCheck(docker, profile), + new ImagePresentCheck(profile, docker), + new ImageVersionCheck(profile, docker), + new ContainerRunningCheck(docker, profile), + new EndpointReachableCheck(profile) ); } @@ -167,7 +167,8 @@ public class DoctorCommand implements Callable { structured.add(m); } printer.structured(structured); - return 0; + // Same contract as text output: scripts read the exit code, whatever the format. + return results.stream().anyMatch(r -> r.status() == CheckStatus.fail) ? 1 : 0; } long fails = results.stream().filter(r -> r.status() == CheckStatus.fail).count(); @@ -176,6 +177,9 @@ public class DoctorCommand implements Callable { printer.println(""); if (fails == 0 && warns == 0) { printer.println(Ansi.green("All checks passed.")); + } else if (fails == 0) { + // Nothing failed: say so, rather than "0 issue(s) found" next to the warnings. + printer.println(Ansi.green("No failures") + ", " + warns + " warning(s) to review."); } else { printer.println(fails + " issue(s) found (" + fails + " fail, " + warns + " warn)." + (fix ? "" : " Run with --fix to auto-resolve fixable issues.")); diff --git a/src/main/java/io/floci/cli/commands/ServicesCommand.java b/src/main/java/io/floci/cli/commands/ServicesCommand.java index 7fb7f7a..55ebae4 100644 --- a/src/main/java/io/floci/cli/commands/ServicesCommand.java +++ b/src/main/java/io/floci/cli/commands/ServicesCommand.java @@ -44,7 +44,7 @@ public class ServicesCommand implements Callable { // Resolve the endpoint from the container's port mapping, like status/wait/env do, // so a container started with --port is still found without --endpoint. String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); List services; try { diff --git a/src/main/java/io/floci/cli/commands/StatusCommand.java b/src/main/java/io/floci/cli/commands/StatusCommand.java index 37b77b8..ba901c8 100644 --- a/src/main/java/io/floci/cli/commands/StatusCommand.java +++ b/src/main/java/io/floci/cli/commands/StatusCommand.java @@ -62,7 +62,7 @@ public class StatusCommand implements Callable { String serverVersion = "unavailable"; String serverEdition = ""; boolean reachable = false; - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); try { var health = client.health(); serverVersion = health.version(); diff --git a/src/main/java/io/floci/cli/commands/VersionCommand.java b/src/main/java/io/floci/cli/commands/VersionCommand.java index 9b87eac..b663e54 100644 --- a/src/main/java/io/floci/cli/commands/VersionCommand.java +++ b/src/main/java/io/floci/cli/commands/VersionCommand.java @@ -46,7 +46,7 @@ public class VersionCommand implements Callable { var docker = new io.floci.cli.docker.DockerClient(); String effectiveEndpoint = global.resolvedEndpoint(docker); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); try { var info = client.info(); serverVersion = info.version(); diff --git a/src/main/java/io/floci/cli/commands/WaitCommand.java b/src/main/java/io/floci/cli/commands/WaitCommand.java index e79a373..a9ed3a0 100644 --- a/src/main/java/io/floci/cli/commands/WaitCommand.java +++ b/src/main/java/io/floci/cli/commands/WaitCommand.java @@ -90,7 +90,7 @@ public class WaitCommand implements Callable { // The deadline starts now, so the container lookup below counts against --timeout too. Instant deadline = Instant.now().plusMillis(timeoutMillis); String effectiveEndpoint = knownEndpoint != null ? knownEndpoint : endpointWithin(deadline); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); // Poll soon after a start, then back off; never let a request or a pause run past the // deadline, so --timeout is a real upper bound. diff --git a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java index 2ee610a..537ec3b 100644 --- a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java @@ -73,7 +73,7 @@ public class AzEnvCommand implements Callable { public AzEnvCommand() { this(InstanceState.defaultRoot(), - endpoint -> new FlociHttpClient(endpoint, ProductProfile.AZ.controlPrefix()).tlsCert()); + endpoint -> new FlociHttpClient(endpoint, ProductProfile.AZ).tlsCert()); } /** diff --git a/src/main/java/io/floci/cli/commands/az/AzSetupCommand.java b/src/main/java/io/floci/cli/commands/az/AzSetupCommand.java index 1353968..c40c3f8 100644 --- a/src/main/java/io/floci/cli/commands/az/AzSetupCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzSetupCommand.java @@ -96,7 +96,7 @@ public class AzSetupCommand implements Callable { public AzSetupCommand() { this(InstanceState.defaultRoot(), new ProcessAzCli(), - endpoint -> new FlociHttpClient(endpoint, ProductProfile.AZ.controlPrefix()).tlsCert(), + endpoint -> new FlociHttpClient(endpoint, ProductProfile.AZ).tlsCert(), new DockerClient(), Duration.ofSeconds(2)); } diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java index f63e302..43bd612 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java @@ -41,7 +41,7 @@ public class SnapshotDeleteCommand implements Callable { // Resolve the endpoint from the container's port mapping, like status/services/wait do, // so a container started with --port is still found without --endpoint. String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); try { client.deleteSnapshot(name); printer.println(Ansi.green("Snapshot deleted:") + " " + name); diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java index 1f0ebf8..226aaf4 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java @@ -39,7 +39,7 @@ public class SnapshotListCommand implements Callable { // Resolve the endpoint from the container's port mapping, like status/services/wait do, // so a container started with --port is still found without --endpoint. String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); try { var node = client.listSnapshots(); if (printer.format() != OutputFormat.text) { diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java index 82ab9f5..9a94732 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java @@ -41,7 +41,7 @@ public class SnapshotLoadCommand implements Callable { // Resolve the endpoint from the container's port mapping, like status/services/wait do, // so a container started with --port is still found without --endpoint. String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); try { client.loadSnapshot(name); printer.println(Ansi.green("Snapshot loaded:") + " " + name); diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java index 5aaa60c..f8d83ad 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java @@ -44,7 +44,7 @@ public class SnapshotSaveCommand implements Callable { // Resolve the endpoint from the container's port mapping, like status/services/wait do, // so a container started with --port is still found without --endpoint. String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile); try { client.postSnapshot(name); printer.println(Ansi.green("Snapshot saved:") + " " + name); diff --git a/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java b/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java index 8e71bf5..6ac2bc6 100644 --- a/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java @@ -37,10 +37,13 @@ public class AwsCliEndpointCheck implements Check { ShellExport.formatExport("bash", "AWS_ENDPOINT_URL", suggested)); } - private int extractPort(String url) { + /** The URL's port, or the scheme default (443 for https, 80 otherwise); -1 if unparseable. */ + public static int extractPort(String url) { try { - int port = URI.create(url).getPort(); - return port == -1 ? 80 : port; + URI uri = URI.create(url); + int port = uri.getPort(); + if (port != -1) return port; + return "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; } catch (Exception e) { return -1; } diff --git a/src/main/java/io/floci/cli/doctor/checks/ContainerRunningCheck.java b/src/main/java/io/floci/cli/doctor/checks/ContainerRunningCheck.java index c306fdd..b613250 100644 --- a/src/main/java/io/floci/cli/doctor/checks/ContainerRunningCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/ContainerRunningCheck.java @@ -1,5 +1,6 @@ package io.floci.cli.doctor.checks; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.doctor.Check; @@ -15,9 +16,19 @@ public class ContainerRunningCheck implements Check { /** {@code docker} is shared across one doctor run so each docker fact is fetched once. */ public ContainerRunningCheck(DockerClient docker) { - this.docker = docker; + this(docker, ProductProfile.AWS); } + /** Hints name {@code product}'s tree ({@code floci gcp start}, ...). */ + public ContainerRunningCheck(DockerClient docker, ProductProfile product) { + this.docker = docker; + this.start = product.commandPrefix() + " start"; + this.stop = product.commandPrefix() + " stop"; + } + + private final String start; + private final String stop; + @Override public String name() { return "container.running"; @@ -30,7 +41,7 @@ public class ContainerRunningCheck implements Check { if (info.isEmpty()) { return CheckResult.warn("container.running", "Container '" + container + "' not found", - "floci start"); + start); } String state = info.get().state(); if ("running".equals(state)) { @@ -38,7 +49,7 @@ public class ContainerRunningCheck implements Check { } return CheckResult.fail("container.running", "Container '" + container + "' exists but state is '" + state + "'", - "floci start (or 'floci stop && floci start' to restart)"); + start + " (or '" + stop + " && " + start + "' to restart)"); } catch (DockerException e) { return CheckResult.warn("container.running", "Could not inspect container: " + e.getMessage(), null); } diff --git a/src/main/java/io/floci/cli/doctor/checks/EndpointReachableCheck.java b/src/main/java/io/floci/cli/doctor/checks/EndpointReachableCheck.java index dcf22c6..6fc72b3 100644 --- a/src/main/java/io/floci/cli/doctor/checks/EndpointReachableCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/EndpointReachableCheck.java @@ -1,5 +1,6 @@ package io.floci.cli.doctor.checks; +import io.floci.cli.ProductProfile; import io.floci.cli.doctor.Check; import io.floci.cli.doctor.CheckResult; import io.floci.cli.http.FlociHttpClient; @@ -14,13 +15,24 @@ public class EndpointReachableCheck implements Check { } private final String controlPrefix; + private final String commandPrefix; public EndpointReachableCheck() { this(FlociHttpClient.DEFAULT_CONTROL_PREFIX); } public EndpointReachableCheck(String controlPrefix) { + this(controlPrefix, "floci"); + } + + /** {@code product}'s health endpoint, with a hint that names its tree. */ + public EndpointReachableCheck(ProductProfile product) { + this(product.controlPrefix(), product.commandPrefix()); + } + + private EndpointReachableCheck(String controlPrefix, String commandPrefix) { this.controlPrefix = controlPrefix; + this.commandPrefix = commandPrefix; } @Override @@ -39,6 +51,6 @@ public class EndpointReachableCheck implements Check { } return CheckResult.fail("endpoint.reachable", "GET " + controlPrefix + "/health at " + endpoint + " did not return 200", - "Is Floci running? Try 'floci start'."); + "Is Floci running? Try '" + commandPrefix + " start'."); } } diff --git a/src/main/java/io/floci/cli/doctor/checks/ImagePresentCheck.java b/src/main/java/io/floci/cli/doctor/checks/ImagePresentCheck.java index d9f66a3..3375a14 100644 --- a/src/main/java/io/floci/cli/doctor/checks/ImagePresentCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/ImagePresentCheck.java @@ -1,5 +1,6 @@ package io.floci.cli.doctor.checks; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.doctor.Check; @@ -25,10 +26,22 @@ public class ImagePresentCheck implements Check { /** {@code docker} is shared across one doctor run so each docker fact is fetched once. */ public ImagePresentCheck(String image, DockerClient docker) { + this(image, docker, "floci"); + } + + /** {@code product}'s image, with hints that name its tree. */ + public ImagePresentCheck(ProductProfile product, DockerClient docker) { + this(product.image(), docker, product.commandPrefix()); + } + + private ImagePresentCheck(String image, DockerClient docker, String commandPrefix) { this.image = image; this.docker = docker; + this.pullHint = commandPrefix + " start --pull always"; } + private final String pullHint; + @Override public CheckResult run(String endpoint, String container) { try { @@ -37,7 +50,7 @@ public class ImagePresentCheck implements Check { } return CheckResult.fail("image.present", image + " not pulled locally", - "floci start --pull always"); + pullHint); } catch (DockerException e) { return CheckResult.warn("image.present", "Could not check image presence: " + e.getMessage(), null); } diff --git a/src/main/java/io/floci/cli/doctor/checks/ImageVersionCheck.java b/src/main/java/io/floci/cli/doctor/checks/ImageVersionCheck.java index 003f557..6cc1936 100644 --- a/src/main/java/io/floci/cli/doctor/checks/ImageVersionCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/ImageVersionCheck.java @@ -1,5 +1,6 @@ package io.floci.cli.doctor.checks; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.doctor.Check; @@ -26,16 +27,28 @@ public class ImageVersionCheck implements Check { /** {@code docker} is shared across one doctor run so each docker fact is fetched once. */ public ImageVersionCheck(String image, DockerClient docker) { + this(image, docker, "floci"); + } + + /** {@code product}'s image, with hints that name its tree. */ + public ImageVersionCheck(ProductProfile product, DockerClient docker) { + this(product.image(), docker, product.commandPrefix()); + } + + private ImageVersionCheck(String image, DockerClient docker, String commandPrefix) { this.image = image; this.docker = docker; + this.pullHint = commandPrefix + " start --pull always"; } + private final String pullHint; + @Override public CheckResult run(String endpoint, String container) { try { if (!docker.isImagePresent(image)) { return CheckResult.warn("image.version", image + " not present — version cannot be checked", - "floci start --pull always"); + pullHint); } // Try reading version label from the image String version = readImageLabel(docker, image); diff --git a/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java b/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java index 26772e9..b6464cb 100644 --- a/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java @@ -1,5 +1,6 @@ package io.floci.cli.doctor.checks; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.doctor.Check; @@ -24,9 +25,19 @@ public class PortAvailableCheck implements Check { /** {@code docker} is shared across one doctor run so each docker fact is fetched once. */ public PortAvailableCheck(DockerClient docker) { - this.docker = docker; + this(docker, ProductProfile.AWS); } + /** Falls back to {@code product}'s default port and names its tree in the hint. */ + public PortAvailableCheck(DockerClient docker, ProductProfile product) { + this.docker = docker; + this.defaultPort = product.defaultPort(); + this.startCommand = product.commandPrefix() + " start"; + } + + private final int defaultPort; + private final String startCommand; + @Override public String name() { return "port.available"; @@ -34,7 +45,7 @@ public class PortAvailableCheck implements Check { @Override public CheckResult run(String endpoint, String container) { - int port = extractPort(endpoint); + int port = extractPort(endpoint, defaultPort); // If a Floci container is already listening on the port, that's fine try { var info = docker.inspectContainer(container); @@ -48,7 +59,7 @@ public class PortAvailableCheck implements Check { } return CheckResult.fail("port.available", "Port " + port + " is occupied by another process", - "Run 'lsof -i :" + port + "' to identify the process, or start Floci with --port "); + "Run 'lsof -i :" + port + "' to identify the process, or '" + startCommand + " --port '."); } /** @@ -89,11 +100,16 @@ public class PortAvailableCheck implements Check { } public static int extractPort(String endpoint) { + return extractPort(endpoint, ProductProfile.AWS.defaultPort()); + } + + /** The endpoint's port, or {@code fallback} when it names none or cannot be parsed. */ + public static int extractPort(String endpoint, int fallback) { try { int port = URI.create(endpoint).getPort(); - return port > 0 ? port : 4566; + return port > 0 ? port : fallback; } catch (Exception e) { - return 4566; + return fallback; } } } diff --git a/src/main/java/io/floci/cli/http/FlociHttpClient.java b/src/main/java/io/floci/cli/http/FlociHttpClient.java index b670275..73ea7f1 100644 --- a/src/main/java/io/floci/cli/http/FlociHttpClient.java +++ b/src/main/java/io/floci/cli/http/FlociHttpClient.java @@ -2,12 +2,14 @@ package io.floci.cli.http; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.floci.cli.ProductProfile; import java.net.ConnectException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.net.http.HttpTimeoutException; import java.time.Duration; import java.util.ArrayList; import java.util.List; @@ -28,6 +30,7 @@ public class FlociHttpClient { private final String endpoint; private final String query; private final String controlPrefix; + private final String commandPrefix; private final HttpClient http; public FlociHttpClient(String endpoint) { @@ -39,6 +42,16 @@ public class FlociHttpClient { * (e.g. {@code /_floci} for AWS/Azure, {@code /_floci-gcp} for GCP). */ public FlociHttpClient(String endpoint, String controlPrefix) { + this(endpoint, controlPrefix, "floci"); + } + + /** A client for {@code product}'s emulator: its control prefix, and hints that name its tree. */ + public FlociHttpClient(String endpoint, ProductProfile product) { + this(endpoint, product.controlPrefix(), product.commandPrefix()); + } + + private FlociHttpClient(String endpoint, String controlPrefix, String commandPrefix) { + this.commandPrefix = commandPrefix; // Request paths go before an endpoint's query: http://h:1/?x=1 + /health is http://h:1/health?x=1. // A fragment is never sent, so it is dropped. int fragment = endpoint.indexOf('#'); @@ -152,7 +165,7 @@ public class FlociHttpClient { } catch (FlociException e) { throw e; } catch (ConnectException e) { - throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try 'floci status' or 'floci start'."); + throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try '" + commandPrefix + " status' or '" + commandPrefix + " start'."); } catch (Exception e) { throw new FlociException("Request failed: " + e.getMessage()); } @@ -194,10 +207,12 @@ public class FlociHttpClient { resp = pending.get(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (TimeoutException e) { pending.cancel(true); - throw new FlociException("No complete response from " + endpoint + path + " within " - + timeout.toMillis() + " ms. Is Floci running? Try 'floci status'."); + throw timedOut(path, timeout); } catch (ExecutionException e) { if (e.getCause() instanceof ConnectException) throw (ConnectException) e.getCause(); + // The request's own timeout (no headers yet) can fire just before the one above: + // the same event, so the same message. + if (e.getCause() instanceof HttpTimeoutException) throw timedOut(path, timeout); throw new FlociException("Request failed: " + e.getCause().getMessage()); } if (resp.statusCode() >= 400) { @@ -210,12 +225,17 @@ public class FlociHttpClient { Thread.currentThread().interrupt(); throw new FlociException("Request interrupted.\nRe-run the command."); } catch (ConnectException e) { - throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try 'floci status' or 'floci start'."); + throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try '" + commandPrefix + " status' or '" + commandPrefix + " start'."); } catch (Exception e) { throw new FlociException("Request failed: " + e.getMessage()); } } + private FlociException timedOut(String path, Duration timeout) { + return new FlociException("No complete response from " + endpoint + path + " within " + + timeout.toMillis() + " ms. Is Floci running? Try '" + commandPrefix + " status'."); + } + @SuppressWarnings("unchecked") private Map postJson(String path, String body) throws FlociException { try { @@ -234,7 +254,7 @@ public class FlociHttpClient { } catch (FlociException e) { throw e; } catch (ConnectException e) { - throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try 'floci start'."); + throw new FlociException("Connection refused at " + endpoint + ". Is Floci running? Try '" + commandPrefix + " start'."); } catch (Exception e) { throw new FlociException("Request failed: " + e.getMessage()); } diff --git a/src/test/java/io/floci/cli/unit/DoctorCommandTest.java b/src/test/java/io/floci/cli/unit/DoctorCommandTest.java index 041abcf..7e36e43 100644 --- a/src/test/java/io/floci/cli/unit/DoctorCommandTest.java +++ b/src/test/java/io/floci/cli/unit/DoctorCommandTest.java @@ -192,4 +192,27 @@ class DoctorCommandTest { Thread.currentThread().interrupt(); } } + + /** BL-036: -o json reports a failed check through the exit code, as text output does. */ + @Test + void jsonOutputExitsOneWhenACheckFails() { + Named failing = new Named("bad.check", (e, c) -> CheckResult.fail("bad.check", "broken", "fix it")); + + Run r = doctor(List.of(ok("a.check"), failing), "-o", "json"); + + assertEquals(1, r.exit(), r.out()); + assertTrue(r.out().contains("\"bad.check\""), r.out()); + } + + /** BL-038: warnings alone are not reported as "0 issue(s) found". */ + @Test + void warningsOnlyDoNotReadAsZeroIssues() { + Named warning = new Named("warn.check", (e, c) -> CheckResult.warn("warn.check", "hmm", null)); + + Run r = doctor(List.of(ok("a.check"), warning)); + + assertEquals(0, r.exit()); + assertFalse(r.out().contains("0 issue(s)"), r.out()); + assertTrue(r.out().contains("1 warning(s) to review"), r.out()); + } } diff --git a/src/test/java/io/floci/cli/unit/DoctorHintsTest.java b/src/test/java/io/floci/cli/unit/DoctorHintsTest.java new file mode 100644 index 0000000..196d07d --- /dev/null +++ b/src/test/java/io/floci/cli/unit/DoctorHintsTest.java @@ -0,0 +1,116 @@ +package io.floci.cli.unit; + +import com.sun.net.httpserver.HttpServer; +import io.floci.cli.ProductProfile; +import io.floci.cli.docker.DockerClient; +import io.floci.cli.doctor.CheckResult; +import io.floci.cli.doctor.checks.AwsCliEndpointCheck; +import io.floci.cli.doctor.checks.ContainerRunningCheck; +import io.floci.cli.doctor.checks.EndpointReachableCheck; +import io.floci.cli.doctor.checks.PortAvailableCheck; +import io.floci.cli.http.FlociException; +import io.floci.cli.http.FlociHttpClient; +import org.junit.jupiter.api.Test; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.time.Duration; +import java.util.Optional; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.*; + +/** BL-037/BL-038: doctor's hints and fallbacks belong to the product being diagnosed. */ +class DoctorHintsTest { + + private static final DockerClient NO_CONTAINER = new DockerClient() { + @Override + public Optional inspectContainer(String name) { + return Optional.empty(); + } + }; + + @Test + void containerHintNamesTheProductTree() { + CheckResult r = new ContainerRunningCheck(NO_CONTAINER, ProductProfile.GCP).run("http://localhost:4588", "floci-gcp"); + + assertEquals("floci gcp start", r.fix()); + } + + @Test + void endpointHintNamesTheProductTree() throws Exception { + // A server of our own that answers 500: the check fails without depending on what the + // machine happens to run on some port. + HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + server.createContext("/", exchange -> { + exchange.sendResponseHeaders(500, -1); + exchange.close(); + }); + server.start(); + try { + CheckResult r = new EndpointReachableCheck(ProductProfile.OCI) + .run("http://127.0.0.1:" + server.getAddress().getPort(), "floci-oci"); + + assertTrue(r.fix().contains("floci oci start"), r.fix()); + } finally { + server.stop(0); + } + } + + @Test + void portFallbackIsTheProductsDefaultPort() { + assertEquals(4588, PortAvailableCheck.extractPort("not a url", ProductProfile.GCP.defaultPort())); + assertEquals(4566, PortAvailableCheck.extractPort("not a url")); + } + + @Test + void httpClientErrorsNameTheProductTree() throws Exception { + // A port that was just bound and released: nothing listens there, so the connection is refused. + int closed; + try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { + closed = socket.getLocalPort(); + } + + FlociException e = assertThrows(FlociException.class, + () -> new FlociHttpClient("http://127.0.0.1:" + closed, ProductProfile.AZ).health()); + + assertTrue(e.getMessage().contains("Connection refused"), e.getMessage()); + assertTrue(e.getMessage().contains("floci az start"), e.getMessage()); + } + + /** A request that times out names the product's own status command, like the refused one. */ + @Test + void httpClientTimeoutsNameTheProductTree() throws Exception { + CountDownLatch release = new CountDownLatch(1); + HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + server.createContext("/", exchange -> { + try { + release.await(10, TimeUnit.SECONDS); // held until the client has given up + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + exchange.close(); + }); + server.start(); + try { + FlociHttpClient client = new FlociHttpClient("http://127.0.0.1:" + server.getAddress().getPort(), ProductProfile.GCP); + + FlociException e = assertThrows(FlociException.class, () -> client.health(Duration.ofMillis(200))); + + assertTrue(e.getMessage().contains("floci gcp status"), e.getMessage()); + assertFalse(e.getMessage().contains("'floci status'"), e.getMessage()); + } finally { + release.countDown(); + server.stop(0); + } + } + + @Test + void httpsWithoutAPortMeans443() { + assertEquals(443, AwsCliEndpointCheck.extractPort("https://floci.example")); + assertEquals(80, AwsCliEndpointCheck.extractPort("http://floci.example")); + assertEquals(4566, AwsCliEndpointCheck.extractPort("https://floci.example:4566")); + } +}