forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetryFilterTest.cs
More file actions
211 lines (192 loc) · 11.5 KB
/
TelemetryFilterTest.cs
File metadata and controls
211 lines (192 loc) · 11.5 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
// 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.Telemetry;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Utilities;
using Parser = Microsoft.DotNet.Cli.Parser;
namespace Microsoft.DotNet.Tests
{
/// <summary>
/// Only adding the performance data tests for now as the TelemetryCommandTests cover most other scenarios already
/// </summary>
public class TelemetryFilterTests : SdkTest
{
private readonly FakeRecordEventNameTelemetry _fakeTelemetry;
public string EventName { get; set; }
public IDictionary<string, string> Properties { get; set; }
public TelemetryFilterTests(ITestOutputHelper log) : base(log)
{
_fakeTelemetry = new FakeRecordEventNameTelemetry();
TelemetryEventEntry.Subscribe(_fakeTelemetry.TrackEvent);
TelemetryEventEntry.TelemetryFilter = new TelemetryFilter(Sha256Hasher.HashWithNormalizedCasing);
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithoutPerformanceData()
{
var parseResult = Parser.Parse(["build"]);
TelemetryEventEntry.SendFiltered(parseResult);
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD") &&
e.Measurement == null);
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithPerformanceData()
{
var parseResult = Parser.Parse(["build"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>() { { "Startup Time", 12345 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD") &&
e.Measurement.ContainsKey("Startup Time") &&
e.Measurement["Startup Time"] == 12345);
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithGlobalJsonState()
{
string globalJsonState = "invalid_data";
var parseResult = Parser.Parse(["build"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>(), globalJsonState));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD") &&
e.Measurement == null &&
e.Properties.ContainsKey("globalJson") &&
e.Properties["globalJson"] == Sha256Hasher.HashWithNormalizedCasing(globalJsonState));
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithZeroPerformanceData()
{
var parseResult = Parser.Parse(["build"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>() { { "Startup Time", 0 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD") &&
e.Measurement == null);
}
[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithSomeZeroPerformanceData()
{
var parseResult = Parser.Parse(["build"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>() { { "Startup Time", 0 }, { "Parse Time", 23456 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD") &&
!e.Measurement.ContainsKey("Startup Time") &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] == 23456);
}
[Fact]
public void SubLevelCommandNameShouldBeSentToTelemetryWithoutPerformanceData()
{
var parseResult = Parser.Parse(["new", "console"]);
TelemetryEventEntry.SendFiltered(parseResult);
_fakeTelemetry
.LogEntries.Should()
.Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash("CONSOLE") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW") &&
e.Measurement == null);
}
[Fact]
public void SubLevelCommandNameShouldBeSentToTelemetryWithPerformanceData()
{
var parseResult = Parser.Parse(["new", "console"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>() { { "Startup Time", 34567 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash("CONSOLE") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW") &&
e.Measurement.ContainsKey("Startup Time") &&
e.Measurement["Startup Time"] == 34567);
}
[Fact]
public void SubLevelCommandNameShouldBeSentToTelemetryWithZeroPerformanceData()
{
var parseResult = Parser.Parse(["new", "console"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>() { { "Startup Time", 0 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash("CONSOLE") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW") &&
e.Measurement == null);
}
[Fact]
public void SubLevelCommandNameShouldBeSentToTelemetryWithSomeZeroPerformanceData()
{
var parseResult = Parser.Parse(["new", "console"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>() { { "Startup Time", 0 }, { "Parse Time", 45678 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("argument") &&
e.Properties["argument"] == Sha256Hasher.Hash("CONSOLE") &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("NEW") &&
!e.Measurement.ContainsKey("Startup Time") &&
e.Measurement.ContainsKey("Parse Time") &&
e.Measurement["Parse Time"] == 45678);
}
[Fact]
public void WorkloadSubLevelCommandNameAndArgumentShouldBeSentToTelemetry()
{
var parseResult =
Parser.Parse(["workload", "install", "microsoft-ios-sdk-full"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult,
new Dictionary<string, double>() { { "Startup Time", 0 }, { "Parse Time", 23456 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("WORKLOAD") &&
e.Properties["subcommand"] ==
Sha256Hasher.Hash("INSTALL") &&
e.Properties["argument"] ==
Sha256Hasher.Hash("MICROSOFT-IOS-SDK-FULL"));
}
[Fact]
public void ToolsSubLevelCommandNameAndArgumentShouldBeSentToTelemetry()
{
var parseResult =
Parser.Parse(["tool", "install", "dotnet-format"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult,
new Dictionary<string, double>() { { "Startup Time", 0 }, { "Parse Time", 23456 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("TOOL") &&
e.Properties["subcommand"] ==
Sha256Hasher.Hash("INSTALL") &&
e.Properties["argument"] ==
Sha256Hasher.Hash("DOTNET-FORMAT"));
}
[Fact]
public void WhenCalledWithDiagnosticWorkloadSubLevelCommandNameAndArgumentShouldBeSentToTelemetry()
{
var parseResult =
Parser.Parse(["-d", "workload", "install", "microsoft-ios-sdk-full"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult,
new Dictionary<string, double>() { { "Startup Time", 0 }, { "Parse Time", 23456 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("WORKLOAD") &&
e.Properties["subcommand"] ==
Sha256Hasher.Hash("INSTALL") &&
e.Properties["argument"] ==
Sha256Hasher.Hash("MICROSOFT-IOS-SDK-FULL"));
}
[Fact]
public void WhenCalledWithMissingArgumentWorkloadSubLevelCommandNameAndArgumentShouldBeSentToTelemetry()
{
var parseResult =
Parser.Parse(["-d", "workload", "install"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult,
new Dictionary<string, double>() { { "Startup Time", 0 }, { "Parse Time", 23456 } }));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "sublevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("WORKLOAD") &&
e.Properties["subcommand"] ==
Sha256Hasher.Hash("INSTALL"));
}
}
}