-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
DockerComposeBuilder.cs
114 lines (98 loc) · 4.71 KB
/
DockerComposeBuilder.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
namespace Testcontainers.DockerCompose;
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class DockerComposeBuilder : ContainerBuilder<DockerComposeBuilder, DockerComposeContainer, DockerComposeConfiguration>
{
public const string DockerComposeFilePath = "/docker-compose.yml";
public const string DockerComposeImage = "docker:25.0-cli";
// TODO: This does not support all container runtimes (host configurations). We should do something similar to what we are doing in the Resource Reaper implementation.
private const string DockerSocketPath = "/var/run/docker.sock";
/// <summary>
/// Initializes a new instance of the <see cref="DockerComposeBuilder" /> class.
/// </summary>
public DockerComposeBuilder()
: this(new DockerComposeConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}
/// <summary>
/// Initializes a new instance of the <see cref="DockerComposeBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private DockerComposeBuilder(DockerComposeConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}
/// <inheritdoc />
protected override DockerComposeConfiguration DockerResourceConfiguration { get; }
/// <summary>
/// Sets the Docker Compose file.
/// </summary>
/// <param name="composeFilePath">The Docker Compose file path.</param>
/// <returns>A configured instance of <see cref="DockerComposeBuilder" />.</returns>
public DockerComposeBuilder WithComposeFile(string composeFilePath)
{
return Merge(DockerResourceConfiguration, new DockerComposeConfiguration(composeFilePath: composeFilePath))
.WithEntrypoint("docker", "compose", "--project-name", Guid.NewGuid().ToString("D"), "--file", composeFilePath)
.WithResourceMapping(new FileInfo(composeFilePath), new FileInfo(DockerComposeFilePath));
}
/// <summary>
/// Sets the Docker Compose mode.
/// </summary>
/// <param name="mode">The Docker Compose mode.</param>
/// <returns>A configured instance of <see cref="DockerComposeBuilder" />.</returns>
public DockerComposeBuilder WithComposeMode(DockerComposeMode mode)
{
return Merge(DockerResourceConfiguration, new DockerComposeConfiguration(mode: mode));
}
/// <inheritdoc />
public override DockerComposeContainer Build()
{
Validate();
switch (DockerResourceConfiguration.Mode)
{
case DockerComposeMode.Local:
return new DockerComposeLocalContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
case DockerComposeMode.Remote:
return new DockerComposeRemoteContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
default:
throw new ArgumentOutOfRangeException(nameof(DockerResourceConfiguration.Mode), "Docker Compose mode not supported.");
}
}
/// <inheritdoc />
protected override DockerComposeBuilder Init()
{
return base.Init()
.WithImage(DockerComposeImage)
.WithCommand("up")
.WithCommand("--detach")
.WithComposeMode(DockerComposeMode.Remote)
.WithBindMount(DockerSocketPath, DockerSocketPath, AccessMode.ReadOnly);
}
/// <inheritdoc />
protected override void Validate()
{
base.Validate();
const string dockerComposeFileNotFound = "Docker Compose file not found.";
_ = Guard.Argument(DockerResourceConfiguration.ComposeFilePath, nameof(DockerComposeConfiguration.ComposeFilePath))
.NotNull()
.NotEmpty()
.ThrowIf(argument => !File.Exists(argument.Value), argument => new FileNotFoundException(dockerComposeFileNotFound, argument.Value));
}
/// <inheritdoc />
protected override DockerComposeBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new DockerComposeConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override DockerComposeBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new DockerComposeConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override DockerComposeBuilder Merge(DockerComposeConfiguration oldValue, DockerComposeConfiguration newValue)
{
return new DockerComposeBuilder(new DockerComposeConfiguration(oldValue, newValue));
}
}