Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static TheoryData<string, bool, bool, bool, Type, string, string> DataCon
return testCases;
}

[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
[EnabledOnDockerPlatformTheory(DockerPlatform.Linux)]
[MemberData(nameof(RawSqlTestCases))]
public async Task TracesRawSql(
string provider,
Expand Down Expand Up @@ -189,7 +189,7 @@ bool ActivityFilter(string? providerName, IDbCommand command)
}
}

[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
[EnabledOnDockerPlatformTheory(DockerPlatform.Linux)]
[MemberData(nameof(DataContextTestCases))]
public async Task TracesDataContext(
string provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Instrumentation.SqlClient.Tests\SqlClientIntegrationTestsFixture.cs" Link="SqlClientIntegrationTestsFixture.cs" />
<Compile Include="$(RepoRoot)\test\Shared\DockerHelper.cs" Link="Includes\DockerHelper.cs" />
<Compile Include="$(RepoRoot)\test\Shared\DockerPlatform.cs" Link="Includes\DockerPlatform.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="Includes\EnabledOnDockerPlatformTheoryAttribute.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\test\Shared\DockerHelper.cs" Link="Includes\DockerHelper.cs" />
<Compile Include="$(RepoRoot)\test\Shared\DockerPlatform.cs" Link="Includes\DockerPlatform.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="Includes\EnabledOnDockerPlatformTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\Shared\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="Includes\SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SqlClientIntegrationTests(SqlClientIntegrationTestsFixture fixture)
this.fixture = fixture;
}

[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
[EnabledOnDockerPlatformTheory(DockerPlatform.Linux)]
[InlineData(CommandType.Text, "select 1/1")]
[InlineData(CommandType.Text, "select 1/1", true, "select ?/?")]
[InlineData(CommandType.Text, "select 1/0", false, null, true)]
Expand Down Expand Up @@ -104,8 +104,7 @@ public void SuccessfulCommandTest(

transaction?.Commit();

Assert.Single(activities);
var activity = activities[0];
var activity = Assert.Single(activities);

VerifyContextInfo(commandText, commandResult, activity);
VerifyActivityData(commandType, sanitizedCommandText, captureTextCommandContent, isFailure, recordException, activity);
Expand Down
68 changes: 68 additions & 0 deletions test/Shared/DockerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using System.Text;

namespace OpenTelemetry.Tests;

/// <summary>
/// Determines if a required Docker engine is available.
/// </summary>
internal static class DockerHelper
{
/// <summary>
/// Gets whether the specified Docker platform is available.
/// </summary>
/// <returns>
/// <see langword="true"/> if the specified Docker platform is available, otherwise <see langword="false"/>.
/// </returns>
public static bool IsAvailable(DockerPlatform dockerPlatform)
{
const string executable = "docker";

var stdout = new StringBuilder();
var stderr = new StringBuilder();

void AppendStdout(object sender, DataReceivedEventArgs e)
{
stdout.Append(e.Data);
}

void AppendStderr(object sender, DataReceivedEventArgs e)
{
stderr.Append(e.Data);
}

var processStartInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = string.Join(" ", "version", "--format '{{.Server.Os}}'"),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};

var process = new Process
{
StartInfo = processStartInfo,
};
process.OutputDataReceived += AppendStdout;
process.ErrorDataReceived += AppendStderr;

try
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
finally
{
process.OutputDataReceived -= AppendStdout;
process.ErrorDataReceived -= AppendStderr;
}

return process.ExitCode == 0 && stdout.ToString().IndexOf(dockerPlatform.ToString(), StringComparison.OrdinalIgnoreCase) > 0;
}
}
17 changes: 17 additions & 0 deletions test/Shared/DockerPlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.Tests;

internal enum DockerPlatform
{
/// <summary>
/// Docker Linux engine.
/// </summary>
Linux,

/// <summary>
/// Docker Windows engine.
/// </summary>
Windows,
}
23 changes: 23 additions & 0 deletions test/Shared/EnabledOnDockerPlatformFactAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using Xunit;

namespace OpenTelemetry.Tests;

/// <summary>
/// This <see cref="FactAttribute" /> skips tests if the required Docker engine is not available.
/// </summary>
internal class EnabledOnDockerPlatformFactAttribute : FactAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="EnabledOnDockerPlatformFactAttribute" /> class.
/// </summary>
public EnabledOnDockerPlatformFactAttribute(DockerPlatform dockerPlatform)
{
if (!DockerHelper.IsAvailable(dockerPlatform))
{
this.Skip = $"The Docker {dockerPlatform} engine is not available.";
}
}
}
65 changes: 2 additions & 63 deletions test/Shared/EnabledOnDockerPlatformTheoryAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using System.Text;
using Xunit;

namespace OpenTelemetry.Tests;
Expand All @@ -17,68 +15,9 @@ internal class EnabledOnDockerPlatformTheoryAttribute : TheoryAttribute
/// </summary>
public EnabledOnDockerPlatformTheoryAttribute(DockerPlatform dockerPlatform)
{
const string executable = "docker";

var stdout = new StringBuilder();
var stderr = new StringBuilder();

void AppendStdout(object sender, DataReceivedEventArgs e)
{
stdout.Append(e.Data);
}

void AppendStderr(object sender, DataReceivedEventArgs e)
{
stderr.Append(e.Data);
}

var processStartInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = string.Join(" ", "version", "--format '{{.Server.Os}}'"),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};

var process = new Process
{
StartInfo = processStartInfo,
};
process.OutputDataReceived += AppendStdout;
process.ErrorDataReceived += AppendStderr;

try
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
finally
{
process.OutputDataReceived -= AppendStdout;
process.ErrorDataReceived -= AppendStderr;
}

if (0.Equals(process.ExitCode) && stdout.ToString().IndexOf(dockerPlatform.ToString(), StringComparison.OrdinalIgnoreCase) > 0)
if (!DockerHelper.IsAvailable(dockerPlatform))
{
return;
this.Skip = $"The Docker {dockerPlatform} engine is not available.";
}

this.Skip = $"The Docker {dockerPlatform} engine is not available.";
}

internal enum DockerPlatform
{
/// <summary>
/// Docker Linux engine.
/// </summary>
Linux,

/// <summary>
/// Docker Windows engine.
/// </summary>
Windows,
}
}
Loading