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

Added smtp server instance in functional tests using test-containers #805

Merged
merged 3 commits into from
Oct 3, 2024
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
1 change: 1 addition & 0 deletions sample/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageVersion Include="SQLite" Version="3.13.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageVersion Include="Testcontainers" Version="3.9.0" />
<PackageVersion Include="xunit" Version="2.9.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using Xunit;

namespace NimblePros.SampleToDo.FunctionalTests.ClassFixtures;

/// <summary>
/// This class ensures that an SMTP server is running and shared between all tests in a specified class.
/// </summary>
public class SmtpServerFixture : IAsyncLifetime
{
private const string SmtpServerImageName = "jijiechen/papercut:latest";
private const int SmtpServerListenPort = 25;

private IContainer? _container;

public async Task InitializeAsync()
{
_container = new ContainerBuilder()
.WithName(Guid.NewGuid().ToString("D"))
.WithImage(SmtpServerImageName)
.WithPortBinding(SmtpServerListenPort, SmtpServerListenPort)
.WithWaitStrategy(
Wait
.ForUnixContainer()
.UntilMessageIsLogged("Server Ready", o => o.WithTimeout(TimeSpan.FromSeconds(30))))
.Build();

await _container.StartAsync().ConfigureAwait(false);
}

public async Task DisposeAsync()
{
if (_container != null)
{
await _container.StopAsync().ConfigureAwait(false);
await _container.DisposeAsync().ConfigureAwait(false);
_container = null;
}
}

/// <summary>
/// Ensures that the container is running and healthy.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown when the SMTP server container was not created or is not running.
/// This could be due to Docker not running or issues with the container image.
/// In such cases, verify that Docker is running correctly.
/// </exception>
public void EnsureContainerIsRunning()
{
if (_container == null)
{
throw new InvalidOperationException("SMTP server container was not created. Ensure Docker is running and the container image is correct.");
}

if (_container.State != TestcontainersStates.Running)
{
throw new InvalidOperationException("The SMTP server container is not running. Please verify that the SMTP server image is correctly configured and that Docker is functioning properly.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Testcontainers" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
using NimblePros.SampleToDo.Web.Endpoints.Projects;
using NimblePros.SampleToDo.Web.Projects;
using FluentAssertions;
using NimblePros.SampleToDo.FunctionalTests.ClassFixtures;

namespace NimblePros.SampleToDo.FunctionalTests.Projects;

[Collection("Sequential")]
public class ProjectItemMarkComplete : IClassFixture<CustomWebApplicationFactory<Program>>
public class ProjectItemMarkComplete :
IClassFixture<CustomWebApplicationFactory<Program>>,
IClassFixture<SmtpServerFixture>
{
private readonly HttpClient _client;

public ProjectItemMarkComplete(CustomWebApplicationFactory<Program> factory)
public ProjectItemMarkComplete(CustomWebApplicationFactory<Program> factory, SmtpServerFixture smtpServer)
{
_client = factory.CreateClient();
smtpServer.EnsureContainerIsRunning();
}

[Fact]
Expand Down
Loading