From d8143ffd3aa5d9b072766fbe611452e994ffba01 Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Mon, 26 Sep 2022 16:15:57 +0200 Subject: [PATCH] fix(tests): start container as part of test fixture --- .agola/config.yml | 1 + .../INetMock.Client.IntegrationTest.csproj | 1 + .../INetMockFixture.cs | 51 ++++++++++++++++--- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/.agola/config.yml b/.agola/config.yml index 237db17..4ae7e78 100644 --- a/.agola/config.yml +++ b/.agola/config.yml @@ -22,6 +22,7 @@ runs: environment: DOCKER_HOST: tcp://127.0.0.1:2375 INETMOCK_SOCKET: http://127.0.0.1:6767 + TESTCONTAINERS_RYUK_DISABLED: "true" - image: code.icb4dc0.de/prskr/ci-images/dind:latest privileged: true steps: diff --git a/tests/INetMock.Client.IntegrationTest/INetMock.Client.IntegrationTest.csproj b/tests/INetMock.Client.IntegrationTest/INetMock.Client.IntegrationTest.csproj index 7df0678..fd30480 100644 --- a/tests/INetMock.Client.IntegrationTest/INetMock.Client.IntegrationTest.csproj +++ b/tests/INetMock.Client.IntegrationTest/INetMock.Client.IntegrationTest.csproj @@ -12,6 +12,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/INetMock.Client.IntegrationTest/INetMockFixture.cs b/tests/INetMock.Client.IntegrationTest/INetMockFixture.cs index f4c6120..7379ce2 100644 --- a/tests/INetMock.Client.IntegrationTest/INetMockFixture.cs +++ b/tests/INetMock.Client.IntegrationTest/INetMockFixture.cs @@ -1,19 +1,56 @@ 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 +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() { - INetMockSocketPath = Environment.GetEnvironmentVariable("INETMOCK_SOCKET") ?? DefaultINetMockSocketPath; - if (INetMockSocketPath.StartsWith("http:")) - { - AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); - } + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); + + + _inetmockContainer = new TestcontainersBuilder() + .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; } + 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(); + } }