client-dotnet/tests/INetMock.Client.IntegrationTest/INetMockFixture.cs

56 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using Xunit;
namespace INetMock.Client.IntegrationTest;
public class INetMockFixture : IAsyncLifetime
{
private const string DefaultINetMockSocketPath = "unix:///var/run/inetmock/inetmock.sock";
2023-09-15 17:01:48 +00:00
private readonly IContainer _inetmockContainer;
private readonly Stream _memStream = new MemoryStream();
public INetMockFixture()
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
2023-09-15 17:01:48 +00:00
_inetmockContainer = new ContainerBuilder()
.WithPrivileged(true)
.WithExposedPort(6767)
.WithPortBinding(6767, true)
2023-09-15 17:01:48 +00:00
.WithCreateParameterModifier(parameters => { parameters.User = "root"; })
.WithEnvironment("INETMOCK_API_LISTEN", "tcp://0.0.0.0:6767")
.WithImage("registry.gitlab.com/inetmock/inetmock:latest")
.WithName("inetmock")
.WithWaitStrategy(Wait.ForUnixContainer().UntilOperationIsSucceeded(() =>
{
Thread.Sleep(5000);
return true;
}, 1))
.Build();
}
public string INetMockSocketPath { get; private set; } = DefaultINetMockSocketPath;
public async Task InitializeAsync()
{
await using (_memStream)
{
await _inetmockContainer.StartAsync();
}
INetMockSocketPath = $"http://{_inetmockContainer.Hostname}:{_inetmockContainer.GetMappedPublicPort(6767)}";
}
public async Task DisposeAsync()
{
await _inetmockContainer.StopAsync();
}
}