mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:44:31 +01:00
WireMock.Net.Testcontainers: implement watching the static mapping folder for changes (#1189)
* WireMock.Net.Testcontainers: implement watching the static mapping files + folder for changes * ReloadStaticMappings * fix * . * . * . * . * . * . * . * CopyAsync * <VersionPrefix>1.6.7-preview-02</VersionPrefix> * <VersionPrefix>1.6.7-preview-03</VersionPrefix>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using DotNet.Testcontainers.Configurations;
|
||||
using System.Runtime.InteropServices;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Net.Testcontainers;
|
||||
@@ -12,6 +13,32 @@ internal class Program
|
||||
{
|
||||
var original = Console.ForegroundColor;
|
||||
|
||||
try
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.DarkGreen;
|
||||
Console.WriteLine("Copy");
|
||||
await TestCopyAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.ForegroundColor = original;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.DarkRed;
|
||||
Console.WriteLine("Automatic");
|
||||
await TestAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.ForegroundColor = original;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
@@ -91,17 +118,44 @@ internal class Program
|
||||
{
|
||||
Console.ForegroundColor = original;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
private static async Task TestCopyAsync()
|
||||
{
|
||||
var builder = new WireMockContainerBuilder()
|
||||
.WithWatchStaticMappings(true)
|
||||
.WithAutoRemove(true)
|
||||
.WithCleanUp(true);
|
||||
|
||||
var container = builder.Build();
|
||||
|
||||
await container.StartAsync();
|
||||
|
||||
if (await GetImageOSAsync.Value == OSPlatform.Linux)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("Automatic");
|
||||
await TestAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.ForegroundColor = original;
|
||||
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());
|
||||
|
||||
var adminClient = container.CreateWireMockAdminClient();
|
||||
|
||||
var mappings = await adminClient.GetMappingsAsync();
|
||||
Console.WriteLine("mappings = " + JsonConvert.SerializeObject(mappings, Formatting.Indented));
|
||||
|
||||
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)
|
||||
@@ -129,10 +183,12 @@ internal class Program
|
||||
|
||||
await container.StartAsync();
|
||||
|
||||
await Task.Delay(1_000);
|
||||
await container.ReloadStaticMappingsAsync();
|
||||
|
||||
var logs = await container.GetLogsAsync(DateTime.Now.AddDays(-1));
|
||||
Console.WriteLine("logs = " + logs.Stdout);
|
||||
//var logs = await container.GetLogsAsync(DateTime.Now.AddDays(-1));
|
||||
//Console.WriteLine("logs = " + logs.Stdout);
|
||||
|
||||
Console.WriteLine("PublicUrl = " + container.GetPublicUrl());
|
||||
|
||||
var restEaseApiClient = container.CreateWireMockAdminClient();
|
||||
|
||||
@@ -140,12 +196,32 @@ internal class Program
|
||||
Console.WriteLine("settings = " + JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
var mappings = await restEaseApiClient.GetMappingsAsync();
|
||||
Console.WriteLine("mappings = " + JsonConvert.SerializeObject(mappings, Formatting.Indented));
|
||||
Console.WriteLine("mappingsStef = " + JsonConvert.SerializeObject(mappings, Formatting.Indented));
|
||||
|
||||
var client = container.CreateClient();
|
||||
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 () =>
|
||||
{
|
||||
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;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user