-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathReusableResourceTest.cs
80 lines (63 loc) · 2.65 KB
/
ReusableResourceTest.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
namespace Testcontainers.Tests;
public sealed class ReusableResourceTest : IAsyncLifetime, IDisposable
{
private readonly DockerClient _dockerClient = TestcontainersSettings.OS.DockerEndpointAuthConfig.GetDockerClientConfiguration(Guid.NewGuid()).CreateClient();
private readonly FilterByProperty _filters = new FilterByProperty();
private readonly IList<IAsyncDisposable> _disposables = new List<IAsyncDisposable>();
private readonly string _labelKey = Guid.NewGuid().ToString("D");
private readonly string _labelValue = Guid.NewGuid().ToString("D");
public ReusableResourceTest()
{
_filters.Add("label", string.Join("=", _labelKey, _labelValue));
}
public async Task InitializeAsync()
{
for (var _ = 0; _ < 3; _++)
{
// We are running in a single session, we do not need to disable the cleanup feature.
var container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint("sleep")
.WithCommand("infinity")
.WithLabel(_labelKey, _labelValue)
.WithReuse(true)
.Build();
var network = new NetworkBuilder()
.WithName(_labelKey)
.WithLabel(_labelKey, _labelValue)
.WithReuse(true)
.Build();
var volume = new VolumeBuilder()
.WithName(_labelKey)
.WithLabel(_labelKey, _labelValue)
.WithReuse(true)
.Build();
await Task.WhenAll(container.StartAsync(), network.CreateAsync(), volume.CreateAsync())
.ConfigureAwait(false);
_disposables.Add(container);
_disposables.Add(network);
_disposables.Add(volume);
}
}
public Task DisposeAsync()
{
return Task.WhenAll(_disposables.Select(disposable => disposable.DisposeAsync().AsTask()));
}
public void Dispose()
{
_dockerClient.Dispose();
}
[Fact]
public async Task ShouldReuseExistingResource()
{
var containers = await _dockerClient.Containers.ListContainersAsync(new ContainersListParameters { Filters = _filters })
.ConfigureAwait(false);
var networks = await _dockerClient.Networks.ListNetworksAsync(new NetworksListParameters { Filters = _filters })
.ConfigureAwait(false);
var response = await _dockerClient.Volumes.ListAsync(new VolumesListParameters { Filters = _filters })
.ConfigureAwait(false);
Assert.Single(containers);
Assert.Single(networks);
Assert.Single(response.Volumes);
}
}