Skip to content

Commit

Permalink
[repo] Migrate shared code tests from main repository (#1806)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
Kielek and cijothomas authored May 16, 2024
1 parent 2035202 commit c7dc6ae
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<EventSourceSupport>true</EventSourceSupport>
<SelfContained>true</SelfContained>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\PropertyFetcher.AOT.cs" Link="Includes\PropertyFetcher.AOT.cs" />
</ItemGroup>

<ItemGroup>
<!--
When adding projects here please also update the verify-aot-compat job in
Expand Down
15 changes: 13 additions & 2 deletions test/OpenTelemetry.AotCompatibility.TestApp/Program.cs
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;
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\ActivityInstrumentationHelper.cs" Link="Includes\ActivityInstrumentationHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\GrpcStatusCanonicalCode.cs" Link="Includes\GrpcStatusCanonicalCode.cs" />
<Compile Include="$(RepoRoot)\src\Shared\PropertyFetcher.AOT.cs" Link="Includes\PropertyFetcher.AOT.cs" />
<Compile Include="$(RepoRoot)\src\Shared\RedactionHelper.cs" Link="Includes\RedactionHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\RequestDataHelper.cs" Link="Includes\RequestDataHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
</ItemGroup>
Expand Down
156 changes: 156 additions & 0 deletions test/OpenTelemetry.Contrib.Shared.Tests/PropertyFetcherTest.cs
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 test/OpenTelemetry.Contrib.Shared.Tests/RedactionHelperTest.cs
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));
}
}

0 comments on commit c7dc6ae

Please sign in to comment.