Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use the actual Docker endpoint to extract the socket path for the Resource Reaper #930

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Testcontainers/Configurations/TestcontainersSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()
{
Expand Down
28 changes: 9 additions & 19 deletions src/Testcontainers/Containers/ResourceReaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
}
}
}