-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[repo] Migrate shared code tests from main repository (#1806)
Co-authored-by: Cijo Thomas <[email protected]>
- Loading branch information
1 parent
2035202
commit c7dc6ae
Showing
7 changed files
with
303 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using OpenTelemetry.AotCompatibility.TestApp; | ||
|
||
Console.WriteLine("Hello, World!"); | ||
try | ||
{ | ||
PropertyFetcherAotTest.Test(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
return -1; | ||
} | ||
|
||
Console.WriteLine("Passed."); | ||
return 0; |
56 changes: 56 additions & 0 deletions
56
test/OpenTelemetry.AotCompatibility.TestApp/PropertyFetcherAotTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using OpenTelemetry.Instrumentation; | ||
|
||
namespace OpenTelemetry.AotCompatibility.TestApp; | ||
|
||
internal class PropertyFetcherAotTest | ||
{ | ||
[UnconditionalSuppressMessage("", "IL2026", Justification = "Property presence guaranteed by explicit hints.")] | ||
public static void Test() | ||
{ | ||
var fetcher = new PropertyFetcher<BaseType>("Property"); | ||
|
||
GuaranteeProperties<PayloadTypeWithBaseType>(); | ||
var r = fetcher.TryFetch(new PayloadTypeWithBaseType(), out var value); | ||
Assert(r, "TryFetch base did not return true."); | ||
Assert(value!.GetType() == typeof(DerivedType), "TryFetch base value is not a derived type."); | ||
|
||
GuaranteeProperties<PayloadTypeWithDerivedType>(); | ||
r = fetcher.TryFetch(new PayloadTypeWithDerivedType(), out value); | ||
Assert(r, "TryFetch derived did not return true."); | ||
Assert(value!.GetType() == typeof(DerivedType), "TryFetch derived value is not a derived type."); | ||
} | ||
|
||
private static void GuaranteeProperties<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>() | ||
{ | ||
} | ||
|
||
private static void Assert(bool condition, string message) | ||
{ | ||
if (!condition) | ||
{ | ||
throw new InvalidOperationException(message); | ||
} | ||
} | ||
|
||
private class BaseType | ||
{ | ||
} | ||
|
||
private class DerivedType : BaseType | ||
{ | ||
} | ||
|
||
private class PayloadTypeWithBaseType | ||
{ | ||
public BaseType Property { get; set; } = new DerivedType(); | ||
} | ||
|
||
private class PayloadTypeWithDerivedType | ||
{ | ||
public DerivedType Property { get; set; } = new DerivedType(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
test/OpenTelemetry.Contrib.Shared.Tests/ActivityInstrumentationHelperTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Diagnostics; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Instrumentation.Tests; | ||
|
||
public class ActivityInstrumentationHelperTest | ||
{ | ||
[Theory] | ||
[InlineData("TestActivitySource", null)] | ||
[InlineData("TestActivitySource", "1.0.0")] | ||
public void SetActivitySource(string name, string version) | ||
{ | ||
using var activity = new Activity("Test"); | ||
using var activitySource = new ActivitySource(name, version); | ||
|
||
activity.Start(); | ||
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, activitySource); | ||
Assert.Equal(activitySource.Name, activity.Source.Name); | ||
Assert.Equal(activitySource.Version, activity.Source.Version); | ||
activity.Stop(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ActivityKind.Client)] | ||
[InlineData(ActivityKind.Consumer)] | ||
[InlineData(ActivityKind.Internal)] | ||
[InlineData(ActivityKind.Producer)] | ||
[InlineData(ActivityKind.Server)] | ||
public void SetActivityKind(ActivityKind activityKind) | ||
{ | ||
using var activity = new Activity("Test"); | ||
activity.Start(); | ||
ActivityInstrumentationHelper.SetKindProperty(activity, activityKind); | ||
Assert.Equal(activityKind, activity.Kind); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
test/OpenTelemetry.Contrib.Shared.Tests/PropertyFetcherTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Instrumentation.Tests; | ||
|
||
public class PropertyFetcherTest | ||
{ | ||
[Fact] | ||
public void FetchValidProperty() | ||
{ | ||
using var activity = new Activity("test"); | ||
|
||
var fetch = new PropertyFetcher<string>("DisplayName"); | ||
|
||
Assert.Equal(0, fetch.NumberOfInnerFetchers); | ||
|
||
Assert.True(fetch.TryFetch(activity, out string? result)); | ||
Assert.Equal(activity.DisplayName, result); | ||
|
||
Assert.Equal(1, fetch.NumberOfInnerFetchers); | ||
|
||
Assert.True(fetch.TryFetch(activity, out result)); | ||
Assert.Equal(activity.DisplayName, result); | ||
|
||
Assert.Equal(1, fetch.NumberOfInnerFetchers); | ||
} | ||
|
||
[Fact] | ||
public void FetchInvalidProperty() | ||
{ | ||
using var activity = new Activity("test"); | ||
var fetch = new PropertyFetcher<string>("DisplayName2"); | ||
Assert.False(fetch.TryFetch(activity, out string? result)); | ||
|
||
var fetchInt = new PropertyFetcher<int>("DisplayName2"); | ||
Assert.False(fetchInt.TryFetch(activity, out int resultInt)); | ||
|
||
Assert.Equal(default, result); | ||
Assert.Equal(default, resultInt); | ||
} | ||
|
||
[Fact] | ||
public void FetchNullProperty() | ||
{ | ||
var fetch = new PropertyFetcher<string>("null"); | ||
Assert.False(fetch.TryFetch(null, out _)); | ||
} | ||
|
||
[Fact] | ||
public void FetchPropertyMultiplePayloadTypes() | ||
{ | ||
var fetch = new PropertyFetcher<string>("Property"); | ||
|
||
Assert.Equal(0, fetch.NumberOfInnerFetchers); | ||
|
||
Assert.True(fetch.TryFetch(new PayloadTypeA(), out string? propertyValue)); | ||
Assert.Equal("A", propertyValue); | ||
|
||
Assert.Equal(1, fetch.NumberOfInnerFetchers); | ||
|
||
Assert.True(fetch.TryFetch(new PayloadTypeB(), out propertyValue)); | ||
Assert.Equal("B", propertyValue); | ||
|
||
Assert.Equal(2, fetch.NumberOfInnerFetchers); | ||
|
||
Assert.False(fetch.TryFetch(new PayloadTypeC(), out _)); | ||
|
||
Assert.Equal(2, fetch.NumberOfInnerFetchers); | ||
|
||
Assert.False(fetch.TryFetch(null, out _)); | ||
|
||
Assert.Equal(2, fetch.NumberOfInnerFetchers); | ||
} | ||
|
||
[Fact] | ||
public void FetchPropertyMultiplePayloadTypes_IgnoreTypesWithoutExpectedPropertyName() | ||
{ | ||
var fetch = new PropertyFetcher<string>("Property"); | ||
|
||
Assert.False(fetch.TryFetch(new PayloadTypeC(), out _)); | ||
|
||
Assert.True(fetch.TryFetch(new PayloadTypeA(), out string? propertyValue)); | ||
Assert.Equal("A", propertyValue); | ||
} | ||
|
||
[Fact] | ||
public void FetchPropertyWithDerivedInstanceType() | ||
{ | ||
var fetch = new PropertyFetcher<BaseType>("Property"); | ||
|
||
Assert.True(fetch.TryFetch(new PayloadTypeWithBaseType(), out BaseType? value)); | ||
Assert.IsType<DerivedType>(value); | ||
} | ||
|
||
[Fact] | ||
public void FetchPropertyWithDerivedDeclaredType() | ||
{ | ||
var fetch = new PropertyFetcher<BaseType>("Property"); | ||
|
||
Assert.True(fetch.TryFetch(new PayloadTypeWithDerivedType(), out BaseType? value)); | ||
Assert.IsType<DerivedType>(value); | ||
} | ||
|
||
[Fact] | ||
public void FetchPropertyWhenPayloadIsValueType() | ||
{ | ||
var fetch = new PropertyFetcher<BaseType>("Property"); | ||
var ex = Assert.Throws<NotSupportedException>(() => fetch.TryFetch(new PayloadTypeIsValueType(), out BaseType? value)); | ||
Assert.Contains("PropertyFetcher can only operate on reference payload types.", ex.Message); | ||
} | ||
|
||
private struct PayloadTypeIsValueType | ||
{ | ||
public PayloadTypeIsValueType() | ||
{ | ||
} | ||
|
||
public DerivedType Property { get; set; } = new DerivedType(); | ||
} | ||
|
||
private class PayloadTypeA | ||
{ | ||
public string Property { get; set; } = "A"; | ||
} | ||
|
||
private class PayloadTypeB | ||
{ | ||
public string Property { get; set; } = "B"; | ||
} | ||
|
||
private class PayloadTypeC | ||
{ | ||
} | ||
|
||
private class BaseType | ||
{ | ||
} | ||
|
||
private class DerivedType : BaseType | ||
{ | ||
} | ||
|
||
private class PayloadTypeWithBaseType | ||
{ | ||
public BaseType Property { get; set; } = new DerivedType(); | ||
} | ||
|
||
private class PayloadTypeWithDerivedType | ||
{ | ||
public DerivedType Property { get; set; } = new DerivedType(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/OpenTelemetry.Contrib.Shared.Tests/RedactionHelperTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using Xunit; | ||
|
||
namespace OpenTelemetry.Internal.Tests; | ||
|
||
public class RedactionHelperTest | ||
{ | ||
[Theory] | ||
[InlineData("?a", "?a")] | ||
[InlineData("?a=b", "?a=Redacted")] | ||
[InlineData("?a=b&", "?a=Redacted&")] | ||
[InlineData("?c=b&", "?c=Redacted&")] | ||
[InlineData("?c=a", "?c=Redacted")] | ||
[InlineData("?a=b&c", "?a=Redacted&c")] | ||
[InlineData("?a=b&c=1&", "?a=Redacted&c=Redacted&")] | ||
[InlineData("?a=b&c=1&a1", "?a=Redacted&c=Redacted&a1")] | ||
[InlineData("?a=b&c=1&a1=", "?a=Redacted&c=Redacted&a1=Redacted")] | ||
[InlineData("?a=b&c=11&a1=&", "?a=Redacted&c=Redacted&a1=Redacted&")] | ||
[InlineData("?c&c&c&", "?c&c&c&")] | ||
[InlineData("?a&a&a&a", "?a&a&a&a")] | ||
[InlineData("?&&&&&&&", "?&&&&&&&")] | ||
[InlineData("?c", "?c")] | ||
[InlineData("?=c", "?=Redacted")] | ||
[InlineData("?=c&=", "?=Redacted&=Redacted")] | ||
public void QueryStringIsRedacted(string input, string expected) | ||
{ | ||
Assert.Equal(expected, RedactionHelper.GetRedactedQueryString(input)); | ||
} | ||
} |