forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetryCommandTest.cs
More file actions
416 lines (374 loc) · 19.7 KB
/
TelemetryCommandTest.cs
File metadata and controls
416 lines (374 loc) · 19.7 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using Microsoft.DotNet.Cli.Commands.Hidden.InternalReportInstallSuccess;
using Microsoft.DotNet.Cli.Telemetry;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Utilities;
namespace Microsoft.DotNet.Tests
{
[Collection(TestConstants.UsesStaticTelemetryState)]
public class TelemetryCommandTests : SdkTest
{
private readonly FakeRecordEventNameTelemetry _fakeTelemetry;
public string EventName { get; set; }
public IDictionary<string, string> Properties { get; set; }
public TelemetryCommandTests(ITestOutputHelper log) : base(log)
{
_fakeTelemetry = new FakeRecordEventNameTelemetry();
TelemetryEventEntry.Subscribe(_fakeTelemetry.TrackEvent);
TelemetryEventEntry.TelemetryFilter = new TelemetryFilter(Sha256Hasher.HashWithNormalizedCasing);
}
[Fact]
public void NoTelemetryIfCommandIsInvalid()
{
string[] args = { "publish", "-r" };
Action a = () => { Cli.Program.ProcessArgs(args); };
a.Should().NotThrow<ArgumentOutOfRangeException>();
}
[Fact]
public void NoTelemetryIfCommandIsInvalid2()
{
string[] args = { "restore", "-v" };
Action a = () => { Cli.Program.ProcessArgs(args); };
a.Should().NotThrow<ArgumentOutOfRangeException>();
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetry()
{
string[] args = { "help" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("HELP"));
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithPerformanceData()
{
string[] args = { "help" };
Cli.Program.ProcessArgs(args, new TimeSpan(12345));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("HELP") &&
e.Measurement.ContainsKey("Startup Time") &&
e.Measurement["Startup Time"] == 1.2345 &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] > 0);
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithoutStartupTime()
{
string[] args = { "help" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("HELP") &&
!e.Measurement.ContainsKey("Startup Time") &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] > 0);
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryZeroStartupTime()
{
string[] args = { "help" };
Cli.Program.ProcessArgs(args, new TimeSpan(0));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("HELP") &&
!e.Measurement.ContainsKey("Startup Time") &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] > 0);
}
[Fact]
public void DotnetNewCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "console";
string[] args = { "new", argumentToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW"));
}
[Fact(Skip = "https://github.com/dotnet/sdk/issues/24190")]
public void DotnetNewCommandFirstArgumentShouldBeSentToTelemetryWithPerformanceData()
{
const string argumentToSend = "console";
string[] args = { "new", argumentToSend };
Cli.Program.ProcessArgs(args, new TimeSpan(23456));
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW") &&
e.Measurement.ContainsKey("Startup Time") &&
e.Measurement["Startup Time"] == 2.3456 &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] > 0);
}
[Fact]
public void DotnetHelpCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "something";
string[] args = { "help", argumentToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("HELP"));
}
[Fact]
public void DotnetAddCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "package";
string[] args = { "add", argumentToSend, "aPackageName" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("ADD"));
}
[Fact]
public void DotnetAddCommandFirstArgumentShouldBeSentToTelemetry2()
{
const string argumentToSend = "reference";
string[] args = { "add", argumentToSend, "aPackageName" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("ADD"));
}
[Fact]
public void DotnetRemoveCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "package";
string[] args = { "remove", argumentToSend, "aPackageName" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("REMOVE"));
}
[Fact]
public void DotnetListCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "reference";
string[] args = { "list", argumentToSend, "aPackageName" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" && e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("LIST"));
}
[Fact]
public void DotnetSlnCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "list";
string[] args = { "sln", "aSolution", argumentToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("SOLUTION"));
}
[Fact]
public void DotnetNugetCommandFirstArgumentShouldBeSentToTelemetry()
{
const string argumentToSend = "push";
string[] args = { "nuget", argumentToSend, "path" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash(argumentToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NUGET"));
}
[Fact(Skip = "https://github.com/dotnet/sdk/issues/47862")]
public void DotnetNewCommandLanguageOpinionShouldBeSentToTelemetry()
{
const string optionKey = "language";
const string optionValueToSend = "c#";
string[] args = { "new", "console", "--" + optionKey, optionValueToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" && e.Properties.ContainsKey(optionKey) &&
e.Properties[optionKey] == Sha256Hasher.Hash(optionValueToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW"));
}
[Fact]
public void AnyDotnetCommandVerbosityOpinionShouldBeSentToTelemetry()
{
const string optionKey = "verbosity";
const string optionValueToSend = "minimal";
string[] args = { "restore", "--" + optionKey, optionValueToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey(optionKey) &&
e.Properties[optionKey] == Sha256Hasher.Hash(optionValueToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("RESTORE"));
}
[Fact]
public void AnyDotnetCommandVerbosityOpinionShouldBeSentToTelemetryWithPerformanceData()
{
const string optionKey = "verbosity";
const string optionValueToSend = "minimal";
string[] args = { "restore", "--" + optionKey, optionValueToSend };
Cli.Program.ProcessArgs(args, new TimeSpan(34567));
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey(optionKey) &&
e.Properties[optionKey] == Sha256Hasher.Hash(optionValueToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("RESTORE") &&
e.Measurement.ContainsKey("Startup Time") &&
e.Measurement["Startup Time"] == 3.4567 &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] > 0);
}
[Fact]
public void DotnetBuildAndPublishCommandOpinionsShouldBeSentToTelemetry()
{
const string optionKey = "configuration";
const string optionValueToSend = "Debug";
string[] args = { "build", "--" + optionKey, optionValueToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey(optionKey) &&
e.Properties[optionKey] == Sha256Hasher.Hash(optionValueToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD"));
}
[Fact]
public void DotnetPublishCommandRuntimeOpinionsShouldBeSentToTelemetry()
{
const string optionKey = "runtime";
const string optionValueToSend = $"{ToolsetInfo.LatestWinRuntimeIdentifier}-x64";
string[] args = { "publish", "--" + optionKey, optionValueToSend };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey(optionKey) &&
e.Properties[optionKey] == Sha256Hasher.Hash(optionValueToSend.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("PUBLISH"));
}
[Fact]
public void DotnetBuildAndPublishCommandOpinionsShouldBeSentToTelemetryWhenThereIsMultipleOption()
{
string[] args = { "build", "--configuration", "Debug", "--runtime", $"{ToolsetInfo.LatestMacRuntimeIdentifier}-x64" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" && e.Properties.ContainsKey("configuration") &&
e.Properties["configuration"] == Sha256Hasher.Hash("DEBUG") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD"));
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" && e.Properties.ContainsKey("runtime") &&
e.Properties["runtime"] == Sha256Hasher.Hash($"{ToolsetInfo.LatestMacRuntimeIdentifier.ToUpper()}-X64") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD"));
}
[Fact]
public void DotnetRunCleanTestCommandOpinionsShouldBeSentToTelemetryWhenThereIsMultipleOption()
{
string[] args = { "clean", "--configuration", "Debug", "--framework", ToolsetInfo.CurrentTargetFramework };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" && e.Properties.ContainsKey("configuration") &&
e.Properties["configuration"] == Sha256Hasher.Hash("DEBUG") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("CLEAN"));
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" && e.Properties.ContainsKey("framework") &&
e.Properties["framework"] == Sha256Hasher.Hash(ToolsetInfo.CurrentTargetFramework.ToUpper()) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("CLEAN"));
}
[Fact]
public void DotnetUpdatePackageVulnerableOptionShouldBeSentToTelemetry()
{
const string optionKey = "vulnerable";
string[] args = { "package", "update", "--vulnerable" };
Cli.Program.ProcessArgs(args);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey(optionKey) &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("PACKAGE UPDATE"));
}
[WindowsOnlyFact]
public void InternalreportinstallsuccessCommandCollectExeNameWithEventname()
{
FakeRecordEventNameTelemetry fakeTelemetry = new();
string[] args = { "c:\\mypath\\dotnet-sdk-latest-win-x64.exe" };
InternalReportInstallSuccessCommand.ProcessInputAndSendTelemetry(args, fakeTelemetry);
fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "install/reportsuccess" && e.Properties.ContainsKey("exeName") &&
e.Properties["exeName"] == Sha256Hasher.Hash("DOTNET-SDK-LATEST-WIN-X64.EXE"));
}
[Fact]
public void ExceptionShouldBeSentToTelemetry()
{
Exception caughtException = null;
try
{
string[] args = { "build" };
Cli.Program.ProcessArgs(args);
throw new ArgumentException("test exception");
}
catch (Exception ex)
{
caughtException = ex;
TelemetryEventEntry.SendFiltered(ex);
}
var exception = new Exception();
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "mainCatchException/exception" &&
e.Properties.ContainsKey("exceptionType") &&
e.Properties["exceptionType"] == "System.ArgumentException" &&
e.Properties.ContainsKey("detail") &&
e.Properties["detail"].Contains(caughtException.StackTrace));
}
}
}