From 5229fc2f554891abacfcdc2f3715a9573809d1ab Mon Sep 17 00:00:00 2001 From: huanghaiwei <3586266813@qq.com> Date: Thu, 1 Oct 2026 23:28:19 +0800 Subject: [PATCH] fix: isApplicable should check the resolved socket path isApplicable read the socketPath field directly. Because the field is annotated with @Getter(lazy = true), Lombok replaces its actual type with AtomicReference, so the != null check always evaluated to true on Linux and macOS - the AtomicReference instance itself is never null. As a result the strategy was reported as applicable on machines without Docker Desktop, and a subsequent getSocketPath() call failed with a NullPointerException. Use the generated lazy getter instead, so the resolved path is checked. Fixes #11829 Signed-off-by: huanghaiwei <3586266813@qq.com> --- .../DockerDesktopClientProviderStrategy.java | 2 +- ...ckerDesktopClientProviderStrategyTest.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategyTest.java diff --git a/core/src/main/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategy.java b/core/src/main/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategy.java index 56fd0b59ee4..0f5bf3585ff 100644 --- a/core/src/main/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategy.java +++ b/core/src/main/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategy.java @@ -66,7 +66,7 @@ public String getRemoteDockerUnixSocketPath() { @Override protected boolean isApplicable() { - return (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) && this.socketPath != null; + return (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) && getSocketPath() != null; } private Optional tryFolder(Path path) { diff --git a/core/src/test/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategyTest.java b/core/src/test/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategyTest.java new file mode 100644 index 00000000000..d9df9ec3c5c --- /dev/null +++ b/core/src/test/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategyTest.java @@ -0,0 +1,53 @@ +package org.testcontainers.dockerclient; + +import org.apache.commons.lang3.SystemUtils; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assumptions.assumeThat; + +class DockerDesktopClientProviderStrategyTest { + + /** + * The lazily resolved socket path should be {@code null} when no Docker Desktop + * socket is present under {@code user.home}. + */ + @Test + void socketPathIsNullWhenNoDockerDesktopSocketExists() throws Exception { + String originalHome = System.getProperty("user.home"); + Path emptyHome = Files.createTempDirectory("tc-no-docker-desktop"); + System.setProperty("user.home", emptyHome.toString()); + try { + assertThat(new DockerDesktopClientProviderStrategy().getSocketPath()).isNull(); + } finally { + System.setProperty("user.home", originalHome); + } + } + + /** + * Regression test for #11829: previously {@code isApplicable()} always returned + * {@code true} on Linux/macOS because {@code @Getter(lazy = true)} turns the field + * into an {@code AtomicReference}, whose instance is never {@code null}. + * + *

Only meaningful on Linux/macOS, where the strategy is eligible in the first place. + */ + @Test + void notApplicableWhenNoDockerDesktopSocketExists() throws Exception { + assumeThat(SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC).isTrue(); + + String originalHome = System.getProperty("user.home"); + Path emptyHome = Files.createTempDirectory("tc-no-docker-desktop"); + System.setProperty("user.home", emptyHome.toString()); + try { + DockerDesktopClientProviderStrategy strategy = new DockerDesktopClientProviderStrategy(); + + assertThat(strategy.getSocketPath()).isNull(); + assertThat(strategy.isApplicable()).isFalse(); + } finally { + System.setProperty("user.home", originalHome); + } + } +}