Skip to content

Commit

Permalink
Fix WireMockContainerBuilder (duplicate entries) (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Dec 31, 2024
1 parent 485f7ad commit ab7ce37
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 55 deletions.
1 change: 1 addition & 0 deletions WireMock.Net Solution.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sigil/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stef/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=templated/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Testcontainers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Victoor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Webhook/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Webhooks/@EntryIndexedValue">True</s:Boolean>
Expand Down
124 changes: 79 additions & 45 deletions examples/WireMock.Net.TestcontainersExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,79 @@
// Copyright © WireMock.Net

using DotNet.Testcontainers.Configurations;
using System.Runtime.InteropServices;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using Newtonsoft.Json;
using WireMock.Net.Testcontainers;

namespace WireMock.Net.TestcontainersExample;

internal class Program
{
private static readonly ConsoleColor OriginalColor = Console.ForegroundColor;

private static async Task Main(string[] args)
{
var original = Console.ForegroundColor;
await TestLinux();

await TestAutomatic();

await TestLinuxWithVersionTag();

await TestLinuxAlpineWithVersionTag();

await TestWindowsWithVersionTag();

await TestWindows();

await TestCopy();
}

private static async Task TestWindows()
{
try
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Copy");
await TestCopyAsync();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("WithWindows");
await TestAsync("WithWindows");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestWindowsWithVersionTag()
{
try
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Automatic");
await TestAsync();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Windows");
await TestAsync("sheyenrath/wiremock.net-windows:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestLinux()
{
try
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Linux");
await TestAsync("sheyenrath/wiremock.net:1.6.5");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("WithLinux");
await TestAsync("WithLinux");
await Task.Delay(1_000);
}
catch (Exception e)
Expand All @@ -52,9 +82,12 @@ private static async Task Main(string[] args)
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestLinuxAlpineWithVersionTag()
{
try
{
Console.ForegroundColor = ConsoleColor.White;
Expand All @@ -68,14 +101,17 @@ private static async Task Main(string[] args)
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestLinuxWithVersionTag()
{
try
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("WithLinux");
await TestAsync("WithLinux");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Linux");
await TestAsync("sheyenrath/wiremock.net:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
Expand All @@ -84,43 +120,43 @@ private static async Task Main(string[] args)
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestAutomatic()
{
try
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Windows");
await TestAsync("sheyenrath/wiremock.net-windows:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Automatic");
await TestAsync();
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestCopy()
{
try
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("WithWindows");
await TestAsync("WithWindows");
await Task.Delay(1_000);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Copy");
await TestWindowsCopyAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}

private static async Task TestCopyAsync()
private static async Task TestWindowsCopyAsync()
{
var builder = new WireMockContainerBuilder()
.WithWatchStaticMappings(true)
Expand Down Expand Up @@ -152,21 +188,25 @@ private static async Task TestCopyAsync()

await Task.Delay(1_000);

//Console.WriteLine("Press any key to stop.");
//Console.ReadKey();

await container.StopAsync();
}

private static async Task TestAsync(string? image = null)
{
var mappingsPath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", "WireMock.Net.Console.NET6", "__admin", "mappings");

var dummyNetwork = new NetworkBuilder()
.WithName($"Dummy Network for {image ?? "null"}")
.WithReuse(true)
.WithCleanUp(true)
.Build();

var builder = new WireMockContainerBuilder()
.WithNetwork(dummyNetwork)
.WithAdminUserNameAndPassword("x", "y")
.WithMappings(mappingsPath)
.WithWatchStaticMappings(true)
.WithAutoRemove(true)
// .WithAutoRemove(true)
.WithCleanUp(true);

if (image != null)
Expand Down Expand Up @@ -202,16 +242,10 @@ private static async Task TestAsync(string? image = null)
var result = await client.GetStringAsync("/static/mapping");
Console.WriteLine("result = " + result);

//if (image == null)
//{
// Console.WriteLine("Press any key to stop.");
// Console.ReadKey();
//}

await container.StopAsync();
}

private static Lazy<Task<OSPlatform>> GetImageOSAsync = new(async () =>
private static readonly Lazy<Task<OSPlatform>> GetImageOSAsync = new(async () =>
{
if (TestcontainersSettings.OS.DockerEndpointAuthConfig == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@

namespace WireMock.Net.Testcontainers.Utils;

internal static class ContainerUtils
/// <summary>
/// Some utility methods for containers.
/// </summary>
public static class TestcontainersUtils
{
/// <summary>
/// Get the OS platform of the Docker image.
/// </summary>
public static Lazy<Task<OSPlatform>> GetImageOSAsync = 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 ContainerUtils.GetImageOSAsync.Value;
var imageOs = await TestcontainersUtils.GetImageOSAsync.Value;

return value.StartsWith(ContainerInfoProvider.Info[imageOs].MappingsPath);
}
Expand Down
22 changes: 15 additions & 7 deletions src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs
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 ??= ContainerUtils.GetImageOSAsync.Value.GetAwaiter().GetResult();
_imageOS ??= TestcontainersUtils.GetImageOSAsync.Value.GetAwaiter().GetResult();
return WithImage(_imageOS.Value);
}

Expand Down Expand Up @@ -108,9 +108,10 @@ public WireMockContainerBuilder WithReadStaticMappings()
[PublicAPI]
public WireMockContainerBuilder WithWatchStaticMappings(bool includeSubDirectories)
{
return Merge(DockerResourceConfiguration, DockerResourceConfiguration.WithWatchStaticMappings(includeSubDirectories))
.WithCommand("--WatchStaticMappings true")
.WithCommand($"--WatchStaticMappingsInSubdirectories {includeSubDirectories}");
DockerResourceConfiguration.WithWatchStaticMappings(includeSubDirectories);
return
WithCommand("--WatchStaticMappings true").
WithCommand("--WatchStaticMappingsInSubdirectories", includeSubDirectories);
}

/// <summary>
Expand All @@ -124,9 +125,16 @@ public WireMockContainerBuilder WithMappings(string path, bool includeSubDirecto
{
Guard.NotNullOrEmpty(path);

return Merge(DockerResourceConfiguration, DockerResourceConfiguration.WithStaticMappingsPath(path))
.WithReadStaticMappings()
.WithCommand($"--WatchStaticMappingsInSubdirectories {includeSubDirectories}");
DockerResourceConfiguration.WithStaticMappingsPath(path);

return
WithReadStaticMappings().
WithCommand("--WatchStaticMappingsInSubdirectories", includeSubDirectories);
}

private WireMockContainerBuilder WithCommand(string param, bool value)
{
return !value ? this : WithCommand($"{param} true");
}

private WireMockContainerBuilder(WireMockConfiguration dockerResourceConfiguration) : base(dockerResourceConfiguration)
Expand Down
19 changes: 19 additions & 0 deletions test/WireMock.Net.Tests/Facts/RunOnDockerPlatformFact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright © WireMock.Net
#if NET6_0_OR_GREATER
using System.Runtime.InteropServices;
using WireMock.Net.Testcontainers.Utils;
using Xunit;

namespace WireMock.Net.Tests.Facts;

public sealed class RunOnDockerPlatformFact : FactAttribute
{
public RunOnDockerPlatformFact(string platform)
{
if (TestcontainersUtils.GetImageOSAsync.Value.Result != OSPlatform.Create(platform))
{
Skip = $"Only run test when Docker OS Platform {platform} is used.";
}
}
}
#endif
24 changes: 23 additions & 1 deletion test/WireMock.Net.Tests/Testcontainers/TestcontainersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using FluentAssertions;
using FluentAssertions.Execution;
using WireMock.Net.Testcontainers;
using WireMock.Net.Tests.Facts;
using Xunit;

namespace WireMock.Net.Tests.Testcontainers;
Expand All @@ -27,7 +29,27 @@ public async Task WireMockContainer_Build_WithNoImage_And_StartAsync_and_StopAsy

await StartTestAndStopAsync(wireMockContainer);
}


// https://github.com/testcontainers/testcontainers-dotnet/issues/1322
[RunOnDockerPlatformFact("Linux")]
public async Task WireMockContainer_Build_WithNoImageAndNetwork_And_StartAsync_and_StopAsync()
{
// Act
var dummyNetwork = new NetworkBuilder()
.WithName("Dummy Network for TestcontainersTests")
.WithCleanUp(true)
.Build();

var wireMockContainer = new WireMockContainerBuilder()
.WithNetwork(dummyNetwork)
.WithWatchStaticMappings(true)
.WithAutoRemove(true)
.WithCleanUp(true)
.Build();

await StartTestAndStopAsync(wireMockContainer);
}

[Fact]
public async Task WireMockContainer_Build_WithImage_And_StartAsync_and_StopAsync()
{
Expand Down

0 comments on commit ab7ce37

Please sign in to comment.