Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert WebRequestTests to snapshot tests #6223

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit annoying, but we need to add this extra sorting layer to get stable snapshots

.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
Loading