forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add docker compose support (testcontainers#122)
- Loading branch information
Showing
15 changed files
with
431 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
123 changes: 123 additions & 0 deletions
123
src/Testcontainers.DockerCompose/DockerComposeBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
namespace Testcontainers.DockerCompose; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class DockerComposeBuilder : ContainerBuilder<DockerComposeBuilder, DockerComposeContainer, DockerComposeConfiguration> | ||
{ | ||
private const string NoComposeFile = "No docker compose file have been provided."; | ||
|
||
//Docker Compose is included as part of this image. | ||
public const string DockerComposeImage = "docker:24-cli"; | ||
|
||
public 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; } | ||
|
||
/// <inheritdoc /> | ||
public override DockerComposeContainer Build() | ||
{ | ||
Validate(); | ||
|
||
return new DockerComposeContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the compose file. | ||
/// </summary> | ||
/// <param name="composeFile">The compose file.</param> | ||
/// <returns></returns> | ||
public DockerComposeBuilder WithComposeFile(string composeFile) | ||
{ | ||
return Merge(DockerResourceConfiguration, new DockerComposeConfiguration | ||
(composeFile: composeFile)); | ||
} | ||
|
||
/// <summary> | ||
/// If true use a local Docker Compose binary instead of a container. | ||
/// </summary> | ||
/// <param name="localCompose"></param> | ||
/// <returns></returns> | ||
public DockerComposeBuilder WithLocalCompose(bool localCompose) | ||
{ | ||
return Merge(DockerResourceConfiguration, new DockerComposeConfiguration | ||
(localCompose: localCompose)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override DockerComposeBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(DockerComposeImage) | ||
.WithEntrypoint(CommonCommands.SleepInfinity) | ||
.WithBindMount(DockerSocketPath, DockerSocketPath, AccessMode.ReadWrite) | ||
.WithStartupCallback(ConfigureDockerComposeAsync); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Validate() | ||
{ | ||
base.Validate(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration.ComposeFile, nameof(DockerResourceConfiguration.ComposeFile)) | ||
.NotEmpty(); | ||
} | ||
|
||
/// <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)); | ||
} | ||
|
||
/// <summary> | ||
/// Configures the compose container. | ||
/// </summary> | ||
/// <param name="container">The container.</param> | ||
/// <param name="ct">Cancellation token.</param> | ||
private async Task ConfigureDockerComposeAsync(IContainer container, CancellationToken ct = default) | ||
{ | ||
if (container is DockerComposeRemote dockerComposeContainer && | ||
!dockerComposeContainer.RuntimeConfiguration.LocalCompose ) | ||
{ | ||
var fileInfo = new FileInfo(dockerComposeContainer.RuntimeConfiguration.ComposeFile); | ||
if (fileInfo.Exists) | ||
{ | ||
await container.CopyAsync(fileInfo, ".", Unix.FileMode644, ct) | ||
.ConfigureAwait(false); | ||
} | ||
await container.ExecAsync(new[] { "docker", "compose", "up", "-d" }, ct) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Testcontainers.DockerCompose/DockerComposeConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
namespace Testcontainers.DockerCompose; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class DockerComposeConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="composeFile">The fully qualified path to the compose file.</param> | ||
/// <param name="localCompose">Whether the local compose will be used.</param> | ||
public DockerComposeConfiguration( | ||
string composeFile = null, | ||
bool localCompose = false) | ||
{ | ||
ComposeFile = composeFile; | ||
LocalCompose = localCompose; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public DockerComposeConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public DockerComposeConfiguration(IContainerConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public DockerComposeConfiguration(DockerComposeConfiguration resourceConfiguration) | ||
: this(new DockerComposeConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public DockerComposeConfiguration(DockerComposeConfiguration oldValue, DockerComposeConfiguration newValue) | ||
: base(oldValue, newValue) | ||
{ | ||
ComposeFile = BuildConfiguration.Combine(oldValue.ComposeFile, newValue.ComposeFile); | ||
LocalCompose = BuildConfiguration.Combine(oldValue.LocalCompose, newValue.LocalCompose); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the path to the compose file. | ||
/// </summary> | ||
public string ComposeFile { get; } | ||
|
||
/// <summary> | ||
/// Indicates whether local compose is enabled. | ||
/// </summary> | ||
public bool LocalCompose { get; } | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Testcontainers.DockerCompose/DockerComposeContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Testcontainers.DockerCompose; | ||
|
||
public class DockerComposeContainer : DockerContainer | ||
{ | ||
private readonly IContainer _proxyContainer; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration"></param> | ||
/// <param name="logger"></param> | ||
public DockerComposeContainer(DockerComposeConfiguration configuration, ILogger logger) : base(configuration, logger) | ||
{ | ||
_proxyContainer = configuration.LocalCompose | ||
? new DockerComposeLocal(configuration, logger) | ||
: new DockerComposeRemote(configuration, logger); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override async Task StartAsync(CancellationToken ct = default) | ||
{ | ||
await _proxyContainer.StartAsync(ct); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override async Task StopAsync(CancellationToken ct = default) | ||
{ | ||
await _proxyContainer.StopAsync(ct); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace Testcontainers.DockerCompose; | ||
|
||
[PublicAPI] | ||
public sealed class DockerComposeLocal : DockerContainer | ||
{ | ||
private readonly string _dockerComposeBinary = | ||
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "docker-compose.exe" : "docker-compose"; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeRemote" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public DockerComposeLocal(DockerComposeConfiguration configuration, ILogger logger) : base(configuration, logger) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the runtime configuration. | ||
/// </summary> | ||
public DockerComposeConfiguration RuntimeConfiguration => _configuration as DockerComposeConfiguration; | ||
|
||
/// <inheritdoc /> | ||
public override async Task StartAsync(CancellationToken ct = default) | ||
{ | ||
await Cli.Wrap(_dockerComposeBinary) | ||
.WithArguments(new[] {"up", "-d"}) | ||
.WithWorkingDirectory(Path.GetDirectoryName(RuntimeConfiguration.ComposeFile)!) | ||
.ExecuteBufferedAsync(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override async Task StopAsync(CancellationToken ct = default) | ||
{ | ||
await Cli.Wrap(_dockerComposeBinary) | ||
.WithArguments(new[] {"down"}) | ||
.WithWorkingDirectory(Path.GetDirectoryName(RuntimeConfiguration.ComposeFile)!) | ||
.ExecuteBufferedAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Testcontainers.DockerCompose; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public class DockerComposeRemote : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DockerComposeRemote" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public DockerComposeRemote(DockerComposeConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the runtime configuration. | ||
/// </summary> | ||
public DockerComposeConfiguration RuntimeConfiguration => _configuration as DockerComposeConfiguration; | ||
|
||
/// <inheritdoc /> | ||
public override async Task StopAsync(CancellationToken ct = default) | ||
{ | ||
await ExecAsync(new[] { "docker", "compose", "down"}, ct) | ||
.ConfigureAwait(false); | ||
await base.StopAsync(ct); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Testcontainers.DockerCompose/Testcontainers.DockerCompose.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="CliWrap" Version="3.6.4" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/> | ||
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/> | ||
<ProjectReference Include="..\..\tests\Testcontainers.Commons\Testcontainers.Commons.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.IO; | ||
global using System.Threading; | ||
global using System.Threading.Tasks; | ||
global using Docker.DotNet.Models; | ||
global using DotNet.Testcontainers; | ||
global using DotNet.Testcontainers.Builders; | ||
global using DotNet.Testcontainers.Configurations; | ||
global using DotNet.Testcontainers.Containers; | ||
global using JetBrains.Annotations; | ||
global using Microsoft.Extensions.Logging; | ||
global using System.Diagnostics; | ||
global using System.Linq; | ||
global using System.Runtime.InteropServices; | ||
global using CliWrap; | ||
global using CliWrap.Buffered; | ||
global using DotNet.Testcontainers.Commons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
Oops, something went wrong.