From 65f780cca26cf518b329bb200060423803086d27 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Thu, 22 Jun 2023 09:56:13 +0200 Subject: [PATCH] fix: Use the actual Docker endpoint to extract the socket path for the Resource Reaper --- .../Configurations/TestcontainersSettings.cs | 8 +++--- .../Containers/ResourceReaper.cs | 28 ++++++------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/Testcontainers/Configurations/TestcontainersSettings.cs b/src/Testcontainers/Configurations/TestcontainersSettings.cs index 4cae25bc1..d565b61b4 100644 --- a/src/Testcontainers/Configurations/TestcontainersSettings.cs +++ b/src/Testcontainers/Configurations/TestcontainersSettings.cs @@ -23,8 +23,8 @@ public static class TestcontainersSettings private static readonly ManualResetEventSlim ManualResetEvent = new ManualResetEventSlim(false); [CanBeNull] - private static readonly IDockerEndpointAuthenticationProvider DockerEndpointAuthProvider = - new IDockerEndpointAuthenticationProvider[] + private static readonly IDockerEndpointAuthenticationProvider DockerEndpointAuthProvider + = new IDockerEndpointAuthenticationProvider[] { new TestcontainersEndpointAuthenticationProvider(), new MTlsEndpointAuthenticationProvider(), @@ -39,8 +39,8 @@ public static class TestcontainersSettings .FirstOrDefault(authProvider => authProvider.IsAvailable()); [CanBeNull] - private static readonly IDockerEndpointAuthenticationConfiguration DockerEndpointAuthConfig = - DockerEndpointAuthProvider?.GetAuthConfig(); + private static readonly IDockerEndpointAuthenticationConfiguration DockerEndpointAuthConfig + = DockerEndpointAuthProvider?.GetAuthConfig(); static TestcontainersSettings() { diff --git a/src/Testcontainers/Containers/ResourceReaper.cs b/src/Testcontainers/Containers/ResourceReaper.cs index 2608f451c..617ee0c37 100644 --- a/src/Testcontainers/Containers/ResourceReaper.cs +++ b/src/Testcontainers/Containers/ResourceReaper.cs @@ -2,7 +2,6 @@ namespace DotNet.Testcontainers.Containers { using System; using System.IO; - using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; @@ -130,7 +129,7 @@ await DefaultLock.WaitAsync(ct) var requiresPrivilegedMode = TestcontainersSettings.ResourceReaperPrivilegedModeEnabled; - _defaultInstance = await GetAndStartNewAsync(DefaultSessionId, dockerEndpointAuthConfig, resourceReaperImage, UnixSocketMount.Instance, requiresPrivilegedMode, ct: ct) + _defaultInstance = await GetAndStartNewAsync(DefaultSessionId, dockerEndpointAuthConfig, resourceReaperImage, new UnixSocketMount(dockerEndpointAuthConfig.Endpoint), requiresPrivilegedMode, ct: ct) .ConfigureAwait(false); return _defaultInstance; @@ -418,28 +417,25 @@ private sealed class UnixSocketMount : IMount { private const string DockerSocketFilePath = "/var/run/docker.sock"; - static UnixSocketMount() + public UnixSocketMount([NotNull] Uri dockerEndpoint) { - } + // If the Docker endpoint is a Unix socket, extract the socket path from the URI; otherwise, fallback to the default Unix socket path. + Source = "unix".Equals(dockerEndpoint.Scheme, StringComparison.OrdinalIgnoreCase) ? dockerEndpoint.AbsolutePath : DockerSocketFilePath; - private UnixSocketMount() - { + // If the user has overridden the Docker socket path, use the user-specified path; otherwise, keep the previously determined source. + Source = !string.IsNullOrEmpty(TestcontainersSettings.DockerSocketOverride) ? TestcontainersSettings.DockerSocketOverride : Source; + Target = DockerSocketFilePath; } - public static IMount Instance { get; } - = new UnixSocketMount(); - public MountType Type => MountType.Bind; public AccessMode AccessMode => AccessMode.ReadOnly; - public string Source - => TestcontainersSettings.DockerSocketOverride ?? GetSocketPath(); + public string Source { get; } - public string Target - => DockerSocketFilePath; + public string Target { get; } public Task CreateAsync(CancellationToken ct = default) { @@ -450,12 +446,6 @@ public Task DeleteAsync(CancellationToken ct = default) { return Task.CompletedTask; } - - private static string GetSocketPath() - { - var dockerEndpoints = new[] { TestcontainersSettings.OS.DockerEndpointAuthConfig.Endpoint, UnixEndpointAuthenticationProvider.DockerEngine }; - return dockerEndpoints.First(dockerEndpoint => "unix".Equals(dockerEndpoint.Scheme, StringComparison.OrdinalIgnoreCase)).AbsolutePath; - } } } }