-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andre Hofmeister <[email protected]>
- Loading branch information
1 parent
b877ebb
commit c56c495
Showing
12 changed files
with
319 additions
and
0 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
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 |
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,132 @@ | ||
namespace Testcontainers.Sftp; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class SftpBuilder : ContainerBuilder<SftpBuilder, SftpContainer, SftpConfiguration> | ||
{ | ||
public const string SftpImage = "atmoz/sftp:alpine"; | ||
|
||
public const ushort SftpPort = 22; | ||
|
||
public const string DefaultUsername = "sftp"; | ||
|
||
public const string DefaultPassword = "sftp"; | ||
|
||
public const string DefaultUploadDirectory = "upload"; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SftpBuilder" /> class. | ||
/// </summary> | ||
public SftpBuilder() | ||
: this(new SftpConfiguration()) | ||
{ | ||
DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SftpBuilder" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
private SftpBuilder(SftpConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
DockerResourceConfiguration = resourceConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override SftpConfiguration DockerResourceConfiguration { get; } | ||
|
||
/// <summary> | ||
/// Sets the Sftp username. | ||
/// </summary> | ||
/// <param name="username">The Sftp username.</param> | ||
/// <returns>A configured instance of <see cref="SftpBuilder" />.</returns> | ||
public SftpBuilder WithUsername(string username) | ||
{ | ||
return Merge(DockerResourceConfiguration, new SftpConfiguration(username: username)); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the Sftp password. | ||
/// </summary> | ||
/// <param name="password">The Sftp password.</param> | ||
/// <returns>A configured instance of <see cref="SftpBuilder" />.</returns> | ||
public SftpBuilder WithPassword(string password) | ||
{ | ||
return Merge(DockerResourceConfiguration, new SftpConfiguration(password: password)); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the directory to which files are uploaded. | ||
/// </summary> | ||
/// <param name="uploadDirectory">The upload directory.</param> | ||
/// <returns>A configured instance of <see cref="SftpBuilder" />.</returns> | ||
public SftpBuilder WithUploadDirectory(string uploadDirectory) | ||
{ | ||
return Merge(DockerResourceConfiguration, new SftpConfiguration(uploadDirectory: uploadDirectory)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override SftpContainer Build() | ||
{ | ||
Validate(); | ||
|
||
var sftpContainer = WithCommand(string.Join( | ||
":", | ||
DockerResourceConfiguration.Username, | ||
DockerResourceConfiguration.Password, | ||
string.Empty, | ||
string.Empty, | ||
DockerResourceConfiguration.UploadDirectory)); | ||
|
||
return new SftpContainer(sftpContainer.DockerResourceConfiguration); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override SftpBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(SftpImage) | ||
.WithPortBinding(SftpPort, true) | ||
.WithUsername(DefaultUsername) | ||
.WithPassword(DefaultPassword) | ||
.WithUploadDirectory(DefaultUploadDirectory) | ||
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Server listening on .+")); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Validate() | ||
{ | ||
base.Validate(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username)) | ||
.NotNull() | ||
.NotEmpty(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration.Password, nameof(DockerResourceConfiguration.Password)) | ||
.NotNull() | ||
.NotEmpty(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration.UploadDirectory, nameof(DockerResourceConfiguration.UploadDirectory)) | ||
.NotNull() | ||
.NotEmpty(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override SftpBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new SftpConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override SftpBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new SftpConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override SftpBuilder Merge(SftpConfiguration oldValue, SftpConfiguration newValue) | ||
{ | ||
return new SftpBuilder(new SftpConfiguration(oldValue, newValue)); | ||
} | ||
} |
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,80 @@ | ||
namespace Testcontainers.Sftp; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class SftpConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SftpConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="username">The Sftp username.</param> | ||
/// <param name="password">The Sftp password.</param> | ||
/// <param name="uploadDirectory">The directory to which files are uploaded.</param> | ||
public SftpConfiguration( | ||
string username = null, | ||
string password = null, | ||
string uploadDirectory = null) | ||
{ | ||
Username = username; | ||
Password = password; | ||
UploadDirectory = uploadDirectory; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SftpConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public SftpConfiguration(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="SftpConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public SftpConfiguration(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="SftpConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public SftpConfiguration(SftpConfiguration resourceConfiguration) | ||
: this(new SftpConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SftpConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public SftpConfiguration(SftpConfiguration oldValue, SftpConfiguration newValue) | ||
: base(oldValue, newValue) | ||
{ | ||
Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username); | ||
Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password); | ||
UploadDirectory = BuildConfiguration.Combine(oldValue.UploadDirectory, newValue.UploadDirectory); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Sftp username. | ||
/// </summary> | ||
public string Username { get; } | ||
|
||
/// <summary> | ||
/// Gets the Sftp password. | ||
/// </summary> | ||
public string Password { get; } | ||
|
||
/// <summary> | ||
/// Gets the directory to which files are uploaded. | ||
/// </summary> | ||
public string UploadDirectory { get; } | ||
} |
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 @@ | ||
namespace Testcontainers.Sftp; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class SftpContainer : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SftpContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
public SftpContainer(SftpConfiguration configuration) | ||
: base(configuration) | ||
{ | ||
} | ||
} |
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net9.0;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="../Testcontainers/Testcontainers.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,6 @@ | ||
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; |
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 |
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,35 @@ | ||
namespace Testcontainers.Sftp; | ||
|
||
public sealed class SftpContainerTest : IAsyncLifetime | ||
{ | ||
private readonly SftpContainer _sftpContainer = new SftpBuilder().Build(); | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return _sftpContainer.StartAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _sftpContainer.DisposeAsync().AsTask(); | ||
} | ||
|
||
[Fact] | ||
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] | ||
public async Task IsConnectedReturnsTrue() | ||
{ | ||
// Given | ||
var host = _sftpContainer.Hostname; | ||
|
||
var port = _sftpContainer.GetMappedPublicPort(SftpBuilder.SftpPort); | ||
|
||
using var sftpClient = new SftpClient(host, port, SftpBuilder.DefaultUsername, SftpBuilder.DefaultPassword); | ||
|
||
// When | ||
await sftpClient.ConnectAsync(CancellationToken.None) | ||
.ConfigureAwait(true); | ||
|
||
// Then | ||
Assert.True(sftpClient.IsConnected); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/Testcontainers.Sftp.Tests/Testcontainers.Sftp.Tests.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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net9.0</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<IsPublishable>false</IsPublishable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk"/> | ||
<PackageReference Include="coverlet.collector"/> | ||
<PackageReference Include="xunit.runner.visualstudio"/> | ||
<PackageReference Include="xunit"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="../../src/Testcontainers.Sftp/Testcontainers.Sftp.csproj"/> | ||
<ProjectReference Include="../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,5 @@ | ||
global using System.Threading; | ||
global using System.Threading.Tasks; | ||
global using DotNet.Testcontainers.Commons; | ||
global using Renci.SshNet; | ||
global using Xunit; |