Bootstrapping tests (#223)
* Add bootstrap tests * Rename test project * Add new tests to the nuke workflow * Fix csproj for non-windows os * Add support for no exporter set scenario * Add accessors to private methods in build steps * Add comment on updating the test list * InstrumentationTests require BOOSTRAPPING_TESTS env var Co-authored-by: Robert Pająk <pellared@hotmail.com>
This commit is contained in:
parent
3a4226bfb5
commit
d88bf5ccb9
|
@ -108,6 +108,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationTests.MongoDB",
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.MongoDB", "test\test-applications\integrations\Samples.MongoDB\Samples.MongoDB.csproj", "{02D6A472-A602-4BF4-A539-8AD165097898}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.ClrProfiler.Managed.Bootstrapping.Tests", "test\OpenTelemetry.ClrProfiler.Managed.Bootstrapping.Tests\OpenTelemetry.ClrProfiler.Managed.Bootstrapping.Tests.csproj", "{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -374,6 +376,18 @@ Global
|
|||
{02D6A472-A602-4BF4-A539-8AD165097898}.Release|x64.Build.0 = Release|x64
|
||||
{02D6A472-A602-4BF4-A539-8AD165097898}.Release|x86.ActiveCfg = Release|x86
|
||||
{02D6A472-A602-4BF4-A539-8AD165097898}.Release|x86.Build.0 = Release|x86
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Release|x64.Build.0 = Release|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -407,6 +421,7 @@ Global
|
|||
{CF5EFBA1-A852-4D11-968D-CE6A9A46B299} = {E3ABDF7D-4592-4923-907C-D351DDF1CBE9}
|
||||
{446D0E14-2788-43DD-A465-8F16DA7014F8} = {357C3F25-98CA-41C6-BA2C-20968695D026}
|
||||
{02D6A472-A602-4BF4-A539-8AD165097898} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
|
||||
{35AB65D8-D76E-40C6-B10C-501A6D44C9E0} = {5C915382-C886-457D-8641-9E766D8E5A17}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
|
||||
|
|
|
@ -109,6 +109,11 @@ partial class Build
|
|||
.SetConfiguration(BuildConfiguration)
|
||||
.SetNoRestore(true));
|
||||
|
||||
DotNetBuild(x => x
|
||||
.SetProjectFile(Solution.GetProject(Projects.Tests.ClrProfilerManagedBootstrappingTests))
|
||||
.SetConfiguration(BuildConfiguration)
|
||||
.SetNoRestore(true));
|
||||
|
||||
DotNetMSBuild(x => x
|
||||
.SetTargetPath(MsBuildProject)
|
||||
.SetTargetPlatform(Platform)
|
||||
|
@ -224,9 +229,11 @@ partial class Build
|
|||
|
||||
private void RunUnitTests()
|
||||
{
|
||||
Project[] unitTests = new[]
|
||||
RunBootstrappingTests();
|
||||
|
||||
var unitTestProjects = new[]
|
||||
{
|
||||
Solution.GetProject(Projects.Tests.ClrProfilerManagedLoaderTests)
|
||||
Solution.GetProject(Projects.Tests.ClrProfilerManagedLoaderTests)
|
||||
};
|
||||
|
||||
DotNetTest(config => config
|
||||
|
@ -234,11 +241,41 @@ partial class Build
|
|||
.SetTargetPlatformAnyCPU()
|
||||
.EnableNoRestore()
|
||||
.EnableNoBuild()
|
||||
.CombineWith(unitTests, (s, project) => s
|
||||
.CombineWith(unitTestProjects, (s, project) => s
|
||||
.EnableTrxLogOutput(GetResultsDirectory(project))
|
||||
.SetProjectFile(project)), degreeOfParallelism: 4);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bootstrapping tests require every single test to be run in a separate process
|
||||
/// so the tracer can be created from scratch for each of them.
|
||||
/// </summary>
|
||||
private void RunBootstrappingTests()
|
||||
{
|
||||
var project = Solution.GetProject(Projects.Tests.ClrProfilerManagedBootstrappingTests);
|
||||
|
||||
const string testPrefix = "OpenTelemetry.ClrProfiler.Managed.Bootstrapping.Tests.InstrumentationTests";
|
||||
var testNames = new[] {
|
||||
"Initialize_WithDisabledFlag_DoesNotCreateTracerProvider",
|
||||
"Initialize_WithDefaultFlag_CreatesTracerProvider",
|
||||
"Initialize_WithEnabledFlag_CreatesTracerProvider",
|
||||
"Initialize_WithPreviouslyCreatedTracerProvider_WorksCorrectly"
|
||||
}.Select(name => $"{testPrefix}.{name}");
|
||||
|
||||
foreach (var testName in testNames)
|
||||
{
|
||||
DotNetTest(config => config
|
||||
.SetConfiguration(BuildConfiguration)
|
||||
.SetTargetPlatformAnyCPU()
|
||||
.EnableNoRestore()
|
||||
.EnableNoBuild()
|
||||
.EnableTrxLogOutput(GetResultsDirectory(project))
|
||||
.SetProjectFile(project)
|
||||
.SetFilter(testName)
|
||||
.SetProcessEnvironmentVariable("BOOSTRAPPING_TESTS", "true"));
|
||||
}
|
||||
}
|
||||
|
||||
private void RunIntegrationTests()
|
||||
{
|
||||
Project[] integrationTests = Solution
|
||||
|
|
|
@ -8,6 +8,7 @@ public static class Projects
|
|||
{
|
||||
public const string ClrProfilerNativeTests = "OpenTelemetry.ClrProfiler.Native.Tests";
|
||||
public const string ClrProfilerManagedLoaderTests = "OpenTelemetry.ClrProfiler.Managed.Loader.Tests";
|
||||
public const string ClrProfilerManagedBootstrappingTests = "OpenTelemetry.ClrProfiler.Managed.Bootstrapping.Tests";
|
||||
}
|
||||
|
||||
public static class Mocks
|
||||
|
|
|
@ -90,6 +90,7 @@ namespace OpenTelemetry.ClrProfiler.Managed.Configuration
|
|||
builder.AddOtlpExporter();
|
||||
break;
|
||||
case "":
|
||||
case null:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("The exporter name is not recognised");
|
||||
|
|
|
@ -75,16 +75,23 @@ namespace OpenTelemetry.ClrProfiler.Managed
|
|||
|
||||
try
|
||||
{
|
||||
// Instantiate the OpenTracing shim. The underlying OpenTelemetry tracer will create
|
||||
// spans using the "OpenTelemetry.ClrProfiler.OpenTracingShim" source.
|
||||
var openTracingShim = new TracerShim(
|
||||
_tracerProvider.GetTracer("OpenTelemetry.ClrProfiler.OpenTracingShim"),
|
||||
Propagators.DefaultTextMapPropagator);
|
||||
if (_tracerProvider is not null)
|
||||
{
|
||||
// Instantiate the OpenTracing shim. The underlying OpenTelemetry tracer will create
|
||||
// spans using the "OpenTelemetry.ClrProfiler.OpenTracingShim" source.
|
||||
var openTracingShim = new TracerShim(
|
||||
_tracerProvider.GetTracer("OpenTelemetry.ClrProfiler.OpenTracingShim"),
|
||||
Propagators.DefaultTextMapPropagator);
|
||||
|
||||
// This registration must occur prior to any reference to the OpenTracing tracer:
|
||||
// otherwise the no-op tracer is going to be used by OpenTracing instead.
|
||||
OpenTracing.Util.GlobalTracer.Register(openTracingShim);
|
||||
Log("OpenTracingShim loaded.");
|
||||
// This registration must occur prior to any reference to the OpenTracing tracer:
|
||||
// otherwise the no-op tracer is going to be used by OpenTracing instead.
|
||||
OpenTracing.Util.GlobalTracer.RegisterIfAbsent(openTracingShim);
|
||||
Log("OpenTracingShim loaded.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log("OpenTracingShim was not loaded as the provider is not initialized.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using OpenTelemetry.Trace;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenTelemetry.ClrProfiler.Managed.Bootstrapping.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// When you add a new tests or change the name of one of the existing ones please remember to reflect the changes
|
||||
/// in the build project, by updating the list in RunBootstrappingTests method of Build.Steps.cs.
|
||||
/// Take notice that each test should be executed as a seperate process. Because of that, the tests require
|
||||
/// BOOSTRAPPING_TESTS environmental variable to be set, to mitigate the risk of running all of the tests at once.
|
||||
/// </summary>
|
||||
public class InstrumentationTests
|
||||
{
|
||||
private readonly ActivitySource _otelActivitySource = new("OpenTelemetry.ClrProfiler.*");
|
||||
private readonly ActivitySource _customActivitySource = new("Custom");
|
||||
|
||||
[FactRequiringEnvVar]
|
||||
public void Initialize_WithDisabledFlag_DoesNotCreateTracerProvider()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OTEL_DOTNET_TRACER_LOAD_AT_STARTUP", "false");
|
||||
|
||||
Instrumentation.Initialize();
|
||||
var otelActivity = _otelActivitySource.StartActivity("OtelActivity");
|
||||
var customActivity = _customActivitySource.StartActivity("CustomActivity");
|
||||
|
||||
Assert.Null(otelActivity);
|
||||
Assert.Null(customActivity);
|
||||
}
|
||||
|
||||
[FactRequiringEnvVar]
|
||||
public void Initialize_WithDefaultFlag_CreatesTracerProvider()
|
||||
{
|
||||
Instrumentation.Initialize();
|
||||
|
||||
Instrumentation.Initialize();
|
||||
var otelActivity = _otelActivitySource.StartActivity("OtelActivity");
|
||||
var customActivity = _customActivitySource.StartActivity("CustomActivity");
|
||||
|
||||
Assert.NotNull(otelActivity);
|
||||
Assert.Null(customActivity);
|
||||
}
|
||||
|
||||
[FactRequiringEnvVar]
|
||||
public void Initialize_WithEnabledFlag_CreatesTracerProvider()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OTEL_DOTNET_TRACER_LOAD_AT_STARTUP", "true");
|
||||
|
||||
Instrumentation.Initialize();
|
||||
var otelActivity = _otelActivitySource.StartActivity("OtelActivity");
|
||||
var customActivity = _customActivitySource.StartActivity("CustomActivity");
|
||||
|
||||
Assert.NotNull(otelActivity);
|
||||
Assert.Null(customActivity);
|
||||
}
|
||||
|
||||
[FactRequiringEnvVar]
|
||||
public void Initialize_WithPreviouslyCreatedTracerProvider_WorksCorrectly()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OTEL_DOTNET_TRACER_LOAD_AT_STARTUP", "false");
|
||||
var tracer = Sdk
|
||||
.CreateTracerProviderBuilder()
|
||||
.AddSource("Custom")
|
||||
.Build();
|
||||
|
||||
Instrumentation.Initialize();
|
||||
var otelActivity = _otelActivitySource.StartActivity("OtelActivity");
|
||||
var customActivity = _customActivitySource.StartActivity("CustomActivity");
|
||||
|
||||
Assert.Null(otelActivity);
|
||||
Assert.NotNull(customActivity);
|
||||
}
|
||||
|
||||
public sealed class FactRequiringEnvVarAttribute : FactAttribute
|
||||
{
|
||||
private const string EnvVar = "BOOSTRAPPING_TESTS";
|
||||
|
||||
public FactRequiringEnvVarAttribute()
|
||||
{
|
||||
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(EnvVar)))
|
||||
{
|
||||
Skip = $"Ignore as {EnvVar} is not set";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">net461;netcoreapp3.1</TargetFrameworks>
|
||||
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp3.1</TargetFrameworks>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\OpenTelemetry.ClrProfiler.Managed\OpenTelemetry.ClrProfiler.Managed.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue