-
Notifications
You must be signed in to change notification settings - Fork 323
/
InProcDataCollectionExtensionManager.cs
333 lines (296 loc) · 13.7 KB
/
InProcDataCollectionExtensionManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollector.InProcDataCollector;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.InProcDataCollector;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
/// <summary>
/// The in process data collection extension manager.
/// </summary>
internal class InProcDataCollectionExtensionManager
{
internal IDictionary<string, IInProcDataCollector> InProcDataCollectors;
private IDataCollectionSink inProcDataCollectionSink;
private const string DataCollectorEndsWithPattern = @"Collector.dll";
private string defaultCodeBase;
private List<string> codeBasePaths;
private IFileHelper fileHelper;
/// <summary>
/// Loaded in-proc datacollectors collection
/// </summary>
private IEnumerable<DataCollectorSettings> inProcDataCollectorSettingsCollection;
/// <summary>
/// Initializes a new instance of the <see cref="InProcDataCollectionExtensionManager"/> class.
/// </summary>
/// <param name="runSettings">
/// The run settings.
/// </param>
/// <param name="testEventsPublisher">
/// The data collection test case event manager.
/// </param>
/// <param name="defaultCodeBase">
/// The default codebase to be used by inproc data collector
/// </param>
public InProcDataCollectionExtensionManager(string runSettings, ITestEventsPublisher testEventsPublisher, string defaultCodeBase, TestPluginCache testPluginCache)
: this(runSettings, testEventsPublisher, defaultCodeBase, testPluginCache, new FileHelper())
{}
protected InProcDataCollectionExtensionManager(string runSettings, ITestEventsPublisher testEventsPublisher, string defaultCodeBase, TestPluginCache testPluginCache, IFileHelper fileHelper)
{
this.InProcDataCollectors = new Dictionary<string, IInProcDataCollector>();
this.inProcDataCollectionSink = new InProcDataCollectionSink();
this.defaultCodeBase = defaultCodeBase;
this.fileHelper = fileHelper;
this.codeBasePaths = new List<string> { this.defaultCodeBase };
// Get Datacollector codebase paths from test plugin cache
var extensionPaths = testPluginCache.GetExtensionPaths(DataCollectorEndsWithPattern);
foreach (var extensionPath in extensionPaths)
{
this.codeBasePaths.Add(Path.GetDirectoryName(extensionPath));
}
// Initialize InProcDataCollectors
this.InitializeInProcDataCollectors(runSettings);
if (this.IsInProcDataCollectionEnabled)
{
testEventsPublisher.TestCaseEnd += this.TriggerTestCaseEnd;
testEventsPublisher.TestCaseStart += this.TriggerTestCaseStart;
testEventsPublisher.TestResult += this.TriggerUpdateTestResult;
testEventsPublisher.SessionStart += this.TriggerTestSessionStart;
testEventsPublisher.SessionEnd += this.TriggerTestSessionEnd;
}
}
/// <summary>
/// Gets a value indicating whether is in proc data collection enabled.
/// </summary>
public bool IsInProcDataCollectionEnabled { get; private set; }
/// <summary>
/// Creates data collector instance based on datacollector settings provided.
/// </summary>
/// <param name="dataCollectorSettings">
/// Settings to be used for creating DataCollector.
/// </param>
/// <param name="interfaceTypeInfo">
/// TypeInfo of datacollector.
/// </param>
/// <returns>
/// The <see cref="IInProcDataCollector"/>.
/// </returns>
protected virtual IInProcDataCollector CreateDataCollector(string assemblyQualifiedName, string codebase, XmlElement configuration, TypeInfo interfaceTypeInfo)
{
var inProcDataCollector = new InProcDataCollector(
codebase,
assemblyQualifiedName,
interfaceTypeInfo,
configuration?.OuterXml);
inProcDataCollector.LoadDataCollector(this.inProcDataCollectionSink);
return inProcDataCollector;
}
/// <summary>
/// The trigger test session start.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void TriggerTestSessionStart(object sender, SessionStartEventArgs e)
{
TestSessionStartArgs testSessionStartArgs = new TestSessionStartArgs(this.GetSessionStartProperties(e));
this.TriggerInProcDataCollectionMethods(Constants.TestSessionStartMethodName, testSessionStartArgs);
}
/// <summary>
/// The trigger session end.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void TriggerTestSessionEnd(object sender, SessionEndEventArgs e)
{
var testSessionEndArgs = new TestSessionEndArgs();
this.TriggerInProcDataCollectionMethods(Constants.TestSessionEndMethodName, testSessionEndArgs);
}
/// <summary>
/// The trigger test case start.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void TriggerTestCaseStart(object sender, TestCaseStartEventArgs e)
{
var testCaseStartArgs = new TestCaseStartArgs(e.TestElement);
this.TriggerInProcDataCollectionMethods(Constants.TestCaseStartMethodName, testCaseStartArgs);
}
/// <summary>
/// The trigger test case end.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void TriggerTestCaseEnd(object sender, TestCaseEndEventArgs e)
{
var dataCollectionContext = new DataCollectionContext(e.TestElement);
var testCaseEndArgs = new TestCaseEndArgs(dataCollectionContext, e.TestOutcome);
this.TriggerInProcDataCollectionMethods(Constants.TestCaseEndMethodName, testCaseEndArgs);
}
/// <summary>
/// Triggers the send test result method
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void TriggerUpdateTestResult(object sender, TestResultEventArgs e)
{
// Just set the cached in-proc data if already exists
this.SetInProcDataCollectionDataInTestResult(e.TestResult);
}
/// <summary>
/// Loads all the inproc data collector dlls
/// </summary>
/// <param name="runSettings">
/// The run Settings.
/// </param>
private void InitializeInProcDataCollectors(string runSettings)
{
try
{
// Check if runsettings contains in-proc datacollector element
var inProcDataCollectionRunSettings = XmlRunSettingsUtilities.GetInProcDataCollectionRunSettings(runSettings);
var inProcDataCollectionSettingsPresentInRunSettings = inProcDataCollectionRunSettings?.IsCollectionEnabled ?? false;
// Verify if it has any valid in-proc datacollectors or just a dummy element
inProcDataCollectionSettingsPresentInRunSettings = inProcDataCollectionSettingsPresentInRunSettings &&
inProcDataCollectionRunSettings.DataCollectorSettingsList.Any();
// Initialize if we have atleast one
if (inProcDataCollectionSettingsPresentInRunSettings)
{
this.inProcDataCollectorSettingsCollection = inProcDataCollectionRunSettings.DataCollectorSettingsList;
var interfaceTypeInfo = typeof(InProcDataCollection).GetTypeInfo();
foreach (var inProcDc in this.inProcDataCollectorSettingsCollection)
{
var codeBase = this.GetCodebase(inProcDc.CodeBase);
var assemblyQualifiedName = inProcDc.AssemblyQualifiedName;
var configuration = inProcDc.Configuration;
var inProcDataCollector = this.CreateDataCollector(assemblyQualifiedName, codeBase, configuration, interfaceTypeInfo);
this.InProcDataCollectors[inProcDataCollector.AssemblyQualifiedName] = inProcDataCollector;
}
}
}
catch (Exception ex)
{
EqtTrace.Error("InProcDataCollectionExtensionManager: Error occured while Initializing the datacollectors : {0}", ex);
}
finally
{
this.IsInProcDataCollectionEnabled = this.InProcDataCollectors.Any();
}
}
/// <summary>
/// Gets codebase for inproc datacollector
/// Uses all codebasePaths to check where the datacollector exists
/// </summary>
/// <param name="codeBase">The codebase.</param>
/// <returns> Codebase </returns>
private string GetCodebase(string codeBase)
{
if (!Path.IsPathRooted(codeBase))
{
foreach (var extensionPath in this.codeBasePaths)
{
var assemblyPath = Path.Combine(extensionPath, codeBase);
if (this.fileHelper.Exists(assemblyPath))
{
return assemblyPath;
}
}
}
return codeBase;
}
private IDictionary<string, object> GetSessionStartProperties(SessionStartEventArgs sessionStartEventArgs)
{
var properties = new Dictionary<string, object>();
properties.Add(Constants.TestSourcesPropertyName, sessionStartEventArgs.GetPropertyValue<IEnumerable<string>>(Constants.TestSourcesPropertyName));
return properties;
}
private void TriggerInProcDataCollectionMethods(string methodName, InProcDataCollectionArgs methodArg)
{
try
{
foreach (var inProcDc in this.InProcDataCollectors.Values)
{
inProcDc.TriggerInProcDataCollectionMethod(methodName, methodArg);
}
}
catch (Exception ex)
{
EqtTrace.Error("InProcDataCollectionExtensionManager: Error occured while Triggering the {0} method : {1}", methodName, ex);
}
}
/// <summary>
/// Set the data sent via datacollection sink in the testresult property for upstream applications to read.
/// And removes the data from the dictionary.
/// </summary>
/// <param name="testResult">
/// The test Result.
/// </param>
private void SetInProcDataCollectionDataInTestResult(TestResult testResult)
{
// Loops through each datacollector reads the data collection data and sets as TestResult property.
foreach (var entry in this.InProcDataCollectors)
{
var dataCollectionData = ((InProcDataCollectionSink)this.inProcDataCollectionSink).GetDataCollectionDataSetForTestCase(testResult.TestCase.Id);
foreach (var keyValuePair in dataCollectionData)
{
var testProperty = TestProperty.Register(id: keyValuePair.Key, label: keyValuePair.Key, category: string.Empty, description: string.Empty, valueType: typeof(string), validateValueCallback: null, attributes: TestPropertyAttributes.None, owner: typeof(TestCase));
testResult.SetPropertyValue(testProperty, keyValuePair.Value);
}
}
}
}
internal static class Constants
{
/// <summary>
/// The test session start method name.
/// </summary>
public const string TestSessionStartMethodName = "TestSessionStart";
/// <summary>
/// The test session end method name.
/// </summary>
public const string TestSessionEndMethodName = "TestSessionEnd";
/// <summary>
/// The test case start method name.
/// </summary>
public const string TestCaseStartMethodName = "TestCaseStart";
/// <summary>
/// The test case end method name.
/// </summary>
public const string TestCaseEndMethodName = "TestCaseEnd";
/// <summary>
/// Test sources property name
/// </summary>
public const string TestSourcesPropertyName = "TestSources";
}
}