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