Skip to content

Commit

Permalink
Convert WebRequestTests to snapshot tests (#6223)
Browse files Browse the repository at this point in the history
## Summary of changes

Converts the `WebRequestTests` to use snapshot tests instead of adhoc
assertions

## Reason for change

While working on something else (.NET 9) these tests were failing, but
it was really hard to understand _why_ - snapshots make that easy.

## Implementation details

Ran locally with .NET FX, .NET 8, and .NET Core 2.1, and checked for
differences

## Test coverage

Coverage is the same - I'll also do [an "all TFMs"
run](https://dev.azure.com/datadoghq/dd-trace-dotnet/_build/results?buildId=166697&view=results)
to make sure nothing will break post-merge
  • Loading branch information
andrewlock authored Oct 31, 2024
1 parent 8707242 commit ddc6f9c
Show file tree
Hide file tree
Showing 5 changed files with 11,431 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
using Datadog.Trace.Configuration;
using Datadog.Trace.TestHelpers;
using FluentAssertions;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

namespace Datadog.Trace.ClrProfiler.IntegrationTests
{
[UsesVerify]
[CollectionDefinition(nameof(WebRequestTests), DisableParallelization = true)]
[Collection(nameof(WebRequestTests))]
public class WebRequestTests : TracingIntegrationTest
Expand Down Expand Up @@ -73,8 +75,8 @@ public async Task TracingDisabled_DoesNotSubmitsTraces()
private async Task RunTest(string metadataSchemaVersion)
{
SetInstrumentationVerification();

var expectedAllSpansCount = 130;
var expectedSpanCount = 87;

int httpPort = TcpPortProvider.GetOpenPort();
Output.WriteLine($"Assigning port {httpPort} for the httpPort.");
Expand All @@ -84,34 +86,34 @@ private async Task RunTest(string metadataSchemaVersion)
var clientSpanServiceName = isExternalSpan ? $"{EnvironmentHelper.FullSampleName}-http-client" : EnvironmentHelper.FullSampleName;

using var telemetry = this.ConfigureTelemetry();
using (var agent = EnvironmentHelper.GetMockAgent())
using (ProcessResult processResult = await RunSampleAndWaitForExit(agent, arguments: $"Port={httpPort}"))
{
var allSpans = agent.WaitForSpans(expectedAllSpansCount).OrderBy(s => s.Start).ToList();
allSpans.Should().OnlyHaveUniqueItems(s => new { s.SpanId, s.TraceId });

var spans = allSpans.Where(s => s.Type == SpanTypes.Http).ToList();
spans.Should().HaveCount(expectedSpanCount);
ValidateIntegrationSpans(spans, metadataSchemaVersion, expectedServiceName: clientSpanServiceName, isExternalSpan);

var okSpans = spans.Where(s => s.Tags[Tags.HttpStatusCode] == "200").ToList();
var notFoundSpans = spans.Where(s => s.Tags[Tags.HttpStatusCode] == "404").ToList();
var teapotSpans = spans.Where(s => s.Tags[Tags.HttpStatusCode] == "418").ToList();

(okSpans.Count + notFoundSpans.Count + teapotSpans.Count).Should().Be(expectedSpanCount);
okSpans.Should().OnlyContain(s => s.Error == 0);
notFoundSpans.Should().OnlyContain(s => s.Error == 0);
teapotSpans.Should().OnlyContain(s => s.Error == 1);

var firstSpan = spans.First();
var traceId = StringUtil.GetHeader(processResult.StandardOutput, HttpHeaderNames.TraceId);
var parentSpanId = StringUtil.GetHeader(processResult.StandardOutput, HttpHeaderNames.ParentId);

Assert.Equal(firstSpan.TraceId.ToString(CultureInfo.InvariantCulture), traceId);
Assert.Equal(firstSpan.SpanId.ToString(CultureInfo.InvariantCulture), parentSpanId);
telemetry.AssertIntegrationEnabled(IntegrationId.WebRequest);
VerifyInstrumentation(processResult.Process);
}
using var agent = EnvironmentHelper.GetMockAgent();
using ProcessResult processResult = await RunSampleAndWaitForExit(agent, arguments: $"Port={httpPort}");

var allSpans = agent.WaitForSpans(expectedAllSpansCount).OrderBy(s => s.Start).ToList();

var settings = VerifyHelper.GetSpanVerifierSettings();
#if NETCOREAPP
// different TFMs use different underlying handlers, which we don't really care about for the snapshots
settings.AddSimpleScrubber("System.Net.Http.HttpClientHandler", "System.Net.Http.SocketsHttpHandler");
#endif
var suffix = EnvironmentHelper.IsCoreClr() ? string.Empty : "_netfx";
await VerifyHelper.VerifySpans(
allSpans,
settings,
spans =>
spans.OrderBy(x => VerifyHelper.GetRootSpanResourceName(x, spans))
.ThenBy(x => VerifyHelper.GetSpanDepth(x, spans))
.ThenBy(x => x.Tags.TryGetValue("http.url", out var url) ? url : string.Empty)
.ThenBy(x => x.Start)
.ThenBy(x => x.Duration))
.UseFileName($"{nameof(WebRequestTests)}{suffix}_{metadataSchemaVersion}");

allSpans.Should().OnlyHaveUniqueItems(s => new { s.SpanId, s.TraceId });
var httpSpans = allSpans.Where(s => s.Type == SpanTypes.Http).ToList();
ValidateIntegrationSpans(httpSpans, metadataSchemaVersion, expectedServiceName: clientSpanServiceName, isExternalSpan);

telemetry.AssertIntegrationEnabled(IntegrationId.WebRequest);
VerifyInstrumentation(processResult.Process);
}
}
}
Loading

0 comments on commit ddc6f9c

Please sign in to comment.