Skip to content

Commit

Permalink
Update OpenTelemetry.Instrumentation.W3cTraceContext.Tests to use fil… (
Browse files Browse the repository at this point in the history
  • Loading branch information
jarosal authored Jul 28, 2023
1 parent 02a40fc commit ce1e196
Showing 1 changed file with 103 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,139 +25,138 @@
using Xunit;
using Xunit.Abstractions;

namespace OpenTelemetry.Instrumentation.W3cTraceContext.Tests
namespace OpenTelemetry.Instrumentation.W3cTraceContext.Tests;

public class W3CTraceContextTests : IDisposable
{
public class W3CTraceContextTests : IDisposable
/*
To run the tests, invoke docker-compose.yml from the root of the repo:
opentelemetry>docker-compose --file=test/OpenTelemetry.Instrumentation.W3cTraceContext.Tests/docker-compose.yml --project-directory=. up --exit-code-from=tests --build
*/
private const string W3cTraceContextEnvVarName = "OTEL_W3CTRACECONTEXT";
private static readonly Version AspNetCoreHostingVersion = typeof(Microsoft.AspNetCore.Hosting.Builder.IApplicationBuilderFactory).Assembly.GetName().Version;
private readonly HttpClient httpClient = new HttpClient();
private readonly ITestOutputHelper output;

public W3CTraceContextTests(ITestOutputHelper output)
{
/*
To run the tests, invoke docker-compose.yml from the root of the repo:
opentelemetry>docker-compose --file=test/OpenTelemetry.Instrumentation.W3cTraceContext.Tests/docker-compose.yml --project-directory=. up --exit-code-from=tests --build
*/
private const string W3cTraceContextEnvVarName = "OTEL_W3CTRACECONTEXT";
private static readonly Version AspNetCoreHostingVersion = typeof(Microsoft.AspNetCore.Hosting.Builder.IApplicationBuilderFactory).Assembly.GetName().Version;
private readonly HttpClient httpClient = new HttpClient();
private readonly ITestOutputHelper output;

public W3CTraceContextTests(ITestOutputHelper output)
{
this.output = output;
}
this.output = output;
}

[Trait("CategoryName", "W3CTraceContextTests")]
[SkipUnlessEnvVarFoundTheory(W3cTraceContextEnvVarName)]
[InlineData("placeholder")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "xUnit1026:Theory methods should use all of their parameters", Justification = "Need to use SkipUnlessEnvVarFoundTheory")]
public void W3CTraceContextTestSuiteAsync(string value)
{
// configure SDK
using var tracerprovider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.Build();
[Trait("CategoryName", "W3CTraceContextTests")]
[SkipUnlessEnvVarFoundTheory(W3cTraceContextEnvVarName)]
[InlineData("placeholder")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "xUnit1026:Theory methods should use all of their parameters", Justification = "Need to use SkipUnlessEnvVarFoundTheory")]
public void W3CTraceContextTestSuiteAsync(string value)
{
// configure SDK
using var tracerprovider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.Build();

var builder = WebApplication.CreateBuilder();
using var app = builder.Build();
var builder = WebApplication.CreateBuilder();
using var app = builder.Build();

// disabling due to failing dotnet-format
// TODO: investigate why dotnet-format fails.
// disabling due to failing dotnet-format
// TODO: investigate why dotnet-format fails.
#pragma warning disable SA1008 // Opening parenthesis should be spaced correctly
app.MapPost("/", async([FromBody] Data[] data) =>
app.MapPost("/", async([FromBody] Data[] data) =>
{
var result = string.Empty;
if (data != null)
{
var result = string.Empty;
if (data != null)
foreach (var argument in data)
{
foreach (var argument in data)
using var request = new HttpRequestMessage(HttpMethod.Post, argument.Url)
{
using var request = new HttpRequestMessage(HttpMethod.Post, argument.Url)
{
Content = new StringContent(
JsonSerializer.Serialize(argument.Arguments),
Encoding.UTF8,
"application/json"),
};
await this.httpClient.SendAsync(request).ConfigureAwait(false);
}
}
else
{
result = "done";
Content = new StringContent(
JsonSerializer.Serialize(argument.Arguments),
Encoding.UTF8,
"application/json"),
};
await this.httpClient.SendAsync(request).ConfigureAwait(false);
}
}
else
{
result = "done";
}

return result;
});
return result;
});
#pragma warning restore SA1008 // Opening parenthesis should be spaced correctly

app.RunAsync();
app.RunAsync();

string result = RunCommand("python", "trace-context/test/test.py http://localhost:5000/");
string result = RunCommand("python", "trace-context/test/test.py http://localhost:5000/");

// Assert
string lastLine = ParseLastLine(result);
// Assert
string lastLine = ParseLastLine(result);

this.output.WriteLine("result:" + result);
this.output.WriteLine("result:" + result);

// Assert on the last line
// Assert on the last line

// TODO: Investigate failures on .NET6 vs .NET7. To see the details
// run the tests with console logger (done automatically by the CI
// jobs).
// TODO: Investigate failures on .NET6 vs .NET7. To see the details
// run the tests with console logger (done automatically by the CI
// jobs).

if (AspNetCoreHostingVersion.Major <= 6)
{
Assert.StartsWith("FAILED (failures=5)", lastLine);
}
else
{
Assert.StartsWith("FAILED (failures=3)", lastLine);
}
}

public void Dispose()
if (AspNetCoreHostingVersion.Major <= 6)
{
this.httpClient.Dispose();
Assert.StartsWith("FAILED (failures=5)", lastLine);
}

private static string RunCommand(string command, string args)
else
{
using var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = $" {args}",
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = ".",
},
};
proc.Start();

// TODO: after W3C Trace Context test suite passes, it might go in standard output
var results = proc.StandardError.ReadToEnd();
proc.WaitForExit();
return results;
Assert.StartsWith("FAILED (failures=3)", lastLine);
}
}

private static string ParseLastLine(string output)
public void Dispose()
{
this.httpClient.Dispose();
}

private static string RunCommand(string command, string args)
{
using var proc = new Process
{
if (output.Length <= 1)
StartInfo = new ProcessStartInfo
{
return output;
}
FileName = command,
Arguments = $" {args}",
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = ".",
},
};
proc.Start();

// TODO: after W3C Trace Context test suite passes, it might go in standard output
var results = proc.StandardError.ReadToEnd();
proc.WaitForExit();
return results;
}

// The output ends with '\n', which should be ignored.
var lastNewLineCharacterPos = output.LastIndexOf('\n', output.Length - 2);
return output.Substring(lastNewLineCharacterPos + 1);
private static string ParseLastLine(string output)
{
if (output.Length <= 1)
{
return output;
}

public class Data
{
[JsonPropertyName("url")]
public string Url { get; set; }
// The output ends with '\n', which should be ignored.
var lastNewLineCharacterPos = output.LastIndexOf('\n', output.Length - 2);
return output.Substring(lastNewLineCharacterPos + 1);
}

[JsonPropertyName("arguments")]
public Data[] Arguments { get; set; }
}
public class Data
{
[JsonPropertyName("url")]
public string Url { get; set; }

[JsonPropertyName("arguments")]
public Data[] Arguments { get; set; }
}
}

0 comments on commit ce1e196

Please sign in to comment.