-
-
Notifications
You must be signed in to change notification settings - Fork 287
/
ProtectDockerDaemonSocket.cs
81 lines (67 loc) · 2.39 KB
/
ProtectDockerDaemonSocket.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace DotNet.Testcontainers.Tests.Fixtures
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Images;
using Xunit;
public abstract class ProtectDockerDaemonSocket : IAsyncLifetime
{
public const string DockerVersion = "20.10.18";
private const string CertsDirectoryName = "certs";
private const ushort TlsPort = 2376;
private readonly string _hostCertsDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"), CertsDirectoryName);
private readonly string _containerCertsDirectoryPath = Path.Combine("/", CertsDirectoryName);
private readonly IImage _image = new DockerImage(string.Empty, "docker", DockerVersion + "-dind");
private readonly IContainer _container;
protected ProtectDockerDaemonSocket(ContainerBuilder containerConfiguration)
{
_container = containerConfiguration
.WithImage(_image)
.WithPrivileged(true)
.WithPortBinding(TlsPort, true)
.WithBindMount(_hostCertsDirectoryPath, _containerCertsDirectoryPath, AccessMode.ReadWrite)
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new UntilListenOn()))
.Build();
}
public virtual IList<string> CustomProperties
{
get
{
var customProperties = new List<string>();
customProperties.Add($"docker.host={TcpEndpoint}");
customProperties.Add($"docker.cert.path={Path.Combine(_hostCertsDirectoryPath, "client")}");
return customProperties;
}
}
private Uri TcpEndpoint
{
get
{
return new UriBuilder("tcp", _container.Hostname, _container.GetMappedPublicPort(TlsPort)).Uri;
}
}
public Task InitializeAsync()
{
_ = Directory.CreateDirectory(_hostCertsDirectoryPath);
return _container.StartAsync();
}
public Task DisposeAsync()
{
return _container.DisposeAsync().AsTask();
}
private sealed class UntilListenOn : IWaitUntil
{
public async Task<bool> UntilAsync(IContainer container)
{
var (_, stderr) = await container.GetLogsAsync()
.ConfigureAwait(false);
return stderr != null && stderr.Contains("API listen on [::]:2376");
}
}
}
}