-
-
Notifications
You must be signed in to change notification settings - Fork 338
feat: Add SFTP module #1362
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
Merged
Merged
feat: Add SFTP module #1362
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5639786
feat: implemented SFTP container atmoz/sftp
wim07101993 524ea99
Update cicd.yml
wim07101993 8f7fb73
Merge branch 'develop' into feat/sftp
wim07101993 92abb7a
Merge branch 'develop' into feat/sftp
wim07101993 ecbfe11
Merge branch 'develop' into feat/sftp
wim07101993 15b36a9
chore: Align repo standards
HofmeisterAn f49c3a5
chore: Remove BOM
HofmeisterAn 439bff9
Merge remote-tracking branch 'origin/develop' into fork/wim07101993/f…
HofmeisterAn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.