-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Reintroduce Papercut module (#1268)
Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
- Loading branch information
1 parent
f4966d8
commit 159e4bf
Showing
13 changed files
with
292 additions
and
1 deletion.
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
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,83 @@ | ||
namespace Testcontainers.Papercut; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class PapercutBuilder : ContainerBuilder<PapercutBuilder, PapercutContainer, PapercutConfiguration> | ||
{ | ||
public const string PapercutImage = "changemakerstudiosus/papercut-smtp:latest"; | ||
|
||
public const ushort SmtpPort = 25; | ||
|
||
public const ushort HttpPort = 80; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PapercutBuilder" /> class. | ||
/// </summary> | ||
public PapercutBuilder() | ||
: this(new PapercutConfiguration()) | ||
{ | ||
DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PapercutBuilder" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
private PapercutBuilder(PapercutConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
DockerResourceConfiguration = resourceConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override PapercutConfiguration DockerResourceConfiguration { get; } | ||
|
||
/// <inheritdoc /> | ||
public override PapercutContainer Build() | ||
{ | ||
Validate(); | ||
return new PapercutContainer(DockerResourceConfiguration); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override PapercutBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(PapercutImage) | ||
.WithPortBinding(SmtpPort, true) | ||
.WithPortBinding(HttpPort, true) | ||
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => | ||
request.ForPath("/health").ForPort(HttpPort).ForResponseMessageMatching(IsInstanceHealthyAsync))); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override PapercutBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new PapercutConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override PapercutBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new PapercutConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override PapercutBuilder Merge(PapercutConfiguration oldValue, PapercutConfiguration newValue) | ||
{ | ||
return new PapercutBuilder(new PapercutConfiguration(oldValue, newValue)); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the instance is healthy or not. | ||
/// </summary> | ||
/// <param name="response">The HTTP response that contains the health information.</param> | ||
/// <returns>A value indicating whether the instance is healthy or not.</returns> | ||
private static async Task<bool> IsInstanceHealthyAsync(HttpResponseMessage response) | ||
{ | ||
var body = await response.Content.ReadAsStringAsync() | ||
.ConfigureAwait(false); | ||
|
||
return "Papercut WebUI server started successfully.".Equals(body, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} |
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,53 @@ | ||
namespace Testcontainers.Papercut; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class PapercutConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PapercutConfiguration" /> class. | ||
/// </summary> | ||
public PapercutConfiguration() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PapercutConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public PapercutConfiguration(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="PapercutConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public PapercutConfiguration(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="PapercutConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public PapercutConfiguration(PapercutConfiguration resourceConfiguration) | ||
: this(new PapercutConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PapercutConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public PapercutConfiguration(PapercutConfiguration oldValue, PapercutConfiguration newValue) | ||
: base(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,29 @@ | ||
namespace Testcontainers.Papercut; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class PapercutContainer : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PapercutContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
public PapercutContainer(PapercutConfiguration configuration) | ||
: base(configuration) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the SMTP port. | ||
/// </summary> | ||
public ushort SmtpPort => GetMappedPublicPort(PapercutBuilder.SmtpPort); | ||
|
||
/// <summary> | ||
/// Gets the Papercut base address. | ||
/// </summary> | ||
/// <returns>The Papercut base address.</returns> | ||
public string GetBaseAddress() | ||
{ | ||
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(PapercutBuilder.HttpPort)).ToString(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Testcontainers.Papercut/Testcontainers.Papercut.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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net8.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,8 @@ | ||
global using System; | ||
global using System.Net.Http; | ||
global using System.Threading.Tasks; | ||
global using Docker.DotNet.Models; | ||
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 |
65 changes: 65 additions & 0 deletions
65
tests/Testcontainers.Papercut.Tests/PapercutContainerTest.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,65 @@ | ||
namespace Testcontainers.Papercut; | ||
|
||
public sealed class PapercutContainerTest : IAsyncLifetime | ||
{ | ||
private readonly PapercutContainer _papercutContainer = new PapercutBuilder().Build(); | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return _papercutContainer.StartAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _papercutContainer.DisposeAsync().AsTask(); | ||
} | ||
|
||
[Fact] | ||
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] | ||
public async Task ReceivesSentMessage() | ||
{ | ||
// Given | ||
const string subject = "Test"; | ||
|
||
using var smtpClient = new SmtpClient(_papercutContainer.Hostname, _papercutContainer.SmtpPort); | ||
|
||
using var httpClient = new HttpClient(); | ||
httpClient.BaseAddress = new Uri(_papercutContainer.GetBaseAddress()); | ||
|
||
// When | ||
smtpClient.Send("from@example.com", "to@example.com", subject, "A test message"); | ||
|
||
var messagesJson = await httpClient.GetStringAsync("/api/messages") | ||
.ConfigureAwait(true); | ||
|
||
var jsonDocument = JsonDocument.Parse(messagesJson); | ||
var messages = jsonDocument.RootElement.GetProperty("messages").Deserialize<Message[]>(); | ||
|
||
// Then | ||
Assert.Single(messages, message => subject.Equals(message.Subject)); | ||
} | ||
|
||
private readonly struct Message | ||
{ | ||
[JsonConstructor] | ||
public Message(string id, string subject, string size, DateTime createdAt) | ||
{ | ||
Id = id; | ||
Subject = subject; | ||
Size = size; | ||
CreatedAt = createdAt; | ||
} | ||
|
||
[JsonPropertyName("id")] | ||
public string Id { get; } | ||
|
||
[JsonPropertyName("subject")] | ||
public string Subject { get; } | ||
|
||
[JsonPropertyName("size")] | ||
public string Size { get; } | ||
|
||
[JsonPropertyName("createdAt")] | ||
public DateTime CreatedAt { get; } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/Testcontainers.Papercut.Tests/Testcontainers.Papercut.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>net8.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.Papercut/Testcontainers.Papercut.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,8 @@ | ||
global using System; | ||
global using System.Net.Http; | ||
global using System.Net.Mail; | ||
global using System.Text.Json; | ||
global using System.Text.Json.Serialization; | ||
global using System.Threading.Tasks; | ||
global using DotNet.Testcontainers.Commons; | ||
global using Xunit; |