Skip to content

Commit

Permalink
feat: Enable SuccessfulCommandTest by leveraging Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Feb 16, 2023
1 parent d0829ff commit ee58108
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions build/Common.nonprod.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<MicrosoftExtensionsLoggingAbstractionsPkgVer>[6.0.0,)</MicrosoftExtensionsLoggingAbstractionsPkgVer>
<MicrosoftNETTestSdkPkgVer>[17.4.1]</MicrosoftNETTestSdkPkgVer>
<MoqPkgVer>[4.18.3,5.0)</MoqPkgVer>
<TestcontainersPkgVer>2.4.0</TestcontainersPkgVer>
<RabbitMQClientPkgVer>[6.4.0,7.0)</RabbitMQClientPkgVer>
<RuntimeInstrumentationPkgVer>[1.0.0,2.0)</RuntimeInstrumentationPkgVer>
<SwashbuckleAspNetCorePkgVer>[6.4.0]</SwashbuckleAspNetCorePkgVer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="Testcontainers" Version="$(TestcontainersPkgVer)" />
<PackageReference Include="xunit" Version="$(XUnitPkgVer)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPkgVer)">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

using System.Data;
using System.Diagnostics;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Instrumentation.SqlClient.Implementation;
Expand All @@ -25,7 +27,7 @@

namespace OpenTelemetry.Instrumentation.SqlClient.Tests
{
public class SqlClientTests : IDisposable
public class SqlClientTests : IAsyncLifetime, IDisposable
{
/*
To run the integration tests, set the OTEL_SQLCONNECTIONSTRING machine-level environment variable to a valid Sql Server connection string.
Expand All @@ -35,18 +37,49 @@ To use Docker...
2) Set OTEL_SQLCONNECTIONSTRING as: Data Source=127.0.0.1,5433; User ID=sa; Password=Pass@word
*/

private const string SqlConnectionStringEnvVarName = "OTEL_SQLCONNECTIONSTRING";
private const string TestConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Database=master";

private static readonly string SqlConnectionString = SkipUnlessEnvVarFoundTheoryAttribute.GetEnvironmentVariable(SqlConnectionStringEnvVarName);
// Please note this image is not compatible with macOS running on Apple silicon.
// Maybe we should replace it with mcr.microsoft.com/azure-sql-edge.
private const string MsSqlImage = "mcr.microsoft.com/mssql/server:2019-CU18-ubuntu-20.04";

private const ushort MsSqlPort = 1433;

private const string MsSqlDatabase = "master";

private const string MsSqlUsername = "sa";

private const string MsSqlPassword = "yourStrong(!)Password";

private readonly FakeSqlClientDiagnosticSource fakeSqlClientDiagnosticSource;

// With the next version of Testcontainers for .NET we can replace the generic build with the Microsoft SQL Server module
// The module takes care of the entire configuration incl. constructing the connection string. This will be a one-liner in the future.
private readonly IContainer mssqlContainer = new ContainerBuilder()
.WithImage(MsSqlImage)
.WithPortBinding(MsSqlPort, true)
.WithEnvironment("ACCEPT_EULA", "Y")
.WithEnvironment("SQLCMDUSER", MsSqlUsername)
.WithEnvironment("SQLCMDPASSWORD", MsSqlPassword)
.WithEnvironment("MSSQL_SA_PASSWORD", MsSqlPassword)
.WithWaitStrategy(Wait.ForUnixContainer().UntilCommandIsCompleted("/opt/mssql-tools/bin/sqlcmd", "-Q", "SELECT 1;"))
.Build();

public SqlClientTests()
{
this.fakeSqlClientDiagnosticSource = new FakeSqlClientDiagnosticSource();
}

public Task InitializeAsync()
{
return this.mssqlContainer.StartAsync();
}

public Task DisposeAsync()
{
return this.mssqlContainer.DisposeAsync().AsTask();
}

public void Dispose()
{
this.fakeSqlClientDiagnosticSource.Dispose();
Expand Down Expand Up @@ -82,7 +115,7 @@ public void SqlClient_NamedOptions()
}

[Trait("CategoryName", "SqlIntegrationTests")]
[SkipUnlessEnvVarFoundTheory(SqlConnectionStringEnvVarName)]
[Theory]
[InlineData(CommandType.Text, "select 1/1", false)]
[InlineData(CommandType.Text, "select 1/1", false, true)]
[InlineData(CommandType.Text, "select 1/0", false, false, true)]
Expand All @@ -105,6 +138,8 @@ public void SuccessfulCommandTest(
shouldEnrich = false;
#endif

var connectionString = $"Server={this.mssqlContainer.Hostname},{this.mssqlContainer.GetMappedPublicPort(MsSqlPort)};Database={MsSqlDatabase};User Id={MsSqlUsername};Password={MsSqlPassword};TrustServerCertificate=True";

var sampler = new TestSampler();
var activities = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand All @@ -126,7 +161,7 @@ public void SuccessfulCommandTest(
})
.Build();

using SqlConnection sqlConnection = new SqlConnection(SqlConnectionString);
using SqlConnection sqlConnection = new SqlConnection(connectionString);

sqlConnection.Open();

Expand Down

0 comments on commit ee58108

Please sign in to comment.