-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGenericContainerFixture.cs
88 lines (73 loc) · 3.35 KB
/
GenericContainerFixture.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
82
83
84
85
86
87
88
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Docker.DotNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using TestContainers.Container.Abstractions;
using TestContainers.Container.Abstractions.Hosting;
using TestContainers.Container.Abstractions.Models;
using Xunit;
namespace Container.Abstractions.Integration.Tests.Fixtures
{
public class GenericContainerFixture : IAsyncLifetime
{
public IContainer Container { get; }
public IDockerClient DockerClient { get; }
public KeyValuePair<string, string> CustomLabel { get; } =
new KeyValuePair<string, string>("custom label", "custom value");
public KeyValuePair<string, string> InjectedEnvVar { get; } =
new KeyValuePair<string, string>("MY_KEY", "my value");
public int ExposedPort { get; } = 1234;
public KeyValuePair<int, int> PortBinding { get; } = new KeyValuePair<int, int>(2345, 34567);
public KeyValuePair<string, string> HostPathBinding { get; }
public string FileTouchedByCommand { get; }
public string WorkingDirectory { get; }
public GenericContainerFixture()
{
HostPathBinding =
new KeyValuePair<string, string>(Directory.GetCurrentDirectory(), "/host");
FileTouchedByCommand = "/tmp/touched";
WorkingDirectory = "/etc";
Container = new ContainerBuilder<GenericContainer>()
.ConfigureHostConfiguration(builder => builder.AddInMemoryCollection())
.ConfigureAppConfiguration((context, builder) => builder.AddInMemoryCollection())
.ConfigureDockerImageName($"{GenericContainer.DefaultImage}:{GenericContainer.DefaultTag}")
.ConfigureLogging(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Debug);
})
.ConfigureContainer((context, container) =>
{
container.Labels.Add(CustomLabel.Key, CustomLabel.Value);
container.Env[InjectedEnvVar.Key] = InjectedEnvVar.Value;
container.ExposedPorts.Add(ExposedPort);
/*
to do something like `docker run -p 2345:34567 alpine:latest`,
both expose port and port binding must be set
*/
container.ExposedPorts.Add(PortBinding.Key);
container.PortBindings.Add(PortBinding.Key, PortBinding.Value);
container.BindMounts.Add(new Bind
{
HostPath = HostPathBinding.Key,
ContainerPath = HostPathBinding.Value,
AccessMode = AccessMode.ReadOnly
});
container.WorkingDirectory = WorkingDirectory;
container.Command = new List<string> {"/bin/sh", "-c", $"touch {FileTouchedByCommand}; /bin/sh"};
})
.Build();
DockerClient = ((GenericContainer) Container).DockerClient;
}
public async Task InitializeAsync()
{
await Container.StartAsync();
}
public async Task DisposeAsync()
{
await Container.StopAsync();
}
}
}