Skip to content

Commit 4902e78

Browse files
[Infra] Add EnabledOnDockerPlatformFact
Split out the implementation in `[EnabledOnDockerPlatformTheory]` to allow for a `[EnabledOnDockerPlatformFact]`.
1 parent abfd71d commit 4902e78

File tree

8 files changed

+118
-68
lines changed

8 files changed

+118
-68
lines changed

test/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkIntegrationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static TheoryData<string, bool, bool, bool, Type, string, string> DataCon
8686
return testCases;
8787
}
8888

89-
[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
89+
[EnabledOnDockerPlatformTheory(DockerPlatform.Linux)]
9090
[MemberData(nameof(RawSqlTestCases))]
9191
public async Task TracesRawSql(
9292
string provider,
@@ -189,7 +189,7 @@ bool ActivityFilter(string? providerName, IDbCommand command)
189189
}
190190
}
191191

192-
[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
192+
[EnabledOnDockerPlatformTheory(DockerPlatform.Linux)]
193193
[MemberData(nameof(DataContextTestCases))]
194194
public async Task TracesDataContext(
195195
string provider,

test/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
<ItemGroup>
2424
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
2525
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Instrumentation.SqlClient.Tests\SqlClientIntegrationTestsFixture.cs" Link="SqlClientIntegrationTestsFixture.cs" />
26+
<Compile Include="$(RepoRoot)\test\Shared\DockerHelper.cs" Link="Includes\DockerHelper.cs" />
27+
<Compile Include="$(RepoRoot)\test\Shared\DockerPlatform.cs" Link="Includes\DockerPlatform.cs" />
2628
<Compile Include="$(RepoRoot)\test\Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="Includes\EnabledOnDockerPlatformTheoryAttribute.cs" />
2729
</ItemGroup>
2830

test/OpenTelemetry.Instrumentation.SqlClient.Tests/OpenTelemetry.Instrumentation.SqlClient.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<ItemGroup>
1010
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
11+
<Compile Include="$(RepoRoot)\test\Shared\DockerHelper.cs" Link="Includes\DockerHelper.cs" />
12+
<Compile Include="$(RepoRoot)\test\Shared\DockerPlatform.cs" Link="Includes\DockerPlatform.cs" />
1113
<Compile Include="$(RepoRoot)\test\Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="Includes\EnabledOnDockerPlatformTheoryAttribute.cs" />
1214
<Compile Include="$(RepoRoot)\test\Shared\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="Includes\SkipUnlessEnvVarFoundTheoryAttribute.cs" />
1315
<Compile Include="$(RepoRoot)\test\Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />

test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientIntegrationTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public SqlClientIntegrationTests(SqlClientIntegrationTestsFixture fixture)
2626
this.fixture = fixture;
2727
}
2828

29-
[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
29+
[EnabledOnDockerPlatformTheory(DockerPlatform.Linux)]
3030
[InlineData(CommandType.Text, "select 1/1")]
3131
[InlineData(CommandType.Text, "select 1/1", true, "select ?/?")]
3232
[InlineData(CommandType.Text, "select 1/0", false, null, true)]
@@ -104,8 +104,7 @@ public void SuccessfulCommandTest(
104104

105105
transaction?.Commit();
106106

107-
Assert.Single(activities);
108-
var activity = activities[0];
107+
var activity = Assert.Single(activities);
109108

110109
VerifyContextInfo(commandText, commandResult, activity);
111110
VerifyActivityData(commandType, sanitizedCommandText, captureTextCommandContent, isFailure, recordException, activity);

test/Shared/DockerHelper.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Diagnostics;
5+
using System.Text;
6+
7+
namespace OpenTelemetry.Tests;
8+
9+
/// <summary>
10+
/// Determines if a required Docker engine is available.
11+
/// </summary>
12+
internal static class DockerHelper
13+
{
14+
/// <summary>
15+
/// Gets whether the specified Docker platform is available.
16+
/// </summary>
17+
/// <returns>
18+
/// <see langword="true"/> if the specified Docker platform is available, otherwise <see langword="false"/>.
19+
/// </returns>
20+
public static bool IsAvailable(DockerPlatform dockerPlatform)
21+
{
22+
const string executable = "docker";
23+
24+
var stdout = new StringBuilder();
25+
var stderr = new StringBuilder();
26+
27+
void AppendStdout(object sender, DataReceivedEventArgs e)
28+
{
29+
stdout.Append(e.Data);
30+
}
31+
32+
void AppendStderr(object sender, DataReceivedEventArgs e)
33+
{
34+
stderr.Append(e.Data);
35+
}
36+
37+
var processStartInfo = new ProcessStartInfo
38+
{
39+
FileName = executable,
40+
Arguments = string.Join(" ", "version", "--format '{{.Server.Os}}'"),
41+
RedirectStandardOutput = true,
42+
RedirectStandardError = true,
43+
UseShellExecute = false,
44+
};
45+
46+
var process = new Process
47+
{
48+
StartInfo = processStartInfo,
49+
};
50+
process.OutputDataReceived += AppendStdout;
51+
process.ErrorDataReceived += AppendStderr;
52+
53+
try
54+
{
55+
process.Start();
56+
process.BeginOutputReadLine();
57+
process.BeginErrorReadLine();
58+
process.WaitForExit();
59+
}
60+
finally
61+
{
62+
process.OutputDataReceived -= AppendStdout;
63+
process.ErrorDataReceived -= AppendStderr;
64+
}
65+
66+
return process.ExitCode == 0 && stdout.ToString().IndexOf(dockerPlatform.ToString(), StringComparison.OrdinalIgnoreCase) > 0;
67+
}
68+
}

test/Shared/DockerPlatform.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace OpenTelemetry.Tests;
5+
6+
internal enum DockerPlatform
7+
{
8+
/// <summary>
9+
/// Docker Linux engine.
10+
/// </summary>
11+
Linux,
12+
13+
/// <summary>
14+
/// Docker Windows engine.
15+
/// </summary>
16+
Windows,
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Xunit;
5+
6+
namespace OpenTelemetry.Tests;
7+
8+
/// <summary>
9+
/// This <see cref="FactAttribute" /> skips tests if the required Docker engine is not available.
10+
/// </summary>
11+
internal class EnabledOnDockerPlatformFactAttribute : FactAttribute
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="EnabledOnDockerPlatformFactAttribute" /> class.
15+
/// </summary>
16+
public EnabledOnDockerPlatformFactAttribute(DockerPlatform dockerPlatform)
17+
{
18+
if (!DockerHelper.IsAvailable(dockerPlatform))
19+
{
20+
this.Skip = $"The Docker {dockerPlatform} engine is not available.";
21+
}
22+
}
23+
}
Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System.Diagnostics;
5-
using System.Text;
64
using Xunit;
75

86
namespace OpenTelemetry.Tests;
@@ -17,68 +15,9 @@ internal class EnabledOnDockerPlatformTheoryAttribute : TheoryAttribute
1715
/// </summary>
1816
public EnabledOnDockerPlatformTheoryAttribute(DockerPlatform dockerPlatform)
1917
{
20-
const string executable = "docker";
21-
22-
var stdout = new StringBuilder();
23-
var stderr = new StringBuilder();
24-
25-
void AppendStdout(object sender, DataReceivedEventArgs e)
26-
{
27-
stdout.Append(e.Data);
28-
}
29-
30-
void AppendStderr(object sender, DataReceivedEventArgs e)
31-
{
32-
stderr.Append(e.Data);
33-
}
34-
35-
var processStartInfo = new ProcessStartInfo
36-
{
37-
FileName = executable,
38-
Arguments = string.Join(" ", "version", "--format '{{.Server.Os}}'"),
39-
RedirectStandardOutput = true,
40-
RedirectStandardError = true,
41-
UseShellExecute = false,
42-
};
43-
44-
var process = new Process
45-
{
46-
StartInfo = processStartInfo,
47-
};
48-
process.OutputDataReceived += AppendStdout;
49-
process.ErrorDataReceived += AppendStderr;
50-
51-
try
52-
{
53-
process.Start();
54-
process.BeginOutputReadLine();
55-
process.BeginErrorReadLine();
56-
process.WaitForExit();
57-
}
58-
finally
59-
{
60-
process.OutputDataReceived -= AppendStdout;
61-
process.ErrorDataReceived -= AppendStderr;
62-
}
63-
64-
if (0.Equals(process.ExitCode) && stdout.ToString().IndexOf(dockerPlatform.ToString(), StringComparison.OrdinalIgnoreCase) > 0)
18+
if (!DockerHelper.IsAvailable(dockerPlatform))
6519
{
66-
return;
20+
this.Skip = $"The Docker {dockerPlatform} engine is not available.";
6721
}
68-
69-
this.Skip = $"The Docker {dockerPlatform} engine is not available.";
70-
}
71-
72-
internal enum DockerPlatform
73-
{
74-
/// <summary>
75-
/// Docker Linux engine.
76-
/// </summary>
77-
Linux,
78-
79-
/// <summary>
80-
/// Docker Windows engine.
81-
/// </summary>
82-
Windows,
8322
}
8423
}

0 commit comments

Comments
 (0)