client-dotnet/tests/INetMock.Client.IntegrationTest/INetMockFixture.cs
Peter Kurfer d8143ffd3a
All checks were successful
agola/client-dotnet/Lint The run finished successfully
fix(tests): start container as part of test fixture
2022-09-26 16:33:59 +02:00

56 lines
1.7 KiB
C#

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";
private readonly ITestcontainersContainer _inetmockContainer;
private readonly Stream _memStream = new MemoryStream();
public INetMockFixture()
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
_inetmockContainer = new TestcontainersBuilder<TestcontainersContainer>()
.WithPrivileged(true)
.WithExposedPort(6767)
.WithPortBinding(6767, true)
.WithCreateContainerParametersModifier(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();
}
}