Skip to content

Commit

Permalink
Update TestcontainersTests to change network setup for Windows OS.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Jan 2, 2025
1 parent 1198fea commit eae3d43
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class TestcontainersUtils
/// <summary>
/// Get the OS platform of the Docker image.
/// </summary>
public static Lazy<Task<OSPlatform>> GetImageOSAsync = new(async () =>
public static Lazy<Task<OSPlatform>> GetDockerImageOSAsync = new(async () =>
{
if (TestcontainersSettings.OS.DockerEndpointAuthConfig == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.Testcontainers/WireMockContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected override ValueTask DisposeAsyncCore()

private static async Task<bool> PathStartsWithContainerMappingsPath(string value)
{
var imageOs = await TestcontainersUtils.GetImageOSAsync.Value;
var imageOs = await TestcontainersUtils.GetDockerImageOSAsync.Value;

return value.StartsWith(ContainerInfoProvider.Info[imageOs].MappingsPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public WireMockContainerBuilder() : this(new WireMockConfiguration())
[PublicAPI]
public WireMockContainerBuilder WithImage()
{
_imageOS ??= TestcontainersUtils.GetImageOSAsync.Value.GetAwaiter().GetResult();
_imageOS ??= TestcontainersUtils.GetDockerImageOSAsync.Value.GetAwaiter().GetResult();
return WithImage(_imageOS.Value);
}

Expand Down
2 changes: 1 addition & 1 deletion test/WireMock.Net.Tests/Facts/RunOnDockerPlatformFact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class RunOnDockerPlatformFact : FactAttribute
{
public RunOnDockerPlatformFact(string platform)
{
if (TestcontainersUtils.GetImageOSAsync.Value.Result != OSPlatform.Create(platform))
if (TestcontainersUtils.GetDockerImageOSAsync.Value.Result != OSPlatform.Create(platform))
{
Skip = $"Only run test when Docker OS Platform {platform} is used.";
}
Expand Down
28 changes: 20 additions & 8 deletions test/WireMock.Net.Tests/Testcontainers/TestcontainersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using FluentAssertions;
using FluentAssertions.Execution;
using WireMock.Net.Testcontainers;
using WireMock.Net.Tests.Facts;
using WireMock.Net.Testcontainers.Utils;
using Xunit;

namespace WireMock.Net.Tests.Testcontainers;
Expand All @@ -30,15 +30,22 @@ public async Task WireMockContainer_Build_WithNoImage_And_StartAsync_and_StopAsy
await StartTestAndStopAsync(wireMockContainer);
}

// https://github.com/testcontainers/testcontainers-dotnet/issues/1322
[RunOnDockerPlatformFact("Linux")]

[Fact]
public async Task WireMockContainer_Build_WithNoImageAndNetwork_And_StartAsync_and_StopAsync()
{
// Act
var dummyNetwork = new NetworkBuilder()
var dummyNetworkBuilder = new NetworkBuilder()
.WithName("Dummy Network for TestcontainersTests")
.WithCleanUp(true)
.Build();
.WithCleanUp(true);

if (await IsDockerImageOSWindows())
{
// https://github.com/testcontainers/testcontainers-dotnet/issues/1322
dummyNetworkBuilder = dummyNetworkBuilder.WithCreateParameterModifier(m => m.Driver = "nat");
}

var dummyNetwork = dummyNetworkBuilder.Build();

var wireMockContainer = new WireMockContainerBuilder()
.WithNetwork(dummyNetwork)
Expand All @@ -61,7 +68,7 @@ public async Task WireMockContainer_Build_WithImage_And_StartAsync_and_StopAsync
.WithCleanUp(true)
.WithAdminUserNameAndPassword(adminUsername, adminPassword);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (await IsDockerImageOSWindows())
{
wireMockContainerBuilder = wireMockContainerBuilder.WithWindowsImage();
}
Expand All @@ -86,7 +93,7 @@ public async Task WireMockContainer_Build_WithImageAsText_And_StartAsync_and_Sto
.WithCleanUp(true)
.WithAdminUserNameAndPassword(adminUsername, adminPassword);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (await IsDockerImageOSWindows())
{
wireMockContainerBuilder = wireMockContainerBuilder.WithImage("sheyenrath/wiremock.net-windows");
}
Expand Down Expand Up @@ -123,5 +130,10 @@ private static async Task StartTestAndStopAsync(WireMockContainer wireMockContai
await wireMockContainer.StopAsync();
}
}

private static async Task<bool> IsDockerImageOSWindows()
{
return (await TestcontainersUtils.GetDockerImageOSAsync.Value) == OSPlatform.Windows;
}
}
#endif

0 comments on commit eae3d43

Please sign in to comment.