Skip to content

Commit

Permalink
fix: Use the actual Docker endpoint to extract the socket path for th…
Browse files Browse the repository at this point in the history
…e Resource Reaper (#930)
  • Loading branch information
HofmeisterAn authored Jun 23, 2023
1 parent 1c92dcf commit 0ecda30
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
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;
}
}
}
}

0 comments on commit 0ecda30

Please sign in to comment.