Skip to content

Commit

Permalink
Force testcontainer/Elasticsearch output onto IMessageSink
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Oct 16, 2023
1 parent ab45515 commit 63b658d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
<ProjectReference Include="$(SrcInstrumentations)\Elastic.Apm.Elasticsearch\Elastic.Apm.Elasticsearch.csproj" />
<ProjectReference Include="$(SolutionRoot)\src\Elastic.Apm\Elastic.Apm.csproj" />
<ProjectReference Include="$(SolutionRoot)\test\Elastic.Apm.Tests.Utilities\Elastic.Apm.Tests.Utilities.csproj" />
<ProjectReference Include="..\..\integrations\Elastic.Apm.AspNetCore.Tests\Elastic.Apm.AspNetCore.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,42 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Elastic.Transport;
using Microsoft.Extensions.Logging;
using Testcontainers.Elasticsearch;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace Elastic.Clients.Elasticsearch.Tests;

public sealed class ElasticsearchTestFixture : IAsyncLifetime
{
public ElasticsearchContainer Container { get; } = new ElasticsearchBuilder().Build();
private readonly IMessageSink _sink;
public ElasticsearchContainer Container { get; }

public ElasticsearchClient? Client { get; private set; }

public ElasticsearchTestFixture(IMessageSink sink)
{
_sink = sink;
Container = new ElasticsearchBuilder().Build();
}


public async Task InitializeAsync()
{
await Container.StartAsync();

var (stdOut, stdErr) = await Container.GetLogsAsync();

_sink.OnMessage(new DiagnosticMessage(stdOut));
_sink.OnMessage(new DiagnosticMessage(stdErr));

var settings = new ElasticsearchClientSettings(new Uri(Container.GetConnectionString()));
settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);

Expand All @@ -36,4 +55,22 @@ async Task IAsyncLifetime.DisposeAsync()
await Container.DisposeAsync();
}
}

private class MessageSinkLogger : ILogger
{
private readonly IMessageSink _messageSink;

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;

public MessageSinkLogger(IMessageSink sink)
{
_messageSink = sink;
_messageSink.OnMessage(new DiagnosticMessage($"Started {nameof(MessageSinkLogger)}"));
}

public bool IsEnabled(LogLevel logLevel) => true;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) =>
_messageSink.OnMessage(new DiagnosticMessage(formatter(state, exception)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using DotNet.Testcontainers;
using DotNet.Testcontainers.Configurations;
using Elastic.Apm;
using Elastic.Apm.Api;
using Elastic.Apm.AspNetCore.Tests;
using Elastic.Apm.DiagnosticSource;
using Elastic.Apm.Elasticsearch;
using Elastic.Apm.Tests.Utilities;
Expand All @@ -21,6 +24,7 @@ public class ElasticsearchTests : IClassFixture<ElasticsearchTestFixture>
private readonly ElasticsearchTestFixture _esClientListenerFixture;
private readonly ElasticsearchClient _client;


public ElasticsearchTests(ITestOutputHelper testOutputHelper, ElasticsearchTestFixture esClientListenerFixture)
{
_testOutputHelper = testOutputHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"diagnosticMessages": true
}

0 comments on commit 63b658d

Please sign in to comment.