Fix WireMockContainerBuilder (duplicate entries) (#1222)

This commit is contained in:
Stef Heyenrath
2024-12-31 18:24:29 +01:00
committed by GitHub
parent 485f7ad952
commit ab7ce37e7e
7 changed files with 199 additions and 109 deletions

View File

@@ -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>

View File

@@ -1,7 +1,8 @@
// 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;
@@ -9,100 +10,27 @@ 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();
try
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Copy");
await TestCopyAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
}
await TestAutomatic();
try
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Automatic");
await TestAsync();
}
finally
{
Console.ForegroundColor = original;
}
await TestLinuxWithVersionTag();
try
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Linux");
await TestAsync("sheyenrath/wiremock.net:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
}
await TestLinuxAlpineWithVersionTag();
try
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Linux Alpine");
await TestAsync("sheyenrath/wiremock.net-alpine:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
}
await TestWindowsWithVersionTag();
try
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("WithLinux");
await TestAsync("WithLinux");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = original;
}
await TestWindows();
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);
}
finally
{
Console.ForegroundColor = original;
}
await TestCopy();
}
private static async Task TestWindows()
{
try
{
Console.ForegroundColor = ConsoleColor.Blue;
@@ -116,11 +44,119 @@ internal class Program
}
finally
{
Console.ForegroundColor = original;
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestCopyAsync()
private static async Task TestWindowsWithVersionTag()
{
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);
}
finally
{
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestLinux()
{
try
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("WithLinux");
await TestAsync("WithLinux");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestLinuxAlpineWithVersionTag()
{
try
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Linux Alpine");
await TestAsync("sheyenrath/wiremock.net-alpine:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestLinuxWithVersionTag()
{
try
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Linux");
await TestAsync("sheyenrath/wiremock.net:1.6.5");
await Task.Delay(1_000);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestAutomatic()
{
try
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Automatic");
await TestAsync();
}
finally
{
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestCopy()
{
try
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Copy");
await TestWindowsCopyAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ForegroundColor = OriginalColor;
}
}
private static async Task TestWindowsCopyAsync()
{
var builder = new WireMockContainerBuilder()
.WithWatchStaticMappings(true)
@@ -152,9 +188,6 @@ internal class Program
await Task.Delay(1_000);
//Console.WriteLine("Press any key to stop.");
//Console.ReadKey();
await container.StopAsync();
}
@@ -162,11 +195,18 @@ internal class Program
{
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)
@@ -202,16 +242,10 @@ internal class Program
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)
{

View File

@@ -7,8 +7,14 @@ using System.Threading.Tasks;
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)

View File

@@ -164,7 +164,7 @@ public sealed class WireMockContainer : DockerContainer
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);
}

View File

@@ -36,7 +36,7 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
[PublicAPI]
public WireMockContainerBuilder WithImage()
{
_imageOS ??= ContainerUtils.GetImageOSAsync.Value.GetAwaiter().GetResult();
_imageOS ??= TestcontainersUtils.GetImageOSAsync.Value.GetAwaiter().GetResult();
return WithImage(_imageOS.Value);
}
@@ -108,9 +108,10 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
[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>
@@ -124,9 +125,16 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
{
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)

View File

@@ -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

View File

@@ -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;
@@ -27,7 +29,27 @@ public class TestcontainersTests
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()
{