From 425c10315a950400c864def007be42ddc976455d Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 22 Oct 2024 17:18:42 +0200 Subject: [PATCH 1/4] Increase logging of testcontainer --- Directory.Packages.props | 16 +++--- sample/StackExchangeRedisSample/Program.cs | 2 +- .../ElasticsearchFixture.cs | 35 +++++++++++- .../Fixture/MongoFixture.cs | 31 +++++++++- .../SqlServerFixture.cs | 21 ++++++- .../ProfilingSessionTests.cs | 19 +++++-- .../ElasticsearchTestFixture.cs | 57 +++++++++++-------- .../AdoNet/MySqlFixture.cs | 35 +++++++++++- .../AdoNet/OracleSqlFixture.cs | 37 +++++++++++- .../AdoNet/PostgreSqlFixture.cs | 35 +++++++++++- .../AdoNet/SqlServerFixture.cs | 37 +++++++++++- .../Kafka/KafkaFixture.cs | 35 +++++++++++- .../RabbitMq/RabbitMqFixture.cs | 35 +++++++++++- 13 files changed, 332 insertions(+), 63 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 265aa2053..dc7fefcd7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -65,14 +65,14 @@ - - - - - - - - + + + + + + + + diff --git a/sample/StackExchangeRedisSample/Program.cs b/sample/StackExchangeRedisSample/Program.cs index 1bd03eb4c..28b7029e7 100644 --- a/sample/StackExchangeRedisSample/Program.cs +++ b/sample/StackExchangeRedisSample/Program.cs @@ -44,5 +44,5 @@ await Agent.Tracer.CaptureTransaction("Set and Get String", ApiConstants.TypeDb, await Task.Delay(TimeSpan.FromMilliseconds(100)); } Console.WriteLine("Stopping Redis Container..."); -await container.StopAsync(); +await container.StopAsync(ctx.Token); Console.WriteLine("Exiting"); diff --git a/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs b/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs index 33e0bd337..ba204fe5d 100644 --- a/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs +++ b/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs @@ -3,20 +3,49 @@ // 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 System; +using System.Threading; using System.Threading.Tasks; using Testcontainers.Elasticsearch; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.Elasticsearch.Tests { - public sealed class ElasticsearchFixture : IAsyncLifetime + public sealed class ElasticsearchFixture(IMessageSink sink) : IAsyncLifetime { private readonly ElasticsearchContainer _container = new ElasticsearchBuilder().Build(); public string ConnectionString => _container.GetConnectionString(); - public Task InitializeAsync() => _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/instrumentations/Elastic.Apm.MongoDb.Tests/Fixture/MongoFixture.cs b/test/instrumentations/Elastic.Apm.MongoDb.Tests/Fixture/MongoFixture.cs index 4df25cdf5..0c6012f13 100644 --- a/test/instrumentations/Elastic.Apm.MongoDb.Tests/Fixture/MongoFixture.cs +++ b/test/instrumentations/Elastic.Apm.MongoDb.Tests/Fixture/MongoFixture.cs @@ -4,23 +4,32 @@ // Elasticsearch B.V licenses this file, including any modifications, to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. +using System; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using Testcontainers.MongoDb; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.MongoDb.Tests.Fixture { public class MongoFixture : IAsyncLifetime where TConfiguration : IMongoConfiguration, new() { + private readonly IMessageSink _sink; private const string MongoDbImage = "mongo:4.4.5"; private readonly TConfiguration _configuration; private readonly MongoDbContainer _container = new MongoDbBuilder().WithImage(MongoDbImage).Build(); - public MongoFixture() => _configuration = new TConfiguration(); + public MongoFixture(IMessageSink sink) + { + _sink = sink; + _configuration = new TConfiguration(); + } public IMongoCollection Collection { get; private set; } @@ -33,6 +42,11 @@ public async Task InitializeAsync() .GetCollection(_configuration.CollectionName); await _configuration.InitializeAsync(Collection); + + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + _sink.OnMessage(new DiagnosticMessage(stdOut)); + _sink.OnMessage(new DiagnosticMessage(stdErr)); } public async Task DisposeAsync() @@ -40,7 +54,20 @@ public async Task DisposeAsync() if (Collection != null) await _configuration.DisposeAsync(Collection); - await _container.StopAsync(); + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(MongoDbContainer)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + _sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(MongoDbContainer)}")); await _container.DisposeAsync(); } } diff --git a/test/instrumentations/Elastic.Apm.SqlClient.Tests/SqlServerFixture.cs b/test/instrumentations/Elastic.Apm.SqlClient.Tests/SqlServerFixture.cs index 1563fe387..3f4a63564 100644 --- a/test/instrumentations/Elastic.Apm.SqlClient.Tests/SqlServerFixture.cs +++ b/test/instrumentations/Elastic.Apm.SqlClient.Tests/SqlServerFixture.cs @@ -3,7 +3,9 @@ // 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 System; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using Testcontainers.MsSql; using Xunit; @@ -47,6 +49,23 @@ public async Task InitializeAsync() _sink.OnMessage(new DiagnosticMessage(stdErr)); } - public async Task DisposeAsync() => await _container.DisposeAsync(); + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(SqlServerFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + _sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(SqlServerFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/instrumentations/Elastic.Apm.StackExchange.Redis.Tests/ProfilingSessionTests.cs b/test/instrumentations/Elastic.Apm.StackExchange.Redis.Tests/ProfilingSessionTests.cs index 9ace34501..cc5d14a44 100644 --- a/test/instrumentations/Elastic.Apm.StackExchange.Redis.Tests/ProfilingSessionTests.cs +++ b/test/instrumentations/Elastic.Apm.StackExchange.Redis.Tests/ProfilingSessionTests.cs @@ -5,6 +5,7 @@ using System; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Elastic.Apm.Api; using Elastic.Apm.Tests.Utilities; @@ -21,8 +22,11 @@ public class ProfilingSessionTests [DockerFact] public async Task Capture_Redis_Commands_On_Transaction() { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + await using var container = new RedisBuilder().Build(); - await container.StartAsync(); + await container.StartAsync(cts.Token); var connection = await ConnectionMultiplexer.ConnectAsync(container.GetConnectionString()); var count = 0; @@ -32,7 +36,7 @@ public async Task Capture_Redis_Commands_On_Transaction() if (count < 5) { count++; - await Task.Delay(500); + await Task.Delay(500, cts.Token); } else throw new Exception("Could not connect to redis for integration test"); @@ -73,14 +77,17 @@ await agent.Tracer.CaptureTransaction("Set and Get String", ApiConstants.TypeDb, foreach (var span in payloadSender.Spans) AssertSpan(span); - await container.StopAsync(); + await container.StopAsync(cts.Token); } [DockerFact] public async Task Capture_Redis_Commands_On_Span() { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + await using var container = new RedisBuilder().Build(); - await container.StartAsync(); + await container.StartAsync(cts.Token); var connection = await ConnectionMultiplexer.ConnectAsync(container.GetConnectionString()); var count = 0; @@ -90,7 +97,7 @@ public async Task Capture_Redis_Commands_On_Span() if (count < 5) { count++; - await Task.Delay(500); + await Task.Delay(500, cts.Token); } else throw new Exception("Could not connect to redis for integration test"); @@ -155,7 +162,7 @@ await t.CaptureSpan($"parent span {i}", ApiConstants.TypeDb, async () => AssertSpan(span); } - await container.StopAsync(); + await container.StopAsync(cts.Token); } private static void AssertSpan(ISpan span) diff --git a/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs b/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs index b3a08035d..5b912ad78 100644 --- a/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs +++ b/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs @@ -17,29 +17,29 @@ public sealed class ElasticsearchTestFixture(IMessageSink sink) : IAsyncLifetime { private readonly IMessageSink _sink = sink; -public ElasticsearchContainer Container { get; } = new ElasticsearchBuilder().Build(); + public ElasticsearchContainer Container { get; } = new ElasticsearchBuilder().Build(); -public ElasticsearchClient? Client { get; private set; } + public ElasticsearchClient? Client { get; private set; } -public async Task InitializeAsync() -{ - await Container.StartAsync(); + public async Task InitializeAsync() + { + await Container.StartAsync(); - var (stdOut, stdErr) = await Container.GetLogsAsync(); + var (stdOut, stdErr) = await Container.GetLogsAsync(); - _sink.OnMessage(new DiagnosticMessage(stdOut)); - _sink.OnMessage(new DiagnosticMessage(stdErr)); + _sink.OnMessage(new DiagnosticMessage(stdOut)); + _sink.OnMessage(new DiagnosticMessage(stdErr)); - var settings = new ElasticsearchClientSettings(new Uri(Container.GetConnectionString())); - settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); + var settings = new ElasticsearchClientSettings(new Uri(Container.GetConnectionString())); + settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); - Client = new ElasticsearchClient(settings); - if (Client == null) - throw new Exception("`new ElasticsearchClient(settings)` returned `null`"); + Client = new ElasticsearchClient(settings); + if (Client == null) + throw new Exception("`new ElasticsearchClient(settings)` returned `null`"); - //Increase Elasticsearch high disk watermarks, Github Actions container typically has around - //~7GB free (8%) of the available space. - var response = await Client.Transport.RequestAsync(HttpMethod.PUT, "_cluster/settings", PostData.String(@"{ + //Increase Elasticsearch high disk watermarks, Github Actions container typically has around + //~7GB free (8%) of the available space. + var response = await Client.Transport.RequestAsync(HttpMethod.PUT, "_cluster/settings", PostData.String(@"{ ""persistent"": { ""cluster.routing.allocation.disk.watermark.low"": ""90%"", ""cluster.routing.allocation.disk.watermark.low.max_headroom"": ""100GB"", @@ -52,16 +52,25 @@ public async Task InitializeAsync() } }")); - if (!response.ApiCallDetails.HasSuccessfulStatusCode) - throw new Exception(response.ToString()); -} + if (!response.ApiCallDetails.HasSuccessfulStatusCode) + throw new Exception(response.ToString()); + } -async Task IAsyncLifetime.DisposeAsync() -{ - if (Container.State == TestcontainersStates.Running) + async Task IAsyncLifetime.DisposeAsync() { - await Container.StopAsync(); + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchTestFixture)}")); + await Container.StopAsync(cts.Token); + } + catch (Exception e) + { + _sink.OnMessage(new DiagnosticMessage(e.Message)); + } + _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchTestFixture)}")); await Container.DisposeAsync(); } } -} diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs index 6e825c69e..a4269eec8 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs @@ -3,23 +3,52 @@ // 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 System; +using System.Threading; using System.Threading.Tasks; using Testcontainers.MySql; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.Profiler.Managed.Tests.AdoNet { [CollectionDefinition("MySql")] public sealed class MySqlCollection : ICollectionFixture { } - public sealed class MySqlFixture : IAsyncLifetime + public sealed class MySqlFixture(IMessageSink sink) : IAsyncLifetime { private readonly MySqlContainer _container = new MySqlBuilder().WithImage("mysql:8.0.32").Build(); public string ConnectionString => _container.GetConnectionString(); - public Task InitializeAsync() => _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(MySqlFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(MySqlFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/OracleSqlFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/OracleSqlFixture.cs index 1a4ccab12..301e6c0dc 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/OracleSqlFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/OracleSqlFixture.cs @@ -3,6 +3,8 @@ // 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 System; +using System.Threading; using System.Threading.Tasks; using Elastic.Apm.Tests.Utilities; using Testcontainers.Oracle; @@ -17,12 +19,21 @@ public sealed class OracleCollection : ICollectionFixture { } public sealed class OracleSqlFixture : IAsyncLifetime { - private readonly OracleContainer _container = new OracleBuilder().Build(); + private readonly OracleContainer _container; private readonly IMessageSink _sink; public string ConnectionString => _container.GetConnectionString(); - public OracleSqlFixture(IMessageSink sink) => _sink = sink; + public OracleSqlFixture(IMessageSink sink) + { + _sink = sink; + if (!TestEnvironment.IsWindows) + { + _sink.OnMessage(new DiagnosticMessage($"Skipping {nameof(OracleSqlFixture)} on non windows platforms.")); + return; + } + _container = new OracleBuilder().Build(); + } public async Task InitializeAsync() { @@ -37,6 +48,26 @@ public async Task InitializeAsync() } - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + public async Task DisposeAsync() + { + if (!TestEnvironment.IsWindows) + return; + + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(OracleSqlFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + _sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(OracleSqlFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs index 472d0eb4a..6d222eedc 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs @@ -3,23 +3,52 @@ // 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 System; +using System.Threading; using System.Threading.Tasks; using Testcontainers.PostgreSql; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.Profiler.Managed.Tests.AdoNet { [CollectionDefinition("Postgres")] public sealed class PostgresCollection : ICollectionFixture { } - public sealed class PostgreSqlFixture : IAsyncLifetime + public sealed class PostgreSqlFixture(IMessageSink sink) : IAsyncLifetime { private readonly PostgreSqlContainer _container = new PostgreSqlBuilder().Build(); public string ConnectionString => _container.GetConnectionString(); - public Task InitializeAsync() => _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(PostgreSqlFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(PostgreSqlFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/SqlServerFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/SqlServerFixture.cs index 3f25f8dcf..fb802fd04 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/SqlServerFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/SqlServerFixture.cs @@ -3,10 +3,14 @@ // 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 System; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using Testcontainers.MsSql; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.Profiler.Managed.Tests.AdoNet { @@ -15,10 +19,12 @@ public sealed class SqlServerCollection : ICollectionFixture { public sealed class SqlServerFixture : IAsyncLifetime { + private readonly IMessageSink _sink; private readonly MsSqlContainer _container; - public SqlServerFixture() + public SqlServerFixture(IMessageSink sink) { + _sink = sink; // see: https://blog.rufer.be/2024/09/22/workaround-fix-testcontainers-sql-error-docker-dotnet-dockerapiexception-docker-api-responded-with-status-codeconflict/ if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { @@ -35,8 +41,33 @@ public SqlServerFixture() public string ConnectionString => _container.GetConnectionString(); - public Task InitializeAsync() => _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); + + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + _sink.OnMessage(new DiagnosticMessage(stdOut)); + _sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + try + { + _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(SqlServerFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + _sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(SqlServerFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs index 80a3bfa4a..b6ae91054 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs @@ -3,23 +3,52 @@ // 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 System; +using System.Threading; using System.Threading.Tasks; using Testcontainers.Kafka; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.Profiler.Managed.Tests.Kafka { [CollectionDefinition("Kafka")] public sealed class KafkaCollection : ICollectionFixture { } - public sealed class KafkaFixture : IAsyncLifetime + public sealed class KafkaFixture(IMessageSink sink) : IAsyncLifetime { private readonly KafkaContainer _container = new KafkaBuilder().Build(); public string BootstrapServers => _container.GetBootstrapAddress(); - public Task InitializeAsync() => _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(KafkaFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(KafkaFixture)}")); + await _container.DisposeAsync(); + } } } diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs index 6f6bf2aff..a60ec599f 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs @@ -3,23 +3,52 @@ // 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 System; +using System.Threading; using System.Threading.Tasks; using Testcontainers.RabbitMq; using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; namespace Elastic.Apm.Profiler.Managed.Tests.RabbitMq { [CollectionDefinition("RabbitMq")] public sealed class RabbitMqCollection : ICollectionFixture { } - public sealed class RabbitMqFixture : IAsyncLifetime + public sealed class RabbitMqFixture(IMessageSink sink) : IAsyncLifetime { private readonly RabbitMqContainer _container = new RabbitMqBuilder().Build(); public string ConnectionString => _container.GetConnectionString(); - public Task InitializeAsync() => _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - public Task DisposeAsync() => _container.DisposeAsync().AsTask(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); + + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); + + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(RabbitMqFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) + { + sink.OnMessage(new DiagnosticMessage(e.Message)); + } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(RabbitMqFixture)}")); + await _container.DisposeAsync(); + } } } From d280d2ad51e3e2cef01037e9aea7e5ea945ad5f9 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 22 Oct 2024 19:36:36 +0200 Subject: [PATCH 2/4] dotnet format --- .../ElasticsearchFixture.cs | 48 ++++++------- .../ElasticsearchTestFixture.cs | 68 +++++++++---------- .../AdoNet/MySqlFixture.cs | 48 ++++++------- .../AdoNet/PostgreSqlFixture.cs | 48 ++++++------- .../Kafka/KafkaFixture.cs | 48 ++++++------- .../RabbitMq/RabbitMqFixture.cs | 48 ++++++------- 6 files changed, 154 insertions(+), 154 deletions(-) diff --git a/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs b/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs index ba204fe5d..5bfef539a 100644 --- a/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs +++ b/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchFixture.cs @@ -17,35 +17,35 @@ public sealed class ElasticsearchFixture(IMessageSink sink) : IAsyncLifetime { private readonly ElasticsearchContainer _container = new ElasticsearchBuilder().Build(); - public string ConnectionString => _container.GetConnectionString(); + public string ConnectionString => _container.GetConnectionString(); - public async Task InitializeAsync() - { - await _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - var (stdOut, stdErr) = await _container.GetLogsAsync(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); - sink.OnMessage(new DiagnosticMessage(stdOut)); - sink.OnMessage(new DiagnosticMessage(stdErr)); - } + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - public async Task DisposeAsync() + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromMinutes(2)); - - try - { - sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchFixture)}")); - await _container.StopAsync(cts.Token); - } - catch (Exception e) - { - sink.OnMessage(new DiagnosticMessage(e.Message)); - } - - sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchFixture)}")); - await _container.DisposeAsync(); + sink.OnMessage(new DiagnosticMessage(e.Message)); } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchFixture)}")); + await _container.DisposeAsync(); } } +} diff --git a/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs b/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs index 5b912ad78..fa14b5dff 100644 --- a/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs +++ b/test/instrumentations/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.cs @@ -17,29 +17,29 @@ public sealed class ElasticsearchTestFixture(IMessageSink sink) : IAsyncLifetime { private readonly IMessageSink _sink = sink; - public ElasticsearchContainer Container { get; } = new ElasticsearchBuilder().Build(); +public ElasticsearchContainer Container { get; } = new ElasticsearchBuilder().Build(); - public ElasticsearchClient? Client { get; private set; } +public ElasticsearchClient? Client { get; private set; } - public async Task InitializeAsync() - { - await Container.StartAsync(); +public async Task InitializeAsync() +{ + await Container.StartAsync(); - var (stdOut, stdErr) = await Container.GetLogsAsync(); + var (stdOut, stdErr) = await Container.GetLogsAsync(); - _sink.OnMessage(new DiagnosticMessage(stdOut)); - _sink.OnMessage(new DiagnosticMessage(stdErr)); + _sink.OnMessage(new DiagnosticMessage(stdOut)); + _sink.OnMessage(new DiagnosticMessage(stdErr)); - var settings = new ElasticsearchClientSettings(new Uri(Container.GetConnectionString())); - settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); + var settings = new ElasticsearchClientSettings(new Uri(Container.GetConnectionString())); + settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); - Client = new ElasticsearchClient(settings); - if (Client == null) - throw new Exception("`new ElasticsearchClient(settings)` returned `null`"); + Client = new ElasticsearchClient(settings); + if (Client == null) + throw new Exception("`new ElasticsearchClient(settings)` returned `null`"); - //Increase Elasticsearch high disk watermarks, Github Actions container typically has around - //~7GB free (8%) of the available space. - var response = await Client.Transport.RequestAsync(HttpMethod.PUT, "_cluster/settings", PostData.String(@"{ + //Increase Elasticsearch high disk watermarks, Github Actions container typically has around + //~7GB free (8%) of the available space. + var response = await Client.Transport.RequestAsync(HttpMethod.PUT, "_cluster/settings", PostData.String(@"{ ""persistent"": { ""cluster.routing.allocation.disk.watermark.low"": ""90%"", ""cluster.routing.allocation.disk.watermark.low.max_headroom"": ""100GB"", @@ -52,25 +52,25 @@ public async Task InitializeAsync() } }")); - if (!response.ApiCallDetails.HasSuccessfulStatusCode) - throw new Exception(response.ToString()); - } + if (!response.ApiCallDetails.HasSuccessfulStatusCode) + throw new Exception(response.ToString()); +} - async Task IAsyncLifetime.DisposeAsync() - { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromMinutes(2)); +async Task IAsyncLifetime.DisposeAsync() +{ + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - try - { - _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchTestFixture)}")); - await Container.StopAsync(cts.Token); - } - catch (Exception e) - { - _sink.OnMessage(new DiagnosticMessage(e.Message)); - } - _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchTestFixture)}")); - await Container.DisposeAsync(); + try + { + _sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchTestFixture)}")); + await Container.StopAsync(cts.Token); } + catch (Exception e) + { + _sink.OnMessage(new DiagnosticMessage(e.Message)); + } + _sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchTestFixture)}")); + await Container.DisposeAsync(); +} } diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs index a4269eec8..3ac93493b 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/MySqlFixture.cs @@ -20,35 +20,35 @@ public sealed class MySqlFixture(IMessageSink sink) : IAsyncLifetime { private readonly MySqlContainer _container = new MySqlBuilder().WithImage("mysql:8.0.32").Build(); - public string ConnectionString => _container.GetConnectionString(); + public string ConnectionString => _container.GetConnectionString(); - public async Task InitializeAsync() - { - await _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - var (stdOut, stdErr) = await _container.GetLogsAsync(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); - sink.OnMessage(new DiagnosticMessage(stdOut)); - sink.OnMessage(new DiagnosticMessage(stdErr)); - } + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - public async Task DisposeAsync() + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(MySqlFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromMinutes(2)); - - try - { - sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(MySqlFixture)}")); - await _container.StopAsync(cts.Token); - } - catch (Exception e) - { - sink.OnMessage(new DiagnosticMessage(e.Message)); - } - - sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(MySqlFixture)}")); - await _container.DisposeAsync(); + sink.OnMessage(new DiagnosticMessage(e.Message)); } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(MySqlFixture)}")); + await _container.DisposeAsync(); } } +} diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs index 6d222eedc..e57611287 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/AdoNet/PostgreSqlFixture.cs @@ -20,35 +20,35 @@ public sealed class PostgreSqlFixture(IMessageSink sink) : IAsyncLifetime { private readonly PostgreSqlContainer _container = new PostgreSqlBuilder().Build(); - public string ConnectionString => _container.GetConnectionString(); + public string ConnectionString => _container.GetConnectionString(); - public async Task InitializeAsync() - { - await _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - var (stdOut, stdErr) = await _container.GetLogsAsync(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); - sink.OnMessage(new DiagnosticMessage(stdOut)); - sink.OnMessage(new DiagnosticMessage(stdErr)); - } + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - public async Task DisposeAsync() + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(PostgreSqlFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromMinutes(2)); - - try - { - sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(PostgreSqlFixture)}")); - await _container.StopAsync(cts.Token); - } - catch (Exception e) - { - sink.OnMessage(new DiagnosticMessage(e.Message)); - } - - sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(PostgreSqlFixture)}")); - await _container.DisposeAsync(); + sink.OnMessage(new DiagnosticMessage(e.Message)); } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(PostgreSqlFixture)}")); + await _container.DisposeAsync(); } } +} diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs index b6ae91054..f263fc589 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/Kafka/KafkaFixture.cs @@ -20,35 +20,35 @@ public sealed class KafkaFixture(IMessageSink sink) : IAsyncLifetime { private readonly KafkaContainer _container = new KafkaBuilder().Build(); - public string BootstrapServers => _container.GetBootstrapAddress(); + public string BootstrapServers => _container.GetBootstrapAddress(); - public async Task InitializeAsync() - { - await _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - var (stdOut, stdErr) = await _container.GetLogsAsync(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); - sink.OnMessage(new DiagnosticMessage(stdOut)); - sink.OnMessage(new DiagnosticMessage(stdErr)); - } + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - public async Task DisposeAsync() + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(KafkaFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromMinutes(2)); - - try - { - sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(KafkaFixture)}")); - await _container.StopAsync(cts.Token); - } - catch (Exception e) - { - sink.OnMessage(new DiagnosticMessage(e.Message)); - } - - sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(KafkaFixture)}")); - await _container.DisposeAsync(); + sink.OnMessage(new DiagnosticMessage(e.Message)); } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(KafkaFixture)}")); + await _container.DisposeAsync(); } } +} diff --git a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs index a60ec599f..bbf85207c 100644 --- a/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs +++ b/test/profiler/Elastic.Apm.Profiler.Managed.Tests/RabbitMq/RabbitMqFixture.cs @@ -20,35 +20,35 @@ public sealed class RabbitMqFixture(IMessageSink sink) : IAsyncLifetime { private readonly RabbitMqContainer _container = new RabbitMqBuilder().Build(); - public string ConnectionString => _container.GetConnectionString(); + public string ConnectionString => _container.GetConnectionString(); - public async Task InitializeAsync() - { - await _container.StartAsync(); + public async Task InitializeAsync() + { + await _container.StartAsync(); - var (stdOut, stdErr) = await _container.GetLogsAsync(); + var (stdOut, stdErr) = await _container.GetLogsAsync(); - sink.OnMessage(new DiagnosticMessage(stdOut)); - sink.OnMessage(new DiagnosticMessage(stdErr)); - } + sink.OnMessage(new DiagnosticMessage(stdOut)); + sink.OnMessage(new DiagnosticMessage(stdErr)); + } + + public async Task DisposeAsync() + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromMinutes(2)); - public async Task DisposeAsync() + try + { + sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(RabbitMqFixture)}")); + await _container.StopAsync(cts.Token); + } + catch (Exception e) { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromMinutes(2)); - - try - { - sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(RabbitMqFixture)}")); - await _container.StopAsync(cts.Token); - } - catch (Exception e) - { - sink.OnMessage(new DiagnosticMessage(e.Message)); - } - - sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(RabbitMqFixture)}")); - await _container.DisposeAsync(); + sink.OnMessage(new DiagnosticMessage(e.Message)); } + + sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(RabbitMqFixture)}")); + await _container.DisposeAsync(); } } +} From 00cddfee6001d4f0b98030727e247c5a150809bf Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 22 Oct 2024 20:21:12 +0200 Subject: [PATCH 3/4] Update Elasticsearch test to handle additional span Modified the test to account for a product check span, restricting the parent span selection to the one associated with 'request' action. This ensures the test accurately identifies the correct parent span to validate against expected behaviors. --- .../Elastic.Apm.Elasticsearch.Tests/ElasticsearchTests.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchTests.cs b/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchTests.cs index b027bb572..be6239a3f 100644 --- a/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchTests.cs +++ b/test/instrumentations/Elastic.Apm.Elasticsearch.Tests/ElasticsearchTests.cs @@ -44,7 +44,11 @@ public async Task Elasticsearch_Span_Should_Align_With_Spec() var spans = payloadSender.SpansOnFirstTransaction; - var parentSpan = spans.Where(s => s.ParentId == s.TransactionId).Single(); + // could be 2 because product check generates a span too + var parentSpans = spans.Where(s => s.ParentId == s.TransactionId && s.Action == "request").ToList(); + + parentSpans.Should().HaveCount(1); + var parentSpan = parentSpans[0]; parentSpan.Name.Should().StartWith("Elasticsearch: "); parentSpan.Action.Should().Be("request"); From 3b8c692816f5789a7df8c400483537a3f9ad1f0b Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 22 Oct 2024 20:28:32 +0200 Subject: [PATCH 4/4] Add CosmosDbFact attribute for CosmosDb-specific tests Introduced a new CosmosDbFact attribute to replace AzureCredentialsFact for CosmosDb tests. This attribute includes checks for Azure credentials and skips tests on CI environments where CosmosDb tests are temporarily disabled. All existing CosmosDb tests have been updated to use CosmosDbFact. --- .../Azure/AzureCredentialsFactAttribute.cs | 11 ++++++++++ .../AzureCosmosDbTestEnvironment.cs | 4 ++++ .../MicrosoftAzureCosmosTests.cs | 22 +++++++++---------- .../MicrosoftAzureDocumentDbTests.cs | 20 ++++++++--------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/test/Elastic.Apm.Tests.Utilities/Azure/AzureCredentialsFactAttribute.cs b/test/Elastic.Apm.Tests.Utilities/Azure/AzureCredentialsFactAttribute.cs index cad1372cc..edb966d6b 100644 --- a/test/Elastic.Apm.Tests.Utilities/Azure/AzureCredentialsFactAttribute.cs +++ b/test/Elastic.Apm.Tests.Utilities/Azure/AzureCredentialsFactAttribute.cs @@ -18,4 +18,15 @@ public AzureCredentialsFactAttribute() Skip = "Azure credentials not available. If running locally, run `az login` to login"; } } + + public class CosmosDbFactAttribute : FactAttribute + { + public CosmosDbFactAttribute() + { + if (AzureCredentials.Instance is Unauthenticated) + Skip = "Azure credentials not available. If running locally, run `az login` to login"; + if (TestEnvironment.IsCi) + Skip = "CosmosDb tests are temporarily disabled on CI environments"; + } + } } diff --git a/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/AzureCosmosDbTestEnvironment.cs b/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/AzureCosmosDbTestEnvironment.cs index d973d1672..221607d5b 100644 --- a/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/AzureCosmosDbTestEnvironment.cs +++ b/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/AzureCosmosDbTestEnvironment.cs @@ -43,6 +43,10 @@ public AzureCosmosDbTestEnvironment(IMessageSink messageSink) if (credentials is Unauthenticated) return; + // TODO remove this and move our cosmosdb tests over to a always ready serverless + if (TestEnvironment.IsCi) + return; + _terraform = new TerraformResources(terraformResourceDirectory, credentials, messageSink); var resourceGroupName = AzureResources.CreateResourceGroupName("cosmosdb-test"); diff --git a/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureCosmosTests.cs b/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureCosmosTests.cs index bd386f9b9..5383ee671 100644 --- a/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureCosmosTests.cs +++ b/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureCosmosTests.cs @@ -34,7 +34,7 @@ public MicrosoftAzureCosmosTests(AzureCosmosDbTestEnvironment environment, ITest }); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Create_Database() { await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants.TypeDb, async () => @@ -45,7 +45,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants. AssertSpan("Create database"); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Delete_Database() { var db = await CreateDatabaseAsync(); @@ -57,7 +57,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Database", ApiConstants. AssertSpan($"Delete database {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Get_Database() { var db = await CreateDatabaseAsync(); @@ -69,7 +69,7 @@ await _agent.Tracer.CaptureTransaction("Get CosmosDb Database", ApiConstants.Typ AssertSpan($"Get database {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_List_Databases() { await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.TypeDb, async () => @@ -83,7 +83,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.T AssertSpan("List databases"); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Create_Collection() { var db = await CreateDatabaseAsync(); @@ -95,7 +95,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Collection", ApiConstant AssertSpan($"Create collection {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Delete_Collection() { var db = await CreateDatabaseAsync(); @@ -111,7 +111,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Collection", ApiConstant private string RandomName() => Guid.NewGuid().ToString("N"); - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_List_Collections() { var db = await CreateDatabaseAsync(); @@ -131,7 +131,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Collections", ApiConstants AssertSpan($"List collections {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Create_Document() { var db = await CreateDatabaseAsync(); @@ -147,7 +147,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Item", ApiConstants.Type AssertSpan($"Create/query document {db.Id} {container.Id}", db.Id, 2); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Upsert_Document() { var db = await CreateDatabaseAsync(); @@ -163,7 +163,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Item", ApiConstants.Type AssertSpan($"Create/query document {db.Id} {container.Id}", db.Id, 2); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Delete_Document() { var db = await CreateDatabaseAsync(); @@ -181,7 +181,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Item", ApiConstants.Type AssertSpan($"Delete document {db.Id} {container.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Replace_Document() { var db = await CreateDatabaseAsync(); diff --git a/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureDocumentDbTests.cs b/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureDocumentDbTests.cs index cf1c12077..ed759ae82 100644 --- a/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureDocumentDbTests.cs +++ b/test/azure/Elastic.Apm.Azure.CosmosDb.Tests/MicrosoftAzureDocumentDbTests.cs @@ -31,7 +31,7 @@ public MicrosoftAzureDocumentDbTests(AzureCosmosDbTestEnvironment environment, I _client = new DocumentClient(new Uri(environment.Endpoint), environment.PrimaryMasterKey); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Create_Database() { await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants.TypeDb, async () => @@ -42,7 +42,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants. AssertSpan("Create database"); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Delete_Database() { var db = await CreateDatabaseAsync(); @@ -54,7 +54,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Database", ApiConstants. AssertSpan($"Delete database {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Get_Database() { var db = await CreateDatabaseAsync(); @@ -66,7 +66,7 @@ await _agent.Tracer.CaptureTransaction("Get CosmosDb Database", ApiConstants.Typ AssertSpan($"Get database {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_List_Databases() { await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.TypeDb, async () => @@ -80,7 +80,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.T AssertSpan("List databases"); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Create_Collection() { var db = await CreateDatabaseAsync(); @@ -92,7 +92,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Collection", ApiConstant AssertSpan($"Create collection {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Delete_Collection() { var db = await CreateDatabaseAsync(); @@ -108,7 +108,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Collection", ApiConstant private string RandomName() => Guid.NewGuid().ToString("N"); - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_List_Collections() { var db = await CreateDatabaseAsync(); @@ -124,7 +124,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Collections", ApiConstants AssertSpan($"List collections {db.Id}", db.Id); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Create_Document() { var db = await CreateDatabaseAsync(); @@ -140,7 +140,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Item", ApiConstants.Type AssertSpan($"Create/query document {db.Id} {container.Id}", db.Id, 2); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Delete_Document() { var db = await CreateDatabaseAsync(); @@ -158,7 +158,7 @@ await _client.DeleteDocumentAsync( AssertSpan($"Delete document {db.Id} {container.Id}", db.Id, 2); } - [AzureCredentialsFact] + [CosmosDbFact] public async Task Capture_Span_When_Upsert_Document() { var db = await CreateDatabaseAsync();