-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDotNetCliTests.cs
112 lines (84 loc) · 3.34 KB
/
DotNetCliTests.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using FluentAssertions;
using IntegrationTests.Helpers;
using Xunit.Abstractions;
namespace IntegrationTests;
[Trait("Category", "EndToEnd")]
public sealed class DotNetCliTests : TestHelper, IDisposable
{
private const string DotNetCli = "dotnet";
private const string TargetAppName = "OTelDotNetCliTest";
private readonly string _prevWorkingDir = Directory.GetCurrentDirectory();
private readonly DirectoryInfo _tempWorkingDir;
public DotNetCliTests(ITestOutputHelper output)
: base(DotNetCli, output)
{
var tempDirName = Path.Combine(
Path.GetTempPath(),
$"otel-dotnet-test-{Guid.NewGuid():N}",
TargetAppName);
_tempWorkingDir = Directory.CreateDirectory(tempDirName);
Directory.SetCurrentDirectory(_tempWorkingDir.FullName);
}
public void Dispose()
{
Directory.SetCurrentDirectory(_prevWorkingDir);
_tempWorkingDir.Delete(recursive: true);
}
[Fact]
public void WorkFlow()
{
// Ensure no MS telemetry spans.
SetEnvironmentVariable("DOTNET_CLI_TELEMETRY_OPTOUT", "1");
// Stop all build servers to ensure user like experience.
// Currently there is an issue trying to launch VBCSCompiler background server.
RunDotNetCli("build-server shutdown");
var tfm = $"net{Environment.Version.Major}.0";
RunDotNetCli($"new console --framework {tfm}");
ChangeDefaultProgramToHttpClient();
RunDotNetCli("build");
var targetAppDllPath = Path.Combine(".", "bin", "Debug", tfm, TargetAppName + ".dll");
RunAppWithDotNetCliAndAssertHttpSpans(targetAppDllPath);
// Not necessary, but, testing a common command.
RunDotNetCli("clean");
RunAppWithDotNetCliAndAssertHttpSpans("run -c Release");
}
private static void ChangeDefaultProgramToHttpClient()
{
const string ProgramContent = @"
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(5);
using var response = await httpClient.GetAsync(""http://example.com"");
Console.WriteLine(response.StatusCode);
";
File.WriteAllText("Program.cs", ProgramContent);
}
private void RunDotNetCli(string arguments)
{
Output.WriteLine($"Running: {DotNetCli} {arguments}");
using var process = InstrumentedProcessHelper.Start(DotNetCli, arguments, EnvironmentHelper);
using var helper = new ProcessHelper(process);
process.Should().NotBeNull();
var processTimeout = !process!.WaitForExit((int)TestTimeout.ProcessExit.TotalMilliseconds);
if (processTimeout)
{
process.Kill();
}
Output.WriteLine("ProcessId: " + process.Id);
Output.WriteLine("Exit Code: " + process.ExitCode);
Output.WriteResult(helper);
processTimeout.Should().BeFalse("Test application timed out");
process.ExitCode.Should().Be(0, "Test application exited with non-zero exit code");
}
private void RunAppWithDotNetCliAndAssertHttpSpans(string arguments)
{
var collector = new MockSpansCollector(Output);
SetExporter(collector);
collector.Expect("System.Net.Http");
RunDotNetCli(arguments);
collector.AssertExpectations();
}
}
#endif