-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathTestHelper.cs
647 lines (538 loc) · 27.1 KB
/
TestHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// <copyright file="TestHelper.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Datadog.Trace.Ci;
using Datadog.Trace.Ci.CiEnvironment;
using Datadog.Trace.Configuration;
using Datadog.Trace.Configuration.Telemetry;
using Datadog.Trace.Debugger.Helpers;
using Datadog.Trace.ExtensionMethods;
using Datadog.Trace.Iast.Telemetry;
using Datadog.Trace.Logging;
using Datadog.Trace.RemoteConfigurationManagement.Protocol;
using Datadog.Trace.RemoteConfigurationManagement.Protocol.Tuf;
using Datadog.Trace.Util;
using Datadog.Trace.Vendors.Newtonsoft.Json;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
namespace Datadog.Trace.TestHelpers
{
public abstract class TestHelper : IDisposable
{
protected TestHelper(string sampleAppName, string samplePathOverrides, ITestOutputHelper output)
: this(new EnvironmentHelper(sampleAppName, typeof(TestHelper), output, samplePathOverrides), output)
{
}
protected TestHelper(string sampleAppName, string samplePathOverrides, ITestOutputHelper output, bool prependSamplesToAppName)
: this(new EnvironmentHelper(sampleAppName, typeof(TestHelper), output, samplePathOverrides, prependSamplesToAppName: false), output)
{
}
protected TestHelper(string sampleAppName, ITestOutputHelper output)
: this(new EnvironmentHelper(sampleAppName, typeof(TestHelper), output), output)
{
}
protected TestHelper(EnvironmentHelper environmentHelper, ITestOutputHelper output)
{
EnvironmentHelper = environmentHelper;
Output = output;
Output.WriteLine($"Platform: {EnvironmentTools.GetPlatform()}");
Output.WriteLine($"TargetPlatform: {EnvironmentTools.GetTestTargetPlatform()}");
Output.WriteLine($"Configuration: {EnvironmentTools.GetBuildConfiguration()}");
Output.WriteLine($"TargetFramework: {EnvironmentHelper.GetTargetFramework()}");
Output.WriteLine($".NET Core: {EnvironmentHelper.IsCoreClr()}");
Output.WriteLine($"Native Loader DLL: {EnvironmentHelper.GetNativeLoaderPath()}");
// the directory would be created anyway, but in certain case a delay can lead to an exception from the LogEntryWatcher
Directory.CreateDirectory(LogDirectory);
SetEnvironmentVariable(ConfigurationKeys.LogDirectory, LogDirectory);
}
public bool SecurityEnabled { get; private set; }
protected virtual string LogDirectory => Path.Combine(DatadogLoggingFactory.GetLogDirectory(NullConfigurationTelemetry.Instance), $"{GetType().Name}Logs");
protected EnvironmentHelper EnvironmentHelper { get; }
protected string TestPrefix => $"{EnvironmentTools.GetBuildConfiguration()}.{EnvironmentHelper.GetTargetFramework()}";
protected ITestOutputHelper Output { get; }
public virtual void Dispose()
{
}
public async Task<Process> StartDotnetTestSample(MockTracerAgent agent, string arguments, string packageVersion, int aspNetCorePort, string framework = "", bool forceVsTestParam = false)
{
// get path to sample app that the profiler will attach to
string sampleAppPath = EnvironmentHelper.GetTestCommandForSampleApplicationPath(packageVersion, framework);
if (!File.Exists(sampleAppPath))
{
throw new Exception($"application not found: {sampleAppPath}");
}
Output.WriteLine($"Starting Application: {sampleAppPath} {arguments ?? string.Empty}");
string testCli = forceVsTestParam ? EnvironmentHelper.GetDotnetExe() : EnvironmentHelper.GetDotNetTest();
string exec = testCli;
string appPath = testCli.StartsWith("dotnet") || testCli.Contains("dotnet.exe") || forceVsTestParam ? $"vstest {sampleAppPath}" : sampleAppPath;
Output.WriteLine("Executable: " + exec);
Output.WriteLine($"ApplicationPath: {appPath} {arguments ?? string.Empty}");
var process = await ProfilerHelper.StartProcessWithProfiler(
exec,
EnvironmentHelper,
agent,
$"{appPath} {arguments ?? string.Empty}",
aspNetCorePort: aspNetCorePort,
processToProfile: exec + ";testhost.exe;testhost.x86.exe");
Output.WriteLine($"ProcessId: {process.Id}");
return process;
}
public async Task<ProcessResult> RunDotnetTestSampleAndWaitForExit(MockTracerAgent agent, string arguments = null, string packageVersion = "", string framework = "", bool forceVsTestParam = false, int expectedExitCode = 0)
{
var process = await StartDotnetTestSample(agent, arguments, packageVersion, aspNetCorePort: 5000, framework: framework, forceVsTestParam: forceVsTestParam);
using var helper = new ProcessHelper(process);
return WaitForProcessResult(helper, expectedExitCode, dumpChildProcesses: true);
}
public async Task<Process> StartSample(MockTracerAgent agent, string arguments, string packageVersion, int aspNetCorePort, string framework = "", bool? enableSecurity = null, string externalRulesFile = null, bool usePublishWithRID = false, string dotnetRuntimeArgs = null)
{
// get path to sample app that the profiler will attach to
var sampleAppPath = EnvironmentHelper.GetSampleApplicationPath(packageVersion, framework, usePublishWithRID);
if (!File.Exists(sampleAppPath))
{
throw new Exception($"application not found: {sampleAppPath}");
}
var runtimeArgs = string.Empty;
if (!string.IsNullOrEmpty(dotnetRuntimeArgs))
{
if (!EnvironmentHelper.IsCoreClr() || usePublishWithRID)
{
throw new Exception($"Cannot use {nameof(dotnetRuntimeArgs)} with .NET Framework or when publishing with RID");
}
runtimeArgs = $"{dotnetRuntimeArgs} ";
}
Output.WriteLine($"Starting Application: {sampleAppPath}");
var executable = EnvironmentHelper.IsCoreClr() && !usePublishWithRID ? EnvironmentHelper.GetSampleExecutionSource() : sampleAppPath;
var args = EnvironmentHelper.IsCoreClr() && !usePublishWithRID ? $"{runtimeArgs}{sampleAppPath} {arguments ?? string.Empty}" : arguments;
var process = await ProfilerHelper.StartProcessWithProfiler(
executable,
EnvironmentHelper,
agent,
args,
aspNetCorePort: aspNetCorePort,
processToProfile: executable,
enableSecurity: enableSecurity,
externalRulesFile: externalRulesFile,
ignoreProfilerProcessesVar: usePublishWithRID);
Output.WriteLine($"ProcessId: {process.Id}");
return process;
}
public async Task<ProcessResult> RunSampleAndWaitForExit(MockTracerAgent agent, string arguments = null, string packageVersion = "", string framework = "", int aspNetCorePort = 5000, bool usePublishWithRID = false, string dotnetRuntimeArgs = null)
{
var process = await StartSample(agent, arguments, packageVersion, aspNetCorePort: aspNetCorePort, framework: framework, usePublishWithRID: usePublishWithRID, dotnetRuntimeArgs: dotnetRuntimeArgs);
using var helper = new ProcessHelper(process);
return WaitForProcessResult(helper);
}
public ProcessResult WaitForProcessResult(ProcessHelper helper, int expectedExitCode = 0, bool dumpChildProcesses = false)
{
// this is _way_ too long, but we want to be v. safe
// the goal is just to make sure we kill the test before
// the whole CI run times out
var process = helper.Process;
var timeoutMs = (int)TimeSpan.FromMinutes(10).TotalMilliseconds;
var ranToCompletion = process.WaitForExit(timeoutMs) && helper.Drain(timeoutMs / 2);
var standardOutput = helper.StandardOutput;
if (!string.IsNullOrWhiteSpace(standardOutput))
{
Output.WriteLine($"StandardOutput:{Environment.NewLine}{standardOutput}");
}
var standardError = helper.ErrorOutput;
if (!string.IsNullOrWhiteSpace(standardError))
{
Output.WriteLine($"StandardError:{Environment.NewLine}{standardError}");
}
if (!ranToCompletion && !process.HasExited)
{
var tookMemoryDump = MemoryDumpHelper.CaptureMemoryDump(process);
process.Kill();
throw new Exception($"The sample did not exit in {timeoutMs}ms. Memory dump taken: {tookMemoryDump}. Killing process.");
}
var exitCode = process.ExitCode;
Output.WriteLine($"ProcessId: " + process.Id);
Output.WriteLine($"Exit Code: " + exitCode);
ErrorHelpers.CheckForKnownSkipConditions(Output, exitCode, standardError, EnvironmentHelper);
ExitCodeException.ThrowIfNonExpected(exitCode, expectedExitCode);
return new ProcessResult(process, standardOutput, standardError, exitCode);
}
public async Task<(Process Process, string ConfigFile)> StartIISExpress(MockTracerAgent agent, int iisPort, IisAppType appType, string subAppPath)
{
var iisExpress = EnvironmentHelper.GetIisExpressPath();
var appPool = appType switch
{
IisAppType.AspNetClassic => "Clr4ClassicAppPool",
IisAppType.AspNetIntegrated => "Clr4IntegratedAppPool",
IisAppType.AspNetCoreInProcess => "UnmanagedClassicAppPool",
IisAppType.AspNetCoreOutOfProcess => "UnmanagedClassicAppPool",
_ => throw new InvalidOperationException($"Unknown {nameof(IisAppType)} '{appType}'"),
};
var appPath = appType switch
{
IisAppType.AspNetClassic => EnvironmentHelper.GetSampleProjectDirectory(),
IisAppType.AspNetIntegrated => EnvironmentHelper.GetSampleProjectDirectory(),
IisAppType.AspNetCoreInProcess => EnvironmentHelper.GetSampleApplicationOutputDirectory(),
IisAppType.AspNetCoreOutOfProcess => EnvironmentHelper.GetSampleApplicationOutputDirectory(),
_ => throw new InvalidOperationException($"Unknown {nameof(IisAppType)} '{appType}'"),
};
var configTemplate = File.ReadAllText("applicationHost.config");
var newConfig = Path.GetTempFileName();
var virtualAppSection = subAppPath switch
{
null or "" or "/" => string.Empty,
_ when !subAppPath.StartsWith("/") => throw new ArgumentException("Application path must start with '/'", nameof(subAppPath)),
_ when subAppPath.EndsWith("/") => throw new ArgumentException("Application path must not end with '/'", nameof(subAppPath)),
_ => $"<application path=\"{subAppPath}\" applicationPool=\"{appPool}\"><virtualDirectory path=\"/\" physicalPath=\"{appPath}\" /></application>",
};
configTemplate = configTemplate
.Replace("[PATH]", appPath)
.Replace("[PORT]", iisPort.ToString())
.Replace("[POOL]", appPool)
.Replace("[VIRTUAL_APPLICATION]", virtualAppSection);
var isAspNetCore = appType == IisAppType.AspNetCoreInProcess || appType == IisAppType.AspNetCoreOutOfProcess;
if (isAspNetCore)
{
var hostingModel = appType == IisAppType.AspNetCoreInProcess ? "inprocess" : "outofprocess";
configTemplate = configTemplate
.Replace("[DOTNET]", EnvironmentHelper.GetDotnetExe())
.Replace("[RELATIVE_SAMPLE_PATH]", $".\\{EnvironmentHelper.GetSampleApplicationFileName()}")
.Replace("[HOSTING_MODEL]", hostingModel);
}
File.WriteAllText(newConfig, configTemplate);
var args = new[]
{
"/site:sample",
$"/config:{newConfig}",
"/systray:false",
"/trace:info"
};
Output.WriteLine($"[webserver] starting {iisExpress} {string.Join(" ", args)}");
var process = await ProfilerHelper.StartProcessWithProfiler(
iisExpress,
EnvironmentHelper,
agent,
arguments: string.Join(" ", args),
redirectStandardInput: true,
processToProfile: appType == IisAppType.AspNetCoreOutOfProcess ? "dotnet.exe" : iisExpress);
var semaphore = new SemaphoreSlim(0, 1);
_ = Task.Factory.StartNew(
() =>
{
while (process.StandardOutput.ReadLine() is { } line)
{
Output.WriteLine($"[webserver][stdout] {line}");
if (line.Contains("IIS Express is running"))
{
semaphore.Release();
}
}
},
TaskCreationOptions.LongRunning);
_ = Task.Factory.StartNew(
() =>
{
while (process.StandardError.ReadLine() is { } line)
{
Output.WriteLine($"[webserver][stderr] {line}");
}
},
TaskCreationOptions.LongRunning);
await semaphore.WaitAsync(TimeSpan.FromSeconds(10));
// Wait for iis express to finish starting up
var retries = 5;
while (true)
{
var usedPorts = IPGlobalProperties.GetIPGlobalProperties()
.GetActiveTcpListeners()
.Select(ipEndPoint => ipEndPoint.Port);
if (usedPorts.Contains(iisPort))
{
break;
}
retries--;
if (retries == 0)
{
throw new Exception("Gave up waiting for IIS Express.");
}
await Task.Delay(1500);
}
return (process, newConfig);
}
public void EnableIast(bool enable = true)
{
SetEnvironmentVariable(ConfigurationKeys.Iast.Enabled, enable.ToString().ToLower());
}
public void EnableRasp(bool enable = true)
{
SetEnvironmentVariable(ConfigurationKeys.AppSec.RaspEnabled, enable.ToString().ToLower());
}
public void EnableEvidenceRedaction(bool? enable = null)
{
if (enable != null)
{
SetEnvironmentVariable(ConfigurationKeys.Iast.RedactionEnabled, enable.ToString().ToLower());
}
}
public void EnableIastTelemetry(int level)
{
SetEnvironmentVariable(ConfigurationKeys.Iast.TelemetryVerbosity, ((IastMetricsVerbosityLevel)level).ToString());
}
public void DisableObfuscationQueryString()
{
SetEnvironmentVariable(ConfigurationKeys.ObfuscationQueryStringRegex, string.Empty);
}
public void SetEnvironmentVariable(string key, string value)
{
EnvironmentHelper.CustomEnvironmentVariables[key] = value;
}
protected void ValidateSpans<T>(IEnumerable<MockSpan> spans, Func<MockSpan, T> mapper, IEnumerable<T> expected)
{
var spanLookup = new Dictionary<T, int>();
foreach (var span in spans)
{
var key = mapper(span);
if (spanLookup.ContainsKey(key))
{
spanLookup[key]++;
}
else
{
spanLookup[key] = 1;
}
}
var missing = new List<T>();
foreach (var e in expected)
{
var found = spanLookup.ContainsKey(e);
if (found)
{
if (--spanLookup[e] <= 0)
{
spanLookup.Remove(e);
}
}
else
{
missing.Add(e);
}
}
foreach (var e in missing)
{
Assert.True(false, $"no span found for `{e}`, remaining spans: `{string.Join(", ", spanLookup.Select(kvp => $"{kvp.Key}").ToArray())}`");
}
}
/// <summary>
/// NOTE: Only use this for local debugging, don't set permanently in tests
/// We have a dedicated run that tests with debug mode enabled, so want to make
/// sure that "normal" runs don't set this flag.
/// </summary>
[Obsolete("Setting this forces debug mode, whereas we want to automatically test in both modes")]
protected void EnableDebugMode()
{
EnvironmentHelper.DebugModeEnabled = true;
}
protected void SetServiceName(string serviceName)
{
SetEnvironmentVariable(ConfigurationKeys.ServiceName, serviceName);
}
protected void SetServiceVersion(string serviceVersion)
{
SetEnvironmentVariable(ConfigurationKeys.ServiceVersion, serviceVersion);
}
protected void SetSecurity(bool security)
{
SecurityEnabled = security;
SetEnvironmentVariable(Configuration.ConfigurationKeys.AppSec.Enabled, security ? "true" : "false");
}
protected void SetInstrumentationVerification()
{
bool verificationEnabled = ShouldUseInstrumentationVerification();
if (verificationEnabled)
{
SetEnvironmentVariable(ConfigurationKeys.LogDirectory, EnvironmentHelper.LogDirectory);
}
}
protected void VerifyInstrumentation(Process process)
{
if (!ShouldUseInstrumentationVerification())
{
return;
}
var logDirectory = EnvironmentHelper.LogDirectory;
InstrumentationVerification.VerifyInstrumentation(process, logDirectory);
}
protected bool ShouldUseInstrumentationVerification()
{
if (!EnvironmentTools.IsWindows())
{
// Instrumentation Verification is currently only supported only on Windows
return false;
}
// verify instrumentation adds a lot of time to tests so we only run it on azure and if it a scheduled build.
// Return 'true' to verify instrumentation on local machine.
// return true;
return EnvironmentHelper.IsRunningInAzureDevOps() && EnvironmentHelper.IsScheduledBuild();
}
protected void EnableDirectLogSubmission(int intakePort, string integrationName, string host = "integration_tests")
{
SetEnvironmentVariable(ConfigurationKeys.DirectLogSubmission.Host, host);
SetEnvironmentVariable(ConfigurationKeys.DirectLogSubmission.Url, $"http://127.0.0.1:{intakePort}");
SetEnvironmentVariable(ConfigurationKeys.DirectLogSubmission.EnabledIntegrations, integrationName);
SetEnvironmentVariable(ConfigurationKeys.ApiKey, "DUMMY_KEY_REQUIRED_FOR_DIRECT_SUBMISSION");
}
protected async Task<IImmutableList<MockSpan>> GetWebServerSpans(
string path,
MockTracerAgent agent,
int httpPort,
HttpStatusCode expectedHttpStatusCode,
int expectedSpanCount = 2,
bool filterServerSpans = true)
{
using var httpClient = new HttpClient();
// disable tracing for this HttpClient request
httpClient.DefaultRequestHeaders.Add(HttpHeaderNames.TracingEnabled, "false");
httpClient.DefaultRequestHeaders.Add(HttpHeaderNames.UserAgent, "testhelper");
var testStart = DateTimeOffset.UtcNow;
var response = await httpClient.GetAsync($"http://localhost:{httpPort}" + path);
var content = await response.Content.ReadAsStringAsync();
Output.WriteLine($"[http] {response.StatusCode} {content}");
Assert.Equal(expectedHttpStatusCode, response.StatusCode);
if (filterServerSpans)
{
agent.SpanFilters.Add(IsServerSpan);
}
return agent.WaitForSpans(
count: expectedSpanCount,
minDateTime: testStart,
returnAllOperations: true);
}
protected async Task AssertWebServerSpan(
string path,
MockTracerAgent agent,
int httpPort,
HttpStatusCode expectedHttpStatusCode,
bool isError,
string expectedAspNetErrorType,
string expectedAspNetErrorMessage,
string expectedErrorType,
string expectedErrorMessage,
string expectedSpanType,
string expectedOperationName,
string expectedAspNetResourceName,
string expectedResourceName,
string expectedServiceVersion,
SerializableDictionary expectedTags = null)
{
IImmutableList<MockSpan> spans;
using (var httpClient = new HttpClient())
{
// disable tracing for this HttpClient request
httpClient.DefaultRequestHeaders.Add(HttpHeaderNames.TracingEnabled, "false");
var testStart = DateTimeOffset.UtcNow;
var response = await httpClient.GetAsync($"http://localhost:{httpPort}" + path);
var content = await response.Content.ReadAsStringAsync();
Output.WriteLine($"[http] {response.StatusCode} {content}");
Assert.Equal(expectedHttpStatusCode, response.StatusCode);
agent.SpanFilters.Add(IsServerSpan);
spans = agent.WaitForSpans(
count: 2,
minDateTime: testStart,
returnAllOperations: true);
Assert.True(spans.Count == 2, $"expected two span, saw {spans.Count}");
}
var aspnetSpan = spans.FirstOrDefault(s => s.Name == "aspnet.request");
var innerSpan = spans.FirstOrDefault(s => s.Name == expectedOperationName);
Assert.NotNull(aspnetSpan);
Assert.Equal(expectedAspNetResourceName, aspnetSpan.Resource);
Assert.NotNull(innerSpan);
Assert.Equal(expectedResourceName, innerSpan.Resource);
foreach (var span in spans)
{
// base properties
Assert.Equal(expectedSpanType, span.Type);
// errors
Assert.Equal(isError, span.Error == 1);
if (span == aspnetSpan)
{
Assert.Equal(expectedAspNetErrorType, span.Tags.GetValueOrDefault(Tags.ErrorType));
Assert.Equal(expectedAspNetErrorMessage, span.Tags.GetValueOrDefault(Tags.ErrorMsg));
}
else if (span == innerSpan)
{
Assert.Equal(expectedErrorType, span.Tags.GetValueOrDefault(Tags.ErrorType));
Assert.Equal(expectedErrorMessage, span.Tags.GetValueOrDefault(Tags.ErrorMsg));
}
// other tags
Assert.Equal(SpanKinds.Server, span.Tags.GetValueOrDefault(Tags.SpanKind));
Assert.Equal(expectedServiceVersion, span.Tags.GetValueOrDefault(Tags.Version));
}
if (expectedTags?.Values is not null)
{
foreach (var expectedTag in expectedTags)
{
Assert.Equal(expectedTag.Value, innerSpan.Tags.GetValueOrDefault(expectedTag.Key));
}
}
}
protected async Task AssertAspNetSpanOnly(
string path,
MockTracerAgent agent,
int httpPort,
HttpStatusCode expectedHttpStatusCode,
bool isError,
string expectedErrorType,
string expectedErrorMessage,
string expectedSpanType,
string expectedResourceName,
string expectedServiceVersion)
{
IImmutableList<MockSpan> spans;
using (var httpClient = new HttpClient())
{
// disable tracing for this HttpClient request
httpClient.DefaultRequestHeaders.Add(HttpHeaderNames.TracingEnabled, "false");
var testStart = DateTimeOffset.UtcNow;
var response = await httpClient.GetAsync($"http://localhost:{httpPort}" + path);
var content = await response.Content.ReadAsStringAsync();
Output.WriteLine($"[http] {response.StatusCode} {content}");
Assert.Equal(expectedHttpStatusCode, response.StatusCode);
spans = agent.WaitForSpans(
count: 1,
minDateTime: testStart,
operationName: "aspnet.request",
returnAllOperations: true);
Assert.True(spans.Count == 1, $"expected two span, saw {spans.Count}");
}
var span = spans[0];
// base properties
Assert.Equal(expectedResourceName, span.Resource);
Assert.Equal(expectedSpanType, span.Type);
// errors
Assert.Equal(isError, span.Error == 1);
Assert.Equal(expectedErrorType, span.Tags.GetValueOrDefault(Tags.ErrorType));
Assert.Equal(expectedErrorMessage, span.Tags.GetValueOrDefault(Tags.ErrorMsg));
// other tags
Assert.Equal(SpanKinds.Server, span.Tags.GetValueOrDefault(Tags.SpanKind));
Assert.Equal(expectedServiceVersion, span.Tags.GetValueOrDefault(Tags.Version));
}
protected async Task ReportRetry(ITestOutputHelper outputHelper, int attemptsRemaining, Exception ex = null)
{
outputHelper.WriteLine($"Error executing test. {attemptsRemaining} attempts remaining. {ex}");
await ErrorHelpers.SendMetric(outputHelper, "dd_trace_dotnet.ci.tests.retries", EnvironmentHelper);
}
private bool IsServerSpan(MockSpan span) =>
span.Tags.GetValueOrDefault(Tags.SpanKind) == SpanKinds.Server;
}
}