Skip to content

Commit

Permalink
Convert WebRequestTests to snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Oct 30, 2024
1 parent e20259b commit 4e63c00
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 4e63c00

Please sign in to comment.