Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add MinIO module #760

Merged
merged 19 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ResourceReap
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Tests", "tests\Testcontainers.Tests\Testcontainers.Tests.csproj", "{27CDB869-A150-4593-958F-6F26E5391E7C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Minio", "src\Testcontainers.Minio\Testcontainers.Minio.csproj", "{1266E1E6-5CEF-4161-8B45-83282455746E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Minio.Tests", "tests\Testcontainers.Minio.Tests\Testcontainers.Minio.Tests.csproj", "{5DB1F35F-B714-4B62-84BE-16A33084D3E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -65,6 +69,14 @@ Global
{27CDB869-A150-4593-958F-6F26E5391E7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27CDB869-A150-4593-958F-6F26E5391E7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27CDB869-A150-4593-958F-6F26E5391E7C}.Release|Any CPU.Build.0 = Release|Any CPU
{1266E1E6-5CEF-4161-8B45-83282455746E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1266E1E6-5CEF-4161-8B45-83282455746E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1266E1E6-5CEF-4161-8B45-83282455746E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1266E1E6-5CEF-4161-8B45-83282455746E}.Release|Any CPU.Build.0 = Release|Any CPU
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand All @@ -75,5 +87,7 @@ Global
{3E55CBE8-AFE8-426D-9470-49D63CD1051C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{9E8E6AA5-65D1-498F-BEAB-BA34723A0050} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{27CDB869-A150-4593-958F-6F26E5391E7C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{1266E1E6-5CEF-4161-8B45-83282455746E} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{5DB1F35F-B714-4B62-84BE-16A33084D3E1} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Testcontainers.Minio/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
98 changes: 98 additions & 0 deletions src/Testcontainers.Minio/MinioBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Testcontainers.Minio;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class MinioBuilder : ContainerBuilder<MinioBuilder, MinioContainer, MinioConfiguration>
{
public const ushort MinioPort = 9000;
public const string MinioImage = "minio/minio:RELEASE.2023-01-31T02-24-19Z";
private const string MinioHealthEndpoint = "/minio/health/ready";

protected override MinioConfiguration DockerResourceConfiguration { get; }


/// <summary>
/// Initializes a new instance of the <see cref="MinioBuilder" /> class.
/// </summary>
public MinioBuilder()
: this(new MinioConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="MinioBuilder" /> class.
/// </summary>
/// <param name="dockerResourceConfiguration">The Docker resource configuration.</param>
private MinioBuilder(MinioConfiguration dockerResourceConfiguration) : base(dockerResourceConfiguration)
{
DockerResourceConfiguration = dockerResourceConfiguration;
}

/// <summary>
/// Sets the Minio username.
/// </summary>
/// <param name="username">The Minio username.</param>
/// <returns>A configured instance of <see cref="MinioBuilder" />.</returns>
public MinioBuilder WithUsername(string username)
{
return Merge(DockerResourceConfiguration, new MinioConfiguration(username: username))
.WithEnvironment("MINIO_ROOT_USER", username);
}

/// <summary>
/// Sets the Minio password.
/// </summary>
/// <param name="password">The Minio password.</param>
/// <returns>A configured instance of <see cref="MinioBuilder" />.</returns>
public MinioBuilder WithPassword(string password)
{
return Merge(DockerResourceConfiguration, new MinioConfiguration(password: password))
.WithEnvironment("MINIO_ROOT_PASSWORD", password);
}

protected override MinioBuilder Init()
{
return base.Init()
.WithImage(MinioImage)
.WithPortBinding(MinioPort, true)
.WithUsername("ROOTNAME")
.WithPassword(Guid.NewGuid().ToString("D"))
.WithCommand("server", "/data")
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilHttpRequestIsSucceeded(req => req.ForPath(MinioHealthEndpoint).ForPort(MinioPort)));
}


public override MinioContainer Build()
{
Validate();
return new MinioContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}


protected override void Validate()
{
base.Validate();

_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username)).NotNull()
.NotEmpty();
_ = Guard.Argument(DockerResourceConfiguration.Password, nameof(DockerResourceConfiguration.Password)).NotNull()
.NotEmpty();
}

protected override MinioBuilder Merge(MinioConfiguration oldValue, MinioConfiguration newValue)
{
return new MinioBuilder(new MinioConfiguration(oldValue, newValue));
}

protected override MinioBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new MinioConfiguration(resourceConfiguration));
}

protected override MinioBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new MinioConfiguration(resourceConfiguration));
}
}
68 changes: 68 additions & 0 deletions src/Testcontainers.Minio/MinioConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Testcontainers.Minio;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class MinioConfiguration : ContainerConfiguration
{
/// <summary>
/// Minio UserName
/// </summary>
public string Username { get; }
/// <summary>
/// Minio Password
/// </summary>
public string Password { get; }

/// <summary>
/// Initializes a new instance of the <see cref="MinioConfiguration" /> class.
/// </summary>
/// <param name="username">The Minio database.</param>
/// <param name="password">The Minio username.</param>
public MinioConfiguration([CanBeNull] string username = null, [CanBeNull] string password = null)
{
this.Username = username;
this.Password = password;
}

/// <summary>
/// Initializes a new instance of the <see cref="MinioConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public MinioConfiguration(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="MinioConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public MinioConfiguration(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="MinioConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public MinioConfiguration(MinioConfiguration resourceConfiguration)
: this(new MinioConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="MinioConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public MinioConfiguration(MinioConfiguration oldValue, MinioConfiguration newValue)
: base(oldValue, newValue)
{
Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username);
Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password);
}
}
46 changes: 46 additions & 0 deletions src/Testcontainers.Minio/MinioContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Testcontainers.Minio;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class MinioContainer: DockerContainer
{
private readonly MinioConfiguration _configuration;

/// <summary>
/// Initializes a new instance of the <see cref="MinioContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public MinioContainer(MinioConfiguration configuration, ILogger logger) : base(configuration, logger)
{
_configuration = configuration;
}

/// <summary>
/// Gets the Minio AccessKeyid for the AmazonS3 purpose.
/// </summary>
/// <returns>The Minio AccessKeyid.</returns>
public string GetAccessId()
{
return _configuration.Username;
}

/// <summary>
/// Gets the Minio AccessKeySecret for the AmazonS3 purpose.
/// </summary>
/// <returns>The Minio AccessKeySecret.</returns>
public string GetAccessKey()
{
return _configuration.Password;
}

/// <summary>
/// Gets the Minio Url.
/// </summary>
/// <returns>The Minio Url.</returns>
public string GetMinioUrl()
{
var port = GetMappedPublicPort(MinioBuilder.MinioPort);
return $"http://{Hostname}:{port}";
}
}
12 changes: 12 additions & 0 deletions src/Testcontainers.Minio/Testcontainers.Minio.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/Testcontainers.Minio/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Text;
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;
1 change: 1 addition & 0 deletions tests/Testcontainers.Minio.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
105 changes: 105 additions & 0 deletions tests/Testcontainers.Minio.Tests/MinioContainerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
namespace Testcontainers.Minio;

public sealed class MinioContainerTests : IAsyncLifetime, IDisposable
{
private const string TestFileContent = "👋";
private readonly string _testFileName;
private readonly string _testFileContentFilePath;

private readonly MinioContainer _minioContainer = new MinioBuilder().Build();

public MinioContainerTests()
{
var tmpFile = Path.GetTempFileName();
_testFileName = Path.GetFileName(tmpFile);
_testFileContentFilePath = Path.Combine(Path.GetTempPath(), tmpFile);
}

[Fact]
public async Task TestMinio()
{
const string bucketName = "somebucket";
var config = new AmazonS3Config
{
AuthenticationRegion = "eu-west-1",
ServiceURL = _minioContainer.GetMinioUrl(),
UseHttp = true,
ForcePathStyle = true,
};
var s3 = new AmazonS3Client(_minioContainer.GetAccessId(), _minioContainer.GetAccessKey(), config);

await s3.PutBucketAsync(bucketName);

var buckets = await s3.ListBucketsAsync();

Assert.NotNull(buckets);
Assert.NotNull(buckets.Buckets);
Assert.NotEmpty(buckets.Buckets);
Assert.Contains(buckets.Buckets, bucket => bucket.BucketName == bucketName);
}

[Fact]
public async Task TestInsertAndGetDataFromMinio()
{
const string bucketName = "somebucket2";
var config = new AmazonS3Config
{
AuthenticationRegion = "eu-west-1",
ServiceURL = _minioContainer.GetMinioUrl(),
UseHttp = true,
ForcePathStyle = true,
};
var s3 = new AmazonS3Client(_minioContainer.GetAccessId(), _minioContainer.GetAccessKey(), config);

await s3.PutBucketAsync(bucketName);
await using var file = File.OpenRead(_testFileContentFilePath);

await s3.PutObjectAsync(new PutObjectRequest()
{
Key = _testFileName,
BucketName = bucketName,
InputStream = file,
});

var subject = await s3.GetObjectAsync(new GetObjectRequest() { Key = _testFileName, BucketName = bucketName });

Assert.NotNull(subject);
Assert.NotEqual(0, subject.ContentLength);
}


[Fact]
public void TestMinioWithEmptyUsername()
{
var ct = new MinioBuilder().WithUsername(string.Empty);

Assert.Throws<ArgumentException>(() => ct.Build());
}

[Fact]
public void TestMinioWithEmptyPassword()
{
var ct = new MinioBuilder().WithPassword(string.Empty);

Assert.Throws<ArgumentException>(() => ct.Build());
}

public Task InitializeAsync()
{
File.WriteAllText(_testFileContentFilePath, TestFileContent);
return _minioContainer.StartAsync();
}

public Task DisposeAsync()
{
return _minioContainer.DisposeAsync().AsTask();
}

public void Dispose()
{
if (File.Exists(_testFileContentFilePath))
{
File.Delete(_testFileContentFilePath);
}
}
}
Loading