-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathTelemetryConfigurationFactoryTest.cs
More file actions
1989 lines (1599 loc) · 92 KB
/
TelemetryConfigurationFactoryTest.cs
File metadata and controls
1989 lines (1599 loc) · 92 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace Microsoft.ApplicationInsights.Extensibility.Implementation
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Platform;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
using Microsoft.ApplicationInsights.Shared.Extensibility.Implementation;
using Microsoft.ApplicationInsights.TestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EventLevel = System.Diagnostics.Tracing.EventLevel;
[TestClass]
public class TelemetryConfigurationFactoryTest
{
private const string EnvironmentVariableName = "APPINSIGHTS_INSTRUMENTATIONKEY";
private const string EnvironmentVariableConnectionString = "APPLICATIONINSIGHTS_CONNECTION_STRING";
[TestCleanup]
public void TestCleanup()
{
Environment.SetEnvironmentVariable(EnvironmentVariableName, null);
Environment.SetEnvironmentVariable(EnvironmentVariableConnectionString, null);
PlatformSingleton.Current = null; // Force reinitialization in future tests so that new environment variables will be loaded.
}
#region Instance
[TestMethod]
public void ClassIsInternalAndNotMeantForPublicConsumption()
{
Assert.IsFalse(typeof(TelemetryConfigurationFactory).GetTypeInfo().IsPublic);
}
[TestMethod]
public void InstanceReturnsDefaultTelemetryConfigurationFactoryInstanceUsedByTelemetryConfiguration()
{
Assert.IsNotNull(TelemetryConfigurationFactory.Instance);
}
[TestMethod]
public void InstanceCanGeSetByTestsToIsolateTestingOfTelemetryConfigurationFromRealFactoryLogic()
{
var replacement = new TestableTelemetryConfigurationFactory();
TelemetryConfigurationFactory.Instance = replacement;
Assert.AreSame(replacement, TelemetryConfigurationFactory.Instance);
}
[TestMethod]
public void InstanceIsLazilyInitializedToSimplifyResettingOfGlobalStateInTests()
{
TelemetryConfigurationFactory.Instance = null;
Assert.IsNotNull(TelemetryConfigurationFactory.Instance);
}
#endregion
#region Initialize
[TestMethod]
public void InitializeCreatesInMemoryChannel()
{
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null);
AssertEx.IsType<InMemoryChannel>(configuration.TelemetryChannel);
}
[TestMethod]
public void InitializesInstanceWithEmptyInstrumentationKey()
{
string configFileContents = Configuration("<InstrumentationKey></InstrumentationKey>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
using (var testableTelemetryModules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(configuration, testableTelemetryModules, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.AreEqual(string.Empty, configuration.InstrumentationKey);
}
}
[TestMethod]
[TestCategory("ConnectionString")]
public void InitializesInstanceWithEmptyConnectionString()
{
string configFileContents = Configuration($"<ConnectionString></ConnectionString>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
using (var testableTelemetryModules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(configuration, testableTelemetryModules, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.AreEqual(null, configuration.ConnectionString);
Assert.AreEqual(string.Empty, configuration.InstrumentationKey);
}
}
[TestMethod]
public void InitializesInstanceWithInformationFromConfigurationFileWhenItExists()
{
string configFileContents = Configuration("<InstrumentationKey>F8474271-D231-45B6-8DD4-D344C309AE69</InstrumentationKey>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
using (var testableTelemetryModules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(configuration, testableTelemetryModules, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.IsFalse(string.IsNullOrEmpty(configuration.InstrumentationKey));
}
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifyChannelEndpointsAreSetWhenParsingFromConfigFile_InMemoryChannel()
{
#pragma warning disable CS0618 // Type or member is obsolete
// PART 1 - CONFIGURATION FACTORY IS EXPECTED TO CREATE A CONFIG THAT MATCHES THE XML
string ikeyConfig = "00000000-0000-0000-1111-000000000000";
string ikeyConfigConnectionString = "00000000-0000-0000-2222-000000000000";
string configString = @"<InstrumentationKey>00000000-0000-0000-1111-000000000000</InstrumentationKey>
<TelemetryChannel Type=""Microsoft.ApplicationInsights.Channel.InMemoryChannel, Microsoft.ApplicationInsights"">
<EndpointAddress>http://10.0.0.0/v2/track</EndpointAddress>
<DeveloperMode>true</DeveloperMode>
</TelemetryChannel>";
string configFileContents = Configuration(configString);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
Assert.AreEqual(ikeyConfig, configuration.InstrumentationKey);
Assert.AreEqual(true, configuration.TelemetryChannel.DeveloperMode);
Assert.AreEqual("http://10.0.0.0/v2/track", configuration.TelemetryChannel.EndpointAddress, "failed to set Channel Endpoint to config value");
// PART 2 - VERIFY SETTING THE CONNECTION STRING WILL OVERWRITE CHANNEL ENDPOINT.
TelemetryConfiguration.Active = configuration;
TelemetryConfiguration.Active.ConnectionString = $"InstrumentationKey={ikeyConfigConnectionString};IngestionEndpoint=https://localhost:63029/";
var client = new TelemetryClient();
Assert.AreEqual(string.Empty, client.InstrumentationKey);
Assert.AreEqual(ikeyConfigConnectionString, client.TelemetryConfiguration.InstrumentationKey);
Assert.AreEqual("https://localhost:63029/v2/track", client.TelemetryConfiguration.TelemetryChannel.EndpointAddress);
#pragma warning restore CS0618 // Type or member is obsolete
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifyChannelEndpointsAreSetWhenParsingFromConfigFile_ServerTelemetryChannel()
{
#pragma warning disable CS0618 // Type or member is obsolete
// PART 1 - CONFIGURATION FACTORY IS EXPECTED TO CREATE A CONFIG THAT MATCHES THE XML
string ikeyConfig = "00000000-0000-0000-1111-000000000000";
string ikeyConfigConnectionString = "00000000-0000-0000-2222-000000000000";
string configString = @"<InstrumentationKey>00000000-0000-0000-1111-000000000000</InstrumentationKey>
<TelemetryChannel Type=""Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"">
<EndpointAddress>http://10.0.0.0/v2/track</EndpointAddress>
</TelemetryChannel>";
string configFileContents = Configuration(configString);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
Assert.AreEqual(ikeyConfig, configuration.InstrumentationKey);
Assert.AreEqual("http://10.0.0.0/v2/track", configuration.TelemetryChannel.EndpointAddress, "failed to set Channel Endpoint to config value");
// PART 2 - VERIFY SETTING THE CONNECTION STRING WILL OVERWRITE CHANNEL ENDPOINT.
TelemetryConfiguration.Active = configuration;
TelemetryConfiguration.Active.ConnectionString = $"InstrumentationKey={ikeyConfigConnectionString};IngestionEndpoint=https://localhost:63029/";
var client = new TelemetryClient();
Assert.AreEqual(string.Empty, client.InstrumentationKey);
Assert.AreEqual(ikeyConfigConnectionString, client.TelemetryConfiguration.InstrumentationKey);
Assert.AreEqual("https://localhost:63029/v2/track", client.TelemetryConfiguration.TelemetryChannel.EndpointAddress);
#pragma warning restore CS0618 // Type or member is obsolete
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifyThatChannelEndpointIsNotOverwrittenIfManuallyConfigured()
{
var configuration = new TelemetryConfiguration();
Assert.AreEqual("https://dc.services.visualstudio.com/", configuration.EndpointContainer.Ingestion.AbsoluteUri);
var customEndpoint = "http://10.0.0.0/v2/track";
var customChannel = new InMemoryChannel
{
EndpointAddress = customEndpoint
};
Assert.AreEqual(customEndpoint, customChannel.EndpointAddress);
configuration.TelemetryChannel = customChannel;
Assert.AreEqual(customEndpoint, customChannel.EndpointAddress, "channel endpoint was overwritten by config");
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifySelectInstrumentationKeyChooses_EnVarConnectionString()
{
// SETUP
string ikeyEnVarConnectionString = Guid.NewGuid().ToString();
Environment.SetEnvironmentVariable(EnvironmentVariableConnectionString, $"InstrumentationKey={ikeyEnVarConnectionString}");
string ikeyEnVar = Guid.NewGuid().ToString();
Environment.SetEnvironmentVariable(EnvironmentVariableName, ikeyEnVar);
string ikeyConfig = "e6f55001-f7d1-4242-b9f4-83660d0487f9";
string ikeyConfigConnectionString = "F8474271-D231-45B6-8DD4-D344C309AE69";
string configFileContents = Configuration($"<InstrumentationKey>{ikeyConfig}</InstrumentationKey><ConnectionString>InstrumentationKey={ikeyConfigConnectionString}</ConnectionString>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// ASSERT
Assert.AreEqual(ikeyEnVarConnectionString, configuration.InstrumentationKey);
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifySelectInstrumentationKeyChooses_EnVar()
{
// SETUP
string ikeyEnVar = Guid.NewGuid().ToString();
Environment.SetEnvironmentVariable(EnvironmentVariableName, ikeyEnVar);
string ikeyConfig = "e6f55001-f7d1-4242-b9f4-83660d0487f9";
string ikeyConfigConnectionString = "F8474271-D231-45B6-8DD4-D344C309AE69";
string configFileContents = Configuration($"<InstrumentationKey>{ikeyConfig}</InstrumentationKey><ConnectionString>InstrumentationKey={ikeyConfigConnectionString}</ConnectionString>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// ASSERT
Assert.AreEqual(ikeyEnVar, configuration.InstrumentationKey);
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifySelectInstrumentationKeyChooses_ConfigConnectionString()
{
// SETUP
string ikeyConfig = "e6f55001-f7d1-4242-b9f4-83660d0487f9";
string ikeyConfigConnectionString = "F8474271-D231-45B6-8DD4-D344C309AE69";
string configFileContents = Configuration($"<InstrumentationKey>{ikeyConfig}</InstrumentationKey><ConnectionString>InstrumentationKey={ikeyConfigConnectionString}</ConnectionString>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// ASSERT
Assert.AreEqual(ikeyConfigConnectionString, configuration.InstrumentationKey);
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifySelectInstrumentationKeyChooses_ConfigConnectionString_Reverse()
{
// SETUP
string ikeyConfig = "e6f55001-f7d1-4242-b9f4-83660d0487f9";
string ikeyConfigConnectionString = "F8474271-D231-45B6-8DD4-D344C309AE69";
string configFileContents = Configuration($"<ConnectionString>InstrumentationKey={ikeyConfigConnectionString}</ConnectionString><InstrumentationKey>{ikeyConfig}</InstrumentationKey>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// ASSERT
Assert.AreEqual(ikeyConfig, configuration.InstrumentationKey);
}
[TestMethod]
[TestCategory("ConnectionString")]
public void VerifySelectInstrumentationKeyChooses_ConfigIKey()
{
// SETUP
string ikeyConfig = "e6f55001-f7d1-4242-b9f4-83660d0487f9";
string configFileContents = Configuration($"<InstrumentationKey>{ikeyConfig}</InstrumentationKey>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// ASSERT
Assert.AreEqual(ikeyConfig, configuration.InstrumentationKey);
}
[TestMethod]
public void InitializeAddsOperationContextTelemetryInitializerByDefault()
{
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null);
var contextInitializer = configuration.TelemetryInitializers[0];
AssertEx.IsType<OperationCorrelationTelemetryInitializer>(contextInitializer);
}
[TestMethod]
public void InitializeNotifiesTelemetryInitializersImplementingITelemetryModuleInterface()
{
var initializer = new StubConfigurableTelemetryInitializer();
var configuration = new TelemetryConfiguration { TelemetryInitializers = { initializer } };
new TestableTelemetryConfigurationFactory().Initialize(configuration, null);
Assert.IsTrue(initializer.Initialized);
Assert.AreSame(configuration, initializer.Configuration);
}
[TestMethod]
public void InitializeCreatesInMemoryChannelEvenWhenConfigIsBroken()
{
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, Configuration("</blah>"));
AssertEx.IsType<InMemoryChannel>(configuration.TelemetryChannel);
}
[TestMethod]
public void InitializeCreatesInMemoryChannelEvenWhenConfigIsInvalid()
{
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, Configuration("<blah></blah>"));
AssertEx.IsType<InMemoryChannel>(configuration.TelemetryChannel);
}
[TestMethod]
public void InitializeReadsInstrumentationKeyFromEnvironmentVariableIfNotSpecifiedInConfig()
{
// ARRANGE
string ikey = Guid.NewGuid().ToString();
Environment.SetEnvironmentVariable(EnvironmentVariableName, ikey);
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null);
// ASSERT
Assert.AreEqual(ikey, configuration.InstrumentationKey);
}
[TestMethod]
public void InitializeReadsInstrumentationKeyFromEnvironmentVariableEvenIfSpecifiedInConfig()
{
// ARRANGE
string ikeyConfig = Guid.NewGuid().ToString();
string ikeyEnvironmentVariable = Guid.NewGuid().ToString();
Environment.SetEnvironmentVariable(EnvironmentVariableName, ikeyEnvironmentVariable);
TelemetryConfiguration configuration = new TelemetryConfiguration(ikeyConfig);
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null);
// ASSERT
Assert.AreEqual(ikeyEnvironmentVariable, configuration.InstrumentationKey);
}
[TestMethod]
public void InitializeLeavesInstrumentationKeyEmptyWhenNotSpecifiedInConfigOrInEnvironmentVariable()
{
// ARRANGE
Environment.SetEnvironmentVariable(EnvironmentVariableName, null);
TelemetryConfiguration configuration = new TelemetryConfiguration();
// ACT
new TestableTelemetryConfigurationFactory().Initialize(configuration, null);
// ASSERT
Assert.AreEqual(string.Empty, configuration.InstrumentationKey);
}
#endregion
#region CreateInstance
[TestMethod]
public void CreateInstanceReturnsInstanceOfTypeSpecifiedByTypeName()
{
Type type = typeof(StubTelemetryInitializer);
object instance = TestableTelemetryConfigurationFactory.CreateInstance(typeof(ITelemetryInitializer), type.AssemblyQualifiedName);
Assert.AreEqual(type, instance.GetType());
}
[TestMethod]
public void CreateInstanceReturnsNullWhenTypeCannotBeFound()
{
Assert.IsNull(TestableTelemetryConfigurationFactory.CreateInstance(typeof(ITelemetryInitializer), "MissingType, MissingAssembly"));
}
[TestMethod]
public void CreateInstanceThrowsInvalidOperationExceptionWhenTypeNameIsInvalidToHelpDeveloperIdentifyAndFixTheProblem()
{
Assert.IsNull(TestableTelemetryConfigurationFactory.CreateInstance(typeof(ITelemetryInitializer), "Invalid Type Name"));
}
[TestMethod]
public void CreateInstanceReturnsNullWhenInstanceDoesNotImplementExpectedInterface()
{
TelemetryConfiguration configuration = new TelemetryConfiguration();
Type invalidType = typeof(object);
Assert.IsNull(TestableTelemetryConfigurationFactory.CreateInstance(typeof(ITelemetryInitializer), invalidType.AssemblyQualifiedName));
}
#endregion
#region LoadFromXml
[TestMethod]
public void LoadFromXmlInitializesGivenTelemetryConfigurationInstanceFromXml()
{
string expected = Guid.NewGuid().ToString();
string profile = Configuration("<InstrumentationKey>" + expected + "</InstrumentationKey>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
TestableTelemetryConfigurationFactory.LoadFromXml(configuration, null, XDocument.Parse(profile));
// Assume LoadFromXml calls LoadInstance, which is tested separately.
Assert.AreEqual(expected, configuration.InstrumentationKey);
}
#endregion
#region LoadInstance
[TestMethod]
public void LoadInstanceReturnsInstanceOfTypeSpecifiedInTypeAttributeOfGivenXmlDefinition()
{
var definition = new XElement("Definition", new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName));
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
Assert.AreEqual(typeof(StubClassWithProperties), instance.GetType());
}
[TestMethod]
public void LoadInstanceSetsInstancePropertiesFromChildElementValuesOfDefinition()
{
var definition = new XElement(
"Definition",
new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
new XElement("StringProperty", "TestValue"));
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
Assert.AreEqual("TestValue", ((StubClassWithProperties)instance).StringProperty);
}
[TestMethod]
public void LoadInstanceSetsInstancePropertiesOfTimeSpanTypeFromChildElementValuesOfDefinitionWithTimeSpanFormat()
{
var definition = new XElement(
"Definition",
new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
new XElement("TimeSpanProperty", "00:00:07"));
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
Assert.AreEqual(TimeSpan.FromSeconds(7), ((StubClassWithProperties)instance).TimeSpanProperty);
}
[TestMethod]
public void LoadInstanceSetsInstancePropertiesOfTimeSpanTypeFromChildElementValuesOfDefinitionWithOneInteger()
{
var definition = new XElement(
"Definition",
new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
new XElement("TimeSpanProperty", "7"));
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
Assert.AreEqual(TimeSpan.FromDays(7), ((StubClassWithProperties)instance).TimeSpanProperty);
}
[TestMethod]
#if NETCOREAPP3_1 || NET5_0
[ExpectedExceptionWithMessage(typeof(ArgumentException), "Failed to parse configuration value. Property: 'TimeSpanProperty' Reason: String 'TestValue' was not recognized as a valid TimeSpan.")]
#else
[ExpectedExceptionWithMessage(typeof(ArgumentException), "Failed to parse configuration value. Property: 'TimeSpanProperty' Reason: String was not recognized as a valid TimeSpan.")]
#endif
public void LoadInstanceSetsInstancePropertiesOfTimeSpanTypeFromChildElementValuesOfDefinitionWithInvalidFormatThrowsException()
{
var definition = new XElement(
"Definition",
new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
new XElement("TimeSpanProperty", "TestValue"));
TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
}
[TestMethod]
public void LoadInstanceInitializesGivenInstanceAndDoesNotRequireSpecifyingTypeAttributeToSimplifyConfiguration()
{
var definition = new XElement(
"Definition",
new XElement("StringProperty", "TestValue"));
var original = new StubClassWithProperties();
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), original, null);
Assert.AreEqual("TestValue", original.StringProperty);
}
[TestMethod]
public void LoadInstanceHandlesEnumPropertiesWithNumericValue()
{
var definition = new XElement(
"Definition",
new XElement("EnumProperty", "3"));
var original = new StubClassWithProperties();
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), original, null);
Assert.AreEqual(EventLevel.Warning, original.EnumProperty);
}
[TestMethod]
public void LoadInstanceHandlesEnumPropertiesWithEnumerationValueName()
{
var definition = new XElement(
"Definition",
new XElement("EnumProperty", "Informational"));
var original = new StubClassWithProperties();
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), original, null);
Assert.AreEqual(EventLevel.Informational, original.EnumProperty);
}
[TestMethod]
public void LoadInstanceConvertsValueToExpectedTypeGivenXmlDefinitionWithNoChildElements()
{
var definition = new XElement("Definition", "42");
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(int), null, null);
Assert.AreEqual(42, instance);
}
[TestMethod]
public void LoadInstanceConvertsValueToExpectedTypeGivenXmlDefinitionWithNoChildElementsParseHexValue()
{
var definition = new XElement("Definition", "0x42");
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(int), null, null);
Assert.AreEqual(66, instance);
}
[TestMethod]
public void LoadInstanceTrimsValueOfGivenXmlElementToIgnoreWhitespaceUsersMayAddToConfiguration()
{
string expected = Guid.NewGuid().ToString();
var definition = new XElement("InstrumentationKey", "\n" + expected + "\n");
object actual = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(string), null, null);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void LoadInstanceReturnsNullGivenEmptyXmlElementForReferenceType()
{
var definition = new XElement("Definition");
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(string), "Test Value", null);
Assert.IsNull(instance);
}
[TestMethod]
public void LoadInstanceReturnsOriginalValueGivenNullXmlElement()
{
var original = "Test Value";
object loaded = TestableTelemetryConfigurationFactory.LoadInstance(null, original.GetType(), original, null);
Assert.AreSame(original, loaded);
}
[TestMethod]
public void LoadInstanceReturnsDefaultValueGivenValueEmptyXmlElementForValueType()
{
var definition = new XElement("Definition");
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(int), 12, null);
Assert.AreEqual(0, instance);
}
[TestMethod]
public void LoadInstanceReturnsNullWhenDefinitionElementDoesNotHaveTypeAttributeAndInstanceIsNotInitialized()
{
var elementWithoutType = new XElement("Add", new XElement("PropertyName"));
Assert.IsNull(TestableTelemetryConfigurationFactory.LoadInstance(elementWithoutType, typeof(IComparable), null, null));
}
[TestMethod]
public void LoadInstanceReturnsNullWhenDefinitionElementContainsInvalidContentAndNoTypeAttribute()
{
var definition = new XElement("InvalidElement", "InvalidText");
Assert.IsNull(TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(ITelemetryChannel), null, null));
}
[TestMethod]
public void LoadInstanceCreatesNewInstanceOfExpectedTypeWhenTypeAttributeIsNotSpecified()
{
var definition = new XElement("Definition", new XElement("Int32Property", 42));
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
var loaded = AssertEx.IsType<StubClassWithProperties>(instance);
Assert.AreEqual(42, loaded.Int32Property);
}
[TestMethod]
public void LoadInstanceCreatesNewInstanceOfExpectedTypeWhenPropertiesAreSpecifiedOnlyAsAttributes()
{
var definition = new XElement("Definition", new XAttribute("Int32Property", 42));
object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null, null);
var loaded = AssertEx.IsType<StubClassWithProperties>(instance);
Assert.AreEqual(42, loaded.Int32Property);
}
#endregion
#region TelemetryProcesors
[TestMethod]
public void InitializeTelemetryProcessorsFromConfigurationFile()
{
string configFileContents = Configuration(
"<TelemetryProcessors>" +
"<Add Type=\"" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + "\" />" +
"<Add Type=\"" + typeof(StubTelemetryProcessor2).AssemblyQualifiedName + "\" />" +
"</TelemetryProcessors>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
using (var testableTelemetryModules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(configuration, testableTelemetryModules, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.IsTrue(configuration.TelemetryProcessors != null);
AssertEx.IsType<StubTelemetryProcessor>(configuration.TelemetryProcessorChain.FirstTelemetryProcessor);
//validate the chain linking stub1->stub2->pass through->sink
var tp1 = (StubTelemetryProcessor)configuration.TelemetryProcessorChain.FirstTelemetryProcessor;
var tp2 = (StubTelemetryProcessor2)tp1.next;
var passThroughProcessor = tp2.next as PassThroughProcessor;
Assert.IsNotNull(passThroughProcessor);
// The sink has only a transmission processor and a default channel.
var sink = passThroughProcessor.Sink;
Assert.IsNotNull(sink);
Assert.AreEqual(1, sink.TelemetryProcessorChain.TelemetryProcessors.Count);
AssertEx.IsType<TransmissionProcessor>(sink.TelemetryProcessorChain.FirstTelemetryProcessor);
AssertEx.IsType<InMemoryChannel>(sink.TelemetryChannel);
}
}
[TestMethod]
public void InitializeTelemetryProcessorsWithWrongProcessorInTheMiddle()
{
string configFileContents = Configuration(
"<TelemetryProcessors>" +
"<Add Type=\"" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + "\" />" +
"<Add Type = \"Invalid, Invalid\" />" +
"<Add Type=\"" + typeof(StubTelemetryProcessor2).AssemblyQualifiedName + "\" />" +
"</TelemetryProcessors>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
using (var testableTelemetryModules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(configuration, testableTelemetryModules, configFileContents);
Assert.IsTrue(configuration.TelemetryProcessors != null);
AssertEx.IsType<StubTelemetryProcessor>(configuration.TelemetryProcessorChain.FirstTelemetryProcessor);
//validate the chain linking stub1->stub2->pass through->sink
var tp1 = (StubTelemetryProcessor)configuration.TelemetryProcessorChain.FirstTelemetryProcessor;
var tp2 = (StubTelemetryProcessor2)tp1.next;
var passThroughProcessor = tp2.next as PassThroughProcessor;
Assert.IsNotNull(passThroughProcessor);
// The sink has only a transmission processor and a default channel.
var sink = passThroughProcessor.Sink;
Assert.IsNotNull(sink);
Assert.AreEqual(1, sink.TelemetryProcessorChain.TelemetryProcessors.Count);
AssertEx.IsType<TransmissionProcessor>(sink.TelemetryProcessorChain.FirstTelemetryProcessor);
AssertEx.IsType<InMemoryChannel>(sink.TelemetryChannel);
}
}
[TestMethod]
public void InitializeTelemetryProcessorFromConfigurationFile()
{
string configFileContents = Configuration(
@"<TelemetryProcessors>
<Add Type=""" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + @""" />
</TelemetryProcessors>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.IsTrue(configuration.TelemetryProcessors != null);
AssertEx.IsType<StubTelemetryProcessor>(configuration.TelemetryProcessorChain.FirstTelemetryProcessor);
// The next telemetry processor should be the PassThroughProcessor that terminates the common telemetry processor chain and feeds into the sink.
var stub1 = (StubTelemetryProcessor)configuration.TelemetryProcessorChain.FirstTelemetryProcessor;
var passThroughProcessor = stub1.next as PassThroughProcessor;
Assert.IsNotNull(passThroughProcessor);
// The sink has only a transmission processor and a default channel.
var sink = passThroughProcessor.Sink;
Assert.IsNotNull(sink);
Assert.AreEqual(1, sink.TelemetryProcessorChain.TelemetryProcessors.Count);
AssertEx.IsType<TransmissionProcessor>(sink.TelemetryProcessorChain.FirstTelemetryProcessor);
AssertEx.IsType<InMemoryChannel>(sink.TelemetryChannel);
}
[TestMethod]
public void InitializeInvokedWhenTelemetryProcessorAlsoImplementsITelemetryModule()
{
string configFileContents = Configuration(
"<TelemetryProcessors>" +
"<Add Type=\"" + typeof(StubTelemetryProcessor2).AssemblyQualifiedName + "\" />" +
"</TelemetryProcessors>");
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
Assert.IsTrue(configuration.TelemetryProcessors != null);
AssertEx.IsType<StubTelemetryProcessor2>(configuration.TelemetryProcessorChain.FirstTelemetryProcessor);
Assert.IsTrue(((StubTelemetryProcessor2)configuration.TelemetryProcessorChain.FirstTelemetryProcessor).Initialized);
}
[TestMethod]
public void InitializeTelemetryProcessorFromConfigurationFileWhenNoTelemetryProcessorsTagSpecified()
{
// no TelemetryProcessors - TransmissionProcessor should be automatically created.
string configFileContents = Configuration(
@"
<!--<TelemetryProcessors>
<Add Type=""" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + @""" />
<Add Type=""" + typeof(StubTelemetryProcessor2).AssemblyQualifiedName + @""" />
</TelemetryProcessors>-->"
);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.IsTrue(configuration.TelemetryProcessors != null);
var passThroughProcessor = configuration.TelemetryProcessorChain.FirstTelemetryProcessor as PassThroughProcessor;
Assert.IsNotNull(passThroughProcessor);
// The sink has only a transmission processor and a default channel.
var sink = passThroughProcessor.Sink;
Assert.IsNotNull(sink);
Assert.AreEqual(1, sink.TelemetryProcessorChain.TelemetryProcessors.Count);
AssertEx.IsType<TransmissionProcessor>(sink.TelemetryProcessorChain.FirstTelemetryProcessor);
AssertEx.IsType<InMemoryChannel>(sink.TelemetryChannel);
}
[TestMethod]
public void InitializeTelemetryProcessorFromConfigurationFileWhenEmptyTelemetryProcessorsTagSpecified()
{
// no TelemetryProcessors - TransmissionProcessor should be automatically created.
string configFileContents = Configuration(
@"
<TelemetryInitializers>
<Add Type=""" + typeof(StubTelemetryInitializer).AssemblyQualifiedName + @""" />
</TelemetryInitializers>
<TelemetryProcessors>
</TelemetryProcessors>"
);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
// Assume that LoadFromXml method is called, tested separately
Assert.IsTrue(configuration.TelemetryProcessors != null);
var passThroughProcessor = configuration.TelemetryProcessorChain.FirstTelemetryProcessor as PassThroughProcessor;
Assert.IsNotNull(passThroughProcessor);
// The sink has only a transmission processor and a default channel.
var sink = passThroughProcessor.Sink;
Assert.IsNotNull(sink);
Assert.AreEqual(1, sink.TelemetryProcessorChain.TelemetryProcessors.Count);
AssertEx.IsType<TransmissionProcessor>(sink.TelemetryProcessorChain.FirstTelemetryProcessor);
AssertEx.IsType<InMemoryChannel>(sink.TelemetryChannel);
}
[TestMethod]
public void RebuildDoesNotRemoveTelemetryProcessorsLoadedFromConfiguration()
{
string configFileContents = Configuration(
@"
<TelemetryProcessors>
<Add Type=""" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + @""" />
</TelemetryProcessors>"
);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
configuration.TelemetryProcessorChainBuilder.Build();
Assert.AreEqual(2, configuration.TelemetryProcessors.Count);
AssertEx.IsType<StubTelemetryProcessor>(configuration.TelemetryProcessors[0]);
}
[TestMethod]
public void UseDoesNotRemoveTelemetryProcessorsLoadedFromConfiguration()
{
string configFileContents = Configuration(
@"
<TelemetryProcessors>
<Add Type=""" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + @""" />
</TelemetryProcessors>"
);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
var builder = configuration.TelemetryProcessorChainBuilder;
builder.Use(_ => new StubTelemetryProcessor2(_));
builder.Build();
Assert.AreEqual(3, configuration.TelemetryProcessors.Count);
AssertEx.IsType<StubTelemetryProcessor>(configuration.TelemetryProcessors[0]);
}
[TestMethod]
public void UseAddsProcessorAfterProcessorsDefinedInConfiguration()
{
string configFileContents = Configuration(
@"
<TelemetryProcessors>
<Add Type=""" + typeof(StubTelemetryProcessor).AssemblyQualifiedName + @""" />
</TelemetryProcessors>"
);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
var builder = configuration.TelemetryProcessorChainBuilder;
builder.Use(_ => new StubTelemetryProcessor2(_));
builder.Build();
Assert.AreEqual(3, configuration.TelemetryProcessors.Count);
AssertEx.IsType<StubTelemetryProcessor2>(configuration.TelemetryProcessors[1]);
}
#endregion
#region Modules
[TestMethod]
public void InitializeTelemetryModulesFromConfigurationFile()
{
string configFileContents = Configuration(
@"<TelemetryModules>
<Add Type = """ + typeof(StubConfigurable).AssemblyQualifiedName + @""" />
</TelemetryModules>"
);
using (var modules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(new TelemetryConfiguration(), modules, configFileContents);
Assert.AreEqual(2, modules.Modules.Count); // Diagnostics module is added by default
}
}
[TestMethod]
public void InitializeTelemetryModulesFromConfigurationFileWithNoModulesHasOneDiagnosticsModuleByDefault()
{
string configFileContents = Configuration(
@"<TelemetryModules>
</TelemetryModules>"
);
using (var modules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(new TelemetryConfiguration(), modules, configFileContents);
// Initialize a 2nd TelemetryConfiguration to check that only one diagnostics module is added.
new TestableTelemetryConfigurationFactory().Initialize(new TelemetryConfiguration(), modules, configFileContents);
Assert.AreEqual(1, modules.Modules.Count);
AssertEx.IsType<DiagnosticsTelemetryModule>(modules.Modules[0]);
}
}
[TestMethod]
public void InitializeTelemetryModulesFromConfigurationFileWhenOneModuleCannotBeLoaded()
{
string configFileContents = Configuration(
@"<TelemetryModules>
<Add Type = """ + typeof(StubConfigurable).AssemblyQualifiedName + @""" />
<Add Type = ""Invalid, Invalid"" />
<Add Type = """ + typeof(StubConfigurableTelemetryInitializer).AssemblyQualifiedName + @""" />
</TelemetryModules>"
);
using (var modules = new TestableTelemetryModules())
{
new TestableTelemetryConfigurationFactory().Initialize(new TelemetryConfiguration(), modules, configFileContents);
Assert.AreEqual(3, modules.Modules.Count); // Diagnostics module is added by default
}
}
[TestMethod]
public void InitializeDoesNotThrowIsModuleInitializationFails()
{
string configFileContents = Configuration(
@"<TelemetryModules>
<Add Type = """ + typeof(StubConfigurableWithProperties).AssemblyQualifiedName + @""" />
</TelemetryModules>"
);
var module = new StubConfigurableWithProperties(null)
{
OnInitialize = _ => { throw new ArgumentException(); }
};
using (var modules = new TestableTelemetryModules())
{
modules.Modules.Add(module);
//Assert.DoesNotThrow
new TestableTelemetryConfigurationFactory().Initialize(
new TelemetryConfiguration(),
modules,
configFileContents);
}
}
#endregion
#region TelemetryInitializers
[TestMethod]
public void InitializeAddTelemetryInitializersWithOneInvalid()
{
string configFileContents = Configuration(
@"<TelemetryInitializers>
<Add Type=""Invalid, Invalid"" />
<Add Type=""" + typeof(StubTelemetryInitializer).AssemblyQualifiedName + @""" />
</TelemetryInitializers>"
);
TelemetryConfiguration configuration = new TelemetryConfiguration();
new TestableTelemetryConfigurationFactory().Initialize(configuration, null, configFileContents);
Assert.AreEqual(2, configuration.TelemetryInitializers.Count); // Time and operation initializers are added by default
Assert.IsNotNull(configuration.TelemetryInitializers.First(item => item.GetType().Name == "StubTelemetryInitializer"));
}
#endregion
#region LoadInstances<T>
[TestMethod]
public void LoadInstancesPopulatesListWithInstancesOfSpecifiedType()
{
var element = XElement.Parse(@"
<List xmlns=""http://schemas.microsoft.com/ApplicationInsights/2013/Settings"">
<Add Type=""" + typeof(StubTelemetryInitializer).AssemblyQualifiedName + @""" />
</List>");
var instances = new List<ITelemetryInitializer>();