Skip to content

Commit f7a0f93

Browse files
committed
add unit tests
1 parent 1ecc180 commit f7a0f93

File tree

7 files changed

+288
-4
lines changed

7 files changed

+288
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <copyright file="MockEnvironmentVariableProvider.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using System.Collections.Generic;
9+
using Datadog.Trace.ClrProfiler.Managed.Loader;
10+
11+
namespace Datadog.Trace.Tests.ClrProfiler.Managed.Loader;
12+
13+
public class MockEnvironmentVariableProvider : IEnvironmentVariableProvider
14+
{
15+
private readonly Dictionary<string, string> _variables = new();
16+
17+
public void SetEnvironmentVariable(string key, string? value)
18+
{
19+
if (value is null)
20+
{
21+
_variables.Remove(key);
22+
}
23+
else
24+
{
25+
_variables[key] = value;
26+
}
27+
}
28+
29+
public string? GetEnvironmentVariable(string key)
30+
{
31+
return _variables.TryGetValue(key, out var value) ? value : null;
32+
}
33+
34+
public void Clear()
35+
{
36+
_variables.Clear();
37+
}
38+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// <copyright file="StartupLoggerTests.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
using System;
7+
using System.IO;
8+
using Datadog.Trace.ClrProfiler.Managed.Loader;
9+
using FluentAssertions;
10+
using Xunit;
11+
12+
namespace Datadog.Trace.Tests.ClrProfiler.Managed.Loader;
13+
14+
public class StartupLoggerTests
15+
{
16+
[Fact]
17+
public void GetLogDirectoryFromEnvVars_WithExplicitLogDirectory_ReturnsSpecifiedDirectory()
18+
{
19+
const string logsDirectory = @"/logs";
20+
21+
var envVars = new MockEnvironmentVariableProvider();
22+
envVars.SetEnvironmentVariable("DD_TRACE_LOG_DIRECTORY", logsDirectory);
23+
24+
StartupLogger.GetLogDirectoryFromEnvVars(envVars).Should().Be(logsDirectory);
25+
}
26+
27+
[Fact]
28+
public void GetLogDirectoryFromEnvVars_WithLogPath_ReturnsDirectoryFromPath()
29+
{
30+
const string logsDirectory = @"/logs";
31+
var logsFilename = Path.Combine(logsDirectory, "log.txt");
32+
33+
var envVars = new MockEnvironmentVariableProvider();
34+
envVars.SetEnvironmentVariable("DD_TRACE_LOG_PATH", logsFilename);
35+
36+
StartupLogger.GetLogDirectoryFromEnvVars(envVars).Should().Be(logsDirectory);
37+
}
38+
39+
[Fact]
40+
public void GetLogDirectoryFromEnvVars_WithNonExistentDirectory_ReturnsDirectory()
41+
{
42+
var nonExistentDir = Path.Combine(Path.GetTempPath(), $"dd-trace-test-{Guid.NewGuid()}");
43+
44+
var envVars = new MockEnvironmentVariableProvider();
45+
envVars.SetEnvironmentVariable("DD_TRACE_LOG_DIRECTORY", nonExistentDir);
46+
47+
var result = StartupLogger.GetLogDirectoryFromEnvVars(envVars);
48+
49+
// The method should return the directory path from env vars
50+
result.Should().Be(nonExistentDir);
51+
}
52+
53+
[Fact]
54+
public void GetLogDirectoryFromEnvVars_WithNoEnvironmentVariables_ReturnsNull()
55+
{
56+
var envVars = new MockEnvironmentVariableProvider();
57+
StartupLogger.GetLogDirectoryFromEnvVars(envVars).Should().BeNull();
58+
}
59+
60+
[Fact]
61+
public void GetDefaultLogDirectory_WithNoEnvironmentVariables_ReturnsDefaultDirectory()
62+
{
63+
var envVars = new MockEnvironmentVariableProvider();
64+
var result = StartupLogger.GetDefaultLogDirectory(envVars);
65+
66+
result.Should().NotBeNullOrEmpty();
67+
68+
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
69+
{
70+
result.Should().EndWith(@"Datadog .NET Tracer\logs");
71+
}
72+
else
73+
{
74+
result.Should().Be("/var/log/datadog/dotnet");
75+
}
76+
}
77+
78+
[Fact]
79+
public void GetDefaultLogDirectory_WithAzureAppServices_ReturnsAzureLogDirectory()
80+
{
81+
var envVars = new MockEnvironmentVariableProvider();
82+
envVars.SetEnvironmentVariable("WEBSITE_SITE_NAME", "my-site-name");
83+
84+
var result = StartupLogger.GetDefaultLogDirectory(envVars);
85+
result.Should().NotBeNullOrEmpty();
86+
87+
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
88+
{
89+
result.Should().Be(@"C:\home\LogFiles\datadog");
90+
}
91+
else
92+
{
93+
result.Should().Be("/home/LogFiles/datadog");
94+
}
95+
}
96+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// <copyright file="StartupNetCoreTests.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#if NETCOREAPP
7+
8+
using System;
9+
using System.IO;
10+
using System.Reflection;
11+
using Datadog.Trace.ClrProfiler.Managed.Loader;
12+
using FluentAssertions;
13+
using Xunit;
14+
15+
namespace Datadog.Trace.Tests.ClrProfiler.Managed.Loader
16+
{
17+
public class StartupNetCoreTests
18+
{
19+
[Theory]
20+
[InlineData("System.Private.CoreLib.resources")]
21+
[InlineData("System.Net.Http")]
22+
public void ResolveAssembly_WithExcludedAssembly_ReturnsNull(string assemblyName)
23+
{
24+
// Use reflection to call the private ResolveAssembly method
25+
var method = typeof(Startup).GetMethod(
26+
"ResolveAssembly",
27+
BindingFlags.NonPublic | BindingFlags.Static,
28+
null,
29+
new[] { typeof(string) },
30+
null);
31+
32+
method.Should().NotBeNull();
33+
34+
var result = method!.Invoke(null, new object[] { assemblyName }) as Assembly;
35+
36+
result.Should().BeNull();
37+
}
38+
39+
[Fact]
40+
public void DependencyLoadContext_ShouldBeInitialized()
41+
{
42+
var loadContext = Startup.DependencyLoadContext;
43+
44+
loadContext.Should().NotBeNull();
45+
}
46+
}
47+
}
48+
49+
#endif
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// <copyright file="StartupNetFrameworkTests.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#if NETFRAMEWORK
7+
8+
#nullable enable
9+
10+
using System;
11+
using System.Collections.Generic;
12+
using System.IO;
13+
using System.Reflection;
14+
using Datadog.Trace.ClrProfiler.Managed.Loader;
15+
using FluentAssertions;
16+
using Xunit;
17+
18+
namespace Datadog.Trace.Tests.ClrProfiler.Managed.Loader
19+
{
20+
public class StartupNetFrameworkTests
21+
{
22+
[Theory]
23+
[InlineData("mscorlib.resources")]
24+
[InlineData("System.Net.Http")]
25+
[InlineData("vstest.console.resources")]
26+
public void ResolveAssembly_WithExcludedAssembly_ReturnsNull(string assemblyName)
27+
{
28+
// Use reflection to call the private ResolveAssembly method
29+
var method = typeof(Startup).GetMethod(
30+
"ResolveAssembly",
31+
BindingFlags.NonPublic | BindingFlags.Static,
32+
null,
33+
new[] { typeof(string) },
34+
null);
35+
36+
method.Should().NotBeNull();
37+
38+
var result = method!.Invoke(null, new object[] { assemblyName }) as Assembly;
39+
40+
result.Should().BeNull();
41+
}
42+
43+
[Fact]
44+
public void ResolveAssembly_WithNonExistentAssembly_ReturnsNull()
45+
{
46+
// Use reflection to call the private ResolveAssembly method
47+
var method = typeof(Startup).GetMethod(
48+
"ResolveAssembly",
49+
BindingFlags.NonPublic | BindingFlags.Static,
50+
null,
51+
new[] { typeof(string) },
52+
null);
53+
54+
method.Should().NotBeNull();
55+
56+
var result = method!.Invoke(null, new object[] { "NonExistent.Assembly, Version=1.0.0.0" }) as Assembly;
57+
58+
result.Should().BeNull();
59+
}
60+
}
61+
}
62+
63+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <copyright file="StartupTests.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using Datadog.Trace.ClrProfiler.Managed.Loader;
9+
using FluentAssertions;
10+
using Xunit;
11+
12+
namespace Datadog.Trace.Tests.ClrProfiler.Managed.Loader
13+
{
14+
public class StartupTests
15+
{
16+
[Fact]
17+
public void ManagedProfilerDirectory_ShouldBeInitialized()
18+
{
19+
// The static constructor should have run and set ManagedProfilerDirectory
20+
// It may be null if DD_DOTNET_TRACER_HOME is not set
21+
var directory = Startup.ManagedProfilerDirectory;
22+
23+
// We just verify it doesn't throw - the value depends on environment
24+
directory.Should().NotBeNull();
25+
}
26+
27+
[Theory]
28+
[InlineData(null)]
29+
[InlineData("")]
30+
public void ResolveManagedProfilerDirectory_WithNullOrEmptyTracerHome_ReturnsNull(string? value)
31+
{
32+
var envVars = new MockEnvironmentVariableProvider();
33+
envVars.SetEnvironmentVariable("DD_DOTNET_TRACER_HOME", value);
34+
35+
Startup.ResolveManagedProfilerDirectory(envVars).Should().BeNull();
36+
}
37+
}
38+
}

tracer/test/Datadog.Trace.Tests/DistributedTracer/DistributedTracerRestorerAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ internal class DistributedTracerRestorerAttribute : BeforeAfterTestAttribute
1717

1818
public override void Before(MethodInfo methodUnderTest)
1919
{
20-
_distributedTracer = ClrProfiler.DistributedTracer.Instance;
20+
_distributedTracer = Datadog.Trace.ClrProfiler.DistributedTracer.Instance;
2121
base.Before(methodUnderTest);
2222
}
2323

2424
public override void After(MethodInfo methodUnderTest)
2525
{
26-
ClrProfiler.DistributedTracer.SetInstanceOnlyForTests(_distributedTracer);
26+
Datadog.Trace.ClrProfiler.DistributedTracer.SetInstanceOnlyForTests(_distributedTracer);
2727
base.After(methodUnderTest);
2828
}
2929
}

tracer/test/Datadog.Trace.Tests/DistributedTracer/DistributedTracerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void GetSpanContext()
2727
distributedTracer.Setup(t => t.GetSpanContext()).Returns(spanContext);
2828
distributedTracer.Setup(t => t.GetRuntimeId()).Returns(Guid.NewGuid().ToString());
2929

30-
ClrProfiler.DistributedTracer.SetInstanceOnlyForTests(distributedTracer.Object);
30+
Datadog.Trace.ClrProfiler.DistributedTracer.SetInstanceOnlyForTests(distributedTracer.Object);
3131

3232
using var parentScope = Tracer.Instance.StartActive("Parent");
3333
using var scope = (Scope)Tracer.Instance.StartActive("Test");
@@ -45,7 +45,7 @@ public void SetSpanContext()
4545

4646
distributedTracer.Setup(t => t.GetRuntimeId()).Returns(Guid.NewGuid().ToString());
4747

48-
ClrProfiler.DistributedTracer.SetInstanceOnlyForTests(distributedTracer.Object);
48+
Datadog.Trace.ClrProfiler.DistributedTracer.SetInstanceOnlyForTests(distributedTracer.Object);
4949

5050
using (var scope = (Scope)Tracer.Instance.StartActive("Test"))
5151
{

0 commit comments

Comments
 (0)