|
| 1 | +package com.gitranker.api.testsupport; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.UncheckedIOException; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.concurrent.CompletableFuture; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
| 10 | + |
| 11 | +public final class DockerPreflightCheck { |
| 12 | + |
| 13 | + private static final List<String> DOCKER_CONTEXT_COMMAND = List.of("docker", "context", "show"); |
| 14 | + private static final List<String> DOCKER_VERSION_COMMAND = |
| 15 | + List.of("docker", "version", "--format", "{{.Server.APIVersion}}"); |
| 16 | + |
| 17 | + private final CommandRunner commandRunner; |
| 18 | + |
| 19 | + public DockerPreflightCheck(CommandRunner commandRunner) { |
| 20 | + this.commandRunner = Objects.requireNonNull(commandRunner); |
| 21 | + } |
| 22 | + |
| 23 | + public DockerPreflightResult run() { |
| 24 | + CommandResult contextResult = commandRunner.run(DOCKER_CONTEXT_COMMAND); |
| 25 | + String context = normalizedOrFallback(contextResult.stdout(), "unavailable"); |
| 26 | + |
| 27 | + CommandResult versionResult = commandRunner.run(DOCKER_VERSION_COMMAND); |
| 28 | + if (versionResult.isSuccess()) { |
| 29 | + String apiVersion = normalizedOrFallback(versionResult.stdout(), "unknown"); |
| 30 | + return DockerPreflightResult.success( |
| 31 | + "Docker preflight passed. context=%s serverApiVersion=%s".formatted(context, apiVersion)); |
| 32 | + } |
| 33 | + |
| 34 | + return DockerPreflightResult.failure(buildFailureMessage(context, versionResult)); |
| 35 | + } |
| 36 | + |
| 37 | + private String buildFailureMessage(String context, CommandResult versionResult) { |
| 38 | + String diagnostic = versionResult.primaryDiagnostic(); |
| 39 | + String cause = determineCause(versionResult); |
| 40 | + String firstStep = isDockerCliMissing(versionResult) |
| 41 | + ? "- Install Docker Desktop, OrbStack, or another compatible Docker runtime." |
| 42 | + : "- Start Docker Desktop, OrbStack, or another compatible Docker runtime and confirm `docker version` succeeds."; |
| 43 | + |
| 44 | + return """ |
| 45 | + Docker preflight failed for Testcontainers integration tests. |
| 46 | + Cause: %s |
| 47 | + Context: %s |
| 48 | + Details: %s |
| 49 | + Next steps: |
| 50 | + %s |
| 51 | + - If you only need code-level verification, run `./gradlew test jacocoTestCoverageVerification`. |
| 52 | + - Re-run `./gradlew integrationTest` after Docker is available. |
| 53 | + """.formatted(cause, context, diagnostic, firstStep); |
| 54 | + } |
| 55 | + |
| 56 | + private String determineCause(CommandResult versionResult) { |
| 57 | + if (isDockerCliMissing(versionResult)) { |
| 58 | + return "Docker CLI is not installed or not available on PATH."; |
| 59 | + } |
| 60 | + if (isDockerDaemonUnavailable(versionResult)) { |
| 61 | + return "Docker daemon is not reachable from the current shell."; |
| 62 | + } |
| 63 | + return "Docker returned a non-zero exit code before Testcontainers could start."; |
| 64 | + } |
| 65 | + |
| 66 | + private boolean isDockerCliMissing(CommandResult versionResult) { |
| 67 | + String diagnostic = versionResult.primaryDiagnostic().toLowerCase(); |
| 68 | + return versionResult.exitCode() == 127 |
| 69 | + || diagnostic.contains("command not found") |
| 70 | + || diagnostic.contains("no such file or directory"); |
| 71 | + } |
| 72 | + |
| 73 | + private boolean isDockerDaemonUnavailable(CommandResult versionResult) { |
| 74 | + String diagnostic = versionResult.primaryDiagnostic().toLowerCase(); |
| 75 | + return diagnostic.contains("cannot connect to the docker daemon") |
| 76 | + || diagnostic.contains("is the docker daemon running") |
| 77 | + || diagnostic.contains("error during connect"); |
| 78 | + } |
| 79 | + |
| 80 | + private String normalizedOrFallback(String value, String fallback) { |
| 81 | + String normalized = value == null ? "" : value.trim(); |
| 82 | + return normalized.isEmpty() ? fallback : normalized; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +@FunctionalInterface |
| 87 | +interface CommandRunner { |
| 88 | + |
| 89 | + CommandResult run(List<String> command); |
| 90 | +} |
| 91 | + |
| 92 | +record CommandResult(int exitCode, String stdout, String stderr) { |
| 93 | + |
| 94 | + static CommandResult success(String stdout, String stderr) { |
| 95 | + return new CommandResult(0, stdout, stderr); |
| 96 | + } |
| 97 | + |
| 98 | + static CommandResult failure(int exitCode, String stdout, String stderr) { |
| 99 | + if (exitCode == 0) { |
| 100 | + throw new IllegalArgumentException("Failure result must have non-zero exit code"); |
| 101 | + } |
| 102 | + return new CommandResult(exitCode, stdout, stderr); |
| 103 | + } |
| 104 | + |
| 105 | + boolean isSuccess() { |
| 106 | + return exitCode == 0; |
| 107 | + } |
| 108 | + |
| 109 | + String primaryDiagnostic() { |
| 110 | + String stderrText = stderr == null ? "" : stderr.trim(); |
| 111 | + if (!stderrText.isEmpty()) { |
| 112 | + return stderrText; |
| 113 | + } |
| 114 | + |
| 115 | + String stdoutText = stdout == null ? "" : stdout.trim(); |
| 116 | + if (!stdoutText.isEmpty()) { |
| 117 | + return stdoutText; |
| 118 | + } |
| 119 | + |
| 120 | + return "No additional docker diagnostic output was produced."; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +record DockerPreflightResult(boolean isSuccess, String message) { |
| 125 | + |
| 126 | + static DockerPreflightResult success(String message) { |
| 127 | + return new DockerPreflightResult(true, message); |
| 128 | + } |
| 129 | + |
| 130 | + static DockerPreflightResult failure(String message) { |
| 131 | + return new DockerPreflightResult(false, message); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +final class ProcessCommandRunner implements CommandRunner { |
| 136 | + |
| 137 | + @Override |
| 138 | + public CommandResult run(List<String> command) { |
| 139 | + ProcessBuilder processBuilder = new ProcessBuilder(command); |
| 140 | + processBuilder.redirectErrorStream(false); |
| 141 | + |
| 142 | + try { |
| 143 | + Process process = processBuilder.start(); |
| 144 | + CompletableFuture<String> stdoutFuture = |
| 145 | + CompletableFuture.supplyAsync(() -> readStream(process.getInputStream())); |
| 146 | + CompletableFuture<String> stderrFuture = |
| 147 | + CompletableFuture.supplyAsync(() -> readStream(process.getErrorStream())); |
| 148 | + int exitCode = process.waitFor(); |
| 149 | + String stdout = stdoutFuture.join(); |
| 150 | + String stderr = stderrFuture.join(); |
| 151 | + return new CommandResult(exitCode, stdout, stderr); |
| 152 | + } catch (IOException exception) { |
| 153 | + return CommandResult.failure(127, "", exception.getMessage()); |
| 154 | + } catch (InterruptedException exception) { |
| 155 | + Thread.currentThread().interrupt(); |
| 156 | + return CommandResult.failure(130, "", "Interrupted while running command: " + String.join(" ", command)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private String readStream(InputStream inputStream) { |
| 161 | + try { |
| 162 | + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); |
| 163 | + } catch (IOException exception) { |
| 164 | + throw new UncheckedIOException(exception); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments