Skip to content

Commit 7a9869a

Browse files
Asafimaardalis
andauthored
Added smtp server instance in functional tests using test-containers (ardalis#805)
Co-authored-by: Steve Smith <[email protected]>
1 parent 1d36ce1 commit 7a9869a

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

sample/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<PackageVersion Include="SQLite" Version="3.13.0" />
3535
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
3636
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
37+
<PackageVersion Include="Testcontainers" Version="3.9.0" />
3738
<PackageVersion Include="xunit" Version="2.9.1" />
3839
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
3940
</ItemGroup>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using DotNet.Testcontainers.Builders;
2+
using DotNet.Testcontainers.Containers;
3+
using Xunit;
4+
5+
namespace NimblePros.SampleToDo.FunctionalTests.ClassFixtures;
6+
7+
/// <summary>
8+
/// This class ensures that an SMTP server is running and shared between all tests in a specified class.
9+
/// </summary>
10+
public class SmtpServerFixture : IAsyncLifetime
11+
{
12+
private const string SmtpServerImageName = "jijiechen/papercut:latest";
13+
private const int SmtpServerListenPort = 25;
14+
15+
private IContainer? _container;
16+
17+
public async Task InitializeAsync()
18+
{
19+
_container = new ContainerBuilder()
20+
.WithName(Guid.NewGuid().ToString("D"))
21+
.WithImage(SmtpServerImageName)
22+
.WithPortBinding(SmtpServerListenPort, SmtpServerListenPort)
23+
.WithWaitStrategy(
24+
Wait
25+
.ForUnixContainer()
26+
.UntilMessageIsLogged("Server Ready", o => o.WithTimeout(TimeSpan.FromSeconds(30))))
27+
.Build();
28+
29+
await _container.StartAsync().ConfigureAwait(false);
30+
}
31+
32+
public async Task DisposeAsync()
33+
{
34+
if (_container != null)
35+
{
36+
await _container.StopAsync().ConfigureAwait(false);
37+
await _container.DisposeAsync().ConfigureAwait(false);
38+
_container = null;
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Ensures that the container is running and healthy.
44+
/// </summary>
45+
/// <exception cref="InvalidOperationException">
46+
/// Thrown when the SMTP server container was not created or is not running.
47+
/// This could be due to Docker not running or issues with the container image.
48+
/// In such cases, verify that Docker is running correctly.
49+
/// </exception>
50+
public void EnsureContainerIsRunning()
51+
{
52+
if (_container == null)
53+
{
54+
throw new InvalidOperationException("SMTP server container was not created. Ensure Docker is running and the container image is correct.");
55+
}
56+
57+
if (_container.State != TestcontainersStates.Running)
58+
{
59+
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.");
60+
}
61+
}
62+
}

sample/tests/NimblePros.SampleToDo.FunctionalTests/NimblePros.SampleToDo.FunctionalTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="FluentAssertions" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" />
12+
<PackageReference Include="Testcontainers" />
1213
<PackageReference Include="xunit" />
1314
<PackageReference Include="xunit.runner.visualstudio">
1415
<PrivateAssets>all</PrivateAssets>

sample/tests/NimblePros.SampleToDo.FunctionalTests/Projects/ProjectItemMarkComplete.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
using NimblePros.SampleToDo.Web.Endpoints.Projects;
77
using NimblePros.SampleToDo.Web.Projects;
88
using FluentAssertions;
9+
using NimblePros.SampleToDo.FunctionalTests.ClassFixtures;
910

1011
namespace NimblePros.SampleToDo.FunctionalTests.Projects;
1112

1213
[Collection("Sequential")]
13-
public class ProjectItemMarkComplete : IClassFixture<CustomWebApplicationFactory<Program>>
14+
public class ProjectItemMarkComplete :
15+
IClassFixture<CustomWebApplicationFactory<Program>>,
16+
IClassFixture<SmtpServerFixture>
1417
{
1518
private readonly HttpClient _client;
1619

17-
public ProjectItemMarkComplete(CustomWebApplicationFactory<Program> factory)
20+
public ProjectItemMarkComplete(CustomWebApplicationFactory<Program> factory, SmtpServerFixture smtpServer)
1821
{
1922
_client = factory.CreateClient();
23+
smtpServer.EnsureContainerIsRunning();
2024
}
2125

2226
[Fact]

0 commit comments

Comments
 (0)