diff --git a/WireMock.Net Solution.sln.DotSettings b/WireMock.Net Solution.sln.DotSettings index d9257030..8d0a52ef 100644 --- a/WireMock.Net Solution.sln.DotSettings +++ b/WireMock.Net Solution.sln.DotSettings @@ -44,6 +44,7 @@ True True True + True True True True diff --git a/examples/WireMock.Net.TestcontainersExample/Program.cs b/examples/WireMock.Net.TestcontainersExample/Program.cs index b6ddb0c3..38a23149 100644 --- a/examples/WireMock.Net.TestcontainersExample/Program.cs +++ b/examples/WireMock.Net.TestcontainersExample/Program.cs @@ -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> GetImageOSAsync = new(async () => + private static readonly Lazy> GetImageOSAsync = new(async () => { if (TestcontainersSettings.OS.DockerEndpointAuthConfig == null) { diff --git a/src/WireMock.Net.Testcontainers/Utils/ContainerUtils.cs b/src/WireMock.Net.Testcontainers/Utils/TestcontainersUtils.cs similarity index 82% rename from src/WireMock.Net.Testcontainers/Utils/ContainerUtils.cs rename to src/WireMock.Net.Testcontainers/Utils/TestcontainersUtils.cs index ce0d8113..ae1a7fc9 100644 --- a/src/WireMock.Net.Testcontainers/Utils/ContainerUtils.cs +++ b/src/WireMock.Net.Testcontainers/Utils/TestcontainersUtils.cs @@ -7,8 +7,14 @@ using System.Threading.Tasks; namespace WireMock.Net.Testcontainers.Utils; -internal static class ContainerUtils +/// +/// Some utility methods for containers. +/// +public static class TestcontainersUtils { + /// + /// Get the OS platform of the Docker image. + /// public static Lazy> GetImageOSAsync = new(async () => { if (TestcontainersSettings.OS.DockerEndpointAuthConfig == null) diff --git a/src/WireMock.Net.Testcontainers/WireMockContainer.cs b/src/WireMock.Net.Testcontainers/WireMockContainer.cs index 35394dce..141f03a4 100644 --- a/src/WireMock.Net.Testcontainers/WireMockContainer.cs +++ b/src/WireMock.Net.Testcontainers/WireMockContainer.cs @@ -164,7 +164,7 @@ public sealed class WireMockContainer : DockerContainer private static async Task PathStartsWithContainerMappingsPath(string value) { - var imageOs = await ContainerUtils.GetImageOSAsync.Value; + var imageOs = await TestcontainersUtils.GetImageOSAsync.Value; return value.StartsWith(ContainerInfoProvider.Info[imageOs].MappingsPath); } diff --git a/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs b/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs index 48c54eae..9ec17b05 100644 --- a/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs +++ b/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs @@ -36,7 +36,7 @@ public sealed class WireMockContainerBuilder : ContainerBuilder @@ -124,9 +125,16 @@ public sealed class WireMockContainerBuilder : ContainerBuilder