fix(doctor): fail -o json on a failed check and name each product in its hints (#52)

* fix(doctor): fail -o json on a failed check and name each product in its hints

doctor -o json now exits 1 when a check fails, as text output does; hints and HTTP errors name the product's own command (floci gcp start, ...) and the port check falls back to the product's default port; a warnings-only run no longer reads as '0 issue(s) found'; an https AWS_ENDPOINT_URL without a port means 443.

* fix(http): name the product's status command when a request times out

Both ways a request can time out now give the same message with the product's own command. The hint tests use their own local endpoints instead of port 9.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-10 18:45:05 -05:00
1 parent 4cd39a957a
commit 771b353733
20 files changed
+266 -35

No files matched your search

@@ -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());
}
}
@@ -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<ContainerInfo> 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"));
}
}