Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(docker): share one helper for running a command in a container (#4505)
Priamo Germosen585bad28c2bb
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerExec.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerExec.javanew file mode 100644index 0000000000..fb2378b21e--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerExec.java@@ -0,0 +1,121 @@+package io.github.hectorvent.floci.core.common.docker;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.async.ResultCallback;+import com.github.dockerjava.api.model.Frame;+import com.github.dockerjava.api.model.StreamType;+import org.jboss.logging.Logger;++import java.io.ByteArrayOutputStream;+import java.io.IOException;+import java.io.OutputStream;+import java.nio.charset.StandardCharsets;+import java.util.concurrent.TimeUnit;++/**+ * Runs a command in a running container and waits for its exit code, shared by every container+ * manager that drives its container through {@code docker exec}: each of them had its own copy of+ * this before it moved here.+ *+ * <p>A command that outlives its timeout is left running in the container, with its attach+ * stream closed, and reported with {@link Result#timedOut()} set and an exit code of {@code -1}.+ * An error on the attach stream propagates as the {@link RuntimeException} docker-java raises for+ * it.+ */+public final class ContainerExec {++ private static final Logger LOG = Logger.getLogger(ContainerExec.class);++ private ContainerExec() {+ }++ /**+ * The outcome of one command. On a timeout, {@code stderr} ends with a note naming the timeout,+ * so a caller that logs the output says why the command stopped.+ */+ public record Result(long exitCode, String stdout, String stderr, boolean timedOut) {++ /** Both streams, trimmed, or {@code "(no output)"} when the command printed nothing. */+ public String summary() {+ String output = joinLines(stdout, stderr).trim();+ return output.isEmpty() ? "(no output)" : output;+ }++ /** This result, or a {@link RuntimeException} naming {@code containerId} when it timed out. */+ public Result throwIfTimedOut(String containerId) {+ if (timedOut) {+ throw new RuntimeException("exec timed out in container " + containerId);+ }+ return this;+ }+ }++ /** Runs {@code cmd}, keeping its stdout and stderr apart. */+ public static Result run(DockerClient dockerClient, String containerId, String[] cmd, int timeoutSeconds) {+ ByteArrayOutputStream stdout = new ByteArrayOutputStream();+ ByteArrayOutputStream stderr = new ByteArrayOutputStream();+ Result result = run(dockerClient, containerId, cmd, timeoutSeconds, stdout, stderr);+ return new Result(result.exitCode(), stdout.toString(StandardCharsets.UTF_8),+ joinLines(stderr.toString(StandardCharsets.UTF_8), result.stderr()), result.timedOut());+ }++ /** Runs {@code cmd}, interleaving stderr into stdout in the order the container wrote them. */+ public static Result runMerged(DockerClient dockerClient, String containerId, String[] cmd, int timeoutSeconds) {+ ByteArrayOutputStream output = new ByteArrayOutputStream();+ Result result = run(dockerClient, containerId, cmd, timeoutSeconds, output, output);+ return new Result(result.exitCode(), output.toString(StandardCharsets.UTF_8), result.stderr(),+ result.timedOut());+ }++ /**+ * Runs {@code cmd}, writing its stdout and stderr to the given streams, which may be the same+ * one. The returned {@code stdout} is empty and its {@code stderr} holds only the timeout note.+ */+ public static Result run(DockerClient dockerClient, String containerId, String[] cmd, int timeoutSeconds,+ OutputStream stdout, OutputStream stderr) {+ String execId = dockerClient.execCreateCmd(containerId)+ .withCmd(cmd)+ .withAttachStdout(true)+ .withAttachStderr(true)+ .exec()+ .getId();++ ResultCallback.Adapter<Frame> callback = new ResultCallback.Adapter<>() {+ @Override+ public void onNext(Frame frame) {+ byte[] payload = frame.getPayload();+ if (payload == null) {+ return;+ }+ try {+ if (frame.getStreamType() == StreamType.STDERR) {+ stderr.write(payload);+ } else {+ stdout.write(payload);+ }+ } catch (IOException e) {+ LOG.warnv(e, "Failed to capture output of container exec {0}", execId);+ }+ }+ };+ dockerClient.execStartCmd(execId).exec(callback);++ try {+ if (!callback.awaitCompletion(timeoutSeconds, TimeUnit.SECONDS)) {+ return new Result(-1, "", "Timed out after " + timeoutSeconds + "s", true);+ }+ Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();+ return new Result(exitCode != null ? exitCode : -1, "", "", false);+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ throw new IllegalStateException("Interrupted running a command in container " + containerId, e);+ }+ }++ private static String joinLines(String first, String second) {+ if (first.isEmpty() || second.isEmpty()) {+ return first + second;+ }+ return first.endsWith("\n") ? first + second : first + "\n" + second;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.javaindex 92ef4fb613..6e12d7ffa1 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;@@ -54,7 +55,6 @@ import java.util.Set;import java.util.function.Consumer;import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;import java.util.concurrent.ArrayBlockingQueue;-import java.util.concurrent.CountDownLatch;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.RejectedExecutionException;@@ -1511,7 +1511,7 @@ public class Ec2ContainerManager {// Exit 1 means sshd is absent and there is nothing to start; exit 2 means sshd is// there but the client package did not land. Only the first is fatal - see// sshdInstallProbeCommand() for why the probe distinguishes them.- ContainerExecResult install = execInContainerForResult(containerId, sshdInstallProbeCommand(), 120);+ ContainerExec.Result install = execInContainerForResult(containerId, sshdInstallProbeCommand(), 120);if (install.exitCode() == SSH_CLIENT_MISSING_EXIT_CODE) {LOG.warnv("sshd is available on EC2 instance {0} but the OpenSSH client package is not:"+ " scp is missing, so provisioners that upload files over scp will fail: {1}",@@ -1522,7 +1522,7 @@ public class Ec2ContainerManager {return;}// Generate host keys- ContainerExecResult keygen = execInContainerForResult(containerId, new String[]{"ssh-keygen", "-A"}, 10);+ ContainerExec.Result keygen = execInContainerForResult(containerId, new String[]{"ssh-keygen", "-A"}, 10);if (keygen.exitCode() != 0) {LOG.warnv("Could not generate SSH host keys for EC2 instance {0}: {1}",instanceId, keygen.summary());@@ -1530,7 +1530,7 @@ public class Ec2ContainerManager {}// Modern OpenSSH refuses to start without its privilege-separation directory, and /run// is a fresh tmpfs in most container images, so /run/sshd genuinely isn't there yet.- ContainerExecResult mkdir = execInContainerForResult(containerId,+ ContainerExec.Result mkdir = execInContainerForResult(containerId,new String[]{"mkdir", "-p", "/run/sshd"}, 5);if (mkdir.exitCode() != 0) {LOG.warnv("Could not create /run/sshd for EC2 instance {0}: {1}",@@ -1540,7 +1540,7 @@ public class Ec2ContainerManager {// Start sshd without -D so it daemonizes itself and survives this exec session. Since sshd// requires execution with an absolute path, several paths are tried until it startsfor (String sshdPath : ALLOWED_SSHD_PATHS) {- ContainerExecResult start = execInContainerForResult(containerId, new String[]{sshdPath}, 5);+ ContainerExec.Result start = execInContainerForResult(containerId, new String[]{sshdPath}, 5);if (start.exitCode() != 0) {LOG.warnv("Could not start sshd using path {0} for EC2 instance {1}: {2}", sshdPath, instanceId, start.summary());continue;@@ -1677,14 +1677,14 @@ public class Ec2ContainerManager {private void configureLinkLocalMetadataEndpoint(String containerId, String instanceId, String flociHost, int imdsPort) {try {- ContainerExecResult install = execInContainerForResult(containerId, metadataProxyInstallCommand(), 180);+ ContainerExec.Result install = execInContainerForResult(containerId, metadataProxyInstallCommand(), 180);if (install.exitCode() != 0) {LOG.warnv("Could not install IMDS proxy dependencies for EC2 instance {0}: {1}",instanceId, install.summary());return;}- ContainerExecResult start = execInContainerForResult(containerId, metadataProxyStartCommand(flociHost, imdsPort), 30);+ ContainerExec.Result start = execInContainerForResult(containerId, metadataProxyStartCommand(flociHost, imdsPort), 30);if (start.exitCode() != 0) {LOG.warnv("Could not start link-local IMDS proxy for EC2 instance {0}: {1}",instanceId, start.summary());@@ -1697,44 +1697,25 @@ public class Ec2ContainerManager {}}- private void execInContainer(String containerId, String[] cmd, int timeoutSeconds) throws Exception {+ private void execInContainer(String containerId, String[] cmd, int timeoutSeconds) {execInContainerForResult(containerId, cmd, timeoutSeconds);}- private ContainerExecResult execInContainerForResult(String containerId, String[] cmd, int timeoutSeconds) throws Exception {- String execId = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec()- .getId();-- CountDownLatch latch = new CountDownLatch(1);+ private ContainerExec.Result execInContainerForResult(String containerId, String[] cmd, int timeoutSeconds) {BoundedOutput output = new BoundedOutput(UserDataPipeline.MAX_EXEC_OUTPUT_BYTES);- dockerClient.execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame.getPayload() != null) {- try { output.write(frame.getPayload()); } catch (IOException ignored) {}- }+ ContainerExec.Result result;+ try {+ result = ContainerExec.run(dockerClient, containerId, cmd, timeoutSeconds, output, output);+ } catch (RuntimeException e) {+ if (Thread.currentThread().isInterrupted()) {+ throw e;}- @Override- public void onComplete() { latch.countDown(); }- @Override- public void onError(Throwable t) { latch.countDown(); }- });- boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);- if (!completed) {- return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s");+ return new ContainerExec.Result(-1, "", Objects.requireNonNullElse(e.getMessage(), e.toString()), false);}- Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();- return new ContainerExecResult(exitCode != null ? exitCode : -1, summarizeUserDataOutput(output));- }-- record ContainerExecResult(long exitCode, String output) {- String summary() {- return output == null || output.isBlank() ? "(no output)" : output;+ if (result.timedOut()) {+ return result;}+ return new ContainerExec.Result(result.exitCode(), summarizeUserDataOutput(output), "", false);}private String getContainerBridgeIp(String containerId) {diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.javaindex 59f7595743..1e9eee1a87 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java@@ -1,14 +1,12 @@package io.github.hectorvent.floci.services.ec2;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;import com.github.dockerjava.api.command.InspectContainerResponse;import com.github.dockerjava.api.exception.NotFoundException;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.Resettable;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;@@ -19,14 +17,13 @@ import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;-import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.util.List;import java.util.Map;+import java.util.Objects;import java.util.Optional;import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.TimeUnit;import java.util.function.Function;import java.util.regex.Pattern;@@ -108,7 +105,7 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {int effectiveSize = sizeGib > 0 ? sizeGib : 8;String rawFile = "/volumes/" + volumeId + ".raw";String cmd = "truncate -s " + effectiveSize + "G " + rawFile;- ContainerExecResult result = execInContainer(helperId, new String[]{"sh", "-c", cmd}, DEFAULT_TIMEOUT_SECONDS);+ ContainerExec.Result result = execInContainer(helperId, new String[]{"sh", "-c", cmd}, DEFAULT_TIMEOUT_SECONDS);if (result.exitCode() != 0) {LOG.warnv("Failed to create backing file for volume {0}: {1}", volumeId, result.summary());}@@ -156,7 +153,7 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {+ " losetup -d \"$loop\" 2>/dev/null || break\n"+ "done\n"+ "rm -f \"$raw\"";- ContainerExecResult result = execInContainer(helperId,+ ContainerExec.Result result = execInContainer(helperId,new String[]{"sh", "-c", script, "delete", rawFile}, DEFAULT_TIMEOUT_SECONDS);activeLoopDevices.remove(volumeId);return result.exitCode() == 0;@@ -222,7 +219,7 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {+ "fi\n"+ "echo \"$loop\"";- ContainerExecResult helperResult;+ ContainerExec.Result helperResult;synchronized (loopAllocationLock) {helperResult = execInContainer(helperId,new String[]{"sh", "-c", helperScript, "helper", rawFile, String.valueOf(effectiveSize)},@@ -258,7 +255,7 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {+ "fi\n"+ "[ -b \"$dev\" ] || [ -L \"$dev\" ]";- ContainerExecResult targetResult = execInContainer(targetContainerId,+ ContainerExec.Result targetResult = execInContainer(targetContainerId,new String[]{"sh", "-c", targetScript, "target", normalizedDevice, loopDev, minorStr},DEFAULT_TIMEOUT_SECONDS);if (targetResult.exitCode() != 0) {@@ -364,34 +361,11 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {}}- private ContainerExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds) {+ private ContainerExec.Result execInContainer(String containerId, String[] cmd, int timeoutSeconds) {try {- ExecCreateCmdResponse exec = dockerClient- .execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- StringBuilder output = new StringBuilder();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- output.append(new String(frame.getPayload(), StandardCharsets.UTF_8));- }- }- })- .awaitCompletion(timeoutSeconds, TimeUnit.SECONDS);-- if (!completed) {- return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s");- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ContainerExecResult(exitCode != null ? exitCode : -1, output.toString());- } catch (Exception e) {- return new ContainerExecResult(-1, e.getMessage());+ return ContainerExec.runMerged(dockerClient, containerId, cmd, timeoutSeconds);+ } catch (RuntimeException e) {+ return new ContainerExec.Result(-1, "", Objects.requireNonNullElse(e.getMessage(), e.toString()), false);}}@@ -417,10 +391,4 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {}activeLoopDevices.clear();}-- public record ContainerExecResult(long exitCode, String output) {- public String summary() {- return output == null || output.isBlank() ? "(no output)" : output.trim();- }- }}diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxy.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxy.javaindex ec5acbdc47..3e6b8ed01a 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxy.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxy.java@@ -1,12 +1,11 @@package io.github.hectorvent.floci.services.ecs.container;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;import com.github.dockerjava.api.model.Container;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.ContainerTeardown;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;@@ -17,7 +16,6 @@ import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;-import java.nio.charset.StandardCharsets;import java.util.List;import java.util.Map;import java.util.Optional;@@ -45,6 +43,7 @@ public class EcsCredentialsProxy implements ContainerTeardown {private static final String OWNS_NETWORK_LABEL = "floci.ecs-task-role-credentials-proxy";private static final int VERIFY_ATTEMPTS = 30;private static final long VERIFY_INTERVAL_MILLIS = 200;+ private static final int EXEC_TIMEOUT_SECONDS = 10;private final DockerClient dockerClient;private final ContainerBuilder containerBuilder;@@ -250,19 +249,10 @@ public class EcsCredentialsProxy implements ContainerTeardown {private String execCapture(String containerId, String... cmd) {try {- String execId = dockerClient.execCreateCmd(containerId)- .withAttachStdout(true).withAttachStderr(true).withCmd(cmd).exec().getId();- StringBuilder output = new StringBuilder();- try (ResultCallback.Adapter<Frame> callback = new ResultCallback.Adapter<>() {- @Override- public void onNext(Frame frame) {- output.append(new String(frame.getPayload(), StandardCharsets.UTF_8));- }- }) {- dockerClient.execStartCmd(execId).exec(callback).awaitCompletion();- }- return output.toString();- } catch (Exception e) {+ ContainerExec.Result result = ContainerExec.runMerged(dockerClient, containerId, cmd, EXEC_TIMEOUT_SECONDS);+ return result.timedOut() ? null : result.stdout();+ } catch (RuntimeException e) {+ LOG.debugv("Probe command in credentials proxy {0} failed: {1}", containerId, e.getMessage());return null;}}diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.javaindex 85c0622bbe..6b01dd352c 100644--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java@@ -1,11 +1,9 @@package io.github.hectorvent.floci.services.eks;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;import com.github.dockerjava.api.command.ExecCreateCmdResponse;import com.github.dockerjava.api.command.InspectContainerResponse;import com.github.dockerjava.api.model.ContainerNetwork;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.config.FlociCertificateAuthority;import io.github.hectorvent.floci.core.common.AwsRegions;@@ -14,6 +12,7 @@ import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource;import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource.ClientVpc;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;@@ -73,7 +72,6 @@ import java.util.Set;import java.util.UUID;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.CopyOnWriteArrayList;-import java.util.concurrent.TimeUnit;import java.util.function.Consumer;import java.util.stream.Collectors;@@ -1787,7 +1785,7 @@ public class EksClusterManagermetadataServer.reconcileContainerAddresses(containerIps.allIps(), nodeInstance);}- ContainerExecResult install = execInContainerForResult(containerId,+ ContainerExec.Result install = execInContainerForResult(containerId,Ec2MetadataProxy.installCommand(), 180);if (install.exitCode() != 0) {LOG.warnv("Could not install IMDS proxy dependencies for EKS cluster {0}: {1}",@@ -1798,7 +1796,7 @@ public class EksClusterManagerString flociHost = dockerHostResolver.resolve();int imdsPort = config.services().ec2().imdsPort();- ContainerExecResult start = execInContainerForResult(containerId,+ ContainerExec.Result start = execInContainerForResult(containerId,Ec2MetadataProxy.startCommand(flociHost, imdsPort), 30);if (start.exitCode() != 0) {LOG.warnv("Could not start link-local IMDS proxy for EKS cluster {0}: {1}",@@ -1822,7 +1820,7 @@ public class EksClusterManagerreturn;}try {- ContainerExecResult install = execInContainerForResult(containerId,+ ContainerExec.Result install = execInContainerForResult(containerId,Ec2MetadataProxy.installCommand(), 180);if (install.exitCode() != 0) {LOG.warnv("Could not install Pod Identity relay dependencies for EKS cluster {0}: {1}",@@ -1833,7 +1831,7 @@ public class EksClusterManagerString flociHost = dockerHostResolver.resolve();int flociPort = config.port();- ContainerExecResult start = execInContainerForResult(containerId,+ ContainerExec.Result start = execInContainerForResult(containerId,Ec2MetadataProxy.podIdentityStartCommand(flociHost, flociPort), 30);if (start.exitCode() != 0) {LOG.warnv("Could not start link-local Pod Identity relay for EKS cluster {0}: {1}",@@ -1859,7 +1857,7 @@ public class EksClusterManagerString[] routingCmd = EksPodNetworkRouting.buildRoutingCommand(EksPodNetworkRouting.DEFAULT_POD_CIDR,endpoints);- ContainerExecResult routing = execInContainerForResult(containerId, routingCmd, 15);+ ContainerExec.Result routing = execInContainerForResult(containerId, routingCmd, 15);if (routing.exitCode() != 0) {LOG.warnv("Could not configure link-local pod network routing for EKS cluster {0}: {1}",cluster.getName(), routing.summary());@@ -1918,7 +1916,7 @@ public class EksClusterManagerprogrammedClusterRoutes.put(clusterKey, desiredDests);return;}- ContainerExecResult result = execInContainerForResult(containerId, cmd.get(), 30);+ ContainerExec.Result result = execInContainerForResult(containerId, cmd.get(), 30);if (result.exitCode() != 0) {LOG.warnv("Could not program VPC routes for EKS cluster {0}: {1}",cluster.getName(), result.summary());@@ -2164,38 +2162,8 @@ public class EksClusterManagerreturn record != null ? record.instance() : null;}- ContainerExecResult execInContainerForResult(String containerId, String[] cmd, int timeoutSeconds) throws Exception {- DockerClient dockerClient = lifecycleManager.getDockerClient();- ExecCreateCmdResponse exec = dockerClient- .execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- StringBuilder output = new StringBuilder();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- output.append(new String(frame.getPayload(), StandardCharsets.UTF_8));- }- }- })- .awaitCompletion(timeoutSeconds, TimeUnit.SECONDS);-- if (!completed) {- return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s");- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ContainerExecResult(exitCode != null ? exitCode : -1, output.toString());- }-- record ContainerExecResult(long exitCode, String output) {- String summary() {- return output == null || output.isBlank() ? "(no output)" : output.trim();- }+ ContainerExec.Result execInContainerForResult(String containerId, String[] cmd, int timeoutSeconds) {+ return ContainerExec.runMerged(lifecycleManager.getDockerClient(), containerId, cmd, timeoutSeconds);}/**@@ -2231,11 +2199,7 @@ public class EksClusterManagerprivate String execInContainer(String containerId, String[] cmd) throws Exception {- ContainerExecResult result = execInContainerForResult(containerId, cmd, 10);- if (result.exitCode() == -1 && result.output().startsWith("Timed out")) {- throw new RuntimeException("exec timed out in container " + containerId);- }- return result.output();+ return execInContainerForResult(containerId, cmd, 10).throwIfTimedOut(containerId).stdout();}private String extractYamlField(String yaml, String fieldName) {diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.javaindex e1d4fb0115..b6a7c07c85 100644--- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;@@ -24,9 +25,6 @@ import io.github.hectorvent.floci.services.s3.S3Service;import io.github.hectorvent.floci.services.s3.model.S3Object;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.model.Frame;-import com.github.dockerjava.api.model.StreamType;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;import org.jboss.logging.Logger;@@ -43,7 +41,6 @@ import java.util.Optional;import java.util.OptionalInt;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.TimeUnit;/*** Manages the backing Apache Flink cluster for a Managed Service for Apache Flink application.@@ -654,36 +651,7 @@ public class FlinkContainerManager {}}- private ExecResult execInContainer(String containerId, String[] cmd) throws Exception {- var dockerClient = lifecycleManager.getDockerClient();- var exec = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- ByteArrayOutputStream stdout = new ByteArrayOutputStream();- ByteArrayOutputStream stderr = new ByteArrayOutputStream();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame.getStreamType() == StreamType.STDERR) {- stderr.writeBytes(frame.getPayload());- } else {- stdout.writeBytes(frame.getPayload());- }- }- })- .awaitCompletion(15, TimeUnit.SECONDS);-- if (!completed) {- throw new RuntimeException("exec timed out in container " + containerId);- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ExecResult(exitCode != null ? exitCode : -1,- stdout.toString(StandardCharsets.UTF_8), stderr.toString(StandardCharsets.UTF_8));+ private ContainerExec.Result execInContainer(String containerId, String[] cmd) throws Exception {+ return ContainerExec.run(lifecycleManager.getDockerClient(), containerId, cmd, 15).throwIfTimedOut(containerId);}-- private record ExecResult(long exitCode, String stdout, String stderr) {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.javaindex 9c98f81d08..f9979cf731 100644--- a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java@@ -4,18 +4,16 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.mwaa.model.Environment;-import com.github.dockerjava.api.async.ResultCallback;import com.github.dockerjava.api.command.InspectContainerResponse;import com.github.dockerjava.api.exception.NotFoundException;import com.github.dockerjava.api.model.ContainerNetwork;-import com.github.dockerjava.api.model.Frame;-import com.github.dockerjava.api.model.StreamType;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;@@ -356,7 +354,7 @@ public class MwaaEnvironmentManager {}copyFileIntoContainer(containerId, "/tmp", "mwaa-requirements.txt", requirementsContent);try {- ExecResult result = execInContainer(containerId,+ ContainerExec.Result result = execInContainer(containerId,new String[]{"pip", "install", "--no-cache-dir", "-r", "/tmp/mwaa-requirements.txt"});if (result.exitCode() != 0) {LOG.warnv("pip install -r requirements.txt exited {0} for environment {1}: {2}",@@ -370,7 +368,7 @@ public class MwaaEnvironmentManager {}/** Runs {@code airflow <cliCommand>} inside the Airflow container via {@code sh -c}. */- public ExecResult runAirflowCli(String airflowContainerId, String cliCommand) throws Exception {+ public ContainerExec.Result runAirflowCli(String airflowContainerId, String cliCommand) throws Exception {return execInContainer(airflowContainerId, new String[]{"sh", "-c", "airflow " + cliCommand});}@@ -531,7 +529,7 @@ public class MwaaEnvironmentManager {Exception last = null;for (int attempt = 1; attempt <= 60; attempt++) {try {- ExecResult result = execInContainer(containerId,+ ContainerExec.Result result = execInContainer(containerId,new String[]{"pg_isready", "-h", "127.0.0.1", "-U", "airflow"});if (result.exitCode() == 0) {return;@@ -608,35 +606,8 @@ public class MwaaEnvironmentManager {}}- ExecResult execInContainer(String containerId, String[] cmd) throws Exception {- var dockerClient = lifecycleManager.getDockerClient();- var exec = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- ByteArrayOutputStream stdout = new ByteArrayOutputStream();- ByteArrayOutputStream stderr = new ByteArrayOutputStream();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame.getStreamType() == StreamType.STDERR) {- stderr.writeBytes(frame.getPayload());- } else {- stdout.writeBytes(frame.getPayload());- }- }- })- .awaitCompletion(30, TimeUnit.SECONDS);-- if (!completed) {- throw new RuntimeException("exec timed out in container " + containerId);- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ExecResult(exitCode != null ? exitCode : -1,- stdout.toString(StandardCharsets.UTF_8), stderr.toString(StandardCharsets.UTF_8));+ ContainerExec.Result execInContainer(String containerId, String[] cmd) throws Exception {+ return ContainerExec.run(lifecycleManager.getDockerClient(), containerId, cmd, 30).throwIfTimedOut(containerId);}private static String generateSecret(int bytes) {@@ -652,5 +623,4 @@ public class MwaaEnvironmentManager {return Base64.getUrlEncoder().encodeToString(buf);}- public record ExecResult(long exitCode, String stdout, String stderr) {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxy.java b/src/main/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxy.javaindex 8173a0d3b3..26fc1d156f 100644--- a/src/main/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxy.java+++ b/src/main/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxy.java@@ -1,6 +1,6 @@package io.github.hectorvent.floci.services.mwaa.proxy;-import io.github.hectorvent.floci.services.mwaa.MwaaEnvironmentManager;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.vertx.core.Vertx;import io.vertx.core.WorkerExecutor;import io.vertx.core.http.HttpClient;@@ -121,7 +121,7 @@ public class MwaaWebProxy {String command = body.toString(StandardCharsets.UTF_8).trim();// The Docker exec can block for up to 30 seconds, so keep it off the event loop.// Unordered: independent CLI calls should not queue behind each other.- cliWorkers.<MwaaEnvironmentManager.ExecResult>executeBlocking(() -> cliExecutor.execute(command), false)+ cliWorkers.<ContainerExec.Result>executeBlocking(() -> cliExecutor.execute(command), false).onSuccess(result -> {String stdoutB64 = Base64.getEncoder().encodeToString(result.stdout().getBytes(StandardCharsets.UTF_8));String stderrB64 = Base64.getEncoder().encodeToString(result.stderr().getBytes(StandardCharsets.UTF_8));@@ -199,6 +199,6 @@ public class MwaaWebProxy {@FunctionalInterfacepublic interface CliExecutor {- MwaaEnvironmentManager.ExecResult execute(String cliCommand) throws Exception;+ ContainerExec.Result execute(String cliCommand) throws Exception;}}diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.javaindex bbb160fee4..756a4554e0 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java@@ -1,8 +1,6 @@package io.github.hectorvent.floci.services.rds.container;-import com.github.dockerjava.api.async.ResultCallback;import com.github.dockerjava.api.command.InspectContainerResponse;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.RegionResolver;@@ -10,6 +8,7 @@ import io.github.hectorvent.floci.core.common.Resettable;import io.github.hectorvent.floci.core.common.ServiceConfigAccess;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;@@ -28,8 +27,8 @@ import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.IOException;-import java.nio.file.Path;import java.nio.charset.StandardCharsets;+import java.nio.file.Path;import java.time.Clock;import java.time.Duration;import java.util.ArrayList;@@ -39,7 +38,6 @@ import java.util.Optional;import java.util.Set;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;@@ -860,8 +858,8 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {String lastOutput = "";for (int attempt = 1; attempt <= 60; attempt++) {try {- ContainerExecResult result = execInContainer(containerId, cmd, 5);- lastOutput = result.output() + (result.stderr().isEmpty() ? "" : "\n" + result.stderr());+ ContainerExec.Result result = execInContainer(containerId, cmd, 5);+ lastOutput = result.stdout() + (result.stderr().isEmpty() ? "" : "\n" + result.stderr());if (result.exitCode() == 0) {LOG.infov("Initialized {0} in RDS container {1}", description, containerName);return;@@ -886,11 +884,11 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {"-U", effectiveUser};try (Lease _ = holdContainer(containerId)) {- ContainerExecResult result = execInContainer(containerId, cmd, 120);+ ContainerExec.Result result = execInContainer(containerId, cmd, 120);if (result.exitCode() != 0) {throw new RuntimeException("pg_dumpall failed with exit code " + result.exitCode() + ": " + result.stderr());}- return result.output();+ return result.stdout();} catch (Exception e) {throw new RuntimeException("Failed to create postgres snapshot", e);}@@ -920,7 +918,7 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {String[] cmd = { "sh", "/tmp/restore.sh", effectiveUser };- ContainerExecResult result = null;+ ContainerExec.Result result = null;for (int i = 0; i < 60; i++) {result = execInContainer(containerId, cmd, 120);if (result.exitCode() == 0) {@@ -938,7 +936,7 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {}if (result == null || result.exitCode() != 0) {- String errMsg = result != null ? (result.stderr().isEmpty() ? result.output() : result.stderr()) : "";+ String errMsg = result != null ? (result.stderr().isEmpty() ? result.stdout() : result.stderr()) : "";throw new RuntimeException("psql restore failed with exit code "+ (result != null ? result.exitCode() : -1) + ": " + errMsg);}@@ -993,61 +991,10 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {tar.closeArchiveEntry();}- private ContainerExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds) throws Exception {- String execId = lifecycleManager.getDockerClient().execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec()- .getId();-- CountDownLatch latch = new CountDownLatch(1);- ByteArrayOutputStream output = new ByteArrayOutputStream();- ByteArrayOutputStream stderr = new ByteArrayOutputStream();- Closeable callback = lifecycleManager.getDockerClient().execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame.getPayload() != null) {- try {- if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDOUT) {- output.write(frame.getPayload());- } else if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDERR) {- stderr.write(frame.getPayload());- }- } catch (IOException e) {- LOG.warnv(e, "Failed to read output stream for container exec {0}", execId);- }- }- }-- @Override- public void onComplete() {- latch.countDown();- }-- @Override- public void onError(Throwable t) {- LOG.warnv(t, "Container exec {0} failed", execId);- latch.countDown();- }- });- try {- boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);- if (!completed) {- return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s", "");- }- Long exitCode = lifecycleManager.getDockerClient().inspectExecCmd(execId).exec().getExitCodeLong();- return new ContainerExecResult(- exitCode != null ? exitCode : -1,- output.toString(StandardCharsets.UTF_8),- stderr.toString(StandardCharsets.UTF_8));- } finally {- callback.close();- }+ private ContainerExec.Result execInContainer(String containerId, String[] cmd, int timeoutSeconds) {+ return ContainerExec.run(lifecycleManager.getDockerClient(), containerId, cmd, timeoutSeconds);}- record ContainerExecResult(long exitCode, String output, String stderr) {}-public void removeVolume(String instanceId, String volumeId) {removeVolume(instanceId, instanceId,ContainerStorageHelper.resourceName(config, "rds", volumeId, instanceId));diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.javaindex 6dbd4a0acf..4a8d1f1e22 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java@@ -1,13 +1,11 @@package io.github.hectorvent.floci.services.redshift.container;-import com.github.dockerjava.api.async.ResultCallback;import com.github.dockerjava.api.model.Container;-import com.github.dockerjava.api.model.Frame;-import com.github.dockerjava.api.model.StreamType;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;@@ -27,8 +25,6 @@ import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.IOException;import java.io.InputStream;-import java.io.OutputStream;-import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.StandardCopyOption;@@ -36,7 +32,6 @@ import java.util.List;import java.util.Map;import java.util.Optional;import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;@ApplicationScoped@@ -181,7 +176,7 @@ public class RedshiftContainerManager {String[] cmd = new String[]{"pg_dump", "-U", effectiveUser, effectiveDb, "-f", "/tmp/dump.sql"};try {- ExecResult result = execInContainer(handle.getContainerId(), cmd, 30);+ ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 30);if (result.exitCode() != 0) {LOG.warnv("pg_dump failed for cluster {0} (exit {1}): {2}", clusterIdentifier, result.exitCode(), result.stderr());throw new AwsException("InternalFailure", "Failed to create snapshot for cluster " + clusterIdentifier + ": " + result.stderr(), 500);@@ -241,7 +236,7 @@ public class RedshiftContainerManager {String sql = "ALTER USER " + effectiveUser + " PASSWORD '" + newPassword + "'";String[] cmd = new String[]{"psql", "-U", effectiveUser, "-d", "dev", "-c", sql};try {- ExecResult result = execInContainer(handle.getContainerId(), cmd, 15);+ ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 15);if (result.exitCode() != 0) {// Deliberately omit result.stderr() from the log and the exception: psql echoes// the failing ALTER USER statement, which contains the password literal.@@ -285,7 +280,7 @@ public class RedshiftContainerManager {.exec();String[] cmd = new String[]{"psql", "-U", effectiveUser, "-d", effectiveDb, "-f", "/tmp/" + fileName};- ExecResult result = execInContainer(handle.getContainerId(), cmd, 60);+ ContainerExec.Result result = execInContainer(handle.getContainerId(), cmd, 60);if (result.exitCode() != 0) {LOG.warnv("psql restore failed for cluster {0} (exit {1}): {2}", clusterIdentifier, result.exitCode(), result.stderr());throw new AwsException("InternalFailure", "Failed to restore snapshot for cluster " + clusterIdentifier + ": " + result.stderr(), 500);@@ -309,70 +304,8 @@ public class RedshiftContainerManager {restoreSnapshot(accountId, cluster.getClusterIdentifier(), cluster.getMasterUsername(), "dev", sqlDumpFile);}- private ExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds) throws Exception {- ByteArrayOutputStream stdout = new ByteArrayOutputStream();- ExecResult res = execInContainer(containerId, cmd, timeoutSeconds, stdout);- return new ExecResult(res.exitCode(), stdout.toString(StandardCharsets.UTF_8), res.stderr());- }-- private ExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds, OutputStream out) throws Exception {- String execId = lifecycleManager.getDockerClient().execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec()- .getId();-- CountDownLatch latch = new CountDownLatch(1);- ByteArrayOutputStream stderr = new ByteArrayOutputStream();-- Closeable callback = lifecycleManager.getDockerClient().execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- byte[] payload = frame.getPayload();- if (payload == null) {- return;- }- try {- if (frame.getStreamType() == StreamType.STDERR) {- stderr.write(payload);- } else {- out.write(payload);- }- } catch (IOException e) {- LOG.warnv(e, "Failed to capture output of container exec {0}", execId);- }- }-- @Override- public void onComplete() {- latch.countDown();- }-- @Override- public void onError(Throwable t) {- LOG.warnv(t, "Container exec {0} failed", execId);- latch.countDown();- }- });-- try {- boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);- if (!completed) {- return new ExecResult(-1, "", "Timed out after " + timeoutSeconds + "s");- }- Long exitCode = lifecycleManager.getDockerClient().inspectExecCmd(execId).exec().getExitCodeLong();- return new ExecResult(- exitCode != null ? exitCode : -1,- "",- stderr.toString(StandardCharsets.UTF_8));- } finally {- try {- callback.close();- } catch (IOException ignored) {- // The exec result is already collected, so a failure to release the callback stream cannot change it.- }- }+ private ContainerExec.Result execInContainer(String containerId, String[] cmd, int timeoutSeconds) {+ return ContainerExec.run(lifecycleManager.getDockerClient(), containerId, cmd, timeoutSeconds);}private byte[] buildSingleFileTar(String filename, byte[] content, int mode) throws IOException {@@ -405,7 +338,7 @@ public class RedshiftContainerManager {String lastOutput = "";for (int attempt = 1; attempt <= 60; attempt++) {try {- ExecResult result = execInContainer(containerId, cmd, 5);+ ContainerExec.Result result = execInContainer(containerId, cmd, 5);lastOutput = result.stderr();if (result.exitCode() == 0) {LOG.infov("Initialized {0} in Redshift container {1}", description, containerName);@@ -455,7 +388,7 @@ public class RedshiftContainerManager {"-d", db,"-f", "/tmp/bootstrap-catalog.sql"};- ExecResult result = execInContainer(containerId, cmd, 30);+ ContainerExec.Result result = execInContainer(containerId, cmd, 30);if (result.exitCode() != 0) {LOG.warnv("Redshift catalog bootstrap for {0} exited with code {1}: {2}", db, result.exitCode(), result.stderr());} else {@@ -467,5 +400,4 @@ public class RedshiftContainerManager {}}- public record ExecResult(long exitCode, String stdout, String stderr) {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.javaindex 0e7b0c707c..0ff8216a74 100644--- a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java@@ -1,14 +1,11 @@package io.github.hectorvent.floci.services.timestreaminfluxdb.container;import com.fasterxml.jackson.databind.JsonNode;-import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.model.Frame;-import com.github.dockerjava.api.model.StreamType;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.ContainerTeardown;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;@@ -18,13 +15,10 @@ import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;-import java.io.ByteArrayOutputStream;-import java.io.Closeable;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URI;-import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.StandardCopyOption;@@ -34,7 +28,6 @@ import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;/**@@ -358,7 +351,7 @@ public class TimestreamInfluxDbContainerManager implements ContainerTeardown {}private void execOrThrow(String containerId, String[] command, String description) {- ExecResult result = exec(containerId, command);+ ContainerExec.Result result = exec(containerId, command);if (result.exitCode() != 0) {throw new IllegalStateException(description + " failed with exit code " + result.exitCode()+ ": " + result.stderr() + result.stdout());@@ -373,64 +366,7 @@ public class TimestreamInfluxDbContainerManager implements ContainerTeardown {}}- private ExecResult exec(String containerId, String[] command) {- DockerClient dockerClient = lifecycleManager.getDockerClient();- String execId = dockerClient.execCreateCmd(containerId)- .withCmd(command)- .withAttachStdout(true)- .withAttachStderr(true)- .exec()- .getId();- CountDownLatch latch = new CountDownLatch(1);- ByteArrayOutputStream stdout = new ByteArrayOutputStream();- ByteArrayOutputStream stderr = new ByteArrayOutputStream();- Closeable callback = dockerClient.execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame.getPayload() == null) {- return;- }- try {- if (frame.getStreamType() == StreamType.STDERR) {- stderr.write(frame.getPayload());- } else {- stdout.write(frame.getPayload());- }- } catch (IOException e) {- LOG.warnv(e, "Failed to read output of container exec {0}", execId);- }- }-- @Override- public void onComplete() {- latch.countDown();- }-- @Override- public void onError(Throwable throwable) {- LOG.warnv(throwable, "Container exec {0} failed", execId);- latch.countDown();- }- });- try {- if (!latch.await(EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {- return new ExecResult(-1, stdout.toString(StandardCharsets.UTF_8), "timed out");- }- Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();- return new ExecResult(exitCode != null ? exitCode : -1,- stdout.toString(StandardCharsets.UTF_8), stderr.toString(StandardCharsets.UTF_8));- } catch (InterruptedException e) {- Thread.currentThread().interrupt();- throw new IllegalStateException("Interrupted running command in container " + containerId, e);- } finally {- try {- callback.close();- } catch (IOException e) {- LOG.debugv("Failed to close exec callback {0}: {1}", execId, e.getMessage());- }- }- }-- private record ExecResult(long exitCode, String stdout, String stderr) {+ private ContainerExec.Result exec(String containerId, String[] command) {+ return ContainerExec.run(lifecycleManager.getDockerClient(), containerId, command, EXEC_TIMEOUT_SECONDS);}}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerExecStubs.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerExecStubs.javanew file mode 100644index 0000000000..9b7b0f7329--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerExecStubs.java@@ -0,0 +1,69 @@+package io.github.hectorvent.floci.core.common.docker;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.async.ResultCallback;+import com.github.dockerjava.api.command.ExecCreateCmd;+import com.github.dockerjava.api.command.ExecCreateCmdResponse;+import com.github.dockerjava.api.command.ExecStartCmd;+import com.github.dockerjava.api.command.InspectExecCmd;+import com.github.dockerjava.api.command.InspectExecResponse;+import com.github.dockerjava.api.model.Frame;+import com.github.dockerjava.api.model.StreamType;++import java.nio.charset.StandardCharsets;+import java.util.ArrayList;+import java.util.Arrays;+import java.util.List;++import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.RETURNS_SELF;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++/**+ * Stubs a mocked {@link DockerClient} so every {@link ContainerExec} run in one container completes+ * at once with a fixed output and exit code, for tests of the managers that call it.+ */+public final class ContainerExecStubs {++ private ContainerExecStubs() {+ }++ /** Returns the commands run in {@code containerId}, in order, as the stubbed execs see them. */+ public static List<List<String>> completeEveryExec(DockerClient dockerClient, String containerId, long exitCode,+ String stdout, String stderr) {+ String execId = "exec-" + containerId;+ List<List<String>> commands = new ArrayList<>();++ ExecCreateCmd createCmd = mock(ExecCreateCmd.class, RETURNS_SELF);+ when(createCmd.withCmd(any(String[].class))).thenAnswer(invocation -> {+ Object[] args = invocation.getArguments();+ String[] cmd = args.length == 1 && args[0] instanceof String[] array+ ? array+ : Arrays.copyOf(args, args.length, String[].class);+ commands.add(List.of(cmd));+ return createCmd;+ });+ ExecCreateCmdResponse createResponse = mock(ExecCreateCmdResponse.class);+ when(createResponse.getId()).thenReturn(execId);+ when(createCmd.exec()).thenReturn(createResponse);+ when(dockerClient.execCreateCmd(containerId)).thenReturn(createCmd);++ ExecStartCmd startCmd = mock(ExecStartCmd.class);+ when(startCmd.exec(any())).thenAnswer(invocation -> {+ ResultCallback.Adapter<Frame> callback = invocation.getArgument(0);+ callback.onNext(new Frame(StreamType.STDOUT, stdout.getBytes(StandardCharsets.UTF_8)));+ callback.onNext(new Frame(StreamType.STDERR, stderr.getBytes(StandardCharsets.UTF_8)));+ callback.onComplete();+ return callback;+ });+ when(dockerClient.execStartCmd(execId)).thenReturn(startCmd);++ InspectExecCmd inspectCmd = mock(InspectExecCmd.class);+ InspectExecResponse inspectResponse = mock(InspectExecResponse.class);+ when(inspectResponse.getExitCodeLong()).thenReturn(exitCode);+ when(inspectCmd.exec()).thenReturn(inspectResponse);+ when(dockerClient.inspectExecCmd(execId)).thenReturn(inspectCmd);+ return commands;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerExecTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerExecTest.javanew file mode 100644index 0000000000..88c316dbb5--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerExecTest.java@@ -0,0 +1,205 @@+package io.github.hectorvent.floci.core.common.docker;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.async.ResultCallback;+import com.github.dockerjava.api.command.ExecCreateCmd;+import com.github.dockerjava.api.command.ExecCreateCmdResponse;+import com.github.dockerjava.api.command.ExecStartCmd;+import com.github.dockerjava.api.command.InspectExecCmd;+import com.github.dockerjava.api.command.InspectExecResponse;+import com.github.dockerjava.api.model.Frame;+import com.github.dockerjava.api.model.StreamType;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.nio.charset.StandardCharsets;+import java.util.List;+import java.util.function.Consumer;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertSame;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.RETURNS_SELF;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++/**+ * Pins the contract every container manager now shares: how frames are routed, what a timeout and+ * a missing exit code turn into, and that an attach-stream error or an interrupt is not mistaken+ * for a finished command. None of it needs a daemon, so the docker-java calls are mocked.+ */+class ContainerExecTest {++ private static final String CONTAINER_ID = "container-1";+ private static final String EXEC_ID = "exec-1";+ private static final String[] CMD = {"sh", "-c", "true"};++ private DockerClient dockerClient;+ private ExecStartCmd execStartCmd;+ private InspectExecResponse inspectResponse;++ @BeforeEach+ void setUp() {+ dockerClient = mock(DockerClient.class);+ ExecCreateCmd createCmd = mock(ExecCreateCmd.class, RETURNS_SELF);+ ExecCreateCmdResponse createResponse = mock(ExecCreateCmdResponse.class);+ when(createResponse.getId()).thenReturn(EXEC_ID);+ when(createCmd.exec()).thenReturn(createResponse);+ when(dockerClient.execCreateCmd(CONTAINER_ID)).thenReturn(createCmd);++ execStartCmd = mock(ExecStartCmd.class);+ when(dockerClient.execStartCmd(EXEC_ID)).thenReturn(execStartCmd);++ InspectExecCmd inspectCmd = mock(InspectExecCmd.class);+ inspectResponse = mock(InspectExecResponse.class);+ when(inspectResponse.getExitCodeLong()).thenReturn(0L);+ when(inspectCmd.exec()).thenReturn(inspectResponse);+ when(dockerClient.inspectExecCmd(EXEC_ID)).thenReturn(inspectCmd);+ }++ @Test+ void runKeepsStdoutAndStderrApart() {+ completeWith(List.of(frame(StreamType.STDOUT, "out-1 "), frame(StreamType.STDERR, "err"),+ frame(StreamType.STDOUT, "out-2")));+ when(inspectResponse.getExitCodeLong()).thenReturn(3L);++ ContainerExec.Result result = ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 5);++ assertEquals(3L, result.exitCode());+ assertEquals("out-1 out-2", result.stdout());+ assertEquals("err", result.stderr());+ assertFalse(result.timedOut());+ }++ @Test+ void runMergedInterleavesBothStreamsInArrivalOrder() {+ completeWith(List.of(frame(StreamType.STDOUT, "a\n"), frame(StreamType.STDERR, "b\n"),+ frame(StreamType.STDOUT, "c\n")));++ ContainerExec.Result result = ContainerExec.runMerged(dockerClient, CONTAINER_ID, CMD, 5);++ assertEquals("a\nb\nc\n", result.stdout());+ assertEquals("", result.stderr());+ assertEquals("a\nb\nc", result.summary());+ }++ @Test+ void aMultiByteCharacterSplitAcrossFramesSurvives() {+ byte[] euro = "€".getBytes(StandardCharsets.UTF_8);+ completeWith(List.of(new Frame(StreamType.STDOUT, new byte[]{euro[0]}),+ new Frame(StreamType.STDOUT, new byte[]{euro[1], euro[2]})));++ assertEquals("€", ContainerExec.runMerged(dockerClient, CONTAINER_ID, CMD, 5).stdout());+ }++ @Test+ void aMissingExitCodeIsReportedAsMinusOne() {+ completeWith(List.of());+ when(inspectResponse.getExitCodeLong()).thenReturn(null);++ assertEquals(-1L, ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 5).exitCode());+ }++ @Test+ void aCommandThatOutlivesItsTimeoutIsReportedWithWhatItPrintedSoFar() {+ startWith(callback -> {+ callback.onNext(frame(StreamType.STDOUT, "partial"));+ callback.onNext(frame(StreamType.STDERR, "warning"));+ });++ ContainerExec.Result result = ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 0);++ assertTrue(result.timedOut());+ assertEquals(-1L, result.exitCode());+ assertEquals("partial", result.stdout());+ assertEquals("warning\nTimed out after 0s", result.stderr());+ verify(dockerClient, never()).inspectExecCmd(EXEC_ID);+ }++ @Test+ void aTimedOutMergedRunSummarisesAsTheTimeout() {+ startWith(callback -> {+ });++ ContainerExec.Result result = ContainerExec.runMerged(dockerClient, CONTAINER_ID, CMD, 0);++ assertTrue(result.timedOut());+ assertEquals("Timed out after 0s", result.summary());+ }++ @Test+ void throwIfTimedOutNamesTheContainer() {+ startWith(callback -> {+ });++ ContainerExec.Result result = ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 0);++ RuntimeException error = assertThrows(RuntimeException.class, () -> result.throwIfTimedOut(CONTAINER_ID));+ assertEquals("exec timed out in container " + CONTAINER_ID, error.getMessage());+ }++ @Test+ void throwIfTimedOutPassesAFinishedResultThrough() {+ completeWith(List.of());+ when(inspectResponse.getExitCodeLong()).thenReturn(1L);++ ContainerExec.Result result = ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 5);++ assertSame(result, result.throwIfTimedOut(CONTAINER_ID));+ }++ @Test+ void anEmptyRunSummarisesAsNoOutput() {+ completeWith(List.of());++ assertEquals("(no output)", ContainerExec.runMerged(dockerClient, CONTAINER_ID, CMD, 5).summary());+ }++ @Test+ void anAttachStreamErrorPropagatesInsteadOfReadingAnExitCode() {+ startWith(callback -> callback.onError(new IllegalStateException("attach stream broke")));++ IllegalStateException error = assertThrows(IllegalStateException.class,+ () -> ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 5));++ assertEquals("attach stream broke", error.getMessage());+ verify(dockerClient, never()).inspectExecCmd(EXEC_ID);+ }++ @Test+ void anInterruptIsRaisedAndTheInterruptFlagKept() {+ startWith(callback -> {+ });+ Thread.currentThread().interrupt();+ try {+ assertThrows(IllegalStateException.class, () -> ContainerExec.run(dockerClient, CONTAINER_ID, CMD, 5));+ assertTrue(Thread.currentThread().isInterrupted());+ } finally {+ Thread.interrupted();+ }+ }++ private void completeWith(List<Frame> frames) {+ startWith(callback -> {+ frames.forEach(callback::onNext);+ callback.onComplete();+ });+ }++ private void startWith(Consumer<ResultCallback.Adapter<Frame>> behaviour) {+ when(execStartCmd.exec(any())).thenAnswer(invocation -> {+ ResultCallback.Adapter<Frame> callback = invocation.getArgument(0);+ behaviour.accept(callback);+ return callback;+ });+ }++ private static Frame frame(StreamType type, String payload) {+ return new Frame(type, payload.getBytes(StandardCharsets.UTF_8));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.javaindex 724612d1d1..319fb0f789 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java@@ -76,6 +76,7 @@ import static org.mockito.ArgumentMatchers.eq;import static org.mockito.Answers.RETURNS_SELF;import static org.mockito.Answers.RETURNS_DEEP_STUBS;import static org.mockito.Mockito.after;+import static org.mockito.Mockito.doAnswer;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.atLeastOnce;@@ -1331,6 +1332,68 @@ class Ec2ContainerManagerTest {+ "\"Missing privilege separation directory\"");}+ @Test+ void anAttachStreamErrorOnOneSshdPathStillTriesTheNext() throws Exception {+ LaunchHarness harness = launchHarness();+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ InspectContainerResponse withIp = inspectResponse("172.18.0.21");+ when(harness.dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenReturn(withIp);+ harness.stubSuccessfulExecs(new CountDownLatch(0), new CountDownLatch(0));+ doAnswer(invocation -> {+ ExecStartCmd execStart = mock(ExecStartCmd.class);+ when(execStart.exec(any())).thenAnswer(startInvocation -> {+ ResultCallback<Frame> callback = startInvocation.getArgument(0);+ String[] command = harness.executedCommands.get(harness.executedCommands.size() - 1);+ if (Arrays.equals(command, new String[]{"/usr/sbin/sshd"})) {+ callback.onError(new IllegalStateException("attach stream broke"));+ } else {+ callback.onComplete();+ }+ return callback;+ });+ return execStart;+ }).when(harness.dockerClient).execStartCmd(anyString());++ harness.manager.launch(instance("i-sshd-retry"), "ubuntu:24.04", null, "us-west-2");++ awaitUntil(() -> commandIndex(harness.executedCommands, "/usr/local/sbin/sshd") >= 0, Duration.ofSeconds(2));+ assertTrue(commandIndex(harness.executedCommands, "/usr/sbin/sshd")+ < commandIndex(harness.executedCommands, "/usr/local/sbin/sshd"));+ }++ @Test+ void anInterruptWhileStartingSshdStopsTryingFurtherSshdPaths() throws Exception {+ LaunchHarness harness = launchHarness();+ InspectContainerCmd inspect = mock(InspectContainerCmd.class);+ InspectContainerResponse withIp = inspectResponse("172.18.0.22");+ when(harness.dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);+ when(inspect.exec()).thenReturn(withIp);+ harness.stubSuccessfulExecs(new CountDownLatch(0), new CountDownLatch(0));+ doAnswer(invocation -> {+ ExecStartCmd execStart = mock(ExecStartCmd.class);+ when(execStart.exec(any())).thenAnswer(startInvocation -> {+ ResultCallback<Frame> callback = startInvocation.getArgument(0);+ String[] command = harness.executedCommands.get(harness.executedCommands.size() - 1);+ if (Arrays.equals(command, new String[]{"/usr/sbin/sshd"})) {+ Thread.currentThread().interrupt();+ } else {+ callback.onComplete();+ }+ return callback;+ });+ return execStart;+ }).when(harness.dockerClient).execStartCmd(anyString());+ Instance instance = instance("i-sshd-interrupted");+ instance.setUserData("#!/bin/sh\necho ready\n");++ harness.manager.launch(instance, "ubuntu:24.04", null, "us-west-2");++ // UserData is the next step after sshd, so its exec marks sshd startup as finished.+ awaitUntil(() -> commandIndex(harness.executedCommands, "/var/lib/user-data.sh") >= 0, Duration.ofSeconds(2));+ assertEquals(-1, commandIndex(harness.executedCommands, "/usr/local/sbin/sshd"));+ }+@Testvoid launchWithoutKeyPairStillStartsSshd() throws Exception {// Regression for #2184: sshd previously only started when a key pair was supplied, sodiff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.javaindex 9810493b0f..14e58f23e1 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java@@ -1,12 +1,10 @@package io.github.hectorvent.floci.services.ec2;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;@@ -25,13 +23,12 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.List;import java.util.Map;+import java.util.Objects;import java.util.Optional;import java.util.UUID;-import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -177,22 +174,22 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {String targetContainerId = instance.getDockerContainerId();// 1. Device node exists in container- ExecResult checkDev = execIn(targetContainerId, "test -b /dev/xvdf || test -L /dev/xvdf");- assertEquals(0, checkDev.exitCode(), "Device /dev/xvdf should exist as block or symlink: " + checkDev.output());+ ContainerExec.Result checkDev = execIn(targetContainerId, "test -b /dev/xvdf || test -L /dev/xvdf");+ assertEquals(0, checkDev.exitCode(), "Device /dev/xvdf should exist as block or symlink: " + checkDev.summary());// 2. Size matches 1 GiB (1073741824 bytes)- ExecResult checkSize = execIn(targetContainerId, "blockdev --getsize64 /dev/xvdf");- assertEquals(0, checkSize.exitCode(), "blockdev query should succeed: " + checkSize.output());- assertEquals("1073741824", checkSize.output().trim());+ ContainerExec.Result checkSize = execIn(targetContainerId, "blockdev --getsize64 /dev/xvdf");+ assertEquals(0, checkSize.exitCode(), "blockdev query should succeed: " + checkSize.summary());+ assertEquals("1073741824", checkSize.stdout().trim());// 3. Format filesystem, mount, write data, unmount- ExecResult mkfs = execIn(targetContainerId, "mkfs.ext4 -F /dev/xvdf");- assertEquals(0, mkfs.exitCode(), "mkfs.ext4 should succeed: " + mkfs.output());+ ContainerExec.Result mkfs = execIn(targetContainerId, "mkfs.ext4 -F /dev/xvdf");+ assertEquals(0, mkfs.exitCode(), "mkfs.ext4 should succeed: " + mkfs.summary());- ExecResult mountAndWrite = execIn(targetContainerId,+ ContainerExec.Result mountAndWrite = execIn(targetContainerId,"mkdir -p /mnt/vol && mount /dev/xvdf /mnt/vol && echo 'floci-ebs-test-data' > /mnt/vol/test.txt && cat /mnt/vol/test.txt && umount /mnt/vol");- assertEquals(0, mountAndWrite.exitCode(), "Mount, write, read and umount should succeed: " + mountAndWrite.output());- assertTrue(mountAndWrite.output().contains("floci-ebs-test-data"));+ assertEquals(0, mountAndWrite.exitCode(), "Mount, write, read and umount should succeed: " + mountAndWrite.summary());+ assertTrue(mountAndWrite.stdout().contains("floci-ebs-test-data"));}@Test@@ -206,7 +203,7 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {ec2Service.attachVolume("us-east-1", volume.getVolumeId(), instance.getInstanceId(), "/dev/xvdf");String targetContainerId = instance.getDockerContainerId();- ExecResult existsBefore = execIn(targetContainerId, "test -e /dev/xvdf");+ ContainerExec.Result existsBefore = execIn(targetContainerId, "test -e /dev/xvdf");assertEquals(0, existsBefore.exitCode());// Detach volume@@ -214,7 +211,7 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {assertEquals("detached", detached.getState());// Device node should be removed from container- ExecResult existsAfter = execIn(targetContainerId, "test ! -e /dev/xvdf");+ ContainerExec.Result existsAfter = execIn(targetContainerId, "test ! -e /dev/xvdf");assertEquals(0, existsAfter.exitCode(), "Device /dev/xvdf should no longer exist in container");// Delete volume@@ -223,7 +220,7 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {// Verify helper container removed backing fileString helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");- ExecResult checkFile = execIn(helperName, "test ! -f /volumes/" + volume.getVolumeId() + ".raw");+ ContainerExec.Result checkFile = execIn(helperName, "test ! -f /volumes/" + volume.getVolumeId() + ".raw");assertEquals(0, checkFile.exitCode(), "Backing file should be removed on volume deletion");}@@ -246,9 +243,9 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {assertEquals("VolumeInUse", ex.getErrorCode());// Format and write data on instance1- ExecResult formatAndWrite = execIn(instance1.getDockerContainerId(),+ ContainerExec.Result formatAndWrite = execIn(instance1.getDockerContainerId(),"mkfs.ext4 -F /dev/xvdf && mkdir -p /mnt/vol1 && mount /dev/xvdf /mnt/vol1 && echo 'persisted-payload' > /mnt/vol1/hello.txt && umount /mnt/vol1");- assertEquals(0, formatAndWrite.exitCode(), formatAndWrite.output());+ assertEquals(0, formatAndWrite.exitCode(), formatAndWrite.summary());// Detach from instance1ec2Service.detachVolume("us-east-1", volume.getVolumeId(), instance1.getInstanceId(), "/dev/xvdf", false);@@ -257,10 +254,10 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {ec2Service.attachVolume("us-east-1", volume.getVolumeId(), instance2.getInstanceId(), "/dev/xvdg");// Verify device exists on instance2 and data is preserved- ExecResult verifyData = execIn(instance2.getDockerContainerId(),+ ContainerExec.Result verifyData = execIn(instance2.getDockerContainerId(),"mkdir -p /mnt/vol2 && mount /dev/xvdg /mnt/vol2 && cat /mnt/vol2/hello.txt && umount /mnt/vol2");- assertEquals(0, verifyData.exitCode(), "Data should be preserved across detach and re-attach: " + verifyData.output());- assertTrue(verifyData.output().contains("persisted-payload"));+ assertEquals(0, verifyData.exitCode(), "Data should be preserved across detach and re-attach: " + verifyData.summary());+ assertTrue(verifyData.stdout().contains("persisted-payload"));}@Test@@ -276,7 +273,7 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {// Simulate device node loss (e.g. container reboot or tmpfs recreation)execIn(targetContainerId, "rm -f /dev/xvdf");- ExecResult deleted = execIn(targetContainerId, "test ! -e /dev/xvdf");+ ContainerExec.Result deleted = execIn(targetContainerId, "test ! -e /dev/xvdf");assertEquals(0, deleted.exitCode());// Call restoreInstanceVolumeDevices@@ -284,8 +281,8 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {volId -> Optional.of(volume));// Verify device node is restored- ExecResult restored = execIn(targetContainerId, "test -b /dev/xvdf || test -L /dev/xvdf");- assertEquals(0, restored.exitCode(), "Device /dev/xvdf should be restored: " + restored.output());+ ContainerExec.Result restored = execIn(targetContainerId, "test -b /dev/xvdf || test -L /dev/xvdf");+ assertEquals(0, restored.exitCode(), "Device /dev/xvdf should be restored: " + restored.summary());}@Test@@ -333,8 +330,8 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {VolumeAttachment attachment = ec2Service.attachVolume("us-east-1", volume.getVolumeId(), instanceId, "/dev/xvdf");assertNotNull(attachment);- ExecResult checkDev = execIn(containerId, "test -b /dev/xvdf || test -L /dev/xvdf");- assertEquals(0, checkDev.exitCode(), "Block device should exist inside cluster node container: " + checkDev.output());+ ContainerExec.Result checkDev = execIn(containerId, "test -b /dev/xvdf || test -L /dev/xvdf");+ assertEquals(0, checkDev.exitCode(), "Block device should exist inside cluster node container: " + checkDev.summary());ec2Service.setClusterNodeInstanceProvider(null);}@@ -396,35 +393,11 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {assertTrue(ex.getMessage().contains("already in use by volume"));}- private ExecResult execIn(String containerIdOrName, String command) {+ private ContainerExec.Result execIn(String containerIdOrName, String command) {try {- ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerIdOrName)- .withCmd("sh", "-c", command)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- StringBuilder sb = new StringBuilder();- boolean done = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- sb.append(new String(frame.getPayload(), StandardCharsets.UTF_8));- }- }- })- .awaitCompletion(30, TimeUnit.SECONDS);-- if (!done) {- return new ExecResult(-1, "Timed out: " + sb);- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ExecResult(exitCode != null ? exitCode : -1, sb.toString());- } catch (Exception e) {- return new ExecResult(-1, e.getMessage());+ return ContainerExec.runMerged(dockerClient, containerIdOrName, new String[]{"sh", "-c", command}, 30);+ } catch (RuntimeException e) {+ return new ContainerExec.Result(-1, "", Objects.requireNonNullElse(e.getMessage(), e.toString()), false);}}-- private record ExecResult(long exitCode, String output) {}}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.javanew file mode 100644index 0000000000..56ca8e50eb--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.java@@ -0,0 +1,63 @@+package io.github.hectorvent.floci.services.ec2;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.InspectContainerResponse;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExecStubs;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.util.List;++import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++/**+ * Covers how the volume manager drives its helper container without a Docker daemon: the command+ * it runs there, and that a Docker failure while running it is reported rather than thrown.+ */+class Ec2VolumeBlockDeviceManagerTest {++ private static final String HELPER_ID = "helper-container-id";++ private DockerClient dockerClient;+ private Ec2VolumeBlockDeviceManager manager;++ @BeforeEach+ void setUp() {+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ when(config.services().ec2().volumeBlockDevices()).thenReturn(true);+ when(config.services().ec2().mock()).thenReturn(false);++ dockerClient = mock(DockerClient.class, RETURNS_DEEP_STUBS);+ InspectContainerResponse helper = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);+ when(helper.getState().getRunning()).thenReturn(true);+ when(helper.getId()).thenReturn(HELPER_ID);+ when(dockerClient.inspectContainerCmd(anyString()).exec()).thenReturn(helper);++ manager = new Ec2VolumeBlockDeviceManager(dockerClient, mock(ContainerBuilder.class),+ mock(ContainerLifecycleManager.class), config);+ }++ @Test+ void createVolumeSizesTheBackingFileInsideTheHelperContainer() {+ List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, HELPER_ID, 0, "", "");++ manager.createVolume("vol-1", 0);++ assertEquals(List.of(List.of("sh", "-c", "truncate -s 8G /volumes/vol-1.raw")), commands);+ }++ @Test+ void aDockerFailureWhileRunningTheCommandIsNotThrown() {+ when(dockerClient.execCreateCmd(HELPER_ID)).thenThrow(new IllegalStateException("daemon went away"));++ assertDoesNotThrow(() -> manager.createVolume("vol-1", 8));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyTest.javaindex 3bc7cc96f6..712e12838c 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyTest.java@@ -8,6 +8,7 @@ import com.github.dockerjava.api.command.ListContainersCmd;import com.github.dockerjava.api.model.Container;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExecStubs;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;@@ -218,25 +219,7 @@ class EcsCredentialsProxyTest {when(inspectResponse.getState()).thenReturn(running);when(running.getRunning()).thenReturn(true);- com.github.dockerjava.api.command.ExecCreateCmd execCreate =- mock(com.github.dockerjava.api.command.ExecCreateCmd.class, RETURNS_SELF);- com.github.dockerjava.api.command.ExecCreateCmdResponse execResponse =- mock(com.github.dockerjava.api.command.ExecCreateCmdResponse.class);- when(dockerClient.execCreateCmd(containerId)).thenReturn(execCreate);- when(execCreate.exec()).thenReturn(execResponse);- when(execResponse.getId()).thenReturn("exec-" + containerId);- com.github.dockerjava.api.command.ExecStartCmd execStart =- mock(com.github.dockerjava.api.command.ExecStartCmd.class);- when(dockerClient.execStartCmd("exec-" + containerId)).thenReturn(execStart);- when(execStart.exec(any())).thenAnswer(invocation -> {- com.github.dockerjava.api.async.ResultCallback<com.github.dockerjava.api.model.Frame> callback =- invocation.getArgument(0);- callback.onNext(new com.github.dockerjava.api.model.Frame(- com.github.dockerjava.api.model.StreamType.STDOUT,- "404".getBytes()));- callback.onComplete();- return callback;- });+ ContainerExecStubs.completeEveryExec(dockerClient, containerId, 0, "404", "");}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.javaindex 47ac97bc24..b41d66694c 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksImdsDockerIntegrationTest.java@@ -1,11 +1,9 @@package io.github.hectorvent.floci.services.eks;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;@@ -21,11 +19,9 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.nio.charset.StandardCharsets;import java.util.List;import java.util.Map;import java.util.UUID;-import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotEquals;@@ -185,7 +181,7 @@ class EksImdsDockerIntegrationTest {// and fails because port 80 is redirected to 9999.execInContainer(containerId, new String[]{"sh", "-c","iptables -t nat -D PREROUTING -j FLOCI-LINK-LOCAL"});- ExecResult intercepted = execInContainerWithExitCode(containerId, new String[]{"sh", "-c",+ ContainerExec.Result intercepted = execInContainerWithExitCode(containerId, new String[]{"sh", "-c","ip netns exec pod-test wget -q -O - -T 2 http://169.254.169.254/latest/meta-data/instance-id"});assertNotEquals(0, intercepted.exitCode(),"Without FLOCI-LINK-LOCAL chain, pod request should be intercepted by CNI-HOSTPORT-DNAT and fail");@@ -217,42 +213,12 @@ class EksImdsDockerIntegrationTest {}}- record ExecResult(long exitCode, String stdout, String stderr) {}-- private ExecResult execInContainerWithExitCode(String containerId, String[] cmd) throws Exception {- ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- StringBuilder stdout = new StringBuilder();- StringBuilder stderr = new StringBuilder();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- String text = new String(frame.getPayload(), StandardCharsets.UTF_8);- if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDERR) {- stderr.append(text);- } else {- stdout.append(text);- }- }- }- })- .awaitCompletion(30, TimeUnit.SECONDS);-- if (!completed) {- throw new RuntimeException("exec timed out in container " + containerId);- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ExecResult(exitCode != null ? exitCode : -1L, stdout.toString(), stderr.toString());+ private ContainerExec.Result execInContainerWithExitCode(String containerId, String[] cmd) {+ return ContainerExec.run(dockerClient, containerId, cmd, 30).throwIfTimedOut(containerId);}private String execInContainer(String containerId, String[] cmd) throws Exception {- ExecResult result = execInContainerWithExitCode(containerId, cmd);+ ContainerExec.Result result = execInContainerWithExitCode(containerId, cmd);if (result.exitCode() != 0) {throw new RuntimeException("exec failed with code " + result.exitCode() + ": " + result.stderr());}diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.javaindex d097a9ab30..f3a1bed37b 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java@@ -1,9 +1,7 @@package io.github.hectorvent.floci.services.eks;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;-import com.github.dockerjava.api.model.Frame;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.services.eks.model.Cluster;import io.quarkus.test.junit.QuarkusTest;import io.quarkus.test.junit.QuarkusTestProfile;@@ -15,11 +13,9 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.nio.charset.StandardCharsets;import java.time.Instant;import java.util.Map;import java.util.UUID;-import java.util.concurrent.TimeUnit;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;@@ -152,7 +148,7 @@ class EksIrsaDockerIntegrationTest {long saDeadline = System.currentTimeMillis() + 30000;boolean saReady = false;while (System.currentTimeMillis() < saDeadline) {- ExecResult saResult = execInContainerWithExitCode(containerId,+ ContainerExec.Result saResult = execInContainerWithExitCode(containerId,new String[]{"kubectl", "get", "serviceaccount", "default"});if (saResult.exitCode() == 0) {saReady = true;@@ -195,7 +191,7 @@ class EksIrsaDockerIntegrationTest {long podDeadline = System.currentTimeMillis() + 45000;boolean podRunning = false;while (System.currentTimeMillis() < podDeadline) {- ExecResult statusResult = execInContainerWithExitCode(containerId,+ ContainerExec.Result statusResult = execInContainerWithExitCode(containerId,new String[]{"kubectl", "get", "pod", "irsa-workload-pod", "-o", "jsonpath={.status.phase}"});if ("Running".equalsIgnoreCase(statusResult.stdout().trim())) {podRunning = true;@@ -207,20 +203,20 @@ class EksIrsaDockerIntegrationTest {// The pod is Running, so it was scheduled onto the node, which the kubelet// registered with its provider ID: the node is guaranteed to exist here.- ExecResult nodeResult = execInContainerWithExitCode(containerId,+ ContainerExec.Result nodeResult = execInContainerWithExitCode(containerId,new String[]{"kubectl", "get", "nodes", "-o", "jsonpath={.items[0].spec.providerID}"});assertEquals(0, nodeResult.exitCode(), "kubectl get nodes failed");assertEquals(eksClusterManager.deriveClusterNodeProviderId(cluster), nodeResult.stdout().trim(),"Node providerID must match the derived AWS provider ID");- ExecResult zoneResult = execInContainerWithExitCode(containerId,+ ContainerExec.Result zoneResult = execInContainerWithExitCode(containerId,new String[]{"kubectl", "get", "nodes", "-o","jsonpath={.items[0].metadata.labels.topology\\.kubernetes\\.io/zone}"});assertEquals(0, zoneResult.exitCode(), "kubectl get nodes failed");assertEquals(eksClusterManager.deriveClusterNodeAvailabilityZone(cluster), zoneResult.stdout().trim(),"Node topology zone label must match the derived availability zone");- ExecResult regionResult = execInContainerWithExitCode(containerId,+ ContainerExec.Result regionResult = execInContainerWithExitCode(containerId,new String[]{"kubectl", "get", "nodes", "-o","jsonpath={.items[0].metadata.labels.topology\\.kubernetes\\.io/region}"});assertEquals(0, regionResult.exitCode(), "kubectl get nodes failed");@@ -362,42 +358,12 @@ class EksIrsaDockerIntegrationTest {}}- record ExecResult(long exitCode, String stdout, String stderr) {}-- private ExecResult execInContainerWithExitCode(String containerId, String[] cmd) throws Exception {- ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- StringBuilder stdout = new StringBuilder();- StringBuilder stderr = new StringBuilder();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- String text = new String(frame.getPayload(), StandardCharsets.UTF_8);- if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDERR) {- stderr.append(text);- } else {- stdout.append(text);- }- }- }- })- .awaitCompletion(30, TimeUnit.SECONDS);-- if (!completed) {- throw new RuntimeException("exec timed out in container " + containerId);- }- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- return new ExecResult(exitCode != null ? exitCode : -1L, stdout.toString(), stderr.toString());+ private ContainerExec.Result execInContainerWithExitCode(String containerId, String[] cmd) {+ return ContainerExec.run(dockerClient, containerId, cmd, 30).throwIfTimedOut(containerId);}private String execInContainer(String containerId, String[] cmd) throws Exception {- ExecResult result = execInContainerWithExitCode(containerId, cmd);+ ContainerExec.Result result = execInContainerWithExitCode(containerId, cmd);if (result.exitCode() != 0) {throw new RuntimeException("exec failed with code " + result.exitCode() + ": " + result.stderr());}diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityDockerIntegrationTest.javaindex 8b512e3093..a18a51bb2e 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityDockerIntegrationTest.java@@ -1,11 +1,9 @@package io.github.hectorvent.floci.services.eks;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;@@ -26,12 +24,10 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.nio.charset.StandardCharsets;import java.time.Instant;import java.util.List;import java.util.Map;import java.util.UUID;-import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -189,21 +185,7 @@ class EksPodIdentityDockerIntegrationTest {}}- private String execInContainer(String containerId, String cmd) throws Exception {- ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)- .withCmd("sh", "-c", cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();- StringBuilder stdout = new StringBuilder();- dockerClient.execStartCmd(exec.getId()).exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- stdout.append(new String(frame.getPayload(), StandardCharsets.UTF_8));- }- }- }).awaitCompletion(30, TimeUnit.SECONDS);- return stdout.toString();+ private String execInContainer(String containerId, String cmd) {+ return ContainerExec.runMerged(dockerClient, containerId, new String[]{"sh", "-c", cmd}, 30).stdout();}}diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksRoute53DnsDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksRoute53DnsDockerIntegrationTest.javaindex b5de1011fd..354cb852b3 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksRoute53DnsDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksRoute53DnsDockerIntegrationTest.java@@ -1,12 +1,9 @@package io.github.hectorvent.floci.services.eks;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;-import com.github.dockerjava.api.model.Frame;-import com.github.dockerjava.api.model.StreamType;import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.services.route53.Route53Service;@@ -23,13 +20,11 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.UUID;-import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -156,31 +151,11 @@ class EksRoute53DnsDockerIntegrationTest {}}- private String execInContainer(String containerId, String[] cmd) throws Exception {- ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)- .withCmd(cmd).withAttachStdout(true).withAttachStderr(true).exec();-- StringBuilder stdout = new StringBuilder();- StringBuilder stderr = new StringBuilder();- boolean completed = dockerClient.execStartCmd(exec.getId())- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame != null && frame.getPayload() != null) {- String text = new String(frame.getPayload(), StandardCharsets.UTF_8);- (frame.getStreamType() == StreamType.STDERR ? stderr : stdout).append(text);- }- }- })- .awaitCompletion(30, TimeUnit.SECONDS);-- if (!completed) {- throw new RuntimeException("exec timed out in container " + containerId);+ private String execInContainer(String containerId, String[] cmd) {+ ContainerExec.Result result = ContainerExec.run(dockerClient, containerId, cmd, 30).throwIfTimedOut(containerId);+ if (result.exitCode() != 0) {+ throw new RuntimeException("exec failed with code " + result.exitCode() + ": " + result.stderr());}- Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();- if (exitCode == null || exitCode != 0) {- throw new RuntimeException("exec failed with code " + exitCode + ": " + stderr);- }- return stdout.toString();+ return result.stdout();}}diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.javaindex fa71fb50fd..7171fc5e03 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.java@@ -1,10 +1,8 @@package io.github.hectorvent.floci.services.eks;import com.github.dockerjava.api.DockerClient;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.ExecCreateCmdResponse;-import com.github.dockerjava.api.model.Frame;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.quarkus.test.junit.QuarkusTest;@@ -15,12 +13,10 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.nio.charset.StandardCharsets;import java.util.List;import java.util.Optional;import java.util.Set;import java.util.UUID;-import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -119,23 +115,7 @@ class EksVpcRouteProgrammingDockerIntegrationTest {}}- private String execInContainer(String containerId, String[] cmd) throws Exception {- ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec();-- StringBuilder output = new StringBuilder();- dockerClient.execStartCmd(exec.getId()).exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame item) {- if (item != null && item.getPayload() != null) {- output.append(new String(item.getPayload(), StandardCharsets.UTF_8));- }- }- }).awaitCompletion(30, TimeUnit.SECONDS);-- return output.toString();+ private String execInContainer(String containerId, String[] cmd) {+ return ContainerExec.runMerged(dockerClient, containerId, cmd, 30).stdout();}}diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.javaindex 17a967c9e6..44defd139a 100644--- a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExecStubs;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;@@ -17,6 +18,7 @@ import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication;+import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot;import io.github.hectorvent.floci.services.s3.S3Service;import io.github.hectorvent.floci.services.s3.model.S3Object;import org.junit.jupiter.api.BeforeEach;@@ -138,6 +140,21 @@ class FlinkContainerManagerTest {return storage;}+ @Test+ void deleteSnapshotFilesRemovesTheSavepointInsideTheJobManager() {+ DockerClient dockerClient = mock(DockerClient.class);+ when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);+ List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, "jm-container-id", 0, "", "");+ FlinkApplication app = application("my-app");+ app.setContainerId("jm-container-id");+ Snapshot snapshot = new Snapshot();+ snapshot.setFlinkLocation("/savepoints/savepoint-1");++ manager.deleteSnapshotFiles(app, snapshot);++ assertEquals(List.of(List.of("rm", "-rf", "/savepoints/savepoint-1")), commands);+ }+private FlinkApplication application(String name) {return new FlinkApplication(name,"arn:aws:kinesisanalytics:us-west-2:000000000000:application/" + name,diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.javaindex 68ace876b4..e135cb78d6 100644--- a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.mwaa;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;+import io.github.hectorvent.floci.core.common.docker.ContainerExecStubs;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;@@ -524,4 +526,19 @@ class MwaaEnvironmentManagerTest {Mockito.verify(lifecycleManager).stopAndRemove("airflow-container-id", null);Mockito.verify(lifecycleManager).stopAndRemove("db-container-id", null);}++ @Test+ void runAirflowCliRunsTheCommandThroughAShellInTheAirflowContainer() throws Exception {+ DockerClient dockerClient = Mockito.mock(DockerClient.class);+ when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);+ List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, "airflow-container-id", 2,+ "dag1\n", "warning\n");++ ContainerExec.Result result = manager.runAirflowCli("airflow-container-id", "dags list");++ assertEquals(List.of(List.of("sh", "-c", "airflow dags list")), commands);+ assertEquals(2, result.exitCode());+ assertEquals("dag1\n", result.stdout());+ assertEquals("warning\n", result.stderr());+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.javaindex e92851b3f8..271586f8d0 100644--- a/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.java@@ -1,6 +1,6 @@package io.github.hectorvent.floci.services.mwaa.proxy;-import io.github.hectorvent.floci.services.mwaa.MwaaEnvironmentManager;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.vertx.core.Vertx;import io.vertx.core.VertxOptions;import io.vertx.core.buffer.Buffer;@@ -27,7 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;/*** Confirms {@code POST /aws_mwaa/cli} runs its Docker exec off the Vert.x event loop.* {@code MwaaEnvironmentManager#execInContainer} can block for up to 30 seconds- * ({@code awaitCompletion(30, TimeUnit.SECONDS)}), so running it synchronously on the event+ * (its exec timeout), so running it synchronously on the event* loop that also serves the rest of this environment's proxied Airflow traffic would stall* every other request for the duration of one CLI call.*@@ -62,7 +62,7 @@ class MwaaWebProxyCliOffloadTest {if (!releaseCli.await(5, TimeUnit.SECONDS)) {throw new IllegalStateException("test did not release the CLI exec in time");}- return new MwaaEnvironmentManager.ExecResult(0, "dag1\ndag2\n", "");+ return new ContainerExec.Result(0, "dag1\ndag2\n", "", false);};MwaaWebProxy proxy = new MwaaWebProxy("env1", vertx, "127.0.0.1", backend.actualPort(),@@ -138,7 +138,7 @@ class MwaaWebProxyCliOffloadTest {if (!releaseCli.await(5, TimeUnit.SECONDS)) {throw new IllegalStateException("test did not release the CLI exec in time");}- return new MwaaEnvironmentManager.ExecResult(0, "", "");+ return new ContainerExec.Result(0, "", "", false);};MwaaWebProxy proxy = new MwaaWebProxy("env1", vertx, "127.0.0.1", 1,(environmentName, token) -> true, slowExecutor);diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerPostgresTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerPostgresTest.javaindex 3554225793..d98a895042 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerPostgresTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerPostgresTest.java@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.ServiceConfigAccess;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExec;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;import org.junit.jupiter.api.AfterAll;@@ -22,18 +23,11 @@ import org.junit.jupiter.api.Tag;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.TestInstance;-import java.io.ByteArrayOutputStream;-import java.io.Closeable;import java.io.IOException;-import java.nio.charset.StandardCharsets;import java.time.Duration;import java.util.List;-import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;-import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.model.Frame;-import com.github.dockerjava.api.model.StreamType;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -157,7 +151,7 @@ class RdsContainerManagerPostgresTest {String[] cmd = {"psql", "-h", "127.0.0.1", "-U", user, "-d", dbName, "-c", "SELECT 1"};for (int i = 0; i < maxSeconds; i++) {try {- ExecResult result = exec(containerId, cmd, 5);+ ContainerExec.Result result = exec(containerId, cmd, 5);if (result.exitCode() == 0) {return;}@@ -176,7 +170,7 @@ class RdsContainerManagerPostgresTest {private void execPsql(String containerId, String user, String db, String sql) throws Exception {String[] cmd = {"psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db, "-c", sql};- ExecResult result = exec(containerId, cmd, 30);+ ContainerExec.Result result = exec(containerId, cmd, 30);if (result.exitCode() != 0) {throw new RuntimeException("psql exec failed (exit " + result.exitCode() + "): "+ result.stderr() + " | " + result.stdout());@@ -185,7 +179,7 @@ class RdsContainerManagerPostgresTest {private String queryPsql(String containerId, String user, String db, String sql) throws Exception {String[] cmd = {"psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db, "-tAc", sql};- ExecResult result = exec(containerId, cmd, 30);+ ContainerExec.Result result = exec(containerId, cmd, 30);if (result.exitCode() != 0) {throw new RuntimeException("psql query failed (exit " + result.exitCode() + "): "+ result.stderr() + " | " + result.stdout());@@ -193,60 +187,8 @@ class RdsContainerManagerPostgresTest {return result.stdout();}- private ExecResult exec(String containerId, String[] cmd, int timeoutSeconds) throws Exception {- String execId = dockerClient.execCreateCmd(containerId)- .withCmd(cmd)- .withAttachStdout(true)- .withAttachStderr(true)- .exec()- .getId();-- CountDownLatch latch = new CountDownLatch(1);- ByteArrayOutputStream stdout = new ByteArrayOutputStream();- ByteArrayOutputStream stderr = new ByteArrayOutputStream();-- Closeable callback = dockerClient.execStartCmd(execId)- .exec(new ResultCallback.Adapter<Frame>() {- @Override- public void onNext(Frame frame) {- if (frame.getPayload() != null) {- try {- if (frame.getStreamType() == StreamType.STDOUT) {- stdout.write(frame.getPayload());- } else if (frame.getStreamType() == StreamType.STDERR) {- stderr.write(frame.getPayload());- }- } catch (IOException ignored) {- // best effort- }- }- }-- @Override- public void onComplete() {- latch.countDown();- }-- @Override- public void onError(Throwable t) {- latch.countDown();- }- });-- try {- boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);- if (!completed) {- return new ExecResult(-1, stdout.toString(StandardCharsets.UTF_8),- "Timed out after " + timeoutSeconds + "s");- }- Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();- return new ExecResult(- exitCode != null ? exitCode : -1,- stdout.toString(StandardCharsets.UTF_8),- stderr.toString(StandardCharsets.UTF_8));- } finally {- callback.close();- }+ private ContainerExec.Result exec(String containerId, String[] cmd, int timeoutSeconds) {+ return ContainerExec.run(dockerClient, containerId, cmd, timeoutSeconds);}private RdsContainerManager createManager() {@@ -293,6 +235,4 @@ class RdsContainerManagerPostgresTest {// best effort}}-- record ExecResult(long exitCode, String stdout, String stderr) {}}diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.javaindex d2fadf9828..17d0f83d31 100644--- a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java@@ -1,9 +1,11 @@package io.github.hectorvent.floci.services.timestreaminfluxdb.container;import com.fasterxml.jackson.databind.ObjectMapper;+import com.github.dockerjava.api.DockerClient;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerExecStubs;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;@@ -15,6 +17,7 @@ import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.mockito.Mockito;+import java.util.List;import java.util.Map;import java.util.Optional;@@ -164,4 +167,18 @@ class TimestreamInfluxDbContainerManagerTest {"INFLUXD_STORAGE_CACHE_SNAPSHOT_WRITE_COLD_DURATION", "48h","INFLUXD_HTTP_WRITE_TIMEOUT", "250ms"), environment);}++ @Test+ void aFailedInfluxBackupReportsTheExitCodeAndTheCommandsOutput() {+ DockerClient dockerClient = mock(DockerClient.class);+ when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);+ List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, "influx-container-id", 1, "",+ "Error: failed to connect");++ IllegalStateException error = assertThrows(IllegalStateException.class,+ () -> manager.backup("influx-container-id", "backup-1"));++ assertEquals("influx backup failed with exit code 1: Error: failed to connect", error.getMessage());+ assertEquals(List.of(List.of("influx", "backup", "/tmp/floci-backup-backup-1")), commands);+ }}