diff --git a/Directory.Build.props b/Directory.Build.props index 37a8fa98..49e65a08 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ - 1.6.7-preview-01 + 1.6.7-preview-02 WireMock.Net-Logo.png https://github.com/WireMock-Net/WireMock.Net Apache-2.0 diff --git a/examples/WireMock.Net.TestcontainersExample/Program.cs b/examples/WireMock.Net.TestcontainersExample/Program.cs index 7f5d804f..b6ddb0c3 100644 --- a/examples/WireMock.Net.TestcontainersExample/Program.cs +++ b/examples/WireMock.Net.TestcontainersExample/Program.cs @@ -1,5 +1,7 @@ // Copyright © WireMock.Net +using DotNet.Testcontainers.Configurations; +using System.Runtime.InteropServices; using Newtonsoft.Json; using WireMock.Net.Testcontainers; @@ -129,13 +131,16 @@ private static async Task TestCopyAsync() await container.StartAsync(); - try - { - await container.CopyAsync(@"C:\temp-wiremock\__admin\mappings\StefBodyAsFileExample.json", "/app/__admin/mappings"); - } - catch (Exception e) + if (await GetImageOSAsync.Value == OSPlatform.Linux) { - Console.WriteLine(e); + try + { + await container.CopyAsync(@"C:\temp-wiremock\__admin\mappings\StefBodyAsFileExample.json", "/app/__admin/mappings"); + } + catch (Exception e) + { + Console.WriteLine(e); + } } Console.WriteLine("PublicUrl = " + container.GetPublicUrl()); @@ -205,4 +210,18 @@ private static async Task TestAsync(string? image = null) await container.StopAsync(); } + + private static Lazy> GetImageOSAsync = new(async () => + { + if (TestcontainersSettings.OS.DockerEndpointAuthConfig == null) + { + throw new InvalidOperationException($"The {nameof(TestcontainersSettings.OS.DockerEndpointAuthConfig)} is null. Check if Docker is started."); + } + + using var dockerClientConfig = TestcontainersSettings.OS.DockerEndpointAuthConfig.GetDockerClientConfiguration(); + using var dockerClient = dockerClientConfig.CreateClient(); + + var version = await dockerClient.System.GetVersionAsync(); + return version.Os.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) >= 0 ? OSPlatform.Windows : OSPlatform.Linux; + }); } \ No newline at end of file diff --git a/src/WireMock.Net.Testcontainers/Utils/PlatformUtils.cs b/src/WireMock.Net.Testcontainers/Utils/ContainerUtils.cs similarity index 68% rename from src/WireMock.Net.Testcontainers/Utils/PlatformUtils.cs rename to src/WireMock.Net.Testcontainers/Utils/ContainerUtils.cs index 2d832cfd..ce0d8113 100644 --- a/src/WireMock.Net.Testcontainers/Utils/PlatformUtils.cs +++ b/src/WireMock.Net.Testcontainers/Utils/ContainerUtils.cs @@ -7,23 +7,8 @@ namespace WireMock.Net.Testcontainers.Utils; -internal static class PlatformUtils +internal static class ContainerUtils { - public static OSPlatform GetCurrentOSPlatform() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return OSPlatform.Windows; - } - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - return OSPlatform.Linux; - } - - throw new NotSupportedException("The current OSPlatform is not supported."); - } - 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 56568d4d..35394dce 100644 --- a/src/WireMock.Net.Testcontainers/WireMockContainer.cs +++ b/src/WireMock.Net.Testcontainers/WireMockContainer.cs @@ -164,7 +164,7 @@ protected override ValueTask DisposeAsyncCore() private static async Task PathStartsWithContainerMappingsPath(string value) { - var imageOs = await PlatformUtils.GetImageOSAsync.Value; + var imageOs = await ContainerUtils.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 83d2eafc..48c54eae 100644 --- a/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs +++ b/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs @@ -36,7 +36,7 @@ public WireMockContainerBuilder() : this(new WireMockConfiguration()) [PublicAPI] public WireMockContainerBuilder WithImage() { - _imageOS ??= PlatformUtils.GetImageOSAsync.Value.GetAwaiter().GetResult(); + _imageOS ??= ContainerUtils.GetImageOSAsync.Value.GetAwaiter().GetResult(); return WithImage(_imageOS.Value); }